Unity 8
dash.py
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Unity Autopilot Test Suite
4 # Copyright (C) 2012, 2013, 2014, 2015 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 import logging
21 
22 import ubuntuuitoolkit
23 from autopilot import logging as autopilot_logging
24 from autopilot.introspection import dbus
25 
26 import unity8
27 
28 
29 logger = logging.getLogger(__name__)
30 
31 
32 class DashApp():
33 
34  """Autopilot helper for the Dash app."""
35 
36  def __init__(self, app_proxy):
37  self.app_proxy = app_proxy
38  self.main_view = self.app_proxy.select_single(
39  ubuntuuitoolkit.MainView)
40  self.dash = self.main_view.select_single(Dash)
41 
42 
43 class Dash(ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase):
44  """A helper that understands the Dash."""
45 
46  def __init__(self, *args):
47  super().__init__(*args)
48  self.dash_content_list = self.wait_select_single(
49  'QQuickListView', objectName='dashContentList')
50 
51  def get_scope(self, scope_name):
52  return self.dash_content_list.wait_select_single(
53  'QQuickLoader', scopeId=scope_name)
54 
55  def get_scope_by_index(self, scope_index=0):
56  return self.dash_content_list.wait_select_single(
57  'QQuickLoader', objectName=("scopeLoader%i" % scope_index))
58 
59  @autopilot_logging.log_action(logger.info)
60  def open_scope(self, scope_id):
61  """Open a dash scope.
62 
63  :parameter scope_id: The id of the scope.
64  :return: The scope.
65 
66  """
67  scope_loader = self._get_scope_loader(scope_id)
68  if scope_loader.isCurrent:
69  logger.info('The scope is already open.')
70  return self._get_scope_from_loader(scope_loader)
71  else:
72  return self._open_scope_scrolling(scope_loader)
73 
74  def _get_scope_loader(self, scope_id):
75  try:
76  aux = self.dash_content_list.get_children_by_type('QQuickItem')[0]
77  for l in aux.get_children_by_type('QQuickLoader'):
78  if (l.scopeId == scope_id):
79  return l
81  'No scope found with id {0}'.format(scope_id))
82  except dbus.StateNotFoundError:
84  'No scope found with id {0}'.format(scope_id))
85 
86  def _get_scope_from_loader(self, loader):
87  return loader.wait_select_single('GenericScopeView')
88 
89  def _open_scope_scrolling(self, scope_loader):
90  scroll = self._get_scroll_direction(scope_loader)
91 
92  while not scope_loader.isCurrent:
93  scroll()
94  self.dash_content_list.moving.wait_for(False)
95 
96  scope_loader.isCurrent.wait_for(True)
97  scope = self._get_scope_from_loader(scope_loader)
98  return scope
99 
100  def _get_scroll_direction(self, scope_loader):
101  current_scope_loader = self.dash_content_list.select_single(
102  'QQuickLoader', isCurrent=True)
103  if scope_loader.globalRect.x < current_scope_loader.globalRect.x:
104  return self._scroll_to_left_scope
105  elif scope_loader.globalRect.x > current_scope_loader.globalRect.x:
106  return self._scroll_to_right_scope
107  else:
108  raise unity8.UnityException('The scope is already open')
109 
110  @autopilot_logging.log_action(logger.info)
111  def _scroll_to_left_scope(self):
112  original_index = self.dash_content_list.currentIndex
113  dash_content = self.select_single(objectName="dashContent")
114  x, y, width, height = dash_content.globalRect
115  # Make the drag range be a multiple of the drag "rate" value.
116  # Workarounds https://bugs.launchpad.net/mir/+bug/1399690
117  rate = 5
118  divisions = 5
119  jump = (width / divisions) // rate * rate
120  start_x = x + jump
121  stop_x = x + jump * (divisions - 1)
122  start_y = stop_y = y + 1
123  self.pointing_device.drag(start_x, start_y, stop_x, stop_y, rate)
124  self.dash_content_list.currentIndex.wait_for(original_index - 1)
125 
126  @autopilot_logging.log_action(logger.info)
127  def _scroll_to_right_scope(self):
128  original_index = self.dash_content_list.currentIndex
129  dash_content = self.select_single(objectName="dashContent")
130  x, y, width, height = dash_content.globalRect
131  # Make the drag range be a multiple of the drag "rate" value.
132  # Workarounds https://bugs.launchpad.net/mir/+bug/1399690
133  rate = 5
134  divisions = 5
135  jump = (width / divisions) // rate * rate
136  start_x = x + jump * (divisions - 1)
137  stop_x = x + jump
138  start_y = stop_y = y + 1
139  self.pointing_device.drag(start_x, start_y, stop_x, stop_y, rate)
140  self.dash_content_list.currentIndex.wait_for(original_index + 1)
141 
142  def enter_search_query(self, query, keyboard):
143  current_header = self._get_current_page_header()
144  search_button = \
145  current_header.select_single(objectName="search_button")
146  self.pointing_device.move(
147  search_button.globalRect.x + search_button.width / 2,
148  search_button.globalRect.y + search_button.height / 2)
149  self.pointing_device.click()
150  headersColumn = current_header.select_single(
151  objectName="headersColumn")
152  headersColumn.y.wait_for(0)
153  keyboard.type(query)
154  self.select_single(
155  objectName="processingIndicator").visible.wait_for(False)
156 
157  def get_search_text_field(self):
158  page_header = self._get_current_page_header()
159  return page_header.select_single(objectName='searchTextField')
160 
161  def _get_current_page_header(self):
162  all_scopes = self.dash_content_list.select_many("GenericScopeView")
163  for i in all_scopes:
164  if i.isCurrent:
165  return i.select_single(objectName="scopePageHeader")
166  return None
167 
168 
169 class ListViewWithPageHeader(ubuntuuitoolkit.QQuickFlickable):
170 
171  margin_to_swipe_from_bottom = ubuntuuitoolkit.units.gu(4)
172 
173 
174 class GenericScopeView(ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase):
175  """Autopilot helper for generic scopes."""
176 
177  @autopilot_logging.log_action(logger.info)
178  def open_preview(self, category, app_name, press_duration=0.10):
179  """Open the preview of an application.
180 
181  :parameter category: The name of the category where the application is.
182  :parameter app_name: The name of the application.
183  :return: The opened preview.
184 
185  """
186  # FIXME some categories need a long press in order to see the preview.
187  # Some categories do not show previews, like recent apps.
188  # --elopio - 2014-1-14
189  self.click_scope_item(category, app_name, press_duration)
190  preview_list = self.wait_select_single(
191  'QQuickLoader', objectName='subPageLoader')
192  preview_list.subPageShown.wait_for(True)
193  preview_list.x.wait_for(0)
194  self.get_root_instance().select_single(
195  objectName='processingIndicator').visible.wait_for(False)
196  return preview_list.select_single(
197  Preview, objectName='preview')
198 
199  @autopilot_logging.log_action(logger.debug)
200  def click_scope_item(self, category, title, press_duration=0.10):
201  """Click an item from the scope.
202 
203  :parameter category: The name of the category where the item is.
204  :parameter title: The title of the item.
205 
206  """
207  category_element = self._get_category_element(category)
208  icon = category_element.wait_select_single(
209  'UCAbstractButton', title=title)
210  list_view = self.select_single(
211  ListViewWithPageHeader, objectName='categoryListView')
212  list_view.swipe_child_into_view(icon)
213  self.pointing_device.click_object(icon, press_duration=press_duration)
214 
215  def _get_category_element(self, category):
216  try:
217  return self.wait_select_single(
218  'DashCategoryBase',
219  objectName='dashCategory{}'.format(category))
220  except dbus.StateNotFoundError:
221  raise unity8.UnityException(
222  'No category found with name {}'.format(category))
223 
224  def get_applications(self, category):
225  """Return the list of applications on a category.
226 
227  :parameter category: The name of the category.
228 
229  """
230  category_element = self._get_category_element(category)
231  see_all = category_element.select_single(objectName='seeAll')
232  application_cards = category_element.select_many('UCAbstractButton')
233 
234  application_cards = sorted(
235  (card for card in application_cards
236  if card.globalRect.y < see_all.globalRect.y),
237  key=lambda card: (card.globalRect.y, card.globalRect.x))
238 
239  result = []
240  for card in application_cards:
241  if card.objectName not in ('cardToolCard', 'seeAll'):
242  result.append(card.title)
243  return result
244 
245 
246 class Preview(ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase):
247  """Autopilot custom proxy object for generic previews."""
def _get_scope_loader(self, scope_id)
Definition: dash.py:74
def __init__(self, app_proxy)
Definition: dash.py:36
def click_scope_item(self, category, title, press_duration=0.10)
Definition: dash.py:200
def _open_scope_scrolling(self, scope_loader)
Definition: dash.py:89
def open_preview(self, category, app_name, press_duration=0.10)
Definition: dash.py:178
def _get_scope_from_loader(self, loader)
Definition: dash.py:86
def _scroll_to_right_scope(self)
Definition: dash.py:127
def _get_category_element(self, category)
Definition: dash.py:215
def get_applications(self, category)
Definition: dash.py:224
def _scroll_to_left_scope(self)
Definition: dash.py:111
def open_scope(self, scope_id)
Definition: dash.py:60
def _get_scroll_direction(self, scope_loader)
Definition: dash.py:100
def _get_current_page_header(self)
Definition: dash.py:161