on xenial help sign in
Autopilot: Helpers for notifications
e8376ffVictor R. Ruiz
tests/autopilot/unity8/shell/__init__.py+51−1
@@ -21,8 +21,9 @@
21
21
22from time import sleep
22from time import sleep
23from functools import wraps
23from functools import wraps
24−import logging
24+from gi.repository import Notify
25
25
26+import logging
26
27
27logger = logging.getLogger(__name__)
28logger = logging.getLogger(__name__)
28
29
@@ -48,6 +49,7 @@ def disable_qml_mocking(fn):
48 return fn(*args, **kwargs)
49 return fn(*args, **kwargs)
49 return wrapper
50 return wrapper
50
51
52+
51class DragMixin(object):
53class DragMixin(object):
52 def _drag(self, x1, y1, x2, y2):
54 def _drag(self, x1, y1, x2, y2):
53 # XXX This ugly code is here just temporarily, waiting for drag
55 # XXX This ugly code is here just temporarily, waiting for drag
@@ -69,3 +71,51 @@ class DragMixin(object):
69 self.touch._finger_move(int(x2), int(y2))
71 self.touch._finger_move(int(x2), int(y2))
70 except AttributeError:
72 except AttributeError:
71 self.touch._device.finger_move(int(x2), int(y2))
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+ """
94+ Notify.init('Unity8')
95+
96+ logger.info(
97+ "Creating ephemeral: summary(%s), body(%s), urgency(%r) "
98+ "and Icon(%s)",
99+ summary,
100+ body,
101+ urgency,
102+ icon
103+ )
104+
105+ notification = Notify.Notification.new(summary, body, icon)
106+
107+ for hint in hints:
108+ key, value = hint
109+ notification.set_hint_string(key, value)
110+ logger.info("Adding hint to notification: (%s, %s)", key, value)
111+ notification.set_urgency(_get_urgency(urgency))
112+
113+ return notification
114+
115+
116+def _get_urgency(urgency):
117+ """Translates urgency string to enum."""
118+ _urgency_enums = {'LOW': Notify.Urgency.LOW,
119+ 'NORMAL': Notify.Urgency.NORMAL,
120+ 'CRITICAL': Notify.Urgency.CRITICAL}
121+ return _urgency_enums.get(urgency.upper())
tests/autopilot/unity8/shell/tests/test_notifications.py+14−57
@@ -21,6 +21,7 @@
21
21
22from __future__ import absolute_import
22from __future__ import absolute_import
23
23
24+from unity8 import shell
24from unity8.process_helpers import unlock_unity
25from unity8.process_helpers import unlock_unity
25from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
26from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
26
27
@@ -64,7 +65,8 @@ class NotificationsBase(UnityTestCase):
64 if os.path.abspath(__file__).startswith('/usr/'):
65 if os.path.abspath(__file__).startswith('/usr/'):
65 return '/usr/share/unity8/graphics/' + icon_name
66 return '/usr/share/unity8/graphics/' + icon_name
66 else:
67 else:
67− return os.path.dirname(__file__) + "/../../../../../qml/graphics/" + icon_name
68+ return os.path.dirname(__file__) + \
69+ "/../../../../../qml/graphics/" + icon_name
68
70
69 def _get_notifications_list(self):
71 def _get_notifications_list(self):
70 return self.main_window.select_single(
72 return self.main_window.select_single(
@@ -354,7 +356,7 @@ class EphemeralNotificationsTests(NotificationsBase):
354 )
356 )
355 ]
357 ]
356
358
357− notification = self._create_ephemeral_notification(
359+ notification = shell.create_ephemeral_notification(
358 summary,
360 summary,
359 body,
361 body,
360 icon_path,
362 icon_path,
@@ -391,7 +393,7 @@ class EphemeralNotificationsTests(NotificationsBase):
391 )
393 )
392 ]
394 ]
393
395
394− notification = self._create_ephemeral_notification(
396+ notification = shell.create_ephemeral_notification(
395 summary,
397 summary,
396 None,
398 None,
397 None,
399 None,
@@ -434,7 +436,7 @@ class EphemeralNotificationsTests(NotificationsBase):
434 body_critical = 'Dude, this is so urgent you have no idea :)'
436 body_critical = 'Dude, this is so urgent you have no idea :)'
435 icon_path_critical = self._get_icon_path('avatars/anna_olsson.png')
437 icon_path_critical = self._get_icon_path('avatars/anna_olsson.png')
436
438
437− notification_normal = self._create_ephemeral_notification(
439+ notification_normal = shell.create_ephemeral_notification(
438 summary_normal,
440 summary_normal,
439 body_normal,
441 body_normal,
440 icon_path_normal,
442 icon_path_normal,
@@ -442,7 +444,7 @@ class EphemeralNotificationsTests(NotificationsBase):
442 )
444 )
443 notification_normal.show()
445 notification_normal.show()
444
446
445− notification_low = self._create_ephemeral_notification(
447+ notification_low = shell.create_ephemeral_notification(
446 summary_low,
448 summary_low,
447 body_low,
449 body_low,
448 icon_path_low,
450 icon_path_low,
@@ -450,7 +452,7 @@ class EphemeralNotificationsTests(NotificationsBase):
450 )
452 )
451 notification_low.show()
453 notification_low.show()
452
454
453− notification_critical = self._create_ephemeral_notification(
455+ notification_critical = shell.create_ephemeral_notification(
454 summary_critical,
456 summary_critical,
455 body_critical,
457 body_critical,
456 icon_path_critical,
458 icon_path_critical,
@@ -511,7 +513,7 @@ class EphemeralNotificationsTests(NotificationsBase):
511 summary = 'Summary-Body'
513 summary = 'Summary-Body'
512 body = 'This is a superfluous notification'
514 body = 'This is a superfluous notification'
513
515
514− notification = self._create_ephemeral_notification(summary, body)
516+ notification = shell.create_ephemeral_notification(summary, body)
515 notification.show()
517 notification.show()
516
518
517 notification = notify_list.wait_select_single(
519 notification = notify_list.wait_select_single(
@@ -535,7 +537,7 @@ class EphemeralNotificationsTests(NotificationsBase):
535
537
536 summary = 'Summary-Only'
538 summary = 'Summary-Only'
537
539
538− notification = self._create_ephemeral_notification(summary)
540+ notification = shell.create_ephemeral_notification(summary)
539 notification.show()
541 notification.show()
540
542
541 notification = notify_list.wait_select_single(
543 notification = notify_list.wait_select_single(
@@ -555,7 +557,7 @@ class EphemeralNotificationsTests(NotificationsBase):
555 body = 'This is the original content of this notification-bubble.'
557 body = 'This is the original content of this notification-bubble.'
556 icon_path = self._get_icon_path('avatars/funky.png')
558 icon_path = self._get_icon_path('avatars/funky.png')
557
559
558− notification = self._create_ephemeral_notification(
560+ notification = shell.create_ephemeral_notification(
559 summary,
561 summary,
560 body,
562 body,
561 icon_path
563 icon_path
@@ -595,7 +597,7 @@ class EphemeralNotificationsTests(NotificationsBase):
595 icon_path = self._get_icon_path('avatars/anna_olsson.png')
597 icon_path = self._get_icon_path('avatars/anna_olsson.png')
596 hint_icon = self._get_icon_path('applicationIcons/phone-app.png')
598 hint_icon = self._get_icon_path('applicationIcons/phone-app.png')
597
599
598− notification = self._create_ephemeral_notification(
600+ notification = shell.create_ephemeral_notification(
599 summary,
601 summary,
600 body,
602 body,
601 icon_path
603 icon_path
@@ -639,7 +641,7 @@ class EphemeralNotificationsTests(NotificationsBase):
639 body = 'Hey Bro Coly!'
641 body = 'Hey Bro Coly!'
640 icon_path = self._get_icon_path('avatars/amanda.png')
642 icon_path = self._get_icon_path('avatars/amanda.png')
641 body_sum = body
643 body_sum = body
642− notification = self._create_ephemeral_notification(
644+ notification = shell.create_ephemeral_notification(
643 summary,
645 summary,
644 body,
646 body,
645 icon_path,
647 icon_path,
@@ -675,7 +677,7 @@ class EphemeralNotificationsTests(NotificationsBase):
675 for new_body in bodies:
677 for new_body in bodies:
676 body = new_body
678 body = new_body
677 body_sum += '\n' + body
679 body_sum += '\n' + body
678− notification = self._create_ephemeral_notification(
680+ notification = shell.create_ephemeral_notification(
679 summary,
681 summary,
680 body,
682 body,
681 icon_path,
683 icon_path,
@@ -696,48 +698,3 @@ class EphemeralNotificationsTests(NotificationsBase):
696 False,
698 False,
697 1.0
699 1.0
698 )
700 )
699−
700− def _create_ephemeral_notification(
701− self,
702− summary="",
703− body="",
704− icon=None,
705− hints=[],
706− urgency="NORMAL"
707− ):
708− """Create an ephemeral (non-interactive) notification
709−
710− :param summary: Summary text for the notification
711− :param body: Body text to display in the notification
712− :param icon: Path string to the icon to use
713− :param hint_strings: List of tuples containing the 'name' and value
714− for setting the hint strings for the notification
715− :param urgency: Urgency string for the noticiation, either: 'LOW',
716− 'NORMAL', 'CRITICAL'
717−
718− """
719− logger.info(
720− "Creating ephemeral: summary(%s), body(%s), urgency(%r) "
721− "and Icon(%s)",
722− summary,
723− body,
724− urgency,
725− icon
726− )
727−
728− n = Notify.Notification.new(summary, body, icon)
729−
730− for hint in hints:
731− key, value = hint
732− n.set_hint_string(key, value)
733− logger.info("Adding hint to notification: (%s, %s)", key, value)
734− n.set_urgency(self._get_urgency(urgency))
735−
736− return n
737−
738− def _get_urgency(self, urgency):
739− """Translates urgency string to enum."""
740− _urgency_enums = {'LOW': Notify.Urgency.LOW,
741− 'NORMAL': Notify.Urgency.NORMAL,
742− 'CRITICAL': Notify.Urgency.CRITICAL}
743− return _urgency_enums.get(urgency.upper())