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