Debian Codegraph @

tests/test_plugin_windows.py

raw symbols SCIP
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 from importlib import reload
29
30 # Disable logging for a cleaner testing output
31 import logging
32 import sys
33 import types
34 from unittest import mock
35
36 import pytest
37
38 import apprise
39
40 logging.disable(logging.CRITICAL)
41
42
43 @pytest.fixture
44 def mocked_win32_environment():
45 """Inject fake win32 modules, reload apprise, and fully restore
46 sys.modules on teardown.
47
48 Previously the setup lived inside test_plugin_windows_mocked itself,
49 with no cleanup. That permanently abandoned all apprise.* module
50 objects, causing order-dependent failures in every test that runs
51 afterwards and holds a module-level `from apprise.plugins.X import`
52 reference. A fixture with a yield guarantees cleanup even on failure.
53 """
54
55 # Build fake win32api
56 win32api = types.ModuleType("win32api")
57 win32api.GetModuleHandle = mock.Mock(name="win32api.GetModuleHandle")
58 win32api.PostQuitMessage = mock.Mock(name="win32api.PostQuitMessage")
59
60 # Build fake win32con
61 win32con = types.ModuleType("win32con")
62 win32con.CW_USEDEFAULT = mock.Mock(name="win32con.CW_USEDEFAULT")
63 win32con.IDI_APPLICATION = mock.Mock(name="win32con.IDI_APPLICATION")
64 win32con.IMAGE_ICON = mock.Mock(name="win32con.IMAGE_ICON")
65 win32con.LR_DEFAULTSIZE = 1
66 win32con.LR_LOADFROMFILE = 2
67 win32con.WM_DESTROY = mock.Mock(name="win32con.WM_DESTROY")
68 win32con.WM_USER = 0
69 win32con.WS_OVERLAPPED = 1
70 win32con.WS_SYSMENU = 2
71
72 # Build fake win32gui
73 win32gui = types.ModuleType("win32gui")
74 win32gui.CreateWindow = mock.Mock(name="win32gui.CreateWindow")
75 win32gui.DestroyWindow = mock.Mock(name="win32gui.DestroyWindow")
76 win32gui.LoadIcon = mock.Mock(name="win32gui.LoadIcon")
77 win32gui.LoadImage = mock.Mock(name="win32gui.LoadImage")
78 win32gui.NIF_ICON = 1
79 win32gui.NIF_INFO = mock.Mock(name="win32gui.NIF_INFO")
80 win32gui.NIF_MESSAGE = 2
81 win32gui.NIF_TIP = 4
82 win32gui.NIM_ADD = mock.Mock(name="win32gui.NIM_ADD")
83 win32gui.NIM_DELETE = mock.Mock(name="win32gui.NIM_DELETE")
84 win32gui.NIM_MODIFY = mock.Mock(name="win32gui.NIM_MODIFY")
85 win32gui.RegisterClass = mock.Mock(name="win32gui.RegisterClass")
86 win32gui.UnregisterClass = mock.Mock(name="win32gui.UnregisterClass")
87 win32gui.Shell_NotifyIcon = mock.Mock(name="win32gui.Shell_NotifyIcon")
88 win32gui.UpdateWindow = mock.Mock(name="win32gui.UpdateWindow")
89 win32gui.WNDCLASS = mock.Mock(name="win32gui.WNDCLASS")
90
91 # Save all apprise.* module references before wiping them.
92 # These are the same module objects whose __dict__ entries are
93 # pointed to by module-level imports in other test files; restoring
94 # them keeps those references valid after the test completes.
95 saved_apprise = {
96 k: v for k, v in sys.modules.items() if k.startswith("apprise.")
97 }
98
99 # Inject win32 stubs so the plugin's top-level import succeeds,
100 # then wipe apprise.* and reload so it picks them up.
101 sys.modules["win32api"] = win32api
102 sys.modules["win32con"] = win32con
103 sys.modules["win32gui"] = win32gui
104 for mod in list(saved_apprise):
105 del sys.modules[mod]
106 reload(apprise)
107
108 # Yield win32gui -- the test uses it to configure side-effects
109 yield win32gui
110
111 # --- Teardown ---
112 # Remove all apprise.* entries created during this test
113 for mod in [k for k in sys.modules if k.startswith("apprise.")]:
114 del sys.modules[mod]
115
116 # Remove the injected win32 stubs
117 for stub in ("win32api", "win32con", "win32gui"):
118 sys.modules.pop(stub, None)
119
120 # Restore the original apprise.* module objects so that any
121 # module-level `from apprise.plugins.X import NotifyX` in other
122 # test files continues to point at live (non-abandoned) module dicts.
123 sys.modules.update(saved_apprise)
124
125
126 @pytest.mark.skipif(
127 (
128 "win32api" in sys.modules
129 or "win32con" in sys.modules
130 or "win32gui" in sys.modules
131 ),
132 reason="Requires non-windows platform",
133 )
134 def test_plugin_windows_mocked(mocked_win32_environment):
135 """NotifyWindows() General Checks (via non-Windows platform)"""
136
137 # Retrieve the win32gui mock that the fixture set up
138 win32gui = mocked_win32_environment
139
140 # Create our instance
141 obj = apprise.Apprise.instantiate("windows://", suppress_exceptions=False)
142 obj.duration = 0
143
144 # Test URL functionality
145 assert isinstance(obj.url(), str)
146
147 # Verify that a URL ID can not be generated
148 assert obj.url_id() is None
149
150 # Check that it found our mocked environments
151 assert obj.enabled is True
152
153 # _on_destroy check
154 obj._on_destroy(0, "", 0, 0)
155
156 # test notifications
157 assert (
158 obj.notify(
159 title="title", body="body", notify_type=apprise.NotifyType.INFO
160 )
161 is True
162 )
163
164 obj = apprise.Apprise.instantiate(
165 "windows://_/?image=True", suppress_exceptions=False
166 )
167 obj.duration = 0
168 assert isinstance(obj.url(), str)
169 assert (
170 obj.notify(
171 title="title", body="body", notify_type=apprise.NotifyType.INFO
172 )
173 is True
174 )
175
176 obj = apprise.Apprise.instantiate(
177 "windows://_/?image=False", suppress_exceptions=False
178 )
179 obj.duration = 0
180 assert isinstance(obj.url(), str)
181 assert (
182 obj.notify(
183 title="title", body="body", notify_type=apprise.NotifyType.INFO
184 )
185 is True
186 )
187
188 obj = apprise.Apprise.instantiate(
189 "windows://_/?duration=1", suppress_exceptions=False
190 )
191 assert isinstance(obj.url(), str)
192 # loads okay
193 assert obj.duration == 1
194 assert (
195 obj.notify(
196 title="title", body="body", notify_type=apprise.NotifyType.INFO
197 )
198 is True
199 )
200
201 obj = apprise.Apprise.instantiate(
202 "windows://_/?duration=invalid", suppress_exceptions=False
203 )
204 # Falls back to default
205 assert obj.duration == obj.default_popup_duration_sec
206
207 obj = apprise.Apprise.instantiate(
208 "windows://_/?duration=-1", suppress_exceptions=False
209 )
210 # Falls back to default
211 assert obj.duration == obj.default_popup_duration_sec
212
213 obj = apprise.Apprise.instantiate(
214 "windows://_/?duration=0", suppress_exceptions=False
215 )
216 # Falls back to default
217 assert obj.duration == obj.default_popup_duration_sec
218
219 # To avoid slowdowns (for testing), turn it to zero for now
220 obj.duration = 0
221
222 # Test our loading of our icon exception; it will still allow the
223 # notification to be sent
224 win32gui.LoadImage.side_effect = AttributeError
225 assert (
226 obj.notify(
227 title="title", body="body", notify_type=apprise.NotifyType.INFO
228 )
229 is True
230 )
231 # Undo our change
232 win32gui.LoadImage.side_effect = None
233
234 # Test our global exception handling
235 win32gui.UpdateWindow.side_effect = AttributeError
236 assert (
237 obj.notify(
238 title="title", body="body", notify_type=apprise.NotifyType.INFO
239 )
240 is False
241 )
242 # Undo our change
243 win32gui.UpdateWindow.side_effect = None
244
245 # Toggle our testing for when we can't send notifications because the
246 # package has been made unavailable to us
247 obj.enabled = False
248 assert (
249 obj.notify(
250 title="title", body="body", notify_type=apprise.NotifyType.INFO
251 )
252 is False
253 )
254
255
256 @pytest.mark.skipif(
257 "win32api" not in sys.modules
258 and "win32con" not in sys.modules
259 and "win32gui" not in sys.modules,
260 reason="Requires win32api, win32con, and win32gui",
261 )
262 @mock.patch("win32gui.UpdateWindow")
263 @mock.patch("win32gui.Shell_NotifyIcon")
264 @mock.patch("win32gui.LoadImage")
265 def test_plugin_windows_native(
266 mock_loadimage, mock_notify, mock_update_window
267 ):
268 """NotifyWindows() General Checks (via Windows platform)"""
269
270 # Create our instance
271 obj = apprise.Apprise.instantiate("windows://", suppress_exceptions=False)
272 obj.duration = 0
273
274 # Test URL functionality
275 assert isinstance(obj.url(), str)
276
277 # Check that it found our mocked environments
278 assert obj.enabled is True
279
280 # _on_destroy check
281 obj._on_destroy(0, "", 0, 0)
282
283 # test notifications
284 assert (
285 obj.notify(
286 title="title", body="body", notify_type=apprise.NotifyType.INFO
287 )
288 is True
289 )
290
291 obj = apprise.Apprise.instantiate(
292 "windows://_/?image=True", suppress_exceptions=False
293 )
294 obj.duration = 0
295 assert isinstance(obj.url(), str)
296 assert (
297 obj.notify(
298 title="title", body="body", notify_type=apprise.NotifyType.INFO
299 )
300 is True
301 )
302
303 obj = apprise.Apprise.instantiate(
304 "windows://_/?image=False", suppress_exceptions=False
305 )
306 obj.duration = 0
307 assert isinstance(obj.url(), str)
308 assert (
309 obj.notify(
310 title="title", body="body", notify_type=apprise.NotifyType.INFO
311 )
312 is True
313 )
314
315 obj = apprise.Apprise.instantiate(
316 "windows://_/?duration=1", suppress_exceptions=False
317 )
318 assert isinstance(obj.url(), str)
319 assert (
320 obj.notify(
321 title="title", body="body", notify_type=apprise.NotifyType.INFO
322 )
323 is True
324 )
325 # loads okay
326 assert obj.duration == 1
327
328 obj = apprise.Apprise.instantiate(
329 "windows://_/?duration=invalid", suppress_exceptions=False
330 )
331 # Falls back to default
332 assert obj.duration == obj.default_popup_duration_sec
333
334 obj = apprise.Apprise.instantiate(
335 "windows://_/?duration=-1", suppress_exceptions=False
336 )
337 # Falls back to default
338 assert obj.duration == obj.default_popup_duration_sec
339
340 obj = apprise.Apprise.instantiate(
341 "windows://_/?duration=0", suppress_exceptions=False
342 )
343 # Falls back to default
344 assert obj.duration == obj.default_popup_duration_sec
345
346 # To avoid slowdowns (for testing), turn it to zero for now
347 obj = apprise.Apprise.instantiate("windows://", suppress_exceptions=False)
348 obj.duration = 0
349
350 # Test our loading of our icon exception; it will still allow the
351 # notification to be sent
352 mock_loadimage.side_effect = AttributeError
353 assert (
354 obj.notify(
355 title="title", body="body", notify_type=apprise.NotifyType.INFO
356 )
357 is True
358 )
359 # Undo our change
360 mock_loadimage.side_effect = None
361
362 # Test our global exception handling
363 mock_update_window.side_effect = AttributeError
364 assert (
365 obj.notify(
366 title="title", body="body", notify_type=apprise.NotifyType.INFO
367 )
368 is False
369 )
370 # Undo our change
371 mock_update_window.side_effect = None
372
373 # Toggle our testing for when we can't send notifications because the
374 # package has been made unavailable to us
375 obj.enabled = False
376 assert (
377 obj.notify(
378 title="title", body="body", notify_type=apprise.NotifyType.INFO
379 )
380 is False
381 )