Debian Codegraph @

tests/unit_tests/test_ax_utilities_application.py

raw
1 # Unit tests for ax_utilities_application.py methods.
2 #
3 # Copyright 2025 Igalia, S.L.
4 # Author: Joanmarie Diggs <[email protected]>
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library 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 GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
19 # Boston MA 02110-1301 USA.
20
21 # pylint: disable=too-many-public-methods
22 # pylint: disable=wrong-import-position
23 # pylint: disable=too-many-lines
24 # pylint: disable=too-many-arguments
25 # pylint: disable=too-many-positional-arguments
26 # pylint: disable=import-outside-toplevel
27
28 """Unit tests for ax_utilities_application.py methods."""
29
30 from __future__ import annotations
31
32 from typing import TYPE_CHECKING
33
34 import gi
35 import pytest
36
37 gi.require_version("Atspi", "2.0")
38 from gi.repository import Atspi, GLib
39
40 if TYPE_CHECKING:
41 from .orca_test_context import OrcaTestContext
42
43
44 @pytest.mark.unit
45 class TestAXUtilitiesApplication:
46 """Test AXUtilitiesApplication class methods."""
47
48 def _setup_dependencies(self, test_context: OrcaTestContext):
49 """Set up mocks for ax_utilities_application dependencies."""
50
51 additional_modules = ["subprocess"]
52 essential_modules = test_context.setup_shared_dependencies(additional_modules)
53
54 debug_mock = essential_modules["orca.debug"]
55 debug_mock.print_message = test_context.Mock()
56 debug_mock.print_tokens = test_context.Mock()
57 debug_mock.LEVEL_INFO = 800
58
59 ax_object_class_mock = test_context.Mock()
60 ax_object_class_mock.is_valid = test_context.Mock(return_value=True)
61 ax_object_class_mock.get_name = test_context.Mock(return_value="")
62 ax_object_class_mock.get_child_count = test_context.Mock(return_value=0)
63 ax_object_class_mock.get_parent = test_context.Mock(return_value=None)
64 ax_object_class_mock.iter_children = test_context.Mock(return_value=[])
65 essential_modules["orca.ax_object"].AXObject = ax_object_class_mock
66
67 subprocess_mock = essential_modules["subprocess"]
68 subprocess_mock.getoutput = test_context.Mock(return_value="")
69
70 return essential_modules
71
72 @pytest.mark.parametrize(
73 "method_name, atspi_method_name, success_value",
74 [
75 pytest.param(
76 "get_application_toolkit_name",
77 "get_toolkit_name",
78 "Gtk",
79 id="toolkit_name",
80 ),
81 pytest.param(
82 "get_application_toolkit_version",
83 "get_toolkit_version",
84 "3.0",
85 id="toolkit_version",
86 ),
87 ],
88 )
89 def test_toolkit_info_methods(
90 self,
91 test_context,
92 method_name,
93 atspi_method_name,
94 success_value,
95 ) -> None:
96 """Test AXUtilitiesApplication toolkit methods."""
97
98 self._setup_dependencies(test_context)
99 from orca.ax_utilities_application import AXUtilitiesApplication
100
101 mock_obj = test_context.Mock(spec=Atspi.Accessible)
102 mock_app = test_context.Mock(spec=Atspi.Accessible)
103 method = getattr(AXUtilitiesApplication, method_name)
104
105 test_context.patch_object(AXUtilitiesApplication, "get_application", return_value=None)
106 result = method(mock_obj)
107 assert result == ""
108
109 test_context.patch_object(AXUtilitiesApplication, "get_application", return_value=mock_app)
110 test_context.patch_object(Atspi.Accessible, atspi_method_name, return_value=success_value)
111 result = method(mock_obj)
112 assert result == success_value
113
114 def raise_glib_error(self):
115 raise GLib.GError("Test error")
116
117 test_context.patch_object(Atspi.Accessible, atspi_method_name, side_effect=raise_glib_error)
118 result = method(mock_obj)
119 assert result == ""
120
121 @pytest.mark.parametrize(
122 "method_name, atspi_method, mock_method_name, success_return, error_return",
123 [
124 pytest.param("get_desktop", Atspi, "get_desktop", "mock_desktop", None, id="desktop"),
125 pytest.param(
126 "get_process_id",
127 Atspi.Accessible,
128 "get_process_id",
129 1234,
130 -1,
131 id="process_id",
132 ),
133 ],
134 )
135 def test_glib_error_handling_methods(
136 self,
137 test_context,
138 method_name,
139 atspi_method,
140 mock_method_name,
141 success_return,
142 error_return,
143 ) -> None:
144 """Test AXUtilitiesApplication GLib error handling."""
145
146 self._setup_dependencies(test_context)
147 from orca.ax_utilities_application import AXUtilitiesApplication
148
149 if method_name == "get_desktop":
150 mock_desktop = test_context.Mock(spec=Atspi.Accessible)
151
152 test_context.patch_object(atspi_method, mock_method_name, return_value=mock_desktop)
153 result = AXUtilitiesApplication.get_desktop()
154 assert result == mock_desktop
155
156 def raise_glib_error(self):
157 raise GLib.GError("Test error")
158
159 test_context.patch_object(atspi_method, mock_method_name, side_effect=raise_glib_error)
160 result = AXUtilitiesApplication.get_desktop()
161 assert result is error_return
162 else:
163 mock_obj = test_context.Mock(spec=Atspi.Accessible)
164 method = getattr(AXUtilitiesApplication, method_name)
165
166 test_context.patch_object(atspi_method, mock_method_name, return_value=success_return)
167 result = method(mock_obj)
168 assert result == success_return
169
170 def raise_glib_error(self):
171 raise GLib.GError("Test error")
172
173 test_context.patch_object(atspi_method, mock_method_name, side_effect=raise_glib_error)
174 result = method(mock_obj)
175 assert result == error_return
176
177 @pytest.mark.parametrize(
178 "app_exists,app_name,toolkit_name,toolkit_version,expected",
179 [
180 (True, "Firefox", "Gtk", "3.0", "Firefox (Gtk 3.0)"),
181 (False, "", "", "", ""),
182 ],
183 )
184 def test_application_as_string(
185 self,
186 test_context: OrcaTestContext,
187 app_exists: bool,
188 app_name: str,
189 toolkit_name: str,
190 toolkit_version: str,
191 expected: str,
192 ) -> None:
193 """Test AXUtilitiesApplication.application_as_string with different scenarios."""
194
195 self._setup_dependencies(test_context)
196 from orca.ax_object import AXObject
197 from orca.ax_utilities_application import AXUtilitiesApplication
198
199 mock_obj = test_context.Mock(spec=Atspi.Accessible)
200 if app_exists:
201 mock_app = test_context.Mock(spec=Atspi.Accessible)
202 test_context.patch_object(
203 AXUtilitiesApplication,
204 "get_application",
205 return_value=mock_app,
206 )
207 test_context.patch_object(AXObject, "get_name", return_value=app_name)
208 test_context.patch_object(
209 AXUtilitiesApplication,
210 "get_application_toolkit_name",
211 return_value=toolkit_name,
212 )
213 test_context.patch_object(
214 AXUtilitiesApplication,
215 "get_application_toolkit_version",
216 return_value=toolkit_version,
217 )
218 else:
219 test_context.patch_object(AXUtilitiesApplication, "get_application", return_value=None)
220 result = AXUtilitiesApplication.application_as_string(mock_obj)
221 assert result == expected
222
223 def test_get_all_applications_no_desktop(self, test_context: OrcaTestContext) -> None:
224 """Test AXUtilitiesApplication.get_all_applications with no desktop."""
225
226 self._setup_dependencies(test_context)
227 from orca.ax_utilities_application import AXUtilitiesApplication
228
229 test_context.patch_object(AXUtilitiesApplication, "get_desktop", return_value=None)
230 result = AXUtilitiesApplication.get_all_applications()
231 assert not result
232
233 def test_get_all_applications(self, test_context: OrcaTestContext) -> None:
234 """Test AXUtilitiesApplication.get_all_applications."""
235
236 self._setup_dependencies(test_context)
237 from orca.ax_object import AXObject
238 from orca.ax_utilities_application import AXUtilitiesApplication
239
240 mock_desktop = test_context.Mock(spec=Atspi.Accessible)
241 mock_app1 = test_context.Mock(spec=Atspi.Accessible)
242 mock_app2 = test_context.Mock(spec=Atspi.Accessible)
243 test_context.patch_object(AXUtilitiesApplication, "get_desktop", return_value=mock_desktop)
244 test_context.patch_object(
245 AXObject,
246 "iter_children",
247 side_effect=lambda parent, pred: filter(pred, [mock_app1, mock_app2]),
248 )
249 test_context.patch_object(
250 AXObject,
251 "get_name",
252 side_effect=lambda obj: "App1" if obj == mock_app1 else "App2",
253 )
254 result = AXUtilitiesApplication.get_all_applications()
255 assert result == [mock_app1, mock_app2]
256
257 def test_get_all_applications_excludes_unresponsive(
258 self,
259 test_context: OrcaTestContext,
260 ) -> None:
261 """Test AXUtilitiesApplication.get_all_applications excludes unresponsive."""
262
263 self._setup_dependencies(test_context)
264 from orca.ax_object import AXObject
265 from orca.ax_utilities_application import AXUtilitiesApplication
266
267 mock_desktop = test_context.Mock(spec=Atspi.Accessible)
268 mock_app1 = test_context.Mock(spec=Atspi.Accessible)
269 mock_app2 = test_context.Mock(spec=Atspi.Accessible)
270 test_context.patch_object(AXUtilitiesApplication, "get_desktop", return_value=mock_desktop)
271 test_context.patch_object(
272 AXObject,
273 "iter_children",
274 side_effect=lambda parent, pred: filter(pred, [mock_app1, mock_app2]),
275 )
276 test_context.patch_object(
277 AXObject,
278 "get_name",
279 side_effect=lambda obj: "App1" if obj == mock_app1 else "App2",
280 )
281 test_context.patch_object(
282 AXUtilitiesApplication,
283 "is_application_unresponsive",
284 side_effect=lambda obj: obj == mock_app2,
285 )
286 result = AXUtilitiesApplication.get_all_applications(exclude_unresponsive=True)
287 assert result == [mock_app1]
288
289 def test_get_all_applications_excludes_mutter_x11_frames(
290 self,
291 test_context: OrcaTestContext,
292 ) -> None:
293 """Test AXUtilitiesApplication.get_all_applications excludes mutter-x11-frames."""
294
295 self._setup_dependencies(test_context)
296 from orca.ax_object import AXObject
297 from orca.ax_utilities_application import AXUtilitiesApplication
298
299 mock_desktop = test_context.Mock(spec=Atspi.Accessible)
300 mock_app1 = test_context.Mock(spec=Atspi.Accessible)
301 mock_app2 = test_context.Mock(spec=Atspi.Accessible)
302 test_context.patch_object(AXUtilitiesApplication, "get_desktop", return_value=mock_desktop)
303 test_context.patch_object(
304 AXObject,
305 "iter_children",
306 side_effect=lambda parent, pred: filter(pred, [mock_app1, mock_app2]),
307 )
308 test_context.patch_object(
309 AXObject,
310 "get_name",
311 side_effect=lambda obj: "mutter-x11-frames" if obj == mock_app1 else "App2",
312 )
313 result = AXUtilitiesApplication.get_all_applications()
314 assert result == [mock_app2]
315
316 def test_get_all_applications_includes_mutter_x11_frames_in_debug(
317 self,
318 test_context: OrcaTestContext,
319 ) -> None:
320 """Test AXUtilitiesApplication.get_all_applications includes mutter-x11-frames in debug."""
321
322 self._setup_dependencies(test_context)
323 from orca.ax_object import AXObject
324 from orca.ax_utilities_application import AXUtilitiesApplication
325
326 mock_desktop = test_context.Mock(spec=Atspi.Accessible)
327 mock_app1 = test_context.Mock(spec=Atspi.Accessible)
328 mock_app2 = test_context.Mock(spec=Atspi.Accessible)
329 test_context.patch_object(AXUtilitiesApplication, "get_desktop", return_value=mock_desktop)
330 test_context.patch_object(
331 AXObject,
332 "iter_children",
333 side_effect=lambda parent, pred: filter(pred, [mock_app1, mock_app2]),
334 )
335 test_context.patch_object(
336 AXObject,
337 "get_name",
338 side_effect=lambda obj: "mutter-x11-frames" if obj == mock_app1 else "App2",
339 )
340 result = AXUtilitiesApplication.get_all_applications(is_debug=True)
341 assert result == [mock_app1, mock_app2]
342
343 def test_get_all_applications_must_have_window(self, test_context: OrcaTestContext) -> None:
344 """Test AXUtilitiesApplication.get_all_applications filters without windows."""
345
346 self._setup_dependencies(test_context)
347 from orca.ax_object import AXObject
348 from orca.ax_utilities_application import AXUtilitiesApplication
349
350 mock_desktop = test_context.Mock(spec=Atspi.Accessible)
351 mock_app1 = test_context.Mock(spec=Atspi.Accessible)
352 mock_app2 = test_context.Mock(spec=Atspi.Accessible)
353 test_context.patch_object(AXUtilitiesApplication, "get_desktop", return_value=mock_desktop)
354 test_context.patch_object(
355 AXObject,
356 "iter_children",
357 side_effect=lambda parent, pred: filter(pred, [mock_app1, mock_app2]),
358 )
359 test_context.patch_object(
360 AXObject,
361 "get_name",
362 side_effect=lambda obj: "App1" if obj == mock_app1 else "App2",
363 )
364 test_context.patch_object(
365 AXObject,
366 "get_child_count",
367 side_effect=lambda obj: 1 if obj == mock_app1 else 0,
368 )
369 result = AXUtilitiesApplication.get_all_applications(must_have_window=True)
370 assert result == [mock_app1]
371
372 def test_get_application_with_none(self, test_context: OrcaTestContext) -> None:
373 """Test AXUtilitiesApplication.get_application with None."""
374
375 self._setup_dependencies(test_context)
376 from orca.ax_utilities_application import AXUtilitiesApplication
377
378 result = AXUtilitiesApplication.get_application(None)
379 assert result is None
380
381 def test_get_application_with_valid_object(self, test_context: OrcaTestContext) -> None:
382 """Test AXUtilitiesApplication.get_application."""
383
384 self._setup_dependencies(test_context)
385 from orca.ax_utilities_application import AXUtilitiesApplication
386
387 mock_obj = test_context.Mock(spec=Atspi.Accessible)
388 mock_app = test_context.Mock(spec=Atspi.Accessible)
389 test_context.patch_object(Atspi.Accessible, "get_application", return_value=mock_app)
390 result = AXUtilitiesApplication.get_application(mock_obj)
391 assert result == mock_app
392
393 def test_get_application_with_glib_error(self, test_context: OrcaTestContext) -> None:
394 """Test AXUtilitiesApplication.get_application handles GLib.GError."""
395
396 self._setup_dependencies(test_context)
397 from orca.ax_utilities_application import AXUtilitiesApplication
398
399 mock_obj = test_context.Mock(spec=Atspi.Accessible)
400
401 def raise_glib_error(self):
402 raise GLib.GError("Test error")
403
404 test_context.patch_object(Atspi.Accessible, "get_application", side_effect=raise_glib_error)
405 result = AXUtilitiesApplication.get_application(mock_obj)
406 assert result is None
407
408 @pytest.mark.parametrize(
409 "search_pid,expected_found",
410 [
411 (1234, True),
412 (9999, False),
413 ],
414 )
415 def test_get_application_with_pid(
416 self,
417 test_context: OrcaTestContext,
418 search_pid: int,
419 expected_found: bool,
420 ) -> None:
421 """Test AXUtilitiesApplication.get_application_with_pid with different scenarios."""
422
423 self._setup_dependencies(test_context)
424 from orca.ax_utilities_application import AXUtilitiesApplication
425
426 mock_app1 = test_context.Mock(spec=Atspi.Accessible)
427 mock_app2 = test_context.Mock(spec=Atspi.Accessible)
428 test_context.patch_object(
429 AXUtilitiesApplication,
430 "get_all_applications",
431 return_value=[mock_app1, mock_app2],
432 )
433 test_context.patch_object(
434 AXUtilitiesApplication,
435 "get_process_id",
436 side_effect=lambda obj: 1234 if obj == mock_app1 else 5678,
437 )
438 result = AXUtilitiesApplication.get_application_with_pid(search_pid)
439 if expected_found:
440 assert result == mock_app1
441 else:
442 assert result is None
443
444 @pytest.mark.parametrize(
445 "search_app_index,expected_found",
446 [
447 (0, True),
448 (2, False),
449 ],
450 )
451 def test_is_application_in_desktop(
452 self,
453 test_context: OrcaTestContext,
454 search_app_index: int,
455 expected_found: bool,
456 ) -> None:
457 """Test AXUtilitiesApplication.is_application_in_desktop with different scenarios."""
458
459 self._setup_dependencies(test_context)
460 from orca.ax_utilities_application import AXUtilitiesApplication
461
462 mock_app1 = test_context.Mock(spec=Atspi.Accessible)
463 mock_app2 = test_context.Mock(spec=Atspi.Accessible)
464 mock_app3 = test_context.Mock(spec=Atspi.Accessible)
465 test_apps = [mock_app1, mock_app2, mock_app3]
466 test_context.patch_object(
467 AXUtilitiesApplication,
468 "get_all_applications",
469 return_value=[mock_app1, mock_app2],
470 )
471 search_app = test_apps[search_app_index]
472 result = AXUtilitiesApplication.is_application_in_desktop(search_app)
473 assert result is expected_found
474
475 @pytest.mark.parametrize(
476 "process_state,expected_unresponsive",
477 [
478 ("State: Z (zombie)", True),
479 ("State: T (stopped)", True),
480 ("State: R (running)", False),
481 ("", False),
482 ],
483 )
484 def test_is_application_unresponsive(
485 self,
486 test_context: OrcaTestContext,
487 process_state: str,
488 expected_unresponsive: bool,
489 ) -> None:
490 """Test AXUtilitiesApplication.is_application_unresponsive with different process states."""
491
492 self._setup_dependencies(test_context)
493 from orca.ax_utilities_application import AXUtilitiesApplication
494
495 mock_app = test_context.Mock(spec=Atspi.Accessible)
496 test_context.patch_object(AXUtilitiesApplication, "get_process_id", return_value=1234)
497 test_context.patch("subprocess.getoutput", return_value=process_state)
498 result = AXUtilitiesApplication.is_application_unresponsive(mock_app)
499 assert result is expected_unresponsive