Unity 8
__init__.py
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Unity Autopilot Test Suite
4 # Copyright (C) 2012-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 """unity shell autopilot tests and helpers - sub level package."""
21 
22 import logging
23 from functools import wraps
24 
25 import ubuntuuitoolkit
26 from autopilot import logging as autopilot_logging
27 from autopilot import input
28 from gi.repository import Notify
29 
30 from unity8 import (
31  greeter,
32  launcher as launcher_helpers
33 )
34 
35 
36 logger = logging.getLogger(__name__)
37 
38 
39 def disable_qml_mocking(fn):
40  """Simple decorator that disables the QML mocks from being loaded."""
41  @wraps(fn)
42  def wrapper(*args, **kwargs):
43  tests_self = args[0]
44  tests_self._qml_mock_enabled = False
45  return fn(*args, **kwargs)
46  return wrapper
47 
48 
49 def create_ephemeral_notification(
50  summary='',
51  body='',
52  icon=None,
53  hints=[],
54  urgency='NORMAL'
55 ):
56  """Create an ephemeral (non-interactive) notification
57 
58  :param summary: Summary text for the notification
59  :param body: Body text to display in the notification
60  :param icon: Path string to the icon to use
61  :param hint_strings: List of tuples containing the 'name' and value
62  for setting the hint strings for the notification
63  :param urgency: Urgency string for the noticiation, either: 'LOW',
64  'NORMAL', 'CRITICAL'
65  """
66  Notify.init('Unity8')
67 
68  logger.info(
69  "Creating ephemeral: summary(%s), body(%s), urgency(%r) "
70  "and Icon(%s)",
71  summary,
72  body,
73  urgency,
74  icon
75  )
76 
77  notification = Notify.Notification.new(summary, body, icon)
78 
79  for hint in hints:
80  key, value = hint
81  notification.set_hint_string(key, value)
82  logger.info("Adding hint to notification: (%s, %s)", key, value)
83  notification.set_urgency(_get_urgency(urgency))
84 
85  return notification
86 
87 
88 def _get_urgency(urgency):
89  """Translates urgency string to enum."""
90  _urgency_enums = {'LOW': Notify.Urgency.LOW,
91  'NORMAL': Notify.Urgency.NORMAL,
92  'CRITICAL': Notify.Urgency.CRITICAL}
93  return _urgency_enums.get(urgency.upper())
94 
95 
96 class ShellView(ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase):
97  """An helper class that makes it easy to interact with the shell"""
98 
99  def get_greeter(self):
100  return self.select_single(greeter.Greeter)
101 
102  def get_login_loader(self):
103  return self.select_single("QQuickLoader", objectName="loginLoader")
104 
105  def get_login_list(self):
106  return self.select_single("LoginList")
107 
108  def get_bottombar(self):
109  return self.select_single("Bottombar")
110 
111  def get_pinPadLoader(self):
112  return self.select_single(
113  "QQuickLoader",
114  objectName="pinPadLoader"
115  )
116 
117  def get_lockscreen(self):
118  return self.select_single("Lockscreen")
119 
120  def get_pinentryField(self):
121  return self.select_single(objectName="pinentryField")
122 
123  def _get_indicator_panel_item(self, indicator_name):
124  return self.select_single(
125  'IndicatorItem',
126  objectName=indicator_name+'-panelItem'
127  )
128 
129  def _get_indicator_page(self, indicator_name):
130  return self.select_single(
131  'IndicatorPage',
132  objectName=indicator_name+'-page'
133  )
134 
135  @autopilot_logging.log_action(logger.info)
136  def open_indicator_page(self, indicator_name):
137  """Swipe to open the indicator, wait until it's open.
138 
139  :returns: The indicator page.
140  """
141  widget = self._get_indicator_panel_item(indicator_name)
142  start_x, start_y = input.get_center_point(widget)
143  end_x = start_x
144  end_y = self.height
145  self.pointing_device.drag(start_x, start_y, end_x, end_y)
146  self.wait_select_single('IndicatorsMenu', fullyOpened=True)
147  return self._get_indicator_page(indicator_name)
148 
149  @autopilot_logging.log_action(logger.info)
151  """Swipe to close the opened indicator, wait until it's closed."""
152  indicators_menu = self.wait_select_single('IndicatorsMenu')
153  end_x, end_y = input.get_center_point(indicators_menu)
154  start_x = end_x
155  start_y = self.height
156  self.pointing_device.drag(start_x, start_y, end_x, end_y)
157  indicators_menu.fullyClosed.wait_for(True)
158 
159  @autopilot_logging.log_action(logger.info)
160  def show_dash_swiping(self):
161  """Show the dash swiping from the left."""
162  x, y, width, height = self._get_shell().globalRect
163  start_x = x
164  end_x = x + width
165  start_y = end_y = y + height // 2
166 
167  self.pointing_device.drag(start_x, start_y, end_x, end_y)
168  self.get_current_focused_app_id().wait_for('unity8-dash')
169 
170  def _get_shell(self):
171  return self.select_single('Shell')
172 
174  """Return the id of the focused application."""
175  return self._get_shell().focusedApplicationId
176 
177  @autopilot_logging.log_action(logger.info)
179  """Open the dash clicking the dash icon on the launcher."""
180  launcher = self.open_launcher()
181  launcher.click_dash_icon()
182  self.get_current_focused_app_id().wait_for('unity8-dash')
183  launcher.shown.wait_for(False)
184 
185  @autopilot_logging.log_action(logger.info)
186  def open_launcher(self):
187  launcher = self._get_launcher()
188  launcher.show()
189  return launcher
190 
191  def _get_launcher(self):
192  return self.select_single(launcher_helpers.Launcher)
193 
194  def is_launcher_open(self):
195  return self._get_launcher().shown
196 
197  @autopilot_logging.log_action(logger.info)
198  def launch_application(self, application_name):
199  """Launch an application.
200 
201  :parameter application_name: The name of the application to launch.
202 
203  """
204  launcher = self.open_launcher()
205  launcher.click_application_launcher_icon(application_name)
206  self.get_current_focused_app_id().wait_for(application_name)
207  launcher.shown.wait_for(False)
208 
209  def enter_pin_code(self, code):
210  """Enter code 'code' into the single-pin lightdm pincode entry screen.
211 
212  :param code: must be a string of numeric characters.
213  :raises: TypeError if code is not a string.
214  :raises: ValueError if code contains non-numeric characters.
215 
216  """
217  if not isinstance(code, str):
218  raise TypeError(
219  "'code' parameter must be a string, not %r."
220  % type(code)
221  )
222  for num in code:
223  if not num.isdigit():
224  raise ValueError(
225  "'code' parameter contains non-numeric characters."
226  )
227  self.pointing_device.click_object(
228  self._get_pinpad_button(int(num)))
229 
230  def _get_pinpad_button(self, button_id):
231  return self.select_single(
232  'PinPadButton',
233  objectName='pinPadButton{}'.format(button_id)
234  )
235 
236  def get_shell_orientation_angle(self):
237  return self._get_shell().orientationAngle
238 
239  def get_shell_orientation(self):
240  return self._get_shell().orientation
241 
242  def get_shell_primary_orientation(self):
243  return self._get_shell().primaryOrientation
244 
245  def get_shell_native_orientation(self):
246  return self._get_shell().nativeOrientation
247 
248  @autopilot_logging.log_action(logger.info)
250  """Wait for a notification dialog to appear.
251 
252  :return: An object for the notification dialog data.
253  :raise StateNotFoundError: if the timeout expires when the
254  notification has not appeared.
255 
256  """
257  notify_list = self.select_single('Notifications',
258  objectName='notificationList')
259  visible_notification = notify_list.wait_select_single('Notification',
260  visible=True)
261  return {'summary': visible_notification.summary,
262  'body': visible_notification.body,
263  'iconSource': visible_notification.iconSource}
def open_indicator_page(self, indicator_name)
Definition: __init__.py:136
def get_current_focused_app_id(self)
Definition: __init__.py:173
def _get_launcher(self)
Definition: __init__.py:191
def wait_for_notification(self)
Definition: __init__.py:249
def show_dash_from_launcher(self)
Definition: __init__.py:178
def _get_indicator_panel_item(self, indicator_name)
Definition: __init__.py:123
def show_dash_swiping(self)
Definition: __init__.py:160
def open_launcher(self)
Definition: __init__.py:186
def _get_shell(self)
Definition: __init__.py:170
def launch_application(self, application_name)
Definition: __init__.py:198
def _get_indicator_page(self, indicator_name)
Definition: __init__.py:129
def close_indicator_page(self)
Definition: __init__.py:150
def _get_pinpad_button(self, button_id)
Definition: __init__.py:230
def enter_pin_code(self, code)
Definition: __init__.py:209