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