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-2013 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 emulators - sub level package."""
21 
22 from time import sleep
23 from functools import wraps
24 from gi.repository import Notify
25 
26 import logging
27 
28 logger = logging.getLogger(__name__)
29 
30 def disable_qml_mocking(fn):
31  """Simple decorator that disables the QML mocks from being loaded."""
32  @wraps(fn)
33  def wrapper(*args, **kwargs):
34  tests_self = args[0]
35  tests_self._qml_mock_enabled = False
36  return fn(*args, **kwargs)
37  return wrapper
38 
39 
40 class DragMixin(object):
41  def _drag(self, x1, y1, x2, y2):
42  # XXX This ugly code is here just temporarily, waiting for drag
43  # improvements to land on autopilot so we don't have to access device
44  # private internal attributes. --elopio - 2014-02-12
45  cur_x = x1
46  cur_y = y1
47  dx = 1.0 * (x2 - x1) / 100
48  dy = 1.0 * (y2 - y1) / 100
49  for i in range(0, 100):
50  try:
51  self.touch._finger_move(int(cur_x), int(cur_y))
52  except AttributeError:
53  self.touch._device.finger_move(int(cur_x), int(cur_y))
54  sleep(0.002)
55  cur_x += dx
56  cur_y += dy
57  try:
58  self.touch._finger_move(int(x2), int(y2))
59  except AttributeError:
60  self.touch._device.finger_move(int(x2), int(y2))
61 
62 
63 def create_ephemeral_notification(
64  summary='',
65  body='',
66  icon=None,
67  hints=[],
68  urgency='NORMAL'
69 ):
70  """Create an ephemeral (non-interactive) notification
71 
72  :param summary: Summary text for the notification
73  :param body: Body text to display in the notification
74  :param icon: Path string to the icon to use
75  :param hint_strings: List of tuples containing the 'name' and value
76  for setting the hint strings for the notification
77  :param urgency: Urgency string for the noticiation, either: 'LOW',
78  'NORMAL', 'CRITICAL'
79  """
80  Notify.init('Unity8')
81 
82  logger.info(
83  "Creating ephemeral: summary(%s), body(%s), urgency(%r) "
84  "and Icon(%s)",
85  summary,
86  body,
87  urgency,
88  icon
89  )
90 
91  notification = Notify.Notification.new(summary, body, icon)
92 
93  for hint in hints:
94  key, value = hint
95  notification.set_hint_string(key, value)
96  logger.info("Adding hint to notification: (%s, %s)", key, value)
97  notification.set_urgency(_get_urgency(urgency))
98 
99  return notification
100 
101 
102 def _get_urgency(urgency):
103  """Translates urgency string to enum."""
104  _urgency_enums = {'LOW': Notify.Urgency.LOW,
105  'NORMAL': Notify.Urgency.NORMAL,
106  'CRITICAL': Notify.Urgency.CRITICAL}
107  return _urgency_enums.get(urgency.upper())