1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
import shutil
import subprocess
import textwrap
import pytest
import libqtile.config
from libqtile.bar import Bar
from libqtile.widget import notify
# Bit of a hack... when we log a timer, save the delay in an attribute
# We'll use this to check message timeouts are being honoured.
def log_timeout(self, delay, func, method_args=None):
self.delay = delay
self.qtile.call_later(delay, func)
def notification(subject, body, urgency=None, timeout=None):
"""Function to build notification text and command list"""
cmds = []
urgs = {0: "low", 1: "normal", 2: "critical"}
urg_level = urgs.get(urgency, "normal")
if urg_level != "normal":
cmds += ["-u", urg_level]
if timeout:
cmds += ["-t", f"{timeout}"]
cmds += [subject, body]
text = '<span weight="bold">'
if urg_level != "normal":
text += '<span color="{{colour}}">'
text += "{subject}"
if urg_level != "normal":
text += "</span>"
text += "</span> - {body}"
text = text.format(subject=subject, body=body)
return text, cmds
# for Github CI/Ubuntu, "notify-send" is provided by libnotify-bin package
NS = shutil.which("notify-send")
BACKGROUND_NORMAL = "111111"
BACKGROUND_URGENT = "222222"
BACKGROUND_LOW = "333333"
URGENT = "#ff00ff"
LOW = "#cccccc"
MESSAGE_1, NOTIFICATION_1 = notification("Message 1", "Test Message 1", timeout=5000)
MESSAGE_2, NOTIFICATION_2 = notification(
"Urgent Message", "This is not a test!", urgency=2, timeout=10000
)
MESSAGE_3, NOTIFICATION_3 = notification("Low priority", "Windows closed unexpectedly", urgency=0)
DEFAULT_TIMEOUT_LOW = 15
DEFAULT_TIMEOUT_NORMAL = 30
DEFAULT_TIMEOUT_URGENT = 45
@pytest.mark.skipif(shutil.which("notify-send") is None, reason="notify-send not installed.")
@pytest.mark.usefixtures("dbus")
def test_notifications(manager_nospawn, minimal_conf_noscreen):
def background(obj):
_, bground = obj.eval("self.background")
return bground
notify.Notify.timeout_add = log_timeout
widget = notify.Notify(
foreground_urgent=URGENT,
foreground_low=LOW,
background=BACKGROUND_NORMAL,
background_urgent=BACKGROUND_URGENT,
background_low=BACKGROUND_LOW,
)
config = minimal_conf_noscreen
config.screens = [libqtile.config.Screen(top=Bar([widget], 10))]
manager_nospawn.start(config)
obj = manager_nospawn.c.widget["notify"]
# Send first notification and check time and display time
notif_1 = [NS]
notif_1.extend(NOTIFICATION_1)
subprocess.run(notif_1)
assert obj.info()["text"] == MESSAGE_1
assert background(obj) == BACKGROUND_NORMAL
_, timeout = obj.eval("self.delay")
assert timeout == "5.0"
# Send second notification and check time and display time
notif_2 = [NS]
notif_2.extend(NOTIFICATION_2)
subprocess.run(notif_2)
assert obj.info()["text"] == MESSAGE_2.format(colour=URGENT)
assert background(obj) == BACKGROUND_URGENT
_, timeout = obj.eval("self.delay")
assert timeout == "10.0"
# Send third notification
notif_3 = [NS]
notif_3.extend(NOTIFICATION_3)
subprocess.run(notif_3)
assert obj.info()["text"] == MESSAGE_3.format(colour=LOW)
assert background(obj) == BACKGROUND_LOW
# Navigation tests
# Hitting next while on last message should not change display
obj.next()
assert obj.info()["text"] == MESSAGE_3.format(colour=LOW)
assert background(obj) == BACKGROUND_LOW
# Show previous
obj.prev()
assert obj.info()["text"] == MESSAGE_2.format(colour=URGENT)
assert background(obj) == BACKGROUND_URGENT
# Show previous
obj.prev()
assert obj.info()["text"] == MESSAGE_1
assert background(obj) == BACKGROUND_NORMAL
# Show previous while on first message should stay on first
obj.prev()
assert obj.info()["text"] == MESSAGE_1
assert background(obj) == BACKGROUND_NORMAL
# Show next
obj.next()
assert obj.info()["text"] == MESSAGE_2.format(colour=URGENT)
assert background(obj) == BACKGROUND_URGENT
# Toggle display (clear)
obj.toggle()
assert obj.info()["text"] == ""
assert background(obj) == BACKGROUND_NORMAL
# Toggle display - restoring display shows last notification
obj.toggle()
assert obj.info()["text"] == MESSAGE_3.format(colour=LOW)
assert background(obj) == BACKGROUND_LOW
# Clear the dispay
obj.clear()
assert obj.info()["text"] == ""
assert background(obj) == BACKGROUND_NORMAL
# Show the display
obj.display()
assert obj.info()["text"] == MESSAGE_3.format(colour=LOW)
assert background(obj) == BACKGROUND_LOW
def test_capabilities():
# Default capabilities are "body" and "actions"
widget_with_actions = notify.Notify()
assert widget_with_actions.capabilities == {"body", "actions"}
# If the user chooses not to have actions, the capabilities
# are adjusted accordingly
widget_no_actions = notify.Notify(action=False)
assert widget_no_actions.capabilities == {"body"}
@pytest.mark.skipif(shutil.which("notify-send") is None, reason="notify-send not installed.")
@pytest.mark.usefixtures("dbus")
def test_invoke_and_clear(manager_nospawn, minimal_conf_noscreen):
# We need to create an object to listen for signals from the qtile
# notification server. This needs to be created within the manager
# object so we rely on "eval" applying "exec".
handler = textwrap.dedent(
"""
from libqtile.utils import add_signal_receiver, create_task
class SignalListener:
def __init__(self):
self.action_invoked = None
self.notification_closed = None
global add_signal_receiver
global create_task
create_task(
add_signal_receiver(
self.on_notification_closed,
session_bus=True,
signal_name="NotificationClosed"
)
)
create_task(
add_signal_receiver(
self.on_action_invoked,
session_bus=True,
signal_name="ActionInvoked"
)
)
def on_action_invoked(self, msg):
self.action_invoked = msg.body
def on_notification_closed(self, msg):
self.notification_closed = msg.body
self.signal_listener = SignalListener()
"""
)
# Create and send a custom notification with a list of actions.
# `utils.send_notfication` is not an option here as it does not
# expose actions so we need a lower-level call
notification_with_actions = textwrap.dedent(
"""
from dbus_fast import Variant
from dbus_fast.constants import MessageType
from libqtile.utils import _send_dbus_message, create_task
notification = [
"qtile",
2,
"",
"Test",
"Test with actions",
["default", "ok"],
{"urgency": Variant("y", 1)},
5000
]
create_task(
_send_dbus_message(
True,
MessageType.METHOD_CALL,
"org.freedesktop.Notifications",
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"Notify",
"susssasa{sv}i",
notification
)
)
"""
)
notify.Notify.timeout_add = log_timeout
widget = notify.Notify()
config = minimal_conf_noscreen
config.screens = [libqtile.config.Screen(top=Bar([widget], 10))]
# Start the manager
manager_nospawn.start(config)
# Create our signal listener
manager_nospawn.c.eval(handler)
_, result = manager_nospawn.c.eval("self.signal_listener")
# Send first notification and check time and display time
notif_1 = [NS]
notif_1.extend(NOTIFICATION_1)
subprocess.run(notif_1)
# Check that listener hasn't received any signals yet
_, result = manager_nospawn.c.eval("self.signal_listener.action_invoked")
assert result == "None"
_, result = manager_nospawn.c.eval("self.signal_listener.notification_closed")
assert result == "None"
# Clicking on notification dismisses it
manager_nospawn.c.bar["top"].fake_button_press(0, 0, button=1)
# Signal listener should get the id and close reason
# id is 1 and dismiss reason is ClosedReason.dismissed which is 2
_, result = manager_nospawn.c.eval("self.signal_listener.notification_closed")
assert result == "[1, 2]"
# Send a new notification with defined actions
_, res = manager_nospawn.c.eval(notification_with_actions)
# Right-clicking on notification invokes it
manager_nospawn.c.bar["top"].fake_button_press(0, 0, button=3)
# Signal listener should get the id and close reason
# id is 2 (as it is the second notification) and action is "default"
_, result = manager_nospawn.c.eval("self.signal_listener.action_invoked")
assert result == "[2, 'default']"
@pytest.mark.skipif(shutil.which("notify-send") is None, reason="notify-send not installed.")
@pytest.mark.usefixtures("dbus")
def test_parse_text(manager_nospawn, minimal_conf_noscreen):
def test_parser(text):
return f"TEST:{text}"
widget = notify.Notify(
foreground_urgent=URGENT,
foreground_low=LOW,
parse_text=test_parser,
)
config = minimal_conf_noscreen
config.screens = [libqtile.config.Screen(top=Bar([widget], 10))]
manager_nospawn.start(config)
obj = manager_nospawn.c.widget["notify"]
# Send first notification and check time and display time
notif_1 = [NS]
notif_1.extend(NOTIFICATION_1)
subprocess.run(notif_1)
assert obj.info()["text"] == f"TEST:{MESSAGE_1}"
@pytest.mark.usefixtures("dbus")
def test_unregister(manager_nospawn, minimal_conf_noscreen):
"""Short test to check if notifier deregisters correctly."""
def notifier_has_callbacks():
_, out = manager_nospawn.c.widget["notify"].eval("notifier.callbacks")
return out != "[]"
widget = notify.Notify()
config = minimal_conf_noscreen
config.screens = [libqtile.config.Screen(top=Bar([widget], 10))]
manager_nospawn.start(config)
assert notifier_has_callbacks()
_ = manager_nospawn.c.widget["notify"].eval("self.finalize()")
assert not notifier_has_callbacks()
@pytest.mark.parametrize(
"urgency,timeout",
[(0, DEFAULT_TIMEOUT_LOW), (1, DEFAULT_TIMEOUT_NORMAL), (2, DEFAULT_TIMEOUT_URGENT)],
)
@pytest.mark.skipif(shutil.which("notify-send") is None, reason="notify-send not installed.")
@pytest.mark.usefixtures("dbus")
def test_notifications_default_timeouts(manager_nospawn, minimal_conf_noscreen, urgency, timeout):
notify.Notify.timeout_add = log_timeout
widget = notify.Notify(
default_timeout_low=DEFAULT_TIMEOUT_LOW,
default_timeout=DEFAULT_TIMEOUT_NORMAL,
default_timeout_urgent=DEFAULT_TIMEOUT_URGENT,
)
config = minimal_conf_noscreen
config.screens = [libqtile.config.Screen(top=Bar([widget], 10))]
manager_nospawn.start(config)
obj = manager_nospawn.c.widget["notify"]
# Send first notification and check time and display time
notif = [NS]
notif.extend(notification("test", "test", urgency=urgency)[1])
subprocess.run(notif)
_, delay = obj.eval("self.delay")
assert delay == str(timeout)
|