Debian Codegraph @

tests/unit_tests/test_ax_utilities_debugging.py

raw
1 # Unit tests for ax_utilities_debugging.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=wrong-import-position
22 # pylint: disable=too-many-public-methods
23 # pylint: disable=import-outside-toplevel
24
25 """Unit tests for ax_utilities_debugging.py methods."""
26
27 from __future__ import annotations
28
29 from typing import TYPE_CHECKING
30
31 import gi
32 import pytest
33
34 gi.require_version("Atspi", "2.0")
35 from gi.repository import Atspi, GLib
36
37 if TYPE_CHECKING:
38 from .orca_test_context import OrcaTestContext
39
40
41 @pytest.mark.unit
42 class TestAXUtilitiesDebugging:
43 """Test debugging utility methods."""
44
45 def _setup_dependencies(self, test_context: OrcaTestContext):
46 """Returns dependencies for ax_utilities_debugging module testing."""
47
48 additional_modules = ["orca.ax_utilities_application", "orca.ax_utilities_relation"]
49 essential_modules = test_context.setup_shared_dependencies(additional_modules)
50
51 ax_object_class_mock = test_context.Mock()
52 ax_object_class_mock.is_valid = test_context.Mock(return_value=True)
53 ax_object_class_mock.is_dead = test_context.Mock(return_value=False)
54 ax_object_class_mock.get_name = test_context.Mock(return_value="test-object")
55 ax_object_class_mock.get_role_name = test_context.Mock(return_value="unknown")
56 ax_object_class_mock.get_description = test_context.Mock(return_value="")
57 ax_object_class_mock.get_help_text = test_context.Mock(return_value="")
58 ax_object_class_mock.get_accessible_id = test_context.Mock(return_value="")
59 ax_object_class_mock.get_path = test_context.Mock(return_value=[])
60 ax_object_class_mock.get_n_actions = test_context.Mock(return_value=0)
61 ax_object_class_mock.get_action_name = test_context.Mock(return_value="")
62 ax_object_class_mock.get_action_key_binding = test_context.Mock(return_value="")
63 ax_object_class_mock.get_attributes_dict = test_context.Mock(return_value={})
64 ax_object_class_mock.get_state_set = test_context.Mock(return_value=test_context.Mock())
65 ax_object_class_mock.supports_text = test_context.Mock(return_value=False)
66 essential_modules["orca.ax_object"].AXObject = ax_object_class_mock
67
68 app_class_mock = test_context.Mock()
69 app_class_mock.application_as_string = test_context.Mock(return_value="TestApp")
70 essential_modules["orca.ax_utilities_application"].AXUtilitiesApplication = app_class_mock
71
72 relation_class_mock = test_context.Mock()
73 relation_class_mock.get_relations = test_context.Mock(return_value=[])
74 relation_class_mock.get_relation_targets_for_debugging = test_context.Mock(return_value=[])
75 essential_modules["orca.ax_utilities_relation"].AXUtilitiesRelation = relation_class_mock
76
77 return essential_modules
78
79 @pytest.mark.parametrize(
80 "input_string, expected_result",
81 [
82 pytest.param("", "", id="empty_string"),
83 pytest.param("short text", "short text", id="short_text"),
84 pytest.param("text with\nnewline", "text with\\nnewline", id="newline_replacement"),
85 pytest.param("text with\ufffcobject", "text with[OBJ]object", id="object_replacement"),
86 pytest.param(
87 "this is a very long text with many words that should be truncated",
88 "this is a very long text with many words that should be truncated",
89 id="under_100_chars_no_truncation",
90 ),
91 pytest.param(
92 "word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 "
93 "word11 word12 word13 word14 word15 word16 word17 word18 word19 word20",
94 "word1 word2 word3 word4 word5 ... word16 word17 word18 word19 word20 (130 chars.)",
95 id="over_100_chars_truncation",
96 ),
97 ],
98 )
99 def test_format_string( # pylint: disable=too-many-arguments,too-many-positional-arguments
100 self,
101 input_string,
102 expected_result,
103 test_context,
104 ) -> None:
105 """Test AXUtilitiesDebugging._format_string."""
106
107 self._setup_dependencies(test_context)
108 from orca.ax_utilities_debugging import AXUtilitiesDebugging
109
110 result = AXUtilitiesDebugging._format_string(input_string) # pylint: disable=protected-access
111 assert result == expected_result
112
113 def test_as_string_accessible_object(self, test_context: OrcaTestContext) -> None:
114 """Test AXUtilitiesDebugging.as_string with Atspi.Accessible objects."""
115
116 self._setup_dependencies(test_context)
117 from orca.ax_utilities_debugging import AXUtilitiesDebugging
118
119 mock_obj = test_context.Mock(spec=Atspi.Accessible)
120
121 test_context.patch(
122 "orca.ax_utilities_debugging.AXObject.get_role_name",
123 return_value="button",
124 )
125 test_context.patch("orca.ax_utilities_debugging.AXObject.get_name", return_value="OK")
126 result = AXUtilitiesDebugging.as_string(mock_obj)
127 assert "button: 'OK'" in result
128 assert hex(id(mock_obj)) in result
129
130 def test_as_string_accessible_dead_object(self, test_context: OrcaTestContext) -> None:
131 """Test AXUtilitiesDebugging.as_string handles dead accessible objects."""
132
133 self._setup_dependencies(test_context)
134 from orca.ax_utilities_debugging import AXUtilitiesDebugging
135
136 mock_obj = test_context.Mock(spec=Atspi.Accessible)
137
138 test_context.patch("orca.ax_utilities_debugging.AXObject.get_role_name", return_value="")
139 test_context.patch("orca.ax_utilities_debugging.AXObject.get_name", return_value="")
140 result = AXUtilitiesDebugging.as_string(mock_obj)
141 assert "[DEAD" in result
142
143 def test_as_string_event_object(self, test_context: OrcaTestContext) -> None:
144 """Test AXUtilitiesDebugging.as_string with Atspi.Event objects."""
145
146 self._setup_dependencies(test_context)
147 from orca.ax_utilities_debugging import AXUtilitiesDebugging
148
149 mock_event = test_context.Mock(spec=Atspi.Event)
150 mock_event.type = "focus:in"
151 mock_event.source = test_context.Mock(spec=Atspi.Accessible)
152 mock_event.detail1 = 0
153 mock_event.detail2 = 0
154 mock_event.any_data = None
155
156 test_context.patch(
157 "orca.ax_utilities_debugging.AXObject.get_role_name",
158 return_value="button",
159 )
160 test_context.patch("orca.ax_utilities_debugging.AXObject.get_name", return_value="OK")
161 test_context.patch(
162 "orca.ax_utilities_debugging.AXUtilitiesApplication.application_as_string",
163 return_value="TestApp",
164 )
165 result = AXUtilitiesDebugging.as_string(mock_event)
166 assert "focus:in for" in result
167 assert "TestApp" in result
168
169 def test_actions_as_string(self, test_context: OrcaTestContext) -> None:
170 """Test AXUtilitiesDebugging.actions_as_string."""
171
172 self._setup_dependencies(test_context)
173 from orca.ax_utilities_debugging import AXUtilitiesDebugging
174
175 mock_obj = test_context.Mock(spec=Atspi.Accessible)
176
177 test_context.patch("orca.ax_utilities_debugging.AXAction.get_n_actions", return_value=2)
178
179 def mock_get_action_name(unused_obj, index):
180 return ["click", "focus"][index]
181
182 def mock_get_action_key_binding(unused_obj, index):
183 return ["Return", ""][index]
184
185 test_context.patch(
186 "orca.ax_utilities_debugging.AXAction.get_action_name",
187 new=mock_get_action_name,
188 )
189 test_context.patch(
190 "orca.ax_utilities_debugging.AXAction.get_action_key_binding",
191 new=mock_get_action_key_binding,
192 )
193 result = AXUtilitiesDebugging.actions_as_string(mock_obj)
194 assert "click (Return)" in result
195 assert "focus" in result
196 assert ";" in result
197
198 def test_attributes_as_string(self, test_context: OrcaTestContext) -> None:
199 """Test AXUtilitiesDebugging.attributes_as_string."""
200
201 self._setup_dependencies(test_context)
202 from orca.ax_utilities_debugging import AXUtilitiesDebugging
203
204 mock_obj = test_context.Mock(spec=Atspi.Accessible)
205
206 attributes_dict = {"level": "2", "placeholder-text": "Enter name"}
207 test_context.patch(
208 "orca.ax_utilities_debugging.AXObject.get_attributes_dict",
209 return_value=attributes_dict,
210 )
211 result = AXUtilitiesDebugging.attributes_as_string(mock_obj)
212 assert "level:2" in result
213 assert "placeholder-text:Enter name" in result
214 assert ", " in result
215
216 def test_interfaces_as_string(self, test_context: OrcaTestContext) -> None:
217 """Test AXUtilitiesDebugging.interfaces_as_string."""
218
219 self._setup_dependencies(test_context)
220 from orca.ax_utilities_debugging import AXUtilitiesDebugging
221
222 mock_obj = test_context.Mock(spec=Atspi.Accessible)
223 test_context.patch("orca.ax_utilities_debugging.AXObject.is_valid", return_value=False)
224 result = AXUtilitiesDebugging.interfaces_as_string(mock_obj)
225 assert result == ""
226
227 interface_support = {
228 "is_valid": True,
229 "supports_action": True,
230 "supports_component": True,
231 "supports_text": True,
232 "supports_collection": False,
233 "supports_document": False,
234 "supports_editable_text": False,
235 "supports_hyperlink": False,
236 "supports_hypertext": False,
237 "supports_image": False,
238 "supports_selection": False,
239 "supports_table": False,
240 "supports_table_cell": False,
241 "supports_value": False,
242 }
243 for method_name, return_value in interface_support.items():
244 test_context.patch(
245 f"orca.ax_utilities_debugging.AXObject.{method_name}",
246 return_value=return_value,
247 )
248 result = AXUtilitiesDebugging.interfaces_as_string(mock_obj)
249 assert "Action" in result
250 assert "Component" in result
251 assert "Text" in result
252 assert ", " in result
253
254 def test_state_set_as_string(self, test_context: OrcaTestContext) -> None:
255 """Test AXUtilitiesDebugging.state_set_as_string."""
256
257 self._setup_dependencies(test_context)
258 from orca.ax_utilities_debugging import AXUtilitiesDebugging
259
260 mock_obj = test_context.Mock(spec=Atspi.Accessible)
261 test_context.patch("orca.ax_utilities_debugging.AXObject.is_valid", return_value=False)
262 result = AXUtilitiesDebugging.state_set_as_string(mock_obj)
263 assert result == ""
264
265 test_context.patch("orca.ax_utilities_debugging.AXObject.is_valid", return_value=True)
266
267 mock_state_set = test_context.Mock()
268 mock_state1 = test_context.Mock()
269 mock_state1.value_name = "ATSPI_STATE_FOCUSED"
270 mock_state2 = test_context.Mock()
271 mock_state2.value_name = "ATSPI_STATE_VISIBLE"
272 mock_state_set.get_states.return_value = [mock_state1, mock_state2]
273 test_context.patch(
274 "orca.ax_utilities_debugging.AXObject.get_state_set",
275 return_value=mock_state_set,
276 )
277 result = AXUtilitiesDebugging.state_set_as_string(mock_obj)
278 assert "focused" in result
279 assert "visible" in result
280 assert ", " in result
281
282 @pytest.mark.parametrize(
283 "supports_text, should_raise_error, expected_result",
284 [
285 pytest.param(False, False, "", id="no_text_support"),
286 pytest.param(True, False, "Sample text content", id="success_case"),
287 pytest.param(True, True, "", id="glib_error"),
288 ],
289 )
290 def test_text_for_debugging(
291 self,
292 test_context: OrcaTestContext,
293 supports_text: bool,
294 should_raise_error: bool,
295 expected_result: str,
296 ) -> None:
297 """Test AXUtilitiesDebugging.text_for_debugging with various scenarios."""
298
299 self._setup_dependencies(test_context)
300 from orca.ax_utilities_debugging import AXUtilitiesDebugging
301
302 mock_obj = test_context.Mock(spec=Atspi.Accessible)
303
304 test_context.patch(
305 "orca.ax_utilities_debugging.AXObject.supports_text",
306 return_value=supports_text,
307 )
308
309 if supports_text:
310 if should_raise_error:
311
312 def mock_get_text_with_error(unused_obj, unused_start, unused_end):
313 raise GLib.GError("Access error")
314
315 test_context.patch_object(Atspi.Text, "get_text", new=mock_get_text_with_error)
316 else:
317
318 def mock_get_text(unused_obj, unused_start, unused_end):
319 return "Sample text content"
320
321 test_context.patch_object(Atspi.Text, "get_text", new=mock_get_text)
322
323 character_count = 10 if should_raise_error else 20
324 test_context.patch_object(
325 Atspi.Text,
326 "get_character_count",
327 return_value=character_count,
328 )
329
330 result = AXUtilitiesDebugging.text_for_debugging(mock_obj)
331 assert result == expected_result
332
333 def test_as_string_role_object(self, test_context: OrcaTestContext) -> None:
334 """Test AXUtilitiesDebugging.as_string with role object."""
335
336 self._setup_dependencies(test_context)
337 from orca.ax_utilities_debugging import AXUtilitiesDebugging
338
339 mock_role = test_context.Mock(spec=Atspi.Role)
340 mock_role.value_nick = "button"
341 result = AXUtilitiesDebugging.as_string(mock_role)
342 assert "button" in result
343
344 def test_as_string_rect_object(self, test_context: OrcaTestContext) -> None:
345 """Test AXUtilitiesDebugging.as_string with rect object."""
346
347 self._setup_dependencies(test_context)
348 from orca.ax_utilities_debugging import AXUtilitiesDebugging
349
350 mock_rect = test_context.Mock(spec=Atspi.Rect)
351 mock_rect.x = 10
352 mock_rect.y = 20
353 mock_rect.width = 100
354 mock_rect.height = 200
355 result = AXUtilitiesDebugging.as_string(mock_rect)
356 assert "(x:10, y:20, width:100, height:200)" in result
357
358 def test_as_string_collection_objects(self, test_context: OrcaTestContext) -> None:
359 """Test AXUtilitiesDebugging.as_string with collection objects."""
360
361 self._setup_dependencies(test_context)
362 from orca.ax_utilities_debugging import AXUtilitiesDebugging
363
364 test_list = ["item1", "item2"]
365 result = AXUtilitiesDebugging.as_string(test_list)
366 assert "[item1, item2]" in result
367
368 test_dict = {"key1": "value1", "key2": "value2"}
369 result = AXUtilitiesDebugging.as_string(test_dict)
370 assert "{'key1': 'value1'" in result
371
372 def test_object_details_as_string_basic(self, test_context: OrcaTestContext) -> None:
373 """Test AXUtilitiesDebugging.object_details_as_string basic functionality."""
374
375 self._setup_dependencies(test_context)
376 from orca.ax_utilities_debugging import AXUtilitiesDebugging
377
378 non_accessible = "not an accessible"
379 result = AXUtilitiesDebugging.object_details_as_string(non_accessible)
380 assert result == ""
381
382 mock_obj = test_context.Mock(spec=Atspi.Accessible)
383 test_context.patch("orca.ax_utilities_debugging.AXObject.is_dead", return_value=True)
384 result = AXUtilitiesDebugging.object_details_as_string(mock_obj)
385 assert result == "(exception fetching data)"
386
387 def test_object_event_details_as_string(self, test_context: OrcaTestContext) -> None:
388 """Test AXUtilitiesDebugging.object_event_details_as_string."""
389
390 self._setup_dependencies(test_context)
391 from orca.ax_utilities_debugging import AXUtilitiesDebugging
392
393 mock_event = test_context.Mock(spec=Atspi.Event)
394 mock_event.type = "mouse:button"
395 result = AXUtilitiesDebugging.object_event_details_as_string(mock_event)
396 assert result == ""
397
398 mock_event.type = "object:state-changed:focused"
399 mock_event.source = test_context.Mock(spec=Atspi.Accessible)
400 mock_event.any_data = test_context.Mock(spec=Atspi.Accessible)
401
402 def mock_object_details(obj, indent="", unused_include_app=True):
403 if obj == mock_event.source:
404 return f"{indent}SOURCE DETAILS"
405 if obj == mock_event.any_data:
406 return f"{indent}ANY_DATA DETAILS"
407 return ""
408
409 test_context.patch_object(
410 AXUtilitiesDebugging,
411 "object_details_as_string",
412 new=mock_object_details,
413 )
414 result = AXUtilitiesDebugging.object_event_details_as_string(mock_event)
415 assert "EVENT SOURCE:" in result
416 assert "SOURCE DETAILS" in result
417 assert "EVENT ANY DATA:" in result
418 assert "ANY_DATA DETAILS" in result
419
420 mock_event.any_data = None
421
422 def mock_object_details_no_any_data(obj, indent="", unused_include_app=True):
423 if obj == mock_event.source:
424 return f"{indent}SOURCE DETAILS"
425 return ""
426
427 test_context.patch_object(
428 AXUtilitiesDebugging,
429 "object_details_as_string",
430 new=mock_object_details_no_any_data,
431 )
432 result = AXUtilitiesDebugging.object_event_details_as_string(mock_event)
433 assert "EVENT SOURCE:" in result
434 assert "SOURCE DETAILS" in result
435 assert "EVENT ANY DATA:" not in result
436
437 def test_relations_as_string(self, test_context: OrcaTestContext) -> None:
438 """Test AXUtilitiesDebugging.relations_as_string."""
439
440 self._setup_dependencies(test_context)
441 from orca.ax_utilities_debugging import AXUtilitiesDebugging
442
443 mock_obj = test_context.Mock(spec=Atspi.Accessible)
444
445 test_context.patch("orca.ax_utilities_debugging.AXObject.is_valid", return_value=False)
446 result = AXUtilitiesDebugging.relations_as_string(mock_obj)
447 assert result == ""
448
449 test_context.patch("orca.ax_utilities_debugging.AXObject.is_valid", return_value=True)
450
451 mock_relation = test_context.Mock()
452 mock_relation_type = test_context.Mock()
453 mock_relation_type.value_name = "ATSPI_RELATION_LABELLED_BY"
454 mock_relation.get_relation_type.return_value = mock_relation_type
455
456 mock_target = test_context.Mock(spec=Atspi.Accessible)
457 test_context.patch(
458 "orca.ax_utilities_debugging.AXUtilitiesRelation.get_relations",
459 return_value=[mock_relation],
460 )
461 test_context.patch(
462 "orca.ax_utilities_debugging.AXUtilitiesRelation.get_relation_targets_for_debugging",
463 side_effect=lambda obj, rel_type: [mock_target],
464 )
465 test_context.patch(
466 "orca.ax_utilities_debugging.AXObject.get_role_name",
467 return_value="label",
468 )
469 test_context.patch("orca.ax_utilities_debugging.AXObject.get_name", return_value="Username")
470 result = AXUtilitiesDebugging.relations_as_string(mock_obj)
471 assert "labelled-by" in result
472 assert "label: 'Username'" in result
473
474 test_context.patch("orca.ax_utilities_debugging.AXObject.get_role_name", return_value="")
475 test_context.patch("orca.ax_utilities_debugging.AXObject.get_name", return_value="")
476 result = AXUtilitiesDebugging.relations_as_string(mock_obj)
477 assert "labelled-by" in result
478 assert "DEAD" in result
479
480 def test_object_details_as_string_complete(self, test_context: OrcaTestContext) -> None:
481 """Test AXUtilitiesDebugging.object_details_as_string complete functionality."""
482
483 self._setup_dependencies(test_context)
484 from orca.ax_utilities_debugging import AXUtilitiesDebugging
485
486 mock_obj = test_context.Mock(spec=Atspi.Accessible)
487
488 test_context.patch("orca.ax_utilities_debugging.AXObject.is_dead", return_value=False)
489
490 test_context.patch(
491 "orca.ax_utilities_debugging.AXUtilitiesApplication.application_as_string",
492 return_value="TestApp",
493 )
494 test_context.patch(
495 "orca.ax_utilities_debugging.AXObject.get_name",
496 return_value="button name",
497 )
498 test_context.patch(
499 "orca.ax_utilities_debugging.AXObject.get_role_name",
500 return_value="button",
501 )
502 test_context.patch(
503 "orca.ax_utilities_debugging.AXObject.get_description",
504 return_value="button desc",
505 )
506 test_context.patch(
507 "orca.ax_utilities_debugging.AXObject.get_help_text",
508 return_value="button help",
509 )
510 test_context.patch(
511 "orca.ax_utilities_debugging.AXObject.get_accessible_id",
512 return_value="btn1",
513 )
514 test_context.patch("orca.ax_utilities_debugging.AXObject.get_path", return_value=[0, 1, 2])
515
516 test_context.patch_object(
517 AXUtilitiesDebugging,
518 "state_set_as_string",
519 return_value="focused, visible",
520 )
521 test_context.patch_object(
522 AXUtilitiesDebugging,
523 "relations_as_string",
524 return_value="labelled-by: [label]",
525 )
526 test_context.patch_object(
527 AXUtilitiesDebugging,
528 "actions_as_string",
529 return_value="click, focus",
530 )
531 test_context.patch_object(
532 AXUtilitiesDebugging,
533 "interfaces_as_string",
534 return_value="Action, Component",
535 )
536 test_context.patch_object(
537 AXUtilitiesDebugging,
538 "attributes_as_string",
539 return_value="level:1",
540 )
541 test_context.patch_object(
542 AXUtilitiesDebugging,
543 "text_for_debugging",
544 return_value="button text",
545 )
546
547 result = AXUtilitiesDebugging.object_details_as_string(mock_obj)
548 assert "app='TestApp'" in result
549 assert "name='button name'" in result
550 assert "role='button'" in result
551 assert "axid='btn1'" in result
552 assert "description='button desc'" in result
553 assert "help='button help'" in result
554 assert "states='focused, visible'" in result
555 assert "relations='labelled-by: [label]'" in result
556 assert "actions='click, focus'" in result
557 assert "interfaces='Action, Component'" in result
558 assert "attributes='level:1'" in result
559 assert "text='button text'" in result
560 assert "path=[0, 1, 2]" in result
561
562 result = AXUtilitiesDebugging.object_details_as_string(mock_obj, include_app=False)
563 assert "app='TestApp'" not in result
564 assert "name='button name'" in result
565 assert "role='button'" in result
566
567 result = AXUtilitiesDebugging.object_details_as_string(mock_obj, indent=" ")
568 assert " app='TestApp' name='button name'" in result