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 unity8 import process_helpers
36 from unity8.shell import emulators, fixture_setup, tests
37 from unity8.shell.emulators import dash as dash_emulators
38 
39 
40 class MainWindowTestCase(tests.UnityTestCase):
41 
42  scenarios = tests._get_device_emulation_scenarios()
43 
44  def setUp(self):
45  super(MainWindowTestCase, self).setUp()
46  unity_proxy = self.launch_unity()
47  process_helpers.unlock_unity(unity_proxy)
48 
49  def test_search(self):
50  self.main_window.search('Test')
51  text_field = self.main_window.get_dash()._get_search_text_field()
52  self.assertEqual(text_field.text, 'Test')
53 
54 
55 class DashBaseTestCase(tests.UnityTestCase):
56 
57  scenarios = tests._get_device_emulation_scenarios()
58 
59  def setUp(self):
60  super(DashBaseTestCase, self).setUp()
61  unity_proxy = self.launch_unity()
62  process_helpers.unlock_unity(unity_proxy)
63  self.dash = self.main_window.get_dash()
64 
65 
66 class DashEmulatorTestCase(DashBaseTestCase):
67 
68  def test_open_unexisting_scope(self):
69  scope_name = 'unexisting'
70  with mock.patch.object(self.dash, 'pointing_device') as mock_pointer:
71  exception = self.assertRaises(
72  emulators.UnityEmulatorException,
73  self.dash.open_scope, scope_name)
74 
75  self.assertEqual(
76  'No scope found with id unexisting', str(exception))
77  self.assertFalse(mock_pointer.called)
78 
79  def test_open_already_opened_scope(self):
80  scope_id = self._get_current_scope_id()
81  with mock.patch.object(self.dash, 'pointing_device') as mock_pointer:
82  scope = self.dash.open_scope(scope_id)
83 
84  self.assertFalse(mock_pointer.called)
85  self._assert_scope_is_opened(scope, scope_id)
86 
87  def _assert_scope_is_opened(self, scope, scope_id):
88  self.assertTrue(scope.isCurrent)
89  scope_loader = scope.get_parent()
90  self.assertEqual(scope_loader.scopeId, scope_id)
91 
92  def _get_current_scope_id(self):
93  scope = self.dash.dash_content_list.select_single(
94  'QQuickLoader', isCurrent=True)
95  return scope.scopeId
96 
97  def test_open_scope_to_the_right(self):
98  leftmost_scope = self._get_leftmost_scope_id()
99  self.dash.open_scope(leftmost_scope)
100 
101  scope_id = self._get_rightmost_scope_id()
102  scope = self.dash.open_scope(scope_id)
103  self._assert_scope_is_opened(scope, scope_id)
104 
105  def _get_leftmost_scope_id(self):
106  scope_loaders = self._get_scope_loaders()
107  leftmost_scope_loader = scope_loaders[0]
108  for loader in scope_loaders[1:]:
109  if loader.globalRect.x < leftmost_scope_loader.globalRect.x:
110  leftmost_scope_loader = loader
111  return leftmost_scope_loader.scopeId
112 
113  def _get_scope_loaders(self):
114  item = self.dash.dash_content_list.get_children_by_type(
115  'QQuickItem')[0]
116  return item.get_children_by_type('QQuickLoader')
117 
118  def _get_rightmost_scope_id(self):
119  scope_loaders = self._get_scope_loaders()
120  rightmost_scope_loader = scope_loaders[0]
121  for loader in scope_loaders[1:]:
122  if loader.globalRect.x > rightmost_scope_loader.globalRect.x:
123  rightmost_scope_loader = loader
124  return rightmost_scope_loader.scopeId
125 
126  def test_open_scope_to_the_left(self):
127  rightmost_scope = self._get_rightmost_scope_id()
128  self.dash.open_scope(rightmost_scope)
129 
130  scope_id = self._get_leftmost_scope_id()
131  scope = self.dash.open_scope(scope_id)
132  self._assert_scope_is_opened(scope, scope_id)
133 
134  def test_open_generic_scope(self):
135  scope_id = 'scopes'
136  scope = self.dash.open_scope(scope_id)
137  self._assert_scope_is_opened(scope, scope_id)
138  self.assertIsInstance(scope, dash_emulators.GenericScopeView)
139 
140  def test_open_applications_scope(self):
141  scope_id = 'clickscope'
142  scope = self.dash.open_scope(scope_id)
143  self._assert_scope_is_opened(scope, scope_id)
144  self.assertIsInstance(scope, dash_emulators.DashApps)
145 
146 
147 class GenericScopeViewEmulatorTestCase(DashBaseTestCase):
148 
149  def setUp(self):
150  # Set up the fake scopes before launching unity.
151  self.useFixture(fixture_setup.FakeScopes())
152  super(GenericScopeViewEmulatorTestCase, self).setUp()
153  self.generic_scope = self.dash.open_scope('MockScope1')
154 
155  def test_open_preview(self):
156  preview = self.generic_scope.open_preview('0', 'Title.0.0')
157  self.assertIsInstance(preview, dash_emulators.Preview)
158  self.assertTrue(preview.isCurrent)
159 
160 
161 class DashAppsEmulatorTestCase(DashBaseTestCase):
162 
163  available_applications = [
164  'Title.2.0', 'Title.2.1', 'Title.2.2', 'Title.2.3', 'Title.2.4',
165  'Title.2.5', 'Title.2.6', 'Title.2.7', 'Title.2.8', 'Title.2.9',
166  'Title.2.10', 'Title.2.11', 'Title.2.12']
167 
168  def setUp(self):
169  # Set up the fake scopes before launching unity.
170  self.useFixture(fixture_setup.FakeScopes())
171  super(DashAppsEmulatorTestCase, self).setUp()
172  self.applications_scope = self.dash.open_scope('clickscope')
173 
174  def test_get_applications_with_unexisting_category(self):
175  exception = self.assertRaises(
176  emulators.UnityEmulatorException,
177  self.applications_scope.get_applications,
178  'unexisting category')
179 
180  self.assertEqual(
181  'No category found with name unexisting category', str(exception))
182 
183  def test_get_applications_should_return_correct_applications(self):
184  category = '2'
185  expected_apps_count = self._get_number_of_application_slots(category)
186  expected_applications = self.available_applications[
187  :expected_apps_count]
188  applications = self.applications_scope.get_applications(category)
189  self.assertEqual(expected_applications, applications)
190 
191  def _get_number_of_application_slots(self, category):
192  category_element = self.applications_scope._get_category_element(
193  category)
194  grid = category_element.select_single('CardFilterGrid')
195  filtergrid = grid.select_single('FilterGrid')
196  if (grid.filtered):
197  return filtergrid.collapsedRowCount * filtergrid.columns
198  else:
199  return filtergrid.uncollapsedRowCount * filtergrid.columns