Unity 8
 All Classes Functions Properties
test_emulators.py
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Unity Autopilot Test Suite
4 # Copyright (C) 2014 Canonical
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 
20 """Tests for the Dash autopilot emulators.
21 
22 The autopilot emulators are helpers for tests that check a user journey that
23 involves the dash. The code for some of those tests will not be inside this
24 branch, but in projects that depend on unity or that test the whole system
25 integration. So, we need to test the helpers in order to make sure that we
26 don't break them for those external projects.
27 
28 """
29 
30 try:
31  from unittest import mock
32 except ImportError:
33  import mock
34 
35 from testtools.matchers import Contains, HasLength
36 
37 from unity8 import process_helpers
38 from unity8.shell import emulators, fixture_setup, tests
39 from unity8.shell.emulators import dash as dash_emulators
40 
41 
42 class MainWindowTestCase(tests.UnityTestCase):
43 
44  scenarios = tests._get_device_emulation_scenarios()
45 
46  def setUp(self):
47  super(MainWindowTestCase, self).setUp()
48  unity_proxy = self.launch_unity()
49  process_helpers.unlock_unity(unity_proxy)
50 
51  def test_search(self):
52  self.main_window.search('Test')
53  text_field = self.main_window.get_dash()._get_search_text_field()
54  self.assertEqual(text_field.text, 'Test')
55  self.assertEqual(text_field.state, 'idle')
56 
57 
58 class DashBaseTestCase(tests.UnityTestCase):
59 
60  scenarios = tests._get_device_emulation_scenarios()
61 
62  def setUp(self):
63  super(DashBaseTestCase, self).setUp()
64  unity_proxy = self.launch_unity()
65  process_helpers.unlock_unity(unity_proxy)
66  self.dash = self.main_window.get_dash()
67 
68 
69 class DashEmulatorTestCase(DashBaseTestCase):
70 
71  def test_open_unexisting_scope(self):
72  scope_name = 'unexisting'
73  with mock.patch.object(self.dash, 'pointing_device') as mock_pointer:
74  exception = self.assertRaises(
75  emulators.UnityEmulatorException,
76  self.dash.open_scope, scope_name)
77 
78  self.assertEqual(
79  'No scope found with id unexisting', str(exception))
80  self.assertFalse(mock_pointer.called)
81 
82  def test_open_already_opened_scope(self):
83  scope_id = self._get_current_scope_id()
84  with mock.patch.object(self.dash, 'pointing_device') as mock_pointer:
85  scope = self.dash.open_scope(scope_id)
86 
87  self.assertFalse(mock_pointer.called)
88  self._assert_scope_is_opened(scope, scope_id)
89 
90  def _assert_scope_is_opened(self, scope, scope_id):
91  self.assertTrue(scope.isCurrent)
92  scope_loader = scope.get_parent()
93  self.assertEqual(scope_loader.scopeId, scope_id)
94 
95  def _get_current_scope_id(self):
96  scope = self.dash.dash_content_list.select_single(
97  'QQuickLoader', isCurrent=True)
98  return scope.scopeId
99 
100  def test_open_scope_to_the_right(self):
101  leftmost_scope = self._get_leftmost_scope_id()
102  self.dash.open_scope(leftmost_scope)
103 
104  scope_id = self._get_rightmost_scope_id()
105  scope = self.dash.open_scope(scope_id)
106  self._assert_scope_is_opened(scope, scope_id)
107 
108  def _get_leftmost_scope_id(self):
109  scope_loaders = self._get_scope_loaders()
110  leftmost_scope_loader = scope_loaders[0]
111  for loader in scope_loaders[1:]:
112  if loader.globalRect.x < leftmost_scope_loader.globalRect.x:
113  leftmost_scope_loader = loader
114  return leftmost_scope_loader.scopeId
115 
116  def _get_scope_loaders(self):
117  item = self.dash.dash_content_list.get_children_by_type(
118  'QQuickItem')[0]
119  return item.get_children_by_type('QQuickLoader')
120 
121  def _get_rightmost_scope_id(self):
122  scope_loaders = self._get_scope_loaders()
123  rightmost_scope_loader = scope_loaders[0]
124  for loader in scope_loaders[1:]:
125  if loader.globalRect.x > rightmost_scope_loader.globalRect.x:
126  rightmost_scope_loader = loader
127  return rightmost_scope_loader.scopeId
128 
129  def test_open_scope_to_the_left(self):
130  rightmost_scope = self._get_rightmost_scope_id()
131  self.dash.open_scope(rightmost_scope)
132 
133  scope_id = self._get_leftmost_scope_id()
134  scope = self.dash.open_scope(scope_id)
135  self._assert_scope_is_opened(scope, scope_id)
136 
137  def test_open_generic_scope(self):
138  scope_id = 'scopes'
139  scope = self.dash.open_scope(scope_id)
140  self._assert_scope_is_opened(scope, scope_id)
141  self.assertIsInstance(scope, dash_emulators.GenericScopeView)
142 
143  def test_open_applications_scope(self):
144  scope_id = 'clickscope'
145  scope = self.dash.open_scope(scope_id)
146  self._assert_scope_is_opened(scope, scope_id)
147  self.assertIsInstance(scope, dash_emulators.DashApps)
148 
149 
150 class GenericScopeViewEmulatorTestCase(DashBaseTestCase):
151 
152  def setUp(self):
153  # Set up the fake scopes before launching unity.
154  self.useFixture(fixture_setup.FakeScopes())
155  super(GenericScopeViewEmulatorTestCase, self).setUp()
156  self.generic_scope = self.dash.open_scope('MockScope1')
157 
158  def test_open_preview(self):
159  preview = self.generic_scope.open_preview('0', 'Title.0.0')
160  self.assertIsInstance(preview, dash_emulators.Preview)
161  self.assertTrue(preview.isCurrent)
162 
163 
164 class DashAppsEmulatorTestCase(DashBaseTestCase):
165 
166  available_applications = [
167  'Title.2.0', 'Title.2.1', 'Title.2.2', 'Title.2.3', 'Title.2.4',
168  'Title.2.5', 'Title.2.6', 'Title.2.7', 'Title.2.8', 'Title.2.9',
169  'Title.2.10', 'Title.2.11', 'Title.2.12']
170 
171  def setUp(self):
172  # Set up the fake scopes before launching unity.
173  self.useFixture(fixture_setup.FakeScopes())
174  super(DashAppsEmulatorTestCase, self).setUp()
175  self.applications_scope = self.dash.open_scope('clickscope')
176 
177  def test_get_applications_with_unexisting_category(self):
178  exception = self.assertRaises(
179  emulators.UnityEmulatorException,
180  self.applications_scope.get_applications,
181  'unexisting category')
182 
183  self.assertEqual(
184  'No category found with name unexisting category', str(exception))
185 
186  def test_get_applications_should_return_correct_applications(self):
187  category = '2'
188  expected_apps_count = self._get_number_of_application_slots(category)
189  expected_applications = self.available_applications[
190  :expected_apps_count]
191  applications = self.applications_scope.get_applications(category)
192  self.assertEqual(expected_applications, applications)
193 
194  def _get_number_of_application_slots(self, category):
195  category_element = self.applications_scope._get_category_element(
196  category)
197  grid = category_element.select_single('CardFilterGrid')
198  filtergrid = grid.select_single('FilterGrid')
199  if (grid.filtered):
200  return filtergrid.collapsedRowCount * filtergrid.columns
201  else:
202  return filtergrid.uncollapsedRowCount * filtergrid.columns