Unity 8
 All Classes Functions
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 from unity8.shell.emulators.dash import ListViewWithPageHeader
20 
21 """Tests for the Dash autopilot emulators.
22 
23 The autopilot emulators are helpers for tests that check a user journey that
24 involves the dash. The code for some of those tests will not be inside this
25 branch, but in projects that depend on unity or that test the whole system
26 integration. So, we need to test the helpers in order to make sure that we
27 don't break them for those external projects.
28 
29 """
30 
31 try:
32  from unittest import mock
33 except ImportError:
34  import mock
35 
36 from unity8 import process_helpers
37 from unity8.shell import emulators, fixture_setup, tests
38 from unity8.shell.emulators import dash as dash_emulators
39 
40 
41 class MainWindowTestCase(tests.UnityTestCase):
42 
43  scenarios = tests._get_device_emulation_scenarios()
44 
45  def setUp(self):
46  super(MainWindowTestCase, self).setUp()
47  unity_proxy = self.launch_unity()
48  process_helpers.unlock_unity(unity_proxy)
49 
50 
51 class DashEmulatorTestCase(tests.DashBaseTestCase):
52 
53  def test_search(self):
54  self.dash.enter_search_query('Test')
55  text_field = self.dash._get_search_text_field()
56  self.assertEqual(text_field.text, 'Test')
57 
58  def test_open_unexisting_scope(self):
59  scope_name = 'unexisting'
60  with mock.patch.object(self.dash, 'pointing_device') as mock_pointer:
61  exception = self.assertRaises(
62  emulators.UnityEmulatorException,
63  self.dash.open_scope, scope_name)
64 
65  self.assertEqual(
66  'No scope found with id unexisting', str(exception))
67  self.assertFalse(mock_pointer.called)
68 
69  def test_open_already_opened_scope(self):
70  scope_id = self._get_current_scope_id()
71  with mock.patch.object(self.dash, 'pointing_device') as mock_pointer:
72  scope = self.dash.open_scope(scope_id)
73 
74  self.assertFalse(mock_pointer.called)
75  self._assert_scope_is_opened(scope, scope_id)
76 
77  def _assert_scope_is_opened(self, scope, scope_id):
78  self.assertTrue(scope.isCurrent)
79  scope_loader = scope.get_parent()
80  self.assertEqual(scope_loader.scopeId, scope_id)
81 
82  def _get_current_scope_id(self):
83  scope = self.dash.dash_content_list.select_single(
84  'QQuickLoader', isCurrent=True)
85  return scope.scopeId
86 
87  def test_open_scope_to_the_right(self):
88  leftmost_scope = self._get_leftmost_scope_id()
89  self.dash.open_scope(leftmost_scope)
90 
91  scope_id = self._get_rightmost_scope_id()
92  scope = self.dash.open_scope(scope_id)
93  self._assert_scope_is_opened(scope, scope_id)
94 
95  def _get_leftmost_scope_id(self):
96  scope_loaders = self._get_scope_loaders()
97  leftmost_scope_loader = scope_loaders[0]
98  for loader in scope_loaders[1:]:
99  if loader.globalRect.x < leftmost_scope_loader.globalRect.x:
100  leftmost_scope_loader = loader
101  return leftmost_scope_loader.scopeId
102 
103  def _get_scope_loaders(self):
104  item = self.dash.dash_content_list.get_children_by_type(
105  'QQuickItem')[0]
106  return item.get_children_by_type('QQuickLoader')
107 
108  def _get_rightmost_scope_id(self):
109  scope_loaders = self._get_scope_loaders()
110  rightmost_scope_loader = scope_loaders[0]
111  for loader in scope_loaders[1:]:
112  if loader.globalRect.x > rightmost_scope_loader.globalRect.x:
113  rightmost_scope_loader = loader
114  return rightmost_scope_loader.scopeId
115 
116  def test_open_scope_to_the_left(self):
117  rightmost_scope = self._get_rightmost_scope_id()
118  self.dash.open_scope(rightmost_scope)
119 
120  scope_id = self._get_leftmost_scope_id()
121  scope = self.dash.open_scope(scope_id)
122  self._assert_scope_is_opened(scope, scope_id)
123 
124  def test_open_generic_scope(self):
125  scope_id = 'musicaggregator'
126  scope = self.dash.open_scope(scope_id)
127  self._assert_scope_is_opened(scope, scope_id)
128  self.assertIsInstance(scope, dash_emulators.GenericScopeView)
129 
130  def test_open_applications_scope(self):
131  scope_id = 'clickscope'
132  scope = self.dash.open_scope(scope_id)
133  self._assert_scope_is_opened(scope, scope_id)
134  self.assertIsInstance(scope, dash_emulators.GenericScopeView)
135 
136 
137 class GenericScopeViewEmulatorTestCase(tests.DashBaseTestCase):
138 
139  def setUp(self):
140  # Set up the fake scopes before launching unity.
141  self.useFixture(fixture_setup.FakeScopes())
142  super(GenericScopeViewEmulatorTestCase, self).setUp()
143  self.generic_scope = self.dash.open_scope('MockScope1')
144 
145  def test_open_preview(self):
146  preview = self.generic_scope.open_preview('0', 'Title.0.0')
147  self.assertIsInstance(preview, dash_emulators.Preview)
148  self.assertTrue(preview.isCurrent)
149 
150 
151 class DashAppsEmulatorTestCase(tests.DashBaseTestCase):
152 
153  available_applications = [
154  'Title.2.0', 'Title.2.1', 'Title.2.2', 'Title.2.3', 'Title.2.4',
155  'Title.2.5', 'Title.2.6', 'Title.2.7', 'Title.2.8', 'Title.2.9',
156  'Title.2.10', 'Title.2.11', 'Title.2.12']
157 
158  def setUp(self):
159  # Set up the fake scopes before launching unity.
160  self.useFixture(fixture_setup.FakeScopes())
161  super(DashAppsEmulatorTestCase, self).setUp()
162  self.applications_scope = self.dash.open_scope('clickscope')
163 
164  def test_get_applications_with_unexisting_category(self):
165  exception = self.assertRaises(
166  emulators.UnityEmulatorException,
167  self.applications_scope.get_applications,
168  'unexisting category')
169 
170  self.assertEqual(
171  'No category found with name unexisting category', str(exception))
172 
173  def test_get_applications_should_return_correct_applications(self):
174  category = '2'
175  category_element = self.applications_scope._get_category_element(
176  category)
177  list_view = self.dash.get_scope('clickscope')\
178  .select_single(ListViewWithPageHeader)
179  expected_apps_count = self._get_number_of_application_slots(category)
180  expected_applications = self.available_applications[
181  :expected_apps_count]
182  x_center = list_view.globalRect.x + list_view.width / 2
183  y_center = list_view.globalRect.y + list_view.height / 2
184  y_diff = category_element.y - list_view.height + category_element.height
185  list_view._slow_drag(x_center, x_center,
186  y_center, y_center - y_diff)
187  applications = self.applications_scope.get_applications(category)
188  self.assertEqual(expected_applications, applications)
189 
190  def _get_number_of_application_slots(self, category):
191  category_element = self.applications_scope._get_category_element(
192  category)
193  cardgrid = category_element.select_single('CardGrid')
194  if (category_element.expanded):
195  return cardgrid.select_single('QQuickGridView').count
196  else:
197  return cardgrid.collapsedRows \
198  * cardgrid.select_single('ResponsiveGridView').columns