Unity 8
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 from unity8 import process_helpers
32 from unity8.shell import fixture_setup, tests
33 from unity8.shell.emulators import dash as dash_emulators
34 
35 
36 class MainWindowTestCase(tests.UnityTestCase):
37 
38  scenarios = tests._get_device_emulation_scenarios()
39 
40  def setUp(self):
41  super(MainWindowTestCase, self).setUp()
42  unity_proxy = self.launch_unity()
43  process_helpers.unlock_unity(unity_proxy)
44 
45 
46 class DashEmulatorTestCase(tests.DashBaseTestCase):
47 
48  def test_search(self):
49  self.dash.enter_search_query('Test')
50  text_field = self.dash._get_search_text_field()
51  self.assertEqual(text_field.text, 'Test')
52 
53  def test_open_scope_to_the_right(self):
54  leftmost_scope = self._get_leftmost_scope_id()
55  self.dash.open_scope(leftmost_scope)
56 
57  scope_id = self._get_rightmost_scope_id()
58  scope = self.dash.open_scope(scope_id)
59  self._assert_scope_is_opened(scope, scope_id)
60 
61  def _assert_scope_is_opened(self, scope, scope_id):
62  self.assertTrue(scope.isCurrent)
63  scope_loader = scope.get_parent()
64  self.assertEqual(scope_loader.scopeId, scope_id)
65 
66  def _get_leftmost_scope_id(self):
67  scope_loaders = self._get_scope_loaders()
68  leftmost_scope_loader = scope_loaders[0]
69  for loader in scope_loaders[1:]:
70  if loader.globalRect.x < leftmost_scope_loader.globalRect.x:
71  leftmost_scope_loader = loader
72  return leftmost_scope_loader.scopeId
73 
74  def _get_scope_loaders(self):
75  item = self.dash.dash_content_list.get_children_by_type(
76  'QQuickItem')[0]
77  return item.get_children_by_type('QQuickLoader')
78 
79  def _get_rightmost_scope_id(self):
80  scope_loaders = self._get_scope_loaders()
81  rightmost_scope_loader = scope_loaders[0]
82  for loader in scope_loaders[1:]:
83  if loader.globalRect.x > rightmost_scope_loader.globalRect.x:
84  rightmost_scope_loader = loader
85  return rightmost_scope_loader.scopeId
86 
87  def test_open_scope_to_the_left(self):
88  rightmost_scope = self._get_rightmost_scope_id()
89  self.dash.open_scope(rightmost_scope)
90 
91  scope_id = self._get_leftmost_scope_id()
92  scope = self.dash.open_scope(scope_id)
93  self._assert_scope_is_opened(scope, scope_id)
94 
95  def test_open_generic_scope(self):
96  scope_id = 'musicaggregator'
97  scope = self.dash.open_scope(scope_id)
98  self._assert_scope_is_opened(scope, scope_id)
99  self.assertIsInstance(scope, dash_emulators.GenericScopeView)
100 
101  def test_open_applications_scope(self):
102  scope_id = 'clickscope'
103  scope = self.dash.open_scope(scope_id)
104  self._assert_scope_is_opened(scope, scope_id)
105  self.assertIsInstance(scope, dash_emulators.GenericScopeView)
106 
107 
108 class GenericScopeViewEmulatorTestCase(tests.DashBaseTestCase):
109 
110  def setUp(self):
111  # Set up the fake scopes before launching unity.
112  self.useFixture(fixture_setup.FakeScopes())
113  super(GenericScopeViewEmulatorTestCase, self).setUp()
114  self.generic_scope = self.dash.open_scope('MockScope1')
115 
116  def test_open_preview(self):
117  preview = self.generic_scope.open_preview('0', 'Title.0.0')
118  self.assertIsInstance(preview, dash_emulators.Preview)
119  self.assertTrue(preview.isCurrent)
120 
121 
122 class DashAppsEmulatorTestCase(tests.DashBaseTestCase):
123 
124  available_applications = [
125  'Title.2.0', 'Title.2.1', 'Title.2.2', 'Title.2.3', 'Title.2.4',
126  'Title.2.5', 'Title.2.6', 'Title.2.7', 'Title.2.8', 'Title.2.9',
127  'Title.2.10', 'Title.2.11', 'Title.2.12']
128 
129  def setUp(self):
130  # Set up the fake scopes before launching unity.
131  self.useFixture(fixture_setup.FakeScopes())
132  super(DashAppsEmulatorTestCase, self).setUp()
133  self.applications_scope = self.dash.open_scope('clickscope')
134 
135  def test_get_applications_should_return_correct_applications(self):
136  category = '2'
137  category_element = self.applications_scope._get_category_element(
138  category)
139  list_view = self.dash.get_scope('clickscope')\
140  .select_single(ListViewWithPageHeader)
141  expected_apps_count = self._get_number_of_application_slots(category)
142  expected_applications = self.available_applications[
143  :expected_apps_count]
144  x_center = list_view.globalRect.x + list_view.width / 2
145  y_center = list_view.globalRect.y + list_view.height / 2
146  y_diff = (
147  category_element.y - list_view.height + category_element.height
148  )
149  list_view._slow_drag(x_center, x_center, y_center, y_center - y_diff)
150  applications = self.applications_scope.get_applications(category)
151  self.assertEqual(expected_applications, applications)
152 
153  def _get_number_of_application_slots(self, category):
154  category_element = self.applications_scope._get_category_element(
155  category)
156  cardgrid = category_element.select_single('CardGrid')
157  if (category_element.expanded):
158  return cardgrid.select_single('QQuickGridView').count
159  else:
160  return cardgrid.collapsedRows \
161  * cardgrid.select_single('ResponsiveGridView').columns