Debian Codegraph @

tests/unit_tests/test_ax_table.py

raw
1 # Unit tests for ax_table.py and ax_utilities_table.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-lines
22 # pylint: disable=too-many-public-methods
23 # pylint: disable=wrong-import-position
24 # pylint: disable=import-outside-toplevel
25 # pylint: disable=protected-access
26 # pylint: disable=too-many-arguments
27 # pylint: disable=too-many-positional-arguments
28
29 """Unit tests for ax_table.py and ax_utilities_table.py methods."""
30
31 from __future__ import annotations
32
33 from typing import TYPE_CHECKING
34
35 import gi
36 import pytest
37
38 gi.require_version("Atspi", "2.0")
39 from gi.repository import Atspi, GLib
40
41 if TYPE_CHECKING:
42 from unittest.mock import MagicMock
43
44 from .orca_test_context import OrcaTestContext
45
46
47 @pytest.mark.unit
48 class TestAXTable:
49 """Test AXTable and AXUtilitiesTable class methods."""
50
51 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
52 """Set up mocks for ax_table dependencies."""
53
54 additional_modules = [
55 "orca.ax_utilities_object",
56 "orca.ax_utilities_role",
57 "orca.ax_utilities_state",
58 "orca.object_properties",
59 ]
60 essential_modules = test_context.setup_shared_dependencies(additional_modules)
61
62 debug_mock = essential_modules["orca.debug"]
63 debug_mock.print_message = test_context.Mock()
64 debug_mock.print_tokens = test_context.Mock()
65 debug_mock.LEVEL_INFO = 800
66
67 ax_object_mock = essential_modules["orca.ax_object"]
68 ax_object_mock.AXObject = test_context.Mock()
69 ax_object_mock.AXObject.supports_table = test_context.Mock(return_value=True)
70 ax_object_mock.AXObject.supports_table_cell = test_context.Mock(return_value=True)
71 ax_object_mock.AXObject.get_name = test_context.Mock(return_value="")
72 ax_object_mock.AXObject.get_role = test_context.Mock()
73 ax_object_mock.AXObject.get_parent = test_context.Mock()
74 ax_object_mock.AXObject.get_child_count = test_context.Mock(return_value=0)
75 ax_object_mock.AXObject.get_child = test_context.Mock()
76 ax_object_mock.AXObject.get_index_in_parent = test_context.Mock(return_value=0)
77
78 ax_utilities_role_mock = essential_modules["orca.ax_utilities_role"]
79 ax_utilities_role_mock.AXUtilitiesRole = test_context.Mock()
80 ax_utilities_role_mock.AXUtilitiesRole.is_table = test_context.Mock(return_value=True)
81 ax_utilities_role_mock.AXUtilitiesRole.is_table_cell = test_context.Mock(return_value=True)
82 ax_utilities_role_mock.AXUtilitiesRole.is_table_header = test_context.Mock(
83 return_value=False,
84 )
85 ax_utilities_role_mock.AXUtilitiesRole.is_table_row = test_context.Mock(return_value=False)
86 ax_utilities_role_mock.AXUtilitiesRole.is_table_column_header = test_context.Mock(
87 return_value=False,
88 )
89 ax_utilities_role_mock.AXUtilitiesRole.is_table_row_header = test_context.Mock(
90 return_value=False,
91 )
92
93 messages_mock = essential_modules["orca.messages"]
94 messages_mock.TABLE_DYNAMIC_HEADERS_SET = "Dynamic headers set"
95 messages_mock.TABLE_DYNAMIC_HEADERS_CLEARED = "Dynamic headers cleared"
96
97 object_properties_mock = essential_modules["orca.object_properties"]
98 object_properties_mock.OBJECT_PROPERTY_TABLE_DYNAMIC_COLUMN_HEADERS_ROW = 1
99 object_properties_mock.OBJECT_PROPERTY_TABLE_DYNAMIC_ROW_HEADERS_COLUMN = 2
100
101 ax_utilities_state_mock = essential_modules["orca.ax_utilities_state"]
102 ax_utilities_state_mock.AXUtilitiesState = test_context.Mock()
103 ax_utilities_state_mock.AXUtilitiesState.is_showing = test_context.Mock(return_value=True)
104
105 ax_utilities_object_mock = essential_modules["orca.ax_utilities_object"]
106 ax_utilities_object_mock.AXUtilitiesObject = test_context.Mock()
107 ax_utilities_object_mock.AXUtilitiesObject.find_descendant = test_context.Mock()
108
109 return essential_modules
110
111 def _setup_table_role_mocks(
112 self,
113 test_context: OrcaTestContext,
114 is_table_func=None,
115 is_tree_table=False,
116 is_tree=False,
117 ) -> None:
118 """Set up common table role checking mocks."""
119
120 from orca.ax_utilities_role import AXUtilitiesRole
121
122 default_is_table = is_table_func or (lambda _obj: True)
123 test_context.patch_object(AXUtilitiesRole, "is_table", side_effect=default_is_table)
124 test_context.patch_object(
125 AXUtilitiesRole,
126 "is_tree_table",
127 side_effect=lambda obj: is_tree_table,
128 )
129 test_context.patch_object(AXUtilitiesRole, "is_tree", side_effect=lambda obj: is_tree)
130
131 def _setup_table_support_mocks(
132 self,
133 test_context: OrcaTestContext,
134 supports_table_cell=True,
135 supports_table=True,
136 ) -> None:
137 """Set up common table support mocks."""
138
139 from orca.ax_object import AXObject
140
141 test_context.patch_object(
142 AXObject,
143 "supports_table_cell",
144 side_effect=lambda obj: supports_table_cell,
145 )
146 test_context.patch_object(
147 AXObject,
148 "supports_table",
149 side_effect=lambda obj: supports_table,
150 )
151
152 def _setup_debug_message_mock(
153 self,
154 test_context: OrcaTestContext,
155 essential_modules: dict[str, MagicMock],
156 ) -> None:
157 """Set up debug message mocking for test verification."""
158
159 from orca import debug
160
161 test_context.patch_object(
162 debug,
163 "print_message",
164 new=essential_modules["orca.debug"].print_message,
165 )
166
167 def _setup_layout_table_mocks(
168 self,
169 test_context: OrcaTestContext,
170 has_col_headers=False,
171 has_row_headers=False,
172 name="",
173 description="",
174 caption=None,
175 ) -> None:
176 """Set up common mocks for layout table testing."""
177
178 from orca.ax_object import AXObject
179 from orca.ax_table import AXTable
180 from orca.ax_utilities_table import AXUtilitiesTable
181
182 test_context.patch_object(
183 AXUtilitiesTable,
184 "has_column_headers",
185 side_effect=lambda obj: has_col_headers,
186 )
187 test_context.patch_object(
188 AXUtilitiesTable,
189 "has_row_headers",
190 side_effect=lambda obj: has_row_headers,
191 )
192 test_context.patch_object(AXObject, "get_name", side_effect=lambda obj: name)
193 test_context.patch_object(AXObject, "get_description", side_effect=lambda obj: description)
194 test_context.patch_object(AXTable, "get_caption", side_effect=lambda obj: caption)
195
196 def _setup_cell_span_mocks(
197 self,
198 test_context: OrcaTestContext,
199 cell_index=5,
200 table_obj=None,
201 result_func=None,
202 ) -> None:
203 """Set up common mocks for cell span testing."""
204
205 from orca.ax_object import AXObject
206 from orca.ax_table import AXTable
207 from orca.ax_utilities_role import AXUtilitiesRole
208
209 if table_obj is None:
210 table_obj = test_context.Mock(spec=Atspi.Accessible)
211
212 default_result = test_context.Mock()
213 default_result.__getitem__ = lambda self, index: False
214
215 test_context.patch_object(AXTable, "_get_cell_index", side_effect=lambda obj: cell_index)
216 test_context.patch_object(
217 AXTable, "_find_ancestor_table", side_effect=lambda obj: table_obj
218 )
219 test_context.patch_object(AXObject, "supports_table", side_effect=lambda _obj: True)
220 test_context.patch_object(AXUtilitiesRole, "is_tree", side_effect=lambda obj: False)
221 test_context.patch_object(
222 Atspi.Table,
223 "get_row_column_extents_at_index",
224 side_effect=result_func or (lambda table, index: default_result),
225 )
226
227 def _setup_row_headers_mocks(
228 self,
229 test_context: OrcaTestContext,
230 table_obj,
231 coordinates=(2, 3),
232 spans=(2, 1),
233 headers=None,
234 ) -> None:
235 """Set up common mocks for row headers testing."""
236
237 from orca.ax_object import AXObject
238 from orca.ax_table import AXTable
239 from orca.ax_utilities_table import AXUtilitiesTable
240
241 if headers is None:
242 headers = [test_context.Mock(spec=Atspi.Accessible)]
243
244 test_context.patch_object(AXObject, "supports_table_cell", side_effect=lambda obj: False)
245 test_context.patch_object(
246 AXTable,
247 "get_cell_coordinates_from_table",
248 side_effect=lambda obj: coordinates,
249 )
250 test_context.patch_object(AXUtilitiesTable, "get_table", side_effect=lambda obj: table_obj)
251 test_context.patch_object(
252 AXTable,
253 "get_cell_spans_from_table",
254 side_effect=lambda obj: spans,
255 )
256 test_context.patch_object(
257 AXTable,
258 "get_row_headers_from_table",
259 side_effect=lambda table, row: headers,
260 )
261
262 def _setup_table_description_mocks(
263 self,
264 test_context: OrcaTestContext,
265 row_count=5,
266 col_count=3,
267 is_non_uniform=False,
268 table_size_msg=None,
269 ) -> None:
270 """Set up common mocks for table description testing."""
271
272 from orca import messages
273 from orca.ax_object import AXObject
274 from orca.ax_table import AXTable
275
276 test_context.patch_object(AXObject, "supports_table", side_effect=lambda _obj: True)
277 test_context.patch_object(AXTable, "get_row_count", side_effect=lambda table: row_count)
278 test_context.patch_object(AXTable, "get_column_count", side_effect=lambda table: col_count)
279 test_context.patch_object(
280 AXTable,
281 "is_non_uniform_table",
282 side_effect=lambda table: is_non_uniform,
283 )
284 if table_size_msg:
285 test_context.patch_object(messages, "table_size", new=table_size_msg)
286 if is_non_uniform:
287 test_context.patch_object(messages, "TABLE_NON_UNIFORM", new="non uniform")
288
289 @pytest.mark.parametrize(
290 "case",
291 [
292 {
293 "id": "with_table_support",
294 "supports_table": True,
295 "raises_error": False,
296 "expected_result": "mock_caption",
297 "should_log": True,
298 },
299 {
300 "id": "without_table_support",
301 "supports_table": False,
302 "raises_error": False,
303 "expected_result": None,
304 "should_log": False,
305 },
306 {
307 "id": "with_glib_error",
308 "supports_table": True,
309 "raises_error": True,
310 "expected_result": None,
311 "should_log": True,
312 },
313 ],
314 ids=lambda case: case["id"],
315 )
316 def test_get_caption(
317 self,
318 test_context: OrcaTestContext,
319 case: dict,
320 ) -> None:
321 """Test AXTable.get_caption with various support and error conditions."""
322
323 mock_table = test_context.Mock(spec=Atspi.Accessible)
324 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
325 from orca.ax_object import AXObject
326 from orca.ax_table import AXTable
327
328 test_context.patch_object(
329 AXObject,
330 "supports_table",
331 side_effect=lambda _obj: case["supports_table"],
332 )
333
334 if case["raises_error"]:
335
336 def raise_glib_error(obj):
337 raise GLib.GError("Test error")
338
339 test_context.patch_object(Atspi.Table, "get_caption", side_effect=raise_glib_error)
340 from orca import debug
341
342 test_context.patch_object(
343 debug,
344 "print_message",
345 new=essential_modules["orca.debug"].print_message,
346 )
347 else:
348 mock_caption = (
349 test_context.Mock(spec=Atspi.Accessible) if case["expected_result"] else None
350 )
351 test_context.patch_object(
352 Atspi.Table,
353 "get_caption",
354 side_effect=lambda obj: mock_caption,
355 )
356
357 result = AXTable.get_caption(mock_table)
358
359 if case["expected_result"] == "mock_caption":
360 assert result is not None
361 else:
362 assert result == case["expected_result"]
363
364 if case["should_log"]:
365 if case["raises_error"]:
366 essential_modules["orca.debug"].print_message.assert_called()
367 else:
368 essential_modules["orca.debug"].print_tokens.assert_called()
369
370 @pytest.mark.parametrize(
371 "case",
372 [
373 {
374 "id": "col_prefer_attribute_available",
375 "count_type": "column",
376 "prefer_attribute": True,
377 "attribute_count": 5,
378 "physical_count": 4,
379 "expected_result": 5,
380 "supports_table": True,
381 "raises_error": False,
382 "attr_name": "colcount",
383 "attr_value": "3",
384 "expected_attr_result": 3,
385 },
386 {
387 "id": "col_prefer_attribute_none_fallback",
388 "count_type": "column",
389 "prefer_attribute": True,
390 "attribute_count": None,
391 "physical_count": 4,
392 "expected_result": 4,
393 "supports_table": True,
394 "raises_error": False,
395 "attr_name": "colcount",
396 "attr_value": None,
397 "expected_attr_result": None,
398 },
399 {
400 "id": "col_prefer_physical",
401 "count_type": "column",
402 "prefer_attribute": False,
403 "attribute_count": 5,
404 "physical_count": 4,
405 "expected_result": 4,
406 "supports_table": True,
407 "raises_error": False,
408 "attr_name": "colcount",
409 "attr_value": "3",
410 "expected_attr_result": 3,
411 },
412 {
413 "id": "col_without_table_support",
414 "count_type": "column",
415 "prefer_attribute": True,
416 "attribute_count": None,
417 "physical_count": -1,
418 "expected_result": -1,
419 "supports_table": False,
420 "raises_error": False,
421 "attr_name": "colcount",
422 "attr_value": "3",
423 "expected_attr_result": 3,
424 },
425 {
426 "id": "col_with_glib_error",
427 "count_type": "column",
428 "prefer_attribute": True,
429 "attribute_count": None,
430 "physical_count": -1,
431 "expected_result": -1,
432 "supports_table": True,
433 "raises_error": True,
434 "attr_name": "colcount",
435 "attr_value": "3",
436 "expected_attr_result": 3,
437 },
438 {
439 "id": "row_prefer_attribute_available",
440 "count_type": "row",
441 "prefer_attribute": True,
442 "attribute_count": 10,
443 "physical_count": 8,
444 "expected_result": 10,
445 "supports_table": True,
446 "raises_error": False,
447 "attr_name": "rowcount",
448 "attr_value": "7",
449 "expected_attr_result": 7,
450 },
451 {
452 "id": "row_prefer_attribute_none_fallback",
453 "count_type": "row",
454 "prefer_attribute": True,
455 "attribute_count": None,
456 "physical_count": 8,
457 "expected_result": 8,
458 "supports_table": True,
459 "raises_error": False,
460 "attr_name": "rowcount",
461 "attr_value": None,
462 "expected_attr_result": None,
463 },
464 {
465 "id": "row_prefer_physical",
466 "count_type": "row",
467 "prefer_attribute": False,
468 "attribute_count": 10,
469 "physical_count": 8,
470 "expected_result": 8,
471 "supports_table": True,
472 "raises_error": False,
473 "attr_name": "rowcount",
474 "attr_value": "7",
475 "expected_attr_result": 7,
476 },
477 {
478 "id": "row_without_table_support",
479 "count_type": "row",
480 "prefer_attribute": True,
481 "attribute_count": None,
482 "physical_count": -1,
483 "expected_result": -1,
484 "supports_table": False,
485 "raises_error": False,
486 "attr_name": "rowcount",
487 "attr_value": "7",
488 "expected_attr_result": 7,
489 },
490 {
491 "id": "row_with_glib_error",
492 "count_type": "row",
493 "prefer_attribute": True,
494 "attribute_count": None,
495 "physical_count": -1,
496 "expected_result": -1,
497 "supports_table": True,
498 "raises_error": True,
499 "attr_name": "rowcount",
500 "attr_value": "7",
501 "expected_attr_result": 7,
502 },
503 ],
504 ids=lambda case: case["id"],
505 )
506 def test_count_methods_scenarios( # pylint: disable=too-many-locals
507 self,
508 test_context,
509 case: dict,
510 ) -> None:
511 """Test AXTable.get_column_count, get_row_count and their attribute methods."""
512
513 mock_table = test_context.Mock(spec=Atspi.Accessible)
514 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
515 from orca import debug
516 from orca.ax_object import AXObject
517 from orca.ax_table import AXTable
518
519 test_context.patch_object(
520 AXObject,
521 "supports_table",
522 side_effect=lambda _obj: case["supports_table"],
523 )
524
525 if case["count_type"] == "column":
526 count_method = AXTable.get_column_count
527 attr_method = AXTable._get_column_count_from_attribute
528 atspi_method_name = "get_n_columns"
529 attr_method_name = "_get_column_count_from_attribute"
530 else:
531 count_method = AXTable.get_row_count
532 attr_method = AXTable._get_row_count_from_attribute
533 atspi_method_name = "get_n_rows"
534 attr_method_name = "_get_row_count_from_attribute"
535
536 test_context.patch_object(
537 AXTable,
538 attr_method_name,
539 side_effect=lambda obj: case["attribute_count"],
540 )
541
542 if case["raises_error"] and case["supports_table"]:
543
544 def raise_glib_error(obj):
545 raise GLib.GError("Test error")
546
547 test_context.patch_object(Atspi.Table, atspi_method_name, side_effect=raise_glib_error)
548 test_context.patch_object(
549 debug,
550 "print_message",
551 new=essential_modules["orca.debug"].print_message,
552 )
553 else:
554 test_context.patch_object(
555 Atspi.Table,
556 atspi_method_name,
557 side_effect=lambda obj: case["physical_count"],
558 )
559
560 if (
561 case["supports_table"]
562 and not case["raises_error"]
563 and case["prefer_attribute"] is not None
564 ):
565 result = count_method(mock_table, case["prefer_attribute"])
566 else:
567 result = count_method(mock_table)
568 assert result == case["expected_result"]
569
570 if (
571 (case["attribute_count"] is None or not case["prefer_attribute"])
572 and case["supports_table"]
573 and not case["raises_error"]
574 ):
575 essential_modules["orca.debug"].print_tokens.assert_called()
576 if case["raises_error"] and case["supports_table"]:
577 essential_modules["orca.debug"].print_message.assert_called()
578
579 attrs = {case["attr_name"]: case["attr_value"]} if case["attr_value"] else {}
580 test_context.patch_object(AXObject, "get_attributes_dict", side_effect=lambda obj: attrs)
581 attr_result = attr_method(mock_table)
582 assert attr_result == case["expected_attr_result"]
583
584 @pytest.mark.parametrize(
585 "case",
586 [
587 {
588 "id": "uniform_table",
589 "row_extents": [1, 1, 1, 1],
590 "col_extents": [1, 1, 1, 1],
591 "expected_result": False,
592 },
593 {
594 "id": "row_span_greater_than_one",
595 "row_extents": [2, 1, 1, 1],
596 "col_extents": [1, 1, 1, 1],
597 "expected_result": True,
598 },
599 {
600 "id": "col_span_greater_than_one",
601 "row_extents": [1, 1, 1, 1],
602 "col_extents": [1, 2, 1, 1],
603 "expected_result": True,
604 },
605 ],
606 ids=lambda case: case["id"],
607 )
608 def test_is_non_uniform_table(
609 self,
610 test_context,
611 case: dict,
612 ) -> None:
613 """Test AXTable.is_non_uniform_table."""
614
615 mock_table = test_context.Mock(spec=Atspi.Accessible)
616 self._setup_dependencies(test_context)
617 from orca.ax_table import AXTable
618
619 call_count = 0
620
621 def mock_get_extent(extent_type):
622 nonlocal call_count
623 if extent_type == "row":
624 result = case["row_extents"][call_count % len(case["row_extents"])]
625 else:
626 result = case["col_extents"][call_count % len(case["col_extents"])]
627 call_count += 1
628 return result
629
630 test_context.patch_object(AXTable, "get_row_count", side_effect=lambda obj, prefer_attr: 2)
631 test_context.patch_object(
632 AXTable,
633 "get_column_count",
634 side_effect=lambda obj, prefer_attr: 2,
635 )
636 test_context.patch_object(
637 Atspi.Table,
638 "get_row_extent_at",
639 side_effect=lambda table, row, col: mock_get_extent("row"),
640 )
641 test_context.patch_object(
642 Atspi.Table,
643 "get_column_extent_at",
644 side_effect=lambda table, row, col: mock_get_extent("col"),
645 )
646 result = AXTable.is_non_uniform_table(mock_table)
647 assert result == case["expected_result"]
648
649 def test_is_non_uniform_table_with_glib_error(self, test_context: OrcaTestContext) -> None:
650 """Test AXTable.is_non_uniform_table handles GLib.GError."""
651
652 mock_table = test_context.Mock(spec=Atspi.Accessible)
653 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
654 from orca import debug
655 from orca.ax_table import AXTable
656
657 def raise_glib_error(obj, row, col):
658 raise GLib.GError("Test error")
659
660 test_context.patch_object(AXTable, "get_row_count", side_effect=lambda obj, prefer_attr: 1)
661 test_context.patch_object(
662 AXTable,
663 "get_column_count",
664 side_effect=lambda obj, prefer_attr: 1,
665 )
666 test_context.patch_object(Atspi.Table, "get_row_extent_at", side_effect=raise_glib_error)
667 test_context.patch_object(
668 debug,
669 "print_message",
670 new=essential_modules["orca.debug"].print_message,
671 )
672 result = AXTable.is_non_uniform_table(mock_table)
673 assert result is False
674 essential_modules["orca.debug"].print_message.assert_called()
675
676 @pytest.mark.parametrize(
677 "case",
678 [
679 {
680 "id": "with_table_support",
681 "supports_table": True,
682 "raises_error": False,
683 "expected_result": 2,
684 },
685 {
686 "id": "without_table_support",
687 "supports_table": False,
688 "raises_error": False,
689 "expected_result": 0,
690 },
691 {
692 "id": "with_glib_error",
693 "supports_table": True,
694 "raises_error": True,
695 "expected_result": 0,
696 },
697 ],
698 ids=lambda case: case["id"],
699 )
700 def test_get_selected_column_count(
701 self,
702 test_context: OrcaTestContext,
703 case: dict,
704 ) -> None:
705 """Test AXTable.get_selected_column_count in various scenarios."""
706
707 mock_table = test_context.Mock(spec=Atspi.Accessible)
708 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
709 from orca import debug
710 from orca.ax_object import AXObject
711 from orca.ax_table import AXTable
712
713 test_context.patch_object(AXObject, "supports_table", return_value=case["supports_table"])
714
715 if case["supports_table"] and not case["raises_error"]:
716 test_context.patch_object(
717 Atspi.Table,
718 "get_n_selected_columns",
719 side_effect=lambda obj: 2,
720 )
721 elif case["supports_table"] and case["raises_error"]:
722
723 def raise_glib_error(obj):
724 raise GLib.GError("Test error")
725
726 test_context.patch_object(
727 Atspi.Table,
728 "get_n_selected_columns",
729 side_effect=raise_glib_error,
730 )
731 test_context.patch_object(
732 debug,
733 "print_message",
734 new=essential_modules["orca.debug"].print_message,
735 )
736
737 result = AXTable.get_selected_column_count(mock_table)
738 assert result == case["expected_result"]
739
740 if case["supports_table"] and not case["raises_error"]:
741 essential_modules["orca.debug"].print_tokens.assert_called()
742 elif case["raises_error"]:
743 essential_modules["orca.debug"].print_message.assert_called()
744
745 @pytest.mark.parametrize(
746 "case",
747 [
748 {
749 "id": "with_table_support",
750 "supports_table": True,
751 "raises_error": False,
752 "expected_result": [0, 2, 4],
753 },
754 {
755 "id": "without_table_support",
756 "supports_table": False,
757 "raises_error": False,
758 "expected_result": [],
759 },
760 {
761 "id": "with_glib_error",
762 "supports_table": True,
763 "raises_error": True,
764 "expected_result": [],
765 },
766 ],
767 ids=lambda case: case["id"],
768 )
769 def test_get_selected_columns(
770 self,
771 test_context: OrcaTestContext,
772 case: dict,
773 ) -> None:
774 """Test AXTable.get_selected_columns in various scenarios."""
775
776 mock_table = test_context.Mock(spec=Atspi.Accessible)
777 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
778 from orca import debug
779 from orca.ax_object import AXObject
780 from orca.ax_table import AXTable
781
782 test_context.patch_object(AXObject, "supports_table", return_value=case["supports_table"])
783
784 if case["supports_table"] and not case["raises_error"]:
785 selected_columns = [0, 2, 4]
786 test_context.patch_object(
787 Atspi.Table,
788 "get_selected_columns",
789 return_value=selected_columns,
790 )
791 elif case["supports_table"] and case["raises_error"]:
792
793 def raise_glib_error(obj):
794 raise GLib.GError("Test error")
795
796 test_context.patch_object(
797 Atspi.Table,
798 "get_selected_columns",
799 side_effect=raise_glib_error,
800 )
801 test_context.patch_object(
802 debug,
803 "print_message",
804 new=essential_modules["orca.debug"].print_message,
805 )
806
807 result = AXTable.get_selected_columns(mock_table)
808 assert result == case["expected_result"]
809
810 if case["supports_table"] and not case["raises_error"]:
811 essential_modules["orca.debug"].print_tokens.assert_called()
812 elif case["raises_error"]:
813 essential_modules["orca.debug"].print_message.assert_called()
814
815 @pytest.mark.parametrize(
816 "case",
817 [
818 {
819 "id": "with_table_support",
820 "supports_table": True,
821 "raises_error": False,
822 "expected_result": 3,
823 },
824 {
825 "id": "without_table_support",
826 "supports_table": False,
827 "raises_error": False,
828 "expected_result": 0,
829 },
830 {
831 "id": "with_glib_error",
832 "supports_table": True,
833 "raises_error": True,
834 "expected_result": 0,
835 },
836 ],
837 ids=lambda case: case["id"],
838 )
839 def test_get_selected_row_count(
840 self,
841 test_context: OrcaTestContext,
842 case: dict,
843 ) -> None:
844 """Test AXTable.get_selected_row_count in various scenarios."""
845
846 mock_table = test_context.Mock(spec=Atspi.Accessible)
847 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
848 from orca import debug
849 from orca.ax_object import AXObject
850 from orca.ax_table import AXTable
851
852 test_context.patch_object(AXObject, "supports_table", return_value=case["supports_table"])
853
854 if case["supports_table"] and not case["raises_error"]:
855 test_context.patch_object(Atspi.Table, "get_n_selected_rows", return_value=3)
856 elif case["supports_table"] and case["raises_error"]:
857
858 def raise_glib_error(obj):
859 raise GLib.GError("Test error")
860
861 test_context.patch_object(
862 Atspi.Table,
863 "get_n_selected_rows",
864 side_effect=raise_glib_error,
865 )
866 test_context.patch_object(
867 debug,
868 "print_message",
869 new=essential_modules["orca.debug"].print_message,
870 )
871
872 result = AXTable.get_selected_row_count(mock_table)
873 assert result == case["expected_result"]
874
875 if case["supports_table"] and not case["raises_error"]:
876 essential_modules["orca.debug"].print_tokens.assert_called()
877 elif case["raises_error"]:
878 essential_modules["orca.debug"].print_message.assert_called()
879
880 @pytest.mark.parametrize(
881 "case",
882 [
883 {
884 "id": "with_table_support",
885 "supports_table": True,
886 "raises_error": False,
887 "expected_result": [1, 3, 5],
888 },
889 {
890 "id": "without_table_support",
891 "supports_table": False,
892 "raises_error": False,
893 "expected_result": [],
894 },
895 {
896 "id": "with_glib_error",
897 "supports_table": True,
898 "raises_error": True,
899 "expected_result": [],
900 },
901 ],
902 ids=lambda case: case["id"],
903 )
904 def test_get_selected_rows(
905 self,
906 test_context: OrcaTestContext,
907 case: dict,
908 ) -> None:
909 """Test AXTable.get_selected_rows in various scenarios."""
910
911 mock_table = test_context.Mock(spec=Atspi.Accessible)
912 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
913 from orca import debug
914 from orca.ax_object import AXObject
915 from orca.ax_table import AXTable
916
917 test_context.patch_object(AXObject, "supports_table", return_value=case["supports_table"])
918
919 if case["supports_table"] and not case["raises_error"]:
920 selected_rows = [1, 3, 5]
921 test_context.patch_object(Atspi.Table, "get_selected_rows", return_value=selected_rows)
922 elif case["supports_table"] and case["raises_error"]:
923
924 def raise_glib_error(obj):
925 raise GLib.GError("Test error")
926
927 test_context.patch_object(
928 Atspi.Table,
929 "get_selected_rows",
930 side_effect=raise_glib_error,
931 )
932 test_context.patch_object(
933 debug,
934 "print_message",
935 new=essential_modules["orca.debug"].print_message,
936 )
937
938 result = AXTable.get_selected_rows(mock_table)
939 assert result == case["expected_result"]
940
941 if case["supports_table"] and not case["raises_error"]:
942 essential_modules["orca.debug"].print_tokens.assert_called()
943 elif case["raises_error"]:
944 essential_modules["orca.debug"].print_message.assert_called()
945
946 @pytest.mark.parametrize(
947 "case",
948 [
949 {
950 "id": "all_rows_selected",
951 "supports_table": True,
952 "row_count": 5,
953 "selected_row_count": 5,
954 "col_count": 3,
955 "selected_col_count": 3,
956 "expected_result": True,
957 },
958 {
959 "id": "all_columns_selected",
960 "supports_table": True,
961 "row_count": 5,
962 "selected_row_count": 3,
963 "col_count": 3,
964 "selected_col_count": 3,
965 "expected_result": True,
966 },
967 {
968 "id": "partial_selection",
969 "supports_table": True,
970 "row_count": 5,
971 "selected_row_count": 3,
972 "col_count": 3,
973 "selected_col_count": 2,
974 "expected_result": False,
975 },
976 {
977 "id": "empty_table",
978 "supports_table": True,
979 "row_count": 0,
980 "selected_row_count": 0,
981 "col_count": 0,
982 "selected_col_count": 0,
983 "expected_result": False,
984 },
985 {
986 "id": "without_table_support",
987 "supports_table": False,
988 "row_count": 0,
989 "selected_row_count": 0,
990 "col_count": 0,
991 "selected_col_count": 0,
992 "expected_result": False,
993 },
994 ],
995 ids=lambda case: case["id"],
996 )
997 def test_all_cells_are_selected_scenarios(
998 self,
999 test_context: OrcaTestContext,
1000 case: dict,
1001 ) -> None:
1002 """Test AXUtilitiesTable.all_cells_are_selected with various scenarios."""
1003
1004 mock_table = test_context.Mock(spec=Atspi.Accessible)
1005 self._setup_dependencies(test_context)
1006 from orca.ax_object import AXObject
1007 from orca.ax_table import AXTable
1008 from orca.ax_utilities_table import AXUtilitiesTable
1009
1010 test_context.patch_object(AXObject, "supports_table", return_value=case["supports_table"])
1011
1012 if case["supports_table"]:
1013 test_context.patch_object(AXTable, "get_row_count", return_value=case["row_count"])
1014 test_context.patch_object(
1015 AXTable,
1016 "get_selected_row_count",
1017 return_value=case["selected_row_count"],
1018 )
1019 test_context.patch_object(AXTable, "get_column_count", return_value=case["col_count"])
1020 test_context.patch_object(
1021 AXTable,
1022 "get_selected_column_count",
1023 return_value=case["selected_col_count"],
1024 )
1025
1026 result = AXUtilitiesTable.all_cells_are_selected(mock_table)
1027 assert result == case["expected_result"]
1028
1029 @pytest.mark.parametrize(
1030 "case",
1031 [
1032 {
1033 "id": "with_table_support",
1034 "supports_table": True,
1035 "raises_error": False,
1036 "expected_result_type": "mock_cell",
1037 },
1038 {
1039 "id": "without_table_support",
1040 "supports_table": False,
1041 "raises_error": False,
1042 "expected_result_type": None,
1043 },
1044 {
1045 "id": "with_glib_error",
1046 "supports_table": True,
1047 "raises_error": True,
1048 "expected_result_type": None,
1049 },
1050 ],
1051 ids=lambda case: case["id"],
1052 )
1053 def test_get_cell_at(
1054 self,
1055 test_context: OrcaTestContext,
1056 case: dict,
1057 ) -> None:
1058 """Test AXTable.get_cell_at in various scenarios."""
1059
1060 mock_table = test_context.Mock(spec=Atspi.Accessible)
1061 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1062 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1063 from orca.ax_object import AXObject
1064 from orca.ax_table import AXTable
1065
1066 test_context.patch_object(AXObject, "supports_table", return_value=case["supports_table"])
1067
1068 if case["supports_table"] and not case["raises_error"]:
1069 test_context.patch_object(
1070 Atspi.Table,
1071 "get_accessible_at",
1072 side_effect=lambda obj, row, col: mock_cell,
1073 )
1074 elif case["supports_table"] and case["raises_error"]:
1075
1076 def raise_glib_error(obj, row, col):
1077 raise GLib.GError("Test error")
1078
1079 test_context.patch_object(
1080 Atspi.Table,
1081 "get_accessible_at",
1082 side_effect=raise_glib_error,
1083 )
1084
1085 result = AXTable.get_cell_at(mock_table, 1, 2)
1086
1087 if case["expected_result_type"] == "mock_cell":
1088 assert result == mock_cell
1089 essential_modules["orca.debug"].print_tokens.assert_called()
1090 else:
1091 assert result is None
1092 if case["raises_error"]:
1093 essential_modules["orca.debug"].print_tokens.assert_called()
1094
1095 @pytest.mark.parametrize(
1096 "case",
1097 [
1098 {
1099 "id": "cell_has_index_attribute",
1100 "cell_index_attr": "5",
1101 "parent_role": Atspi.Role.BUTTON,
1102 "expected_index": 5,
1103 },
1104 {
1105 "id": "parent_is_table_cell",
1106 "cell_index_attr": "",
1107 "parent_role": Atspi.Role.TABLE_CELL,
1108 "expected_index": 3,
1109 },
1110 {
1111 "id": "use_index_in_parent",
1112 "cell_index_attr": None,
1113 "parent_role": Atspi.Role.BUTTON,
1114 "expected_index": 2,
1115 },
1116 ],
1117 ids=lambda case: case["id"],
1118 )
1119 def test_get_cell_index(
1120 self,
1121 test_context,
1122 case: dict,
1123 ) -> None:
1124 """Test AXTable._get_cell_index."""
1125
1126 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1127 self._setup_dependencies(test_context)
1128 from orca.ax_object import AXObject
1129 from orca.ax_table import AXTable
1130
1131 mock_parent = test_context.Mock(spec=Atspi.Accessible)
1132 test_context.patch_object(
1133 AXObject,
1134 "get_attribute",
1135 side_effect=lambda obj, attr: case["cell_index_attr"],
1136 )
1137 test_context.patch_object(AXObject, "get_parent", return_value=mock_parent)
1138 test_context.patch_object(AXObject, "get_role", return_value=case["parent_role"])
1139 test_context.patch_object(
1140 AXObject,
1141 "get_index_in_parent",
1142 side_effect=lambda obj: case["expected_index"],
1143 )
1144 result = AXTable._get_cell_index(mock_cell)
1145 if case["parent_role"] == Atspi.Role.TABLE_CELL:
1146 assert result == 3
1147 else:
1148 assert result == case["expected_index"]
1149
1150 @pytest.mark.parametrize(
1151 "case",
1152 [
1153 {
1154 "id": "table_cell_with_attributes",
1155 "supports_table_cell": True,
1156 "cell_role": Atspi.Role.TABLE_CELL,
1157 "prefer_attribute": True,
1158 "rowspan_attr": "2",
1159 "colspan_attr": "3",
1160 "expected_spans": (2, 3),
1161 },
1162 {
1163 "id": "table_interface_with_attributes",
1164 "supports_table_cell": False,
1165 "cell_role": Atspi.Role.TABLE_CELL,
1166 "prefer_attribute": True,
1167 "rowspan_attr": "2",
1168 "colspan_attr": "3",
1169 "expected_spans": (2, 3),
1170 },
1171 {
1172 "id": "table_cell_no_attributes",
1173 "supports_table_cell": True,
1174 "cell_role": Atspi.Role.TABLE_CELL,
1175 "prefer_attribute": False,
1176 "rowspan_attr": "2",
1177 "colspan_attr": "3",
1178 "expected_spans": (2, 1),
1179 },
1180 {
1181 "id": "table_cell_no_attr_values",
1182 "supports_table_cell": True,
1183 "cell_role": Atspi.Role.TABLE_CELL,
1184 "prefer_attribute": True,
1185 "rowspan_attr": None,
1186 "colspan_attr": None,
1187 "expected_spans": (2, 1),
1188 },
1189 {
1190 "id": "not_table_cell",
1191 "supports_table_cell": False,
1192 "cell_role": Atspi.Role.BUTTON,
1193 "prefer_attribute": True,
1194 "rowspan_attr": "2",
1195 "colspan_attr": "3",
1196 "expected_spans": (-1, -1),
1197 },
1198 ],
1199 ids=lambda case: case["id"],
1200 )
1201 def test_get_cell_spans(
1202 self,
1203 test_context,
1204 case: dict,
1205 ) -> None:
1206 """Test AXTable.get_cell_spans."""
1207
1208 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1209 self._setup_dependencies(test_context)
1210 from orca.ax_object import AXObject
1211 from orca.ax_table import AXTable
1212 from orca.ax_utilities_role import AXUtilitiesRole
1213
1214 test_context.patch_object(
1215 AXUtilitiesRole,
1216 "is_table_cell_or_header",
1217 side_effect=lambda obj: case["cell_role"] == Atspi.Role.TABLE_CELL,
1218 )
1219 test_context.patch_object(
1220 AXObject,
1221 "supports_table_cell",
1222 side_effect=lambda obj: case["supports_table_cell"],
1223 )
1224 test_context.patch_object(AXTable, "_get_cell_spans_from_table_cell", return_value=(2, 1))
1225 test_context.patch_object(AXTable, "get_cell_spans_from_table", return_value=(1, 2))
1226 test_context.patch_object(
1227 AXTable,
1228 "_get_cell_spans_from_attribute",
1229 side_effect=lambda obj: (case["rowspan_attr"], case["colspan_attr"]),
1230 )
1231 result = AXTable.get_cell_spans(mock_cell, case["prefer_attribute"])
1232 assert result == case["expected_spans"]
1233
1234 @pytest.mark.parametrize(
1235 "case",
1236 [
1237 {
1238 "id": "both_attributes_present",
1239 "rowspan_attr": "2",
1240 "colspan_attr": "3",
1241 "expected_spans": ("2", "3"),
1242 },
1243 {
1244 "id": "only_colspan",
1245 "rowspan_attr": None,
1246 "colspan_attr": "3",
1247 "expected_spans": (None, "3"),
1248 },
1249 {
1250 "id": "only_rowspan",
1251 "rowspan_attr": "2",
1252 "colspan_attr": None,
1253 "expected_spans": ("2", None),
1254 },
1255 {
1256 "id": "no_attributes",
1257 "rowspan_attr": None,
1258 "colspan_attr": None,
1259 "expected_spans": (None, None),
1260 },
1261 ],
1262 ids=lambda case: case["id"],
1263 )
1264 def test_get_cell_spans_from_attribute(
1265 self,
1266 test_context,
1267 case: dict,
1268 ) -> None:
1269 """Test AXTable._get_cell_spans_from_attribute."""
1270
1271 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1272 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1273 from orca.ax_object import AXObject
1274 from orca.ax_table import AXTable
1275
1276 attrs = {}
1277 if case["rowspan_attr"]:
1278 attrs["rowspan"] = case["rowspan_attr"]
1279 if case["colspan_attr"]:
1280 attrs["colspan"] = case["colspan_attr"]
1281 test_context.patch_object(AXObject, "get_attributes_dict", side_effect=lambda obj: attrs)
1282 result = AXTable._get_cell_spans_from_attribute(mock_cell)
1283 assert result == case["expected_spans"]
1284 essential_modules["orca.debug"].print_tokens.assert_called()
1285
1286 @pytest.mark.parametrize(
1287 "case",
1288 [
1289 {
1290 "id": "with_table_cell_support",
1291 "supports_table_cell": True,
1292 "raises_error": False,
1293 "expected_result": (2, 3),
1294 },
1295 {
1296 "id": "without_table_cell_support",
1297 "supports_table_cell": False,
1298 "raises_error": False,
1299 "expected_result": (-1, -1),
1300 },
1301 {
1302 "id": "with_glib_error",
1303 "supports_table_cell": True,
1304 "raises_error": True,
1305 "expected_result": (-1, -1),
1306 },
1307 ],
1308 ids=lambda case: case["id"],
1309 )
1310 def test_get_cell_spans_from_table_cell(
1311 self,
1312 test_context: OrcaTestContext,
1313 case: dict,
1314 ) -> None:
1315 """Test AXTable._get_cell_spans_from_table_cell in various scenarios."""
1316
1317 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1318 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1319 from orca import debug
1320 from orca.ax_object import AXObject
1321 from orca.ax_table import AXTable
1322
1323 test_context.patch_object(
1324 AXObject,
1325 "supports_table_cell",
1326 return_value=case["supports_table_cell"],
1327 )
1328
1329 if case["supports_table_cell"] and not case["raises_error"]:
1330 test_context.patch_object(Atspi.TableCell, "get_row_span", return_value=2)
1331 test_context.patch_object(Atspi.TableCell, "get_column_span", return_value=3)
1332 elif case["supports_table_cell"] and case["raises_error"]:
1333
1334 def raise_glib_error(obj):
1335 raise GLib.GError("Test error")
1336
1337 test_context.patch_object(Atspi.TableCell, "get_row_span", side_effect=raise_glib_error)
1338 test_context.patch_object(
1339 debug,
1340 "print_message",
1341 new=essential_modules["orca.debug"].print_message,
1342 )
1343
1344 result = AXTable._get_cell_spans_from_table_cell(mock_cell)
1345 assert result == case["expected_result"]
1346
1347 if case["supports_table_cell"] and not case["raises_error"]:
1348 essential_modules["orca.debug"].print_tokens.assert_called()
1349 elif case["raises_error"]:
1350 essential_modules["orca.debug"].print_message.assert_called()
1351
1352 @pytest.mark.parametrize(
1353 "case",
1354 [
1355 {
1356 "id": "table_cell_success",
1357 "scenario": "table_cell",
1358 "obj_type": "mock",
1359 "expected_result": "table",
1360 "expects_debug": False,
1361 },
1362 {
1363 "id": "table_cell_glib_error",
1364 "scenario": "table_cell_error",
1365 "obj_type": "mock",
1366 "expected_result": "table",
1367 "expects_debug": True,
1368 },
1369 {
1370 "id": "table_object",
1371 "scenario": "table_object",
1372 "obj_type": "mock",
1373 "expected_result": "table",
1374 "expects_debug": False,
1375 },
1376 {
1377 "id": "none_object",
1378 "scenario": "none_object",
1379 "obj_type": "none",
1380 "expected_result": None,
1381 "expects_debug": False,
1382 },
1383 ],
1384 ids=lambda case: case["id"],
1385 )
1386 def test_get_table( # pylint: disable=too-many-locals
1387 self,
1388 test_context: OrcaTestContext,
1389 case: dict,
1390 ) -> None:
1391 """Test AXUtilitiesTable.get_table with various scenarios."""
1392
1393 if case["scenario"] in ["table_cell_error", "table_object"]:
1394 essential_modules = self._setup_dependencies(test_context)
1395 else:
1396 essential_modules = self._setup_dependencies(test_context)
1397
1398 from orca.ax_object import AXObject
1399 from orca.ax_utilities_object import AXUtilitiesObject
1400 from orca.ax_utilities_table import AXUtilitiesTable
1401
1402 if case["obj_type"] == "none":
1403 result = AXUtilitiesTable.get_table(None)
1404 assert result is None
1405 return
1406
1407 mock_table = test_context.Mock(spec=Atspi.Accessible)
1408 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1409
1410 if case["scenario"] == "table_cell":
1411 test_context.patch_object(AXObject, "supports_table_cell", return_value=True)
1412 test_context.patch_object(
1413 Atspi.TableCell,
1414 "get_table",
1415 side_effect=lambda obj: mock_table,
1416 )
1417 test_context.patch_object(AXObject, "supports_table", return_value=True)
1418 elif case["scenario"] == "table_cell_error":
1419
1420 def raise_glib_error(obj):
1421 raise GLib.GError("Test error")
1422
1423 def is_table(obj):
1424 return obj == mock_table
1425
1426 self._setup_table_support_mocks(test_context, supports_table_cell=True)
1427 self._setup_table_role_mocks(test_context, is_table_func=is_table)
1428 self._setup_debug_message_mock(test_context, essential_modules)
1429
1430 test_context.patch_object(Atspi.TableCell, "get_table", side_effect=raise_glib_error)
1431 test_context.patch_object(AXObject, "supports_table", side_effect=is_table)
1432 test_context.patch_object(
1433 AXUtilitiesObject,
1434 "find_ancestor_inclusive",
1435 side_effect=lambda obj, func: mock_table,
1436 )
1437 elif case["scenario"] == "table_object":
1438 self._setup_table_support_mocks(
1439 test_context,
1440 supports_table_cell=False,
1441 supports_table=True,
1442 )
1443 self._setup_table_role_mocks(test_context, is_table_func=lambda _obj: True)
1444 test_context.patch_object(
1445 AXUtilitiesObject,
1446 "find_ancestor_inclusive",
1447 side_effect=lambda obj, func: mock_table,
1448 )
1449 mock_obj = mock_table
1450
1451 result = AXUtilitiesTable.get_table(mock_obj)
1452
1453 if case["expected_result"] == "table":
1454 assert result == mock_table
1455 else:
1456 assert result == case["expected_result"]
1457
1458 if case["expects_debug"]:
1459 essential_modules["orca.debug"].print_message.assert_called()
1460
1461 def test_is_layout_table_with_layout_guess(self, test_context: OrcaTestContext) -> None:
1462 """Test AXUtilitiesTable.is_layout_table with layout-guess attribute."""
1463
1464 mock_table = test_context.Mock(spec=Atspi.Accessible)
1465 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1466 from orca.ax_object import AXObject
1467 from orca.ax_utilities_role import AXUtilitiesRole
1468 from orca.ax_utilities_table import AXUtilitiesTable
1469
1470 attrs = {"layout-guess": "true"}
1471 test_context.patch_object(AXObject, "get_attributes_dict", side_effect=lambda obj: attrs)
1472 test_context.patch_object(AXUtilitiesRole, "is_table", return_value=True)
1473 result = AXUtilitiesTable.is_layout_table(mock_table)
1474 assert result is True
1475 essential_modules["orca.debug"].print_tokens.assert_called()
1476
1477 def test_is_layout_table_without_table_support(self, test_context: OrcaTestContext) -> None:
1478 """Test AXUtilitiesTable.is_layout_table without table support."""
1479
1480 mock_table = test_context.Mock(spec=Atspi.Accessible)
1481 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1482 from orca.ax_object import AXObject
1483 from orca.ax_utilities_role import AXUtilitiesRole
1484 from orca.ax_utilities_table import AXUtilitiesTable
1485
1486 attrs: dict[str, str | int | float | bool] = {}
1487 test_context.patch_object(AXObject, "get_attributes_dict", side_effect=lambda obj: attrs)
1488 test_context.patch_object(AXUtilitiesRole, "is_table", return_value=True)
1489 test_context.patch_object(AXObject, "supports_table", return_value=False)
1490 result = AXUtilitiesTable.is_layout_table(mock_table)
1491 assert result is True
1492 essential_modules["orca.debug"].print_tokens.assert_called()
1493
1494 def test_is_layout_table_with_headers(self, test_context: OrcaTestContext) -> None:
1495 """Test AXUtilitiesTable.is_layout_table with headers present."""
1496
1497 mock_table = test_context.Mock(spec=Atspi.Accessible)
1498 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1499 from orca.ax_object import AXObject
1500 from orca.ax_utilities_role import AXUtilitiesRole
1501 from orca.ax_utilities_table import AXUtilitiesTable
1502
1503 attrs: dict[str, str | int | float | bool] = {}
1504 test_context.patch_object(AXObject, "get_attributes_dict", side_effect=lambda obj: attrs)
1505 test_context.patch_object(AXUtilitiesRole, "is_table", return_value=True)
1506 test_context.patch_object(AXObject, "supports_table", return_value=True)
1507 test_context.patch_object(AXUtilitiesTable, "has_column_headers", return_value=True)
1508 test_context.patch_object(AXUtilitiesTable, "has_row_headers", return_value=False)
1509 result = AXUtilitiesTable.is_layout_table(mock_table)
1510 assert result is False
1511 essential_modules["orca.debug"].print_tokens.assert_called()
1512
1513 def test_is_layout_table_with_name(self, test_context: OrcaTestContext) -> None:
1514 """Test AXUtilitiesTable.is_layout_table with table name present."""
1515
1516 mock_table = test_context.Mock(spec=Atspi.Accessible)
1517 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1518 from orca.ax_object import AXObject
1519 from orca.ax_utilities_table import AXUtilitiesTable
1520
1521 attrs: dict[str, str | int | float | bool] = {}
1522 self._setup_table_role_mocks(test_context, is_table_func=lambda _obj: True)
1523 self._setup_table_support_mocks(test_context, supports_table=True)
1524 self._setup_layout_table_mocks(test_context, name="Table Name")
1525 test_context.patch_object(AXObject, "get_attributes_dict", side_effect=lambda obj: attrs)
1526 result = AXUtilitiesTable.is_layout_table(mock_table)
1527 assert result is False
1528 essential_modules["orca.debug"].print_tokens.assert_called()
1529
1530 def test_is_layout_table_with_caption(self, test_context: OrcaTestContext) -> None:
1531 """Test AXUtilitiesTable.is_layout_table with table caption present."""
1532
1533 essential_modules = self._setup_dependencies(test_context)
1534 from orca.ax_object import AXObject
1535 from orca.ax_utilities_table import AXUtilitiesTable
1536
1537 mock_table = test_context.Mock(spec=Atspi.Accessible)
1538 mock_caption = test_context.Mock(spec=Atspi.Accessible)
1539 attrs: dict[str, str | int | float | bool] = {}
1540
1541 self._setup_table_role_mocks(test_context, is_table_func=lambda _obj: True)
1542 self._setup_table_support_mocks(test_context, supports_table=True)
1543 self._setup_layout_table_mocks(test_context, caption=mock_caption)
1544
1545 test_context.patch_object(AXObject, "get_attributes_dict", side_effect=lambda obj: attrs)
1546
1547 result = AXUtilitiesTable.is_layout_table(mock_table)
1548 assert result is False
1549 essential_modules["orca.debug"].print_tokens.assert_called()
1550
1551 def test_get_first_cell(self, test_context: OrcaTestContext) -> None:
1552 """Test AXTable.get_first_cell."""
1553
1554 mock_table = test_context.Mock(spec=Atspi.Accessible)
1555 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1556 self._setup_dependencies(test_context)
1557 from orca.ax_table import AXTable
1558
1559 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1560 test_context.patch_object(
1561 AXTable,
1562 "get_cell_at",
1563 side_effect=lambda table, row, col: mock_cell,
1564 )
1565 result = AXTable.get_first_cell(mock_table)
1566 assert result == mock_cell
1567
1568 def test_get_last_cell(self, test_context: OrcaTestContext) -> None:
1569 """Test AXTable.get_last_cell."""
1570
1571 mock_table = test_context.Mock(spec=Atspi.Accessible)
1572 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1573 self._setup_dependencies(test_context)
1574 from orca.ax_table import AXTable
1575
1576 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1577 test_context.patch_object(AXTable, "get_row_count", return_value=3)
1578 test_context.patch_object(AXTable, "get_column_count", return_value=4)
1579 test_context.patch_object(
1580 AXTable,
1581 "get_cell_at",
1582 side_effect=lambda table, row, col: mock_cell,
1583 )
1584 result = AXTable.get_last_cell(mock_table)
1585 assert result == mock_cell
1586
1587 @pytest.mark.parametrize(
1588 "case",
1589 [
1590 {"id": "move_up_one_row", "cell_row": 2, "cell_col": 3},
1591 {"id": "top_row", "cell_row": 0, "cell_col": 3},
1592 ],
1593 ids=lambda case: case["id"],
1594 )
1595 def test_get_cell_above(
1596 self,
1597 test_context,
1598 case: dict,
1599 ) -> None:
1600 """Test AXTable.get_cell_above."""
1601
1602 mock_table = test_context.Mock(spec=Atspi.Accessible)
1603 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1604 mock_table = test_context.Mock(spec=Atspi.Accessible)
1605 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1606 self._setup_dependencies(test_context)
1607 from orca.ax_table import AXTable
1608
1609 mock_table = test_context.Mock(spec=Atspi.Accessible)
1610 mock_result_cell = test_context.Mock(spec=Atspi.Accessible)
1611 test_context.patch_object(
1612 AXTable,
1613 "get_cell_coordinates",
1614 side_effect=lambda obj, prefer_attribute: (case["cell_row"], case["cell_col"]),
1615 )
1616 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
1617 test_context.patch_object(
1618 AXTable,
1619 "get_cell_at",
1620 side_effect=lambda table, row, col: mock_result_cell,
1621 )
1622 result = AXTable.get_cell_above(mock_cell)
1623 assert result == mock_result_cell
1624
1625 @pytest.mark.parametrize(
1626 "case",
1627 [
1628 {"id": "single_row_span", "cell_row": 2, "cell_col": 3, "row_span": 1},
1629 {"id": "multi_row_span", "cell_row": 2, "cell_col": 3, "row_span": 2},
1630 ],
1631 ids=lambda case: case["id"],
1632 )
1633 def test_get_cell_below(
1634 self,
1635 test_context,
1636 case: dict,
1637 ) -> None:
1638 """Test AXTable.get_cell_below."""
1639
1640 mock_table = test_context.Mock(spec=Atspi.Accessible)
1641 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1642 mock_table = test_context.Mock(spec=Atspi.Accessible)
1643 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1644 self._setup_dependencies(test_context)
1645 from orca.ax_table import AXTable
1646
1647 mock_table = test_context.Mock(spec=Atspi.Accessible)
1648 mock_result_cell = test_context.Mock(spec=Atspi.Accessible)
1649 test_context.patch_object(
1650 AXTable,
1651 "get_cell_coordinates",
1652 side_effect=lambda obj, prefer_attribute: (case["cell_row"], case["cell_col"]),
1653 )
1654 test_context.patch_object(
1655 AXTable,
1656 "get_cell_spans",
1657 side_effect=lambda obj, prefer_attribute: (case["row_span"], 1),
1658 )
1659 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
1660 test_context.patch_object(
1661 AXTable,
1662 "get_cell_at",
1663 side_effect=lambda table, row, col: mock_result_cell,
1664 )
1665 result = AXTable.get_cell_below(mock_cell)
1666 assert result == mock_result_cell
1667
1668 @pytest.mark.parametrize(
1669 "case",
1670 [
1671 {"id": "move_left_one_column", "cell_row": 2, "cell_col": 3},
1672 {"id": "leftmost_column", "cell_row": 2, "cell_col": 0},
1673 ],
1674 ids=lambda case: case["id"],
1675 )
1676 def test_get_cell_on_left(
1677 self,
1678 test_context,
1679 case: dict,
1680 ) -> None:
1681 """Test AXTable.get_cell_on_left."""
1682
1683 mock_table = test_context.Mock(spec=Atspi.Accessible)
1684 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1685 mock_table = test_context.Mock(spec=Atspi.Accessible)
1686 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1687 self._setup_dependencies(test_context)
1688 from orca.ax_table import AXTable
1689
1690 mock_table = test_context.Mock(spec=Atspi.Accessible)
1691 mock_result_cell = test_context.Mock(spec=Atspi.Accessible)
1692 test_context.patch_object(
1693 AXTable,
1694 "get_cell_coordinates",
1695 side_effect=lambda obj, prefer_attribute: (case["cell_row"], case["cell_col"]),
1696 )
1697 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
1698 test_context.patch_object(
1699 AXTable,
1700 "get_cell_at",
1701 side_effect=lambda table, row, col: mock_result_cell,
1702 )
1703 result = AXTable.get_cell_on_left(mock_cell)
1704 assert result == mock_result_cell
1705
1706 @pytest.mark.parametrize(
1707 "case",
1708 [
1709 {"id": "single_col_span", "cell_row": 2, "cell_col": 3, "col_span": 1},
1710 {"id": "multi_col_span", "cell_row": 2, "cell_col": 3, "col_span": 2},
1711 ],
1712 ids=lambda case: case["id"],
1713 )
1714 def test_get_cell_on_right(
1715 self,
1716 test_context,
1717 case: dict,
1718 ) -> None:
1719 """Test AXTable.get_cell_on_right."""
1720
1721 mock_table = test_context.Mock(spec=Atspi.Accessible)
1722 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1723 mock_table = test_context.Mock(spec=Atspi.Accessible)
1724 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1725 self._setup_dependencies(test_context)
1726 from orca.ax_table import AXTable
1727
1728 mock_table = test_context.Mock(spec=Atspi.Accessible)
1729 mock_result_cell = test_context.Mock(spec=Atspi.Accessible)
1730 test_context.patch_object(
1731 AXTable,
1732 "get_cell_coordinates",
1733 side_effect=lambda obj, prefer_attribute: (case["cell_row"], case["cell_col"]),
1734 )
1735 test_context.patch_object(
1736 AXTable,
1737 "get_cell_spans",
1738 side_effect=lambda obj, prefer_attribute: (1, case["col_span"]),
1739 )
1740 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
1741 test_context.patch_object(
1742 AXTable,
1743 "get_cell_at",
1744 side_effect=lambda table, row, col: mock_result_cell,
1745 )
1746 result = AXTable.get_cell_on_right(mock_cell)
1747 assert result == mock_result_cell
1748
1749 def test_get_start_of_row(self, test_context: OrcaTestContext) -> None:
1750 """Test AXTable.get_start_of_row."""
1751
1752 mock_table = test_context.Mock(spec=Atspi.Accessible)
1753 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1754 self._setup_dependencies(test_context)
1755 from orca.ax_table import AXTable
1756
1757 mock_table = test_context.Mock(spec=Atspi.Accessible)
1758 mock_result_cell = test_context.Mock(spec=Atspi.Accessible)
1759 test_context.patch_object(AXTable, "get_cell_coordinates", return_value=(2, 3))
1760 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
1761 test_context.patch_object(
1762 AXTable,
1763 "get_cell_at",
1764 side_effect=lambda table, row, col: mock_result_cell,
1765 )
1766 result = AXTable.get_start_of_row(mock_cell)
1767 assert result == mock_result_cell
1768
1769 def test_get_end_of_row(self, test_context: OrcaTestContext) -> None:
1770 """Test AXTable.get_end_of_row."""
1771
1772 mock_table = test_context.Mock(spec=Atspi.Accessible)
1773 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1774 self._setup_dependencies(test_context)
1775 from orca.ax_table import AXTable
1776
1777 mock_table = test_context.Mock(spec=Atspi.Accessible)
1778 mock_result_cell = test_context.Mock(spec=Atspi.Accessible)
1779 test_context.patch_object(AXTable, "get_cell_coordinates", return_value=(2, 3))
1780 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
1781 test_context.patch_object(AXTable, "get_column_count", return_value=5)
1782 test_context.patch_object(
1783 AXTable,
1784 "get_cell_at",
1785 side_effect=lambda table, row, col: mock_result_cell,
1786 )
1787 result = AXTable.get_end_of_row(mock_cell)
1788 assert result == mock_result_cell
1789
1790 def test_get_top_of_column(self, test_context: OrcaTestContext) -> None:
1791 """Test AXTable.get_top_of_column."""
1792
1793 mock_table = test_context.Mock(spec=Atspi.Accessible)
1794 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1795 self._setup_dependencies(test_context)
1796 from orca.ax_table import AXTable
1797
1798 mock_table = test_context.Mock(spec=Atspi.Accessible)
1799 mock_result_cell = test_context.Mock(spec=Atspi.Accessible)
1800 test_context.patch_object(AXTable, "get_cell_coordinates", return_value=(2, 3))
1801 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
1802 test_context.patch_object(
1803 AXTable,
1804 "get_cell_at",
1805 side_effect=lambda table, row, col: mock_result_cell,
1806 )
1807 result = AXTable.get_top_of_column(mock_cell)
1808 assert result == mock_result_cell
1809
1810 def test_get_bottom_of_column(self, test_context: OrcaTestContext) -> None:
1811 """Test AXTable.get_bottom_of_column."""
1812
1813 mock_table = test_context.Mock(spec=Atspi.Accessible)
1814 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1815 self._setup_dependencies(test_context)
1816 from orca.ax_table import AXTable
1817
1818 mock_table = test_context.Mock(spec=Atspi.Accessible)
1819 mock_result_cell = test_context.Mock(spec=Atspi.Accessible)
1820 test_context.patch_object(AXTable, "get_cell_coordinates", return_value=(2, 3))
1821 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
1822 test_context.patch_object(AXTable, "get_row_count", return_value=8)
1823 test_context.patch_object(
1824 AXTable,
1825 "get_cell_at",
1826 side_effect=lambda table, row, col: mock_result_cell,
1827 )
1828 result = AXTable.get_bottom_of_column(mock_cell)
1829 assert result == mock_result_cell
1830
1831 @pytest.mark.parametrize(
1832 "case",
1833 [
1834 {
1835 "id": "formula_attribute",
1836 "formula_attr": "=SUM(A1:A5)",
1837 "formula_attr_alt": None,
1838 "expected_result": "=SUM(A1:A5)",
1839 },
1840 {
1841 "id": "formula_alt_attribute",
1842 "formula_attr": None,
1843 "formula_attr_alt": "=A1+B1",
1844 "expected_result": "=A1+B1",
1845 },
1846 {
1847 "id": "no_formula",
1848 "formula_attr": None,
1849 "formula_attr_alt": None,
1850 "expected_result": None,
1851 },
1852 ],
1853 ids=lambda case: case["id"],
1854 )
1855 def test_get_cell_formula(
1856 self,
1857 test_context,
1858 case: dict,
1859 ) -> None:
1860 """Test AXUtilitiesTable.get_cell_formula."""
1861
1862 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1863 self._setup_dependencies(test_context)
1864 from orca.ax_object import AXObject
1865 from orca.ax_utilities_table import AXUtilitiesTable
1866
1867 attrs = {}
1868 if case["formula_attr"]:
1869 attrs["formula"] = case["formula_attr"]
1870 if case["formula_attr_alt"]:
1871 attrs["Formula"] = case["formula_attr_alt"]
1872 test_context.patch_object(
1873 AXObject,
1874 "get_attributes_dict",
1875 side_effect=lambda obj, use_cache: attrs,
1876 )
1877 result = AXUtilitiesTable.get_cell_formula(mock_cell)
1878 assert result == case["expected_result"]
1879
1880 @pytest.mark.parametrize(
1881 "case",
1882 [
1883 {"id": "first_cell", "cell_row": 0, "cell_col": 0, "expected_result": True},
1884 {"id": "not_first_cell", "cell_row": 0, "cell_col": 1, "expected_result": False},
1885 {"id": "not_first_row", "cell_row": 1, "cell_col": 0, "expected_result": False},
1886 ],
1887 ids=lambda case: case["id"],
1888 )
1889 def test_is_first_cell(self, test_context, case: dict) -> None:
1890 """Test AXUtilitiesTable.is_first_cell."""
1891
1892 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1893 self._setup_dependencies(test_context)
1894 from orca.ax_table import AXTable
1895 from orca.ax_utilities_table import AXUtilitiesTable
1896
1897 test_context.patch_object(
1898 AXTable,
1899 "get_cell_coordinates",
1900 side_effect=lambda obj, prefer_attribute: (case["cell_row"], case["cell_col"]),
1901 )
1902 result = AXUtilitiesTable.is_first_cell(mock_cell)
1903 assert result == case["expected_result"]
1904
1905 @pytest.mark.parametrize(
1906 "case",
1907 [
1908 {
1909 "id": "last_cell",
1910 "cell_row": 2,
1911 "cell_col": 3,
1912 "row_count": 3,
1913 "col_count": 4,
1914 "expected_result": True,
1915 },
1916 {
1917 "id": "not_last_row",
1918 "cell_row": 1,
1919 "cell_col": 3,
1920 "row_count": 3,
1921 "col_count": 4,
1922 "expected_result": False,
1923 },
1924 {
1925 "id": "not_last_column",
1926 "cell_row": 2,
1927 "cell_col": 2,
1928 "row_count": 3,
1929 "col_count": 4,
1930 "expected_result": False,
1931 },
1932 {
1933 "id": "invalid_coordinates",
1934 "cell_row": -1,
1935 "cell_col": -1,
1936 "row_count": 3,
1937 "col_count": 4,
1938 "expected_result": False,
1939 },
1940 ],
1941 ids=lambda case: case["id"],
1942 )
1943 def test_is_last_cell(
1944 self,
1945 test_context,
1946 case: dict,
1947 ) -> None:
1948 """Test AXUtilitiesTable.is_last_cell."""
1949
1950 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1951 self._setup_dependencies(test_context)
1952 from orca.ax_table import AXTable
1953 from orca.ax_utilities_table import AXUtilitiesTable
1954
1955 mock_table = (
1956 test_context.Mock(spec=Atspi.Accessible)
1957 if case["cell_row"] >= 0 and case["cell_col"] >= 0
1958 else None
1959 )
1960 test_context.patch_object(
1961 AXTable,
1962 "get_cell_coordinates",
1963 side_effect=lambda obj, prefer_attribute: (case["cell_row"], case["cell_col"]),
1964 )
1965 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=mock_table)
1966 test_context.patch_object(
1967 AXTable,
1968 "get_row_count",
1969 side_effect=lambda table, prefer_attribute: case["row_count"],
1970 )
1971 test_context.patch_object(
1972 AXTable,
1973 "get_column_count",
1974 side_effect=lambda table, prefer_attribute: case["col_count"],
1975 )
1976 result = AXUtilitiesTable.is_last_cell(mock_cell)
1977 assert result == case["expected_result"]
1978
1979 @pytest.mark.parametrize(
1980 "case",
1981 [
1982 {"id": "start_of_row", "cell_col": 0, "expected_result": True},
1983 {"id": "not_start_of_row", "cell_col": 1, "expected_result": False},
1984 ],
1985 ids=lambda case: case["id"],
1986 )
1987 def test_is_start_of_row(self, test_context, case: dict) -> None:
1988 """Test AXTable.is_start_of_row."""
1989
1990 mock_cell = test_context.Mock(spec=Atspi.Accessible)
1991 self._setup_dependencies(test_context)
1992 from orca.ax_table import AXTable
1993
1994 test_context.patch_object(
1995 AXTable,
1996 "get_cell_coordinates",
1997 side_effect=lambda obj, prefer_attribute: (2, case["cell_col"]),
1998 )
1999 result = AXTable.is_start_of_row(mock_cell)
2000 assert result == case["expected_result"]
2001
2002 @pytest.mark.parametrize(
2003 "case",
2004 [
2005 {"id": "end_of_row", "cell_col": 3, "col_count": 4, "expected_result": True},
2006 {"id": "not_end_of_row", "cell_col": 2, "col_count": 4, "expected_result": False},
2007 {"id": "invalid_column", "cell_col": -1, "col_count": 4, "expected_result": False},
2008 ],
2009 ids=lambda case: case["id"],
2010 )
2011 def test_is_end_of_row(self, test_context, case: dict) -> None:
2012 """Test AXTable.is_end_of_row."""
2013
2014 mock_table = test_context.Mock(spec=Atspi.Accessible)
2015 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2016 self._setup_dependencies(test_context)
2017 from orca.ax_table import AXTable
2018
2019 mock_table = test_context.Mock(spec=Atspi.Accessible) if case["cell_col"] >= 0 else None
2020 test_context.patch_object(
2021 AXTable,
2022 "get_cell_coordinates",
2023 side_effect=lambda obj, prefer_attribute: (2, case["cell_col"]),
2024 )
2025 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
2026 test_context.patch_object(
2027 AXTable,
2028 "get_column_count",
2029 side_effect=lambda table, prefer_attribute: case["col_count"],
2030 )
2031 result = AXTable.is_end_of_row(mock_cell)
2032 assert result == case["expected_result"]
2033
2034 @pytest.mark.parametrize(
2035 "case",
2036 [
2037 {"id": "top_of_column", "cell_row": 0, "expected_result": True},
2038 {"id": "not_top_of_column", "cell_row": 1, "expected_result": False},
2039 ],
2040 ids=lambda case: case["id"],
2041 )
2042 def test_is_top_of_column(self, test_context, case: dict) -> None:
2043 """Test AXTable.is_top_of_column."""
2044
2045 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2046 self._setup_dependencies(test_context)
2047 from orca.ax_table import AXTable
2048
2049 test_context.patch_object(
2050 AXTable,
2051 "get_cell_coordinates",
2052 side_effect=lambda obj, prefer_attribute: (case["cell_row"], 3),
2053 )
2054 result = AXTable.is_top_of_column(mock_cell)
2055 assert result == case["expected_result"]
2056
2057 @pytest.mark.parametrize(
2058 "case",
2059 [
2060 {"id": "bottom_of_column", "cell_row": 4, "row_count": 5, "expected_result": True},
2061 {"id": "not_bottom_of_column", "cell_row": 3, "row_count": 5, "expected_result": False},
2062 {"id": "invalid_row", "cell_row": -1, "row_count": 5, "expected_result": False},
2063 ],
2064 ids=lambda case: case["id"],
2065 )
2066 def test_is_bottom_of_column(self, test_context, case: dict) -> None:
2067 """Test AXTable.is_bottom_of_column."""
2068
2069 mock_table = test_context.Mock(spec=Atspi.Accessible)
2070 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2071 self._setup_dependencies(test_context)
2072 from orca.ax_table import AXTable
2073
2074 mock_table = test_context.Mock(spec=Atspi.Accessible) if case["cell_row"] >= 0 else None
2075 test_context.patch_object(
2076 AXTable,
2077 "get_cell_coordinates",
2078 side_effect=lambda obj, prefer_attribute: (case["cell_row"], 3),
2079 )
2080 test_context.patch_object(AXTable, "_get_table", return_value=mock_table)
2081 test_context.patch_object(
2082 AXTable,
2083 "get_row_count",
2084 side_effect=lambda table, prefer_attribute: case["row_count"],
2085 )
2086 result = AXTable.is_bottom_of_column(mock_cell)
2087 assert result == case["expected_result"]
2088
2089 def test_get_table_description_for_presentation_with_support(
2090 self,
2091 test_context: OrcaTestContext,
2092 ) -> None:
2093 """Test AXUtilitiesTable.get_table_description_for_presentation with table support."""
2094
2095 mock_table = test_context.Mock(spec=Atspi.Accessible)
2096 self._setup_dependencies(test_context)
2097 from orca import messages
2098 from orca.ax_object import AXObject
2099 from orca.ax_table import AXTable
2100 from orca.ax_utilities_table import AXUtilitiesTable
2101
2102 test_context.patch_object(AXObject, "supports_table", return_value=True)
2103 test_context.patch_object(AXTable, "get_row_count", return_value=5)
2104 test_context.patch_object(AXTable, "get_column_count", return_value=3)
2105 test_context.patch_object(AXTable, "is_non_uniform_table", return_value=False)
2106 test_context.patch_object(
2107 messages,
2108 "table_size",
2109 side_effect=lambda rows, cols: f"{rows} by {cols} table",
2110 )
2111 result = AXUtilitiesTable.get_table_description_for_presentation(mock_table)
2112 assert result == "5 by 3 table"
2113
2114 def test_get_table_description_for_presentation_non_uniform(
2115 self,
2116 test_context: OrcaTestContext,
2117 ) -> None:
2118 """Test AXUtilitiesTable.get_table_description_for_presentation with non-uniform table."""
2119
2120 mock_table = test_context.Mock(spec=Atspi.Accessible)
2121 self._setup_dependencies(test_context)
2122 from orca.ax_utilities_table import AXUtilitiesTable
2123
2124 self._setup_table_description_mocks(
2125 test_context,
2126 row_count=5,
2127 col_count=3,
2128 is_non_uniform=True,
2129 table_size_msg=lambda rows, cols: f"{rows} by {cols} table",
2130 )
2131 result = AXUtilitiesTable.get_table_description_for_presentation(mock_table)
2132 assert result == "non uniform 5 by 3 table"
2133
2134 def test_get_table_description_for_presentation_without_support(
2135 self,
2136 test_context: OrcaTestContext,
2137 ) -> None:
2138 """Test AXUtilitiesTable.get_table_description_for_presentation without table support."""
2139
2140 mock_table = test_context.Mock(spec=Atspi.Accessible)
2141 self._setup_dependencies(test_context)
2142 from orca.ax_object import AXObject
2143 from orca.ax_utilities_table import AXUtilitiesTable
2144
2145 test_context.patch_object(AXObject, "supports_table", return_value=False)
2146 result = AXUtilitiesTable.get_table_description_for_presentation(mock_table)
2147 assert result == ""
2148
2149 def test_clear_cache_now(self, test_context: OrcaTestContext) -> None:
2150 """Test AXTable.clear_cache_now."""
2151
2152 self._setup_dependencies(test_context)
2153 from orca.ax_table import AXTable
2154
2155 clear_called = []
2156
2157 def mock_clear(reason):
2158 clear_called.append(reason)
2159
2160 test_context.patch("orca.ax_table.AXTable._clear_all_dictionaries", new=mock_clear)
2161 AXTable.clear_cache_now("test reason")
2162 assert clear_called == ["test reason"]
2163
2164 def test_clear_all_dictionaries(self, test_context: OrcaTestContext) -> None:
2165 """Test AXTable._clear_all_dictionaries."""
2166
2167 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
2168 from orca import debug
2169 from orca.ax_table import AXTable
2170
2171 AXTable.CAPTIONS[123] = test_context.Mock()
2172 AXTable.PHYSICAL_COORDINATES_FROM_CELL[456] = (1, 2)
2173 AXTable.PHYSICAL_SPANS_FROM_CELL[789] = (1, 1)
2174 AXTable.PRESENTABLE_SPANS[321] = ("1", "2")
2175 test_context.patch_object(
2176 debug,
2177 "print_message",
2178 new=essential_modules["orca.debug"].print_message,
2179 )
2180 AXTable._clear_all_dictionaries("test clear")
2181 assert len(AXTable.CAPTIONS) == 0
2182 assert len(AXTable.PHYSICAL_COORDINATES_FROM_CELL) == 0
2183 assert len(AXTable.PHYSICAL_SPANS_FROM_CELL) == 0
2184 assert len(AXTable.PRESENTABLE_SPANS) == 0
2185 essential_modules["orca.debug"].print_message.assert_called()
2186
2187 @pytest.mark.parametrize(
2188 "case",
2189 [
2190 {
2191 "id": "with_negative_index",
2192 "scenario": "negative_index",
2193 "expected_result": (-1, -1),
2194 },
2195 {"id": "with_glib_error", "scenario": "glib_error", "expected_result": (-1, -1)},
2196 ],
2197 ids=lambda case: case["id"],
2198 )
2199 def test_get_cell_spans_from_table_error_scenarios(
2200 self,
2201 test_context: OrcaTestContext,
2202 case: dict,
2203 ) -> None:
2204 """Test AXTable.get_cell_spans_from_table error handling scenarios."""
2205
2206 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2207
2208 if case["scenario"] == "negative_index":
2209 self._setup_dependencies(test_context)
2210 from orca.ax_table import AXTable
2211
2212 test_context.patch_object(AXTable, "_get_cell_index", return_value=-1)
2213 result = AXTable.get_cell_spans_from_table(mock_cell)
2214 assert result == case["expected_result"]
2215 else: # glib_error
2216 mock_table = test_context.Mock(spec=Atspi.Accessible)
2217 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
2218 from orca import debug
2219 from orca.ax_object import AXObject
2220 from orca.ax_table import AXTable
2221 from orca.ax_utilities_role import AXUtilitiesRole
2222
2223 def raise_glib_error(obj, index):
2224 raise GLib.GError("Test error")
2225
2226 test_context.patch_object(AXTable, "_get_cell_index", return_value=5)
2227 test_context.patch_object(AXTable, "_find_ancestor_table", return_value=mock_table)
2228 test_context.patch_object(AXObject, "supports_table", return_value=True)
2229 test_context.patch_object(AXUtilitiesRole, "is_tree", return_value=False)
2230 test_context.patch_object(
2231 Atspi.Table,
2232 "get_row_column_extents_at_index",
2233 side_effect=raise_glib_error,
2234 )
2235 test_context.patch_object(
2236 debug,
2237 "print_message",
2238 new=essential_modules["orca.debug"].print_message,
2239 )
2240 result = AXTable.get_cell_spans_from_table(mock_cell)
2241 assert result == case["expected_result"]
2242 essential_modules["orca.debug"].print_message.assert_called()
2243
2244 def test_get_cell_spans_from_table_no_table(self, test_context: OrcaTestContext) -> None:
2245 """Test AXTable.get_cell_spans_from_table with no table found."""
2246
2247 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2248 self._setup_dependencies(test_context)
2249 from orca.ax_table import AXTable
2250
2251 test_context.patch_object(AXTable, "_get_cell_index", return_value=5)
2252 test_context.patch_object(AXTable, "_find_ancestor_table", return_value=None)
2253 result = AXTable.get_cell_spans_from_table(mock_cell)
2254 assert result == (-1, -1)
2255
2256 def test_get_cell_spans_from_table_tree_table(self, test_context: OrcaTestContext) -> None:
2257 """Test AXTable.get_cell_spans_from_table with tree table."""
2258
2259 mock_table = test_context.Mock(spec=Atspi.Accessible)
2260 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2261 self._setup_dependencies(test_context)
2262 from orca.ax_object import AXObject
2263 from orca.ax_table import AXTable
2264 from orca.ax_utilities_role import AXUtilitiesRole
2265
2266 mock_table = test_context.Mock(spec=Atspi.Accessible)
2267 test_context.patch_object(AXTable, "_get_cell_index", return_value=5)
2268 test_context.patch_object(AXTable, "_find_ancestor_table", return_value=mock_table)
2269 test_context.patch_object(AXObject, "supports_table", return_value=True)
2270 test_context.patch_object(AXUtilitiesRole, "is_tree", return_value=True)
2271 result = AXTable.get_cell_spans_from_table(mock_cell)
2272 assert result == (1, 1)
2273
2274 def test_get_cell_spans_from_table_failed_result(self, test_context: OrcaTestContext) -> None:
2275 """Test AXTable.get_cell_spans_from_table with failed result."""
2276
2277 mock_table = test_context.Mock(spec=Atspi.Accessible)
2278 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2279 self._setup_dependencies(test_context)
2280 from orca.ax_table import AXTable
2281
2282 self._setup_cell_span_mocks(test_context, cell_index=5, table_obj=mock_table)
2283 result = AXTable.get_cell_spans_from_table(mock_cell)
2284 assert result == (-1, -1)
2285
2286 def test_get_cell_spans_from_table_span_validation(self, test_context: OrcaTestContext) -> None:
2287 """Test AXTable.get_cell_spans_from_table with span validation."""
2288
2289 mock_table = test_context.Mock(spec=Atspi.Accessible)
2290 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2291 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
2292 from orca.ax_object import AXObject
2293 from orca.ax_table import AXTable
2294 from orca.ax_utilities_role import AXUtilitiesRole
2295
2296 mock_table = test_context.Mock(spec=Atspi.Accessible)
2297 mock_result = test_context.Mock()
2298 mock_result.__getitem__ = lambda self, index: True
2299 mock_result.row_extents = 5
2300 mock_result.col_extents = 3
2301 test_context.patch_object(AXTable, "_get_cell_index", return_value=5)
2302 test_context.patch_object(AXTable, "_find_ancestor_table", return_value=mock_table)
2303 test_context.patch_object(AXObject, "supports_table", return_value=True)
2304 test_context.patch_object(AXUtilitiesRole, "is_tree", return_value=False)
2305 test_context.patch_object(
2306 Atspi.Table,
2307 "get_row_column_extents_at_index",
2308 side_effect=lambda table, index: mock_result,
2309 )
2310 test_context.patch_object(
2311 AXTable,
2312 "get_row_count",
2313 side_effect=lambda table, prefer_attr: 3,
2314 )
2315 test_context.patch_object(
2316 AXTable,
2317 "get_column_count",
2318 side_effect=lambda table, prefer_attr: 2,
2319 )
2320 result = AXTable.get_cell_spans_from_table(mock_cell)
2321 assert result == (1, 1)
2322 essential_modules["orca.debug"].print_tokens.assert_called()
2323
2324 @pytest.mark.parametrize(
2325 "case",
2326 [
2327 {"id": "with_table_support", "scenario": "table_support"},
2328 {"id": "with_glib_error", "scenario": "glib_error"},
2329 ],
2330 ids=lambda case: case["id"],
2331 )
2332 def test_get_column_headers_from_table_scenarios(
2333 self,
2334 test_context: OrcaTestContext,
2335 case: dict,
2336 ) -> None:
2337 """Test AXTable.get_column_headers_from_table with different scenarios."""
2338
2339 mock_table = test_context.Mock(spec=Atspi.Accessible)
2340 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
2341 from orca.ax_object import AXObject
2342 from orca.ax_table import AXTable
2343
2344 if case["scenario"] == "table_support":
2345 mock_header = test_context.Mock(spec=Atspi.Accessible)
2346 test_context.patch_object(AXObject, "supports_table", return_value=True)
2347 test_context.patch_object(
2348 Atspi.Table,
2349 "get_column_header",
2350 side_effect=lambda table, col: mock_header,
2351 )
2352 result = AXTable.get_column_headers_from_table(mock_table, 2)
2353 assert result == [mock_header]
2354 essential_modules["orca.debug"].print_tokens.assert_called()
2355 else: # glib_error
2356 from orca import debug
2357
2358 def raise_glib_error(obj, column):
2359 raise GLib.GError("Test error")
2360
2361 test_context.patch_object(AXObject, "supports_table", return_value=True)
2362 test_context.patch_object(
2363 Atspi.Table,
2364 "get_column_header",
2365 side_effect=raise_glib_error,
2366 )
2367 test_context.patch_object(
2368 debug,
2369 "print_message",
2370 new=essential_modules["orca.debug"].print_message,
2371 )
2372 result = AXTable.get_column_headers_from_table(mock_table, 2)
2373 assert not result
2374 essential_modules["orca.debug"].print_message.assert_called()
2375
2376 def test_get_column_headers_from_table_without_support(
2377 self,
2378 test_context: OrcaTestContext,
2379 ) -> None:
2380 """Test AXTable.get_column_headers_from_table without table support."""
2381
2382 mock_table = test_context.Mock(spec=Atspi.Accessible)
2383 self._setup_dependencies(test_context)
2384 from orca.ax_object import AXObject
2385 from orca.ax_table import AXTable
2386
2387 mock_table = test_context.Mock(spec=Atspi.Accessible)
2388 test_context.patch_object(AXObject, "supports_table", return_value=False)
2389 result = AXTable.get_column_headers_from_table(mock_table, 2)
2390 assert not result
2391
2392 def test_get_column_headers_from_table_negative_column(
2393 self,
2394 test_context: OrcaTestContext,
2395 ) -> None:
2396 """Test AXTable.get_column_headers_from_table with negative column."""
2397
2398 mock_table = test_context.Mock(spec=Atspi.Accessible)
2399 self._setup_dependencies(test_context)
2400 from orca.ax_object import AXObject
2401 from orca.ax_table import AXTable
2402
2403 mock_table = test_context.Mock(spec=Atspi.Accessible)
2404 test_context.patch_object(AXObject, "supports_table", return_value=True)
2405 result = AXTable.get_column_headers_from_table(mock_table, -1)
2406 assert not result
2407
2408 def test_get_column_headers_from_table_cell_with_support(
2409 self,
2410 test_context: OrcaTestContext,
2411 ) -> None:
2412 """Test AXTable.get_column_headers_from_table_cell with table cell support."""
2413
2414 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2415 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
2416 from orca.ax_object import AXObject
2417 from orca.ax_table import AXTable
2418
2419 mock_headers = [
2420 test_context.Mock(spec=Atspi.Accessible),
2421 test_context.Mock(spec=Atspi.Accessible),
2422 ]
2423 test_context.patch_object(AXObject, "supports_table_cell", return_value=True)
2424 test_context.patch_object(
2425 Atspi.TableCell,
2426 "get_column_header_cells",
2427 return_value=mock_headers,
2428 )
2429 result = AXTable.get_column_headers_from_table_cell(mock_cell)
2430 assert result == mock_headers
2431 essential_modules["orca.debug"].print_tokens.assert_called()
2432
2433 def test_get_column_headers_from_table_cell_without_support(
2434 self,
2435 test_context: OrcaTestContext,
2436 ) -> None:
2437 """Test AXTable.get_column_headers_from_table_cell without table cell support."""
2438
2439 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2440 self._setup_dependencies(test_context)
2441 from orca.ax_object import AXObject
2442 from orca.ax_table import AXTable
2443
2444 test_context.patch_object(AXObject, "supports_table_cell", return_value=False)
2445 result = AXTable.get_column_headers_from_table_cell(mock_cell)
2446 assert result == []
2447
2448 def test_get_column_headers_from_table_cell_with_glib_error(
2449 self,
2450 test_context: OrcaTestContext,
2451 ) -> None:
2452 """Test AXTable.get_column_headers_from_table_cell handles GLib.GError."""
2453
2454 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2455 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
2456 from orca import debug
2457 from orca.ax_object import AXObject
2458 from orca.ax_table import AXTable
2459
2460 def raise_glib_error(obj):
2461 raise GLib.GError("Test error")
2462
2463 test_context.patch_object(AXObject, "supports_table_cell", return_value=True)
2464 test_context.patch_object(
2465 Atspi.TableCell,
2466 "get_column_header_cells",
2467 side_effect=raise_glib_error,
2468 )
2469 test_context.patch_object(
2470 debug,
2471 "print_message",
2472 new=essential_modules["orca.debug"].print_message,
2473 )
2474 result = AXTable.get_column_headers_from_table_cell(mock_cell)
2475 assert result == []
2476 essential_modules["orca.debug"].print_message.assert_called()
2477
2478 @pytest.mark.parametrize(
2479 "case",
2480 [
2481 {"id": "with_table_support", "scenario": "table_support"},
2482 {"id": "with_glib_error", "scenario": "glib_error"},
2483 ],
2484 ids=lambda case: case["id"],
2485 )
2486 def test_get_row_headers_from_table_scenarios(
2487 self,
2488 test_context: OrcaTestContext,
2489 case: dict,
2490 ) -> None:
2491 """Test AXTable.get_row_headers_from_table with different scenarios."""
2492
2493 mock_table = test_context.Mock(spec=Atspi.Accessible)
2494 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
2495 from orca.ax_object import AXObject
2496 from orca.ax_table import AXTable
2497
2498 if case["scenario"] == "table_support":
2499 mock_header = test_context.Mock(spec=Atspi.Accessible)
2500 test_context.patch_object(AXObject, "supports_table", return_value=True)
2501 test_context.patch_object(
2502 Atspi.Table,
2503 "get_row_header",
2504 side_effect=lambda table, row: mock_header,
2505 )
2506 result = AXTable.get_row_headers_from_table(mock_table, 1)
2507 assert result == [mock_header]
2508 essential_modules["orca.debug"].print_tokens.assert_called()
2509 else: # glib_error
2510 from orca import debug
2511
2512 def raise_glib_error(obj, row):
2513 raise GLib.GError("Test error")
2514
2515 test_context.patch_object(AXObject, "supports_table", return_value=True)
2516 test_context.patch_object(Atspi.Table, "get_row_header", side_effect=raise_glib_error)
2517 test_context.patch_object(
2518 debug,
2519 "print_message",
2520 new=essential_modules["orca.debug"].print_message,
2521 )
2522 result = AXTable.get_row_headers_from_table(mock_table, 1)
2523 assert not result
2524 essential_modules["orca.debug"].print_message.assert_called()
2525
2526 def test_get_row_headers_from_table_without_support(
2527 self,
2528 test_context: OrcaTestContext,
2529 ) -> None:
2530 """Test AXTable.get_row_headers_from_table without table support."""
2531
2532 mock_table = test_context.Mock(spec=Atspi.Accessible)
2533 self._setup_dependencies(test_context)
2534 from orca.ax_object import AXObject
2535 from orca.ax_table import AXTable
2536
2537 mock_table = test_context.Mock(spec=Atspi.Accessible)
2538 test_context.patch_object(AXObject, "supports_table", return_value=False)
2539 result = AXTable.get_row_headers_from_table(mock_table, 1)
2540 assert not result
2541
2542 def test_get_row_headers_from_table_negative_row(self, test_context: OrcaTestContext) -> None:
2543 """Test AXTable.get_row_headers_from_table with negative row."""
2544
2545 mock_table = test_context.Mock(spec=Atspi.Accessible)
2546 self._setup_dependencies(test_context)
2547 from orca.ax_object import AXObject
2548 from orca.ax_table import AXTable
2549
2550 mock_table = test_context.Mock(spec=Atspi.Accessible)
2551 test_context.patch_object(AXObject, "supports_table", return_value=True)
2552 result = AXTable.get_row_headers_from_table(mock_table, -1)
2553 assert not result
2554
2555 def test_get_row_headers_from_table_cell_with_support(
2556 self,
2557 test_context: OrcaTestContext,
2558 ) -> None:
2559 """Test AXTable.get_row_headers_from_table_cell with table cell support."""
2560
2561 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2562 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
2563 from orca.ax_object import AXObject
2564 from orca.ax_table import AXTable
2565
2566 mock_headers = [
2567 test_context.Mock(spec=Atspi.Accessible),
2568 test_context.Mock(spec=Atspi.Accessible),
2569 ]
2570 test_context.patch_object(AXObject, "supports_table_cell", return_value=True)
2571 test_context.patch_object(
2572 Atspi.TableCell,
2573 "get_row_header_cells",
2574 return_value=mock_headers,
2575 )
2576 result = AXTable.get_row_headers_from_table_cell(mock_cell)
2577 assert result == mock_headers
2578 essential_modules["orca.debug"].print_tokens.assert_called()
2579
2580 def test_get_row_headers_from_table_cell_without_support(
2581 self,
2582 test_context: OrcaTestContext,
2583 ) -> None:
2584 """Test AXTable.get_row_headers_from_table_cell without table cell support."""
2585
2586 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2587 self._setup_dependencies(test_context)
2588 from orca.ax_object import AXObject
2589 from orca.ax_table import AXTable
2590
2591 test_context.patch_object(AXObject, "supports_table_cell", return_value=False)
2592 result = AXTable.get_row_headers_from_table_cell(mock_cell)
2593 assert result == []
2594
2595 def test_get_row_headers_from_table_cell_with_glib_error(
2596 self,
2597 test_context: OrcaTestContext,
2598 ) -> None:
2599 """Test AXTable.get_row_headers_from_table_cell handles GLib.GError."""
2600
2601 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2602 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
2603 from orca import debug
2604 from orca.ax_object import AXObject
2605 from orca.ax_table import AXTable
2606
2607 def raise_glib_error(obj):
2608 raise GLib.GError("Test error")
2609
2610 test_context.patch_object(AXObject, "supports_table_cell", return_value=True)
2611 test_context.patch_object(
2612 Atspi.TableCell,
2613 "get_row_header_cells",
2614 side_effect=raise_glib_error,
2615 )
2616 test_context.patch_object(
2617 debug,
2618 "print_message",
2619 new=essential_modules["orca.debug"].print_message,
2620 )
2621 result = AXTable.get_row_headers_from_table_cell(mock_cell)
2622 assert result == []
2623 essential_modules["orca.debug"].print_message.assert_called()
2624
2625 @pytest.mark.parametrize(
2626 "case",
2627 [
2628 {"id": "old_cell_not_table_cell", "old_cell_type": "not_table_cell"},
2629 {"id": "no_old_cell", "old_cell_type": None},
2630 {"id": "different_headers", "old_cell_type": "table_cell"},
2631 ],
2632 ids=lambda case: case["id"],
2633 )
2634 def test_get_new_row_headers_scenarios( # pylint: disable=too-many-locals
2635 self,
2636 test_context: OrcaTestContext,
2637 case: dict,
2638 ) -> None:
2639 """Test AXUtilitiesTable.get_new_row_headers with various scenarios."""
2640
2641 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2642 self._setup_dependencies(test_context)
2643 from orca.ax_utilities_table import AXUtilitiesTable
2644
2645 if case["old_cell_type"] == "not_table_cell":
2646 from orca.ax_utilities_object import AXUtilitiesObject
2647 from orca.ax_utilities_role import AXUtilitiesRole
2648
2649 mock_old_cell = test_context.Mock(spec=Atspi.Accessible)
2650 mock_ancestor = test_context.Mock(spec=Atspi.Accessible)
2651 mock_headers = [test_context.Mock(spec=Atspi.Accessible)]
2652 test_context.patch_object(
2653 AXUtilitiesRole,
2654 "is_table_cell_or_header",
2655 side_effect=lambda obj: obj == mock_ancestor,
2656 )
2657 test_context.patch_object(
2658 AXUtilitiesObject,
2659 "find_ancestor",
2660 side_effect=lambda obj, func: mock_ancestor,
2661 )
2662 test_context.patch_object(
2663 AXUtilitiesTable,
2664 "get_row_headers",
2665 side_effect=lambda obj: mock_headers if obj == mock_cell else [],
2666 )
2667 result = AXUtilitiesTable.get_new_row_headers(mock_cell, mock_old_cell)
2668 assert result == mock_headers
2669 elif case["old_cell_type"] is None:
2670 mock_headers = [test_context.Mock(spec=Atspi.Accessible)]
2671 test_context.patch_object(
2672 AXUtilitiesTable,
2673 "get_row_headers",
2674 return_value=mock_headers,
2675 )
2676 result = AXUtilitiesTable.get_new_row_headers(mock_cell, None)
2677 assert result == mock_headers
2678 else: # table_cell with different headers
2679 mock_old_cell = test_context.Mock(spec=Atspi.Accessible)
2680 mock_header1 = test_context.Mock(spec=Atspi.Accessible)
2681 mock_header2 = test_context.Mock(spec=Atspi.Accessible)
2682 mock_header3 = test_context.Mock(spec=Atspi.Accessible)
2683
2684 def get_headers(obj):
2685 if obj == mock_cell:
2686 return [mock_header1, mock_header2, mock_header3]
2687 return [mock_header1, mock_header2]
2688
2689 test_context.patch_object(AXUtilitiesTable, "get_row_headers", side_effect=get_headers)
2690 result = AXUtilitiesTable.get_new_row_headers(mock_cell, mock_old_cell)
2691 assert result == [mock_header3]
2692
2693 @pytest.mark.parametrize(
2694 "old_cell_type",
2695 [
2696 pytest.param("not_table_cell", id="old_cell_not_table_cell"),
2697 pytest.param(None, id="no_old_cell"),
2698 pytest.param("table_cell", id="different_headers"),
2699 ],
2700 )
2701 def test_get_new_column_headers_scenarios( # pylint: disable=too-many-locals
2702 self,
2703 test_context: OrcaTestContext,
2704 old_cell_type: str | None,
2705 ) -> None:
2706 """Test AXUtilitiesTable.get_new_column_headers with various scenarios."""
2707
2708 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2709 self._setup_dependencies(test_context)
2710 from orca.ax_utilities_table import AXUtilitiesTable
2711
2712 if old_cell_type == "not_table_cell":
2713 from orca.ax_utilities_object import AXUtilitiesObject
2714 from orca.ax_utilities_role import AXUtilitiesRole
2715
2716 mock_old_cell = test_context.Mock(spec=Atspi.Accessible)
2717 mock_ancestor = test_context.Mock(spec=Atspi.Accessible)
2718 mock_headers = [test_context.Mock(spec=Atspi.Accessible)]
2719 test_context.patch_object(
2720 AXUtilitiesRole,
2721 "is_table_cell_or_header",
2722 side_effect=lambda obj: obj == mock_ancestor,
2723 )
2724 test_context.patch_object(
2725 AXUtilitiesObject,
2726 "find_ancestor",
2727 side_effect=lambda obj, func: mock_ancestor,
2728 )
2729 test_context.patch_object(
2730 AXUtilitiesTable,
2731 "get_column_headers",
2732 side_effect=lambda obj: mock_headers if obj == mock_cell else [],
2733 )
2734 result = AXUtilitiesTable.get_new_column_headers(mock_cell, mock_old_cell)
2735 assert result == mock_headers
2736 elif old_cell_type is None:
2737 mock_headers = [test_context.Mock(spec=Atspi.Accessible)]
2738 test_context.patch_object(
2739 AXUtilitiesTable,
2740 "get_column_headers",
2741 return_value=mock_headers,
2742 )
2743 result = AXUtilitiesTable.get_new_column_headers(mock_cell, None)
2744 assert result == mock_headers
2745 else: # table_cell with different headers
2746 mock_old_cell = test_context.Mock(spec=Atspi.Accessible)
2747 mock_header1 = test_context.Mock(spec=Atspi.Accessible)
2748 mock_header2 = test_context.Mock(spec=Atspi.Accessible)
2749 mock_header3 = test_context.Mock(spec=Atspi.Accessible)
2750
2751 def get_headers(obj):
2752 if obj == mock_cell:
2753 return [mock_header1, mock_header2, mock_header3]
2754 return [mock_header1, mock_header2]
2755
2756 test_context.patch_object(
2757 AXUtilitiesTable,
2758 "get_column_headers",
2759 side_effect=get_headers,
2760 )
2761 result = AXUtilitiesTable.get_new_column_headers(mock_cell, mock_old_cell)
2762 assert result == [mock_header3]
2763
2764 def test_get_row_headers_not_table_cell(self, test_context: OrcaTestContext) -> None:
2765 """Test AXUtilitiesTable.get_row_headers with cell that is not a table cell."""
2766
2767 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2768 self._setup_dependencies(test_context)
2769 from orca.ax_utilities_role import AXUtilitiesRole
2770 from orca.ax_utilities_table import AXUtilitiesTable
2771
2772 test_context.patch_object(AXUtilitiesRole, "is_table_cell", return_value=False)
2773 result = AXUtilitiesTable.get_row_headers(mock_cell)
2774 assert result == []
2775
2776 @pytest.mark.parametrize(
2777 "header_scenario",
2778 [
2779 pytest.param("dynamic_header", id="with_dynamic_header"),
2780 pytest.param("multiple_headers", id="with_multiple_headers"),
2781 pytest.param("nested_headers", id="with_nested_headers"),
2782 ],
2783 )
2784 def test_get_row_headers_scenarios(
2785 self,
2786 test_context: OrcaTestContext,
2787 header_scenario: str,
2788 ) -> None:
2789 """Test AXUtilitiesTable.get_row_headers with different header scenarios."""
2790
2791 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2792 self._setup_dependencies(test_context)
2793 from orca.ax_utilities_role import AXUtilitiesRole
2794 from orca.ax_utilities_table import AXUtilitiesTable
2795
2796 test_context.patch_object(AXUtilitiesRole, "is_table_cell", return_value=True)
2797
2798 if header_scenario == "dynamic_header":
2799 mock_header = test_context.Mock(spec=Atspi.Accessible)
2800 test_context.patch_object(
2801 AXUtilitiesTable,
2802 "get_dynamic_row_header",
2803 return_value=mock_header,
2804 )
2805 result = AXUtilitiesTable.get_row_headers(mock_cell)
2806 assert result == [mock_header]
2807 elif header_scenario == "multiple_headers":
2808 mock_headers = [
2809 test_context.Mock(spec=Atspi.Accessible),
2810 test_context.Mock(spec=Atspi.Accessible),
2811 ]
2812 test_context.patch_object(AXUtilitiesTable, "get_dynamic_row_header", return_value=None)
2813 test_context.patch_object(
2814 AXUtilitiesTable,
2815 "_get_row_headers",
2816 return_value=mock_headers,
2817 )
2818 result = AXUtilitiesTable.get_row_headers(mock_cell)
2819 assert result == mock_headers
2820 else: # nested_headers
2821 mock_header1 = test_context.Mock(spec=Atspi.Accessible)
2822 mock_header2 = test_context.Mock(spec=Atspi.Accessible)
2823 call_count = 0
2824
2825 def mock_get_row_headers(_obj):
2826 nonlocal call_count
2827 call_count += 1
2828 if call_count == 1:
2829 return [mock_header1]
2830 if call_count == 2:
2831 return [mock_header2]
2832 return []
2833
2834 test_context.patch_object(AXUtilitiesTable, "get_dynamic_row_header", return_value=None)
2835 test_context.patch_object(
2836 AXUtilitiesTable,
2837 "_get_row_headers",
2838 side_effect=mock_get_row_headers,
2839 )
2840 result = AXUtilitiesTable.get_row_headers(mock_cell)
2841 assert result == [mock_header2, mock_header1]
2842
2843 def test_get_row_headers_via_table_interface(self, test_context: OrcaTestContext) -> None:
2844 """Test AXUtilitiesTable._get_row_headers via table interface."""
2845
2846 mock_table = test_context.Mock(spec=Atspi.Accessible)
2847 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2848 self._setup_dependencies(test_context)
2849 from orca.ax_utilities_table import AXUtilitiesTable
2850
2851 mock_headers = [test_context.Mock(spec=Atspi.Accessible)]
2852 self._setup_row_headers_mocks(
2853 test_context,
2854 mock_table,
2855 coordinates=(2, 3),
2856 spans=(2, 1),
2857 headers=mock_headers,
2858 )
2859 result = AXUtilitiesTable._get_row_headers(mock_cell)
2860 assert result == mock_headers + mock_headers
2861
2862 def test_get_row_headers_via_table_interface_invalid_coords(
2863 self,
2864 test_context: OrcaTestContext,
2865 ) -> None:
2866 """Test AXUtilitiesTable._get_row_headers via table interface with invalid coordinates."""
2867
2868 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2869 self._setup_dependencies(test_context)
2870 from orca.ax_object import AXObject
2871 from orca.ax_table import AXTable
2872 from orca.ax_utilities_table import AXUtilitiesTable
2873
2874 test_context.patch_object(AXObject, "supports_table_cell", return_value=False)
2875 test_context.patch_object(
2876 AXTable,
2877 "get_cell_coordinates_from_table",
2878 return_value=(-1, -1),
2879 )
2880 result = AXUtilitiesTable._get_row_headers(mock_cell)
2881 assert result == []
2882
2883 def test_get_row_headers_via_table_interface_no_table(
2884 self,
2885 test_context: OrcaTestContext,
2886 ) -> None:
2887 """Test AXUtilitiesTable._get_row_headers via table interface with no table."""
2888
2889 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2890 self._setup_dependencies(test_context)
2891 from orca.ax_object import AXObject
2892 from orca.ax_table import AXTable
2893 from orca.ax_utilities_table import AXUtilitiesTable
2894
2895 test_context.patch_object(AXObject, "supports_table_cell", return_value=False)
2896 test_context.patch_object(AXTable, "get_cell_coordinates_from_table", return_value=(2, 3))
2897 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=None)
2898 result = AXUtilitiesTable._get_row_headers(mock_cell)
2899 assert result == []
2900
2901 def test_has_row_headers_with_table_support(self, test_context: OrcaTestContext) -> None:
2902 """Test AXUtilitiesTable.has_row_headers with table support."""
2903
2904 mock_table = test_context.Mock(spec=Atspi.Accessible)
2905 self._setup_dependencies(test_context)
2906 from orca.ax_object import AXObject
2907 from orca.ax_table import AXTable
2908 from orca.ax_utilities_table import AXUtilitiesTable
2909
2910 mock_headers = [test_context.Mock(spec=Atspi.Accessible)]
2911 test_context.patch_object(AXObject, "supports_table", return_value=True)
2912 test_context.patch_object(AXTable, "get_row_count", return_value=5)
2913 test_context.patch_object(
2914 AXTable,
2915 "get_row_headers_from_table",
2916 side_effect=lambda table, row: mock_headers if row == 2 else [],
2917 )
2918 result = AXUtilitiesTable.has_row_headers(mock_table, 3)
2919 assert result is True
2920
2921 def test_has_row_headers_without_table_support(self, test_context: OrcaTestContext) -> None:
2922 """Test AXUtilitiesTable.has_row_headers without table support."""
2923
2924 mock_table = test_context.Mock(spec=Atspi.Accessible)
2925 self._setup_dependencies(test_context)
2926 from orca.ax_object import AXObject
2927 from orca.ax_utilities_table import AXUtilitiesTable
2928
2929 test_context.patch_object(AXObject, "supports_table", return_value=False)
2930 result = AXUtilitiesTable.has_row_headers(mock_table)
2931 assert result is False
2932
2933 def test_has_row_headers_no_headers_found(self, test_context: OrcaTestContext) -> None:
2934 """Test AXUtilitiesTable.has_row_headers with no headers found."""
2935
2936 mock_table = test_context.Mock(spec=Atspi.Accessible)
2937 self._setup_dependencies(test_context)
2938 from orca.ax_object import AXObject
2939 from orca.ax_table import AXTable
2940 from orca.ax_utilities_table import AXUtilitiesTable
2941
2942 test_context.patch_object(AXObject, "supports_table", return_value=True)
2943 test_context.patch_object(AXTable, "get_row_count", return_value=5)
2944 test_context.patch_object(
2945 AXTable,
2946 "get_row_headers_from_table",
2947 side_effect=lambda table, row: [],
2948 )
2949 result = AXUtilitiesTable.has_row_headers(mock_table, 3)
2950 assert result is False
2951
2952 def test_get_column_headers_not_table_cell(self, test_context: OrcaTestContext) -> None:
2953 """Test AXUtilitiesTable.get_column_headers with cell that is not a table cell."""
2954
2955 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2956 self._setup_dependencies(test_context)
2957 from orca.ax_utilities_role import AXUtilitiesRole
2958 from orca.ax_utilities_table import AXUtilitiesTable
2959
2960 test_context.patch_object(AXUtilitiesRole, "is_table_cell", return_value=False)
2961 result = AXUtilitiesTable.get_column_headers(mock_cell)
2962 assert result == []
2963
2964 @pytest.mark.parametrize(
2965 "header_scenario",
2966 [
2967 pytest.param("dynamic_header", id="with_dynamic_header"),
2968 pytest.param("multiple_headers", id="with_multiple_headers"),
2969 pytest.param("nested_headers", id="with_nested_headers"),
2970 ],
2971 )
2972 def test_get_column_headers_scenarios(
2973 self,
2974 test_context: OrcaTestContext,
2975 header_scenario: str,
2976 ) -> None:
2977 """Test AXUtilitiesTable.get_column_headers with different header scenarios."""
2978
2979 mock_cell = test_context.Mock(spec=Atspi.Accessible)
2980 self._setup_dependencies(test_context)
2981 from orca.ax_utilities_role import AXUtilitiesRole
2982 from orca.ax_utilities_table import AXUtilitiesTable
2983
2984 test_context.patch_object(AXUtilitiesRole, "is_table_cell", return_value=True)
2985
2986 if header_scenario == "dynamic_header":
2987 mock_header = test_context.Mock(spec=Atspi.Accessible)
2988 test_context.patch_object(
2989 AXUtilitiesTable,
2990 "get_dynamic_column_header",
2991 return_value=mock_header,
2992 )
2993 result = AXUtilitiesTable.get_column_headers(mock_cell)
2994 assert result == [mock_header]
2995 elif header_scenario == "multiple_headers":
2996 mock_headers = [
2997 test_context.Mock(spec=Atspi.Accessible),
2998 test_context.Mock(spec=Atspi.Accessible),
2999 ]
3000 test_context.patch_object(
3001 AXUtilitiesTable,
3002 "get_dynamic_column_header",
3003 return_value=None,
3004 )
3005 test_context.patch_object(
3006 AXUtilitiesTable,
3007 "_get_column_headers",
3008 return_value=mock_headers,
3009 )
3010 result = AXUtilitiesTable.get_column_headers(mock_cell)
3011 assert result == mock_headers
3012 else: # nested_headers
3013 mock_header1 = test_context.Mock(spec=Atspi.Accessible)
3014 mock_header2 = test_context.Mock(spec=Atspi.Accessible)
3015 call_count = 0
3016
3017 def mock_get_column_headers(_obj):
3018 nonlocal call_count
3019 call_count += 1
3020 if call_count == 1:
3021 return [mock_header1]
3022 if call_count == 2:
3023 return [mock_header2]
3024 return []
3025
3026 test_context.patch_object(
3027 AXUtilitiesTable,
3028 "get_dynamic_column_header",
3029 return_value=None,
3030 )
3031 test_context.patch_object(
3032 AXUtilitiesTable,
3033 "_get_column_headers",
3034 side_effect=mock_get_column_headers,
3035 )
3036 result = AXUtilitiesTable.get_column_headers(mock_cell)
3037 assert result == [mock_header2, mock_header1]
3038
3039 def test_get_column_headers_via_table_interface(self, test_context: OrcaTestContext) -> None:
3040 """Test AXUtilitiesTable._get_column_headers via table interface."""
3041
3042 mock_table = test_context.Mock(spec=Atspi.Accessible)
3043 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3044 self._setup_dependencies(test_context)
3045 from orca.ax_object import AXObject
3046 from orca.ax_table import AXTable
3047 from orca.ax_utilities_table import AXUtilitiesTable
3048
3049 mock_table = test_context.Mock(spec=Atspi.Accessible)
3050 mock_headers = [test_context.Mock(spec=Atspi.Accessible)]
3051 test_context.patch_object(AXObject, "supports_table_cell", return_value=False)
3052 test_context.patch_object(AXTable, "get_cell_coordinates_from_table", return_value=(2, 3))
3053 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=mock_table)
3054 test_context.patch_object(AXTable, "get_cell_spans_from_table", return_value=(1, 2))
3055 test_context.patch_object(
3056 AXTable,
3057 "get_column_headers_from_table",
3058 side_effect=lambda table, col: mock_headers,
3059 )
3060 result = AXUtilitiesTable._get_column_headers(mock_cell)
3061 assert result == mock_headers + mock_headers
3062
3063 def test_get_column_headers_via_table_interface_invalid_coords(
3064 self,
3065 test_context: OrcaTestContext,
3066 ) -> None:
3067 """Test AXUtilitiesTable._get_column_headers with invalid coordinates."""
3068
3069 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3070 self._setup_dependencies(test_context)
3071 from orca.ax_object import AXObject
3072 from orca.ax_table import AXTable
3073 from orca.ax_utilities_table import AXUtilitiesTable
3074
3075 test_context.patch_object(AXObject, "supports_table_cell", return_value=False)
3076 test_context.patch_object(
3077 AXTable,
3078 "get_cell_coordinates_from_table",
3079 return_value=(-1, -1),
3080 )
3081 result = AXUtilitiesTable._get_column_headers(mock_cell)
3082 assert result == []
3083
3084 def test_get_column_headers_via_table_interface_no_table(
3085 self,
3086 test_context: OrcaTestContext,
3087 ) -> None:
3088 """Test AXUtilitiesTable._get_column_headers via table interface with no table."""
3089
3090 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3091 self._setup_dependencies(test_context)
3092 from orca.ax_object import AXObject
3093 from orca.ax_table import AXTable
3094 from orca.ax_utilities_table import AXUtilitiesTable
3095
3096 test_context.patch_object(AXObject, "supports_table_cell", return_value=False)
3097 test_context.patch_object(AXTable, "get_cell_coordinates_from_table", return_value=(2, 3))
3098 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=None)
3099 result = AXUtilitiesTable._get_column_headers(mock_cell)
3100 assert result == []
3101
3102 def test_has_column_headers_with_table_support(self, test_context: OrcaTestContext) -> None:
3103 """Test AXUtilitiesTable.has_column_headers with table support."""
3104
3105 mock_table = test_context.Mock(spec=Atspi.Accessible)
3106 self._setup_dependencies(test_context)
3107 from orca.ax_object import AXObject
3108 from orca.ax_table import AXTable
3109 from orca.ax_utilities_table import AXUtilitiesTable
3110
3111 mock_headers = [test_context.Mock(spec=Atspi.Accessible)]
3112 test_context.patch_object(AXObject, "supports_table", return_value=True)
3113 test_context.patch_object(AXTable, "get_column_count", return_value=5)
3114 test_context.patch_object(
3115 AXTable,
3116 "get_column_headers_from_table",
3117 side_effect=lambda table, col: mock_headers if col == 1 else [],
3118 )
3119 result = AXUtilitiesTable.has_column_headers(mock_table, 3)
3120 assert result is True
3121
3122 def test_has_column_headers_without_table_support(self, test_context: OrcaTestContext) -> None:
3123 """Test AXUtilitiesTable.has_column_headers without table support."""
3124
3125 mock_table = test_context.Mock(spec=Atspi.Accessible)
3126 self._setup_dependencies(test_context)
3127 from orca.ax_object import AXObject
3128 from orca.ax_utilities_table import AXUtilitiesTable
3129
3130 test_context.patch_object(AXObject, "supports_table", return_value=False)
3131 result = AXUtilitiesTable.has_column_headers(mock_table)
3132 assert result is False
3133
3134 def test_has_column_headers_no_headers_found(self, test_context: OrcaTestContext) -> None:
3135 """Test AXUtilitiesTable.has_column_headers with no headers found."""
3136
3137 mock_table = test_context.Mock(spec=Atspi.Accessible)
3138 self._setup_dependencies(test_context)
3139 from orca.ax_object import AXObject
3140 from orca.ax_table import AXTable
3141 from orca.ax_utilities_table import AXUtilitiesTable
3142
3143 test_context.patch_object(AXObject, "supports_table", return_value=True)
3144 test_context.patch_object(AXTable, "get_column_count", return_value=5)
3145 test_context.patch_object(
3146 AXTable,
3147 "get_column_headers_from_table",
3148 side_effect=lambda table, col: [],
3149 )
3150 result = AXUtilitiesTable.has_column_headers(mock_table, 3)
3151 assert result is False
3152
3153 def test_get_cell_coordinates_with_find_cell(self, test_context: OrcaTestContext) -> None:
3154 """Test AXTable.get_cell_coordinates with find_cell=True."""
3155
3156 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3157 self._setup_dependencies(test_context)
3158 from orca.ax_object import AXObject
3159 from orca.ax_table import AXTable
3160 from orca.ax_utilities_object import AXUtilitiesObject
3161 from orca.ax_utilities_role import AXUtilitiesRole
3162
3163 mock_ancestor = test_context.Mock(spec=Atspi.Accessible)
3164 test_context.patch_object(
3165 AXUtilitiesRole,
3166 "is_table_cell_or_header",
3167 side_effect=lambda obj: obj == mock_ancestor,
3168 )
3169 test_context.patch_object(AXUtilitiesObject, "find_ancestor", return_value=mock_ancestor)
3170 test_context.patch_object(AXObject, "supports_table_cell", return_value=True)
3171 test_context.patch_object(
3172 AXTable,
3173 "_get_cell_coordinates_from_table_cell",
3174 return_value=(2, 3),
3175 )
3176 test_context.patch_object(
3177 AXTable,
3178 "_get_cell_coordinates_from_attribute",
3179 return_value=(None, None),
3180 )
3181 result = AXTable.get_cell_coordinates(mock_cell, find_cell=True)
3182 assert result == (2, 3)
3183
3184 def test_get_cell_coordinates_not_table_cell_or_header(
3185 self,
3186 test_context: OrcaTestContext,
3187 ) -> None:
3188 """Test AXTable.get_cell_coordinates with object that is not a table cell or header."""
3189
3190 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3191 self._setup_dependencies(test_context)
3192 from orca.ax_table import AXTable
3193 from orca.ax_utilities_role import AXUtilitiesRole
3194
3195 test_context.patch_object(AXUtilitiesRole, "is_table_cell_or_header", return_value=False)
3196 result = AXTable.get_cell_coordinates(mock_cell)
3197 assert result == (-1, -1)
3198
3199 @pytest.mark.parametrize(
3200 "scenario, expected_result",
3201 [
3202 pytest.param("negative_index", (-1, -1), id="with_negative_index"),
3203 pytest.param("glib_error", (-1, -1), id="with_glib_error"),
3204 ],
3205 )
3206 def test_get_cell_coordinates_from_table_error_scenarios(
3207 self,
3208 test_context: OrcaTestContext,
3209 scenario: str,
3210 expected_result: tuple[int, int],
3211 ) -> None:
3212 """Test AXTable.get_cell_coordinates_from_table error handling scenarios."""
3213
3214 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3215
3216 if scenario == "negative_index":
3217 self._setup_dependencies(test_context)
3218 from orca.ax_table import AXTable
3219
3220 test_context.patch_object(AXTable, "_get_cell_index", return_value=-1)
3221 result = AXTable.get_cell_coordinates_from_table(mock_cell)
3222 assert result == expected_result
3223 else: # glib_error
3224 mock_table = test_context.Mock(spec=Atspi.Accessible)
3225 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
3226 from orca import debug
3227 from orca.ax_table import AXTable
3228
3229 def raise_glib_error(obj, index):
3230 raise GLib.GError("Test error")
3231
3232 test_context.patch_object(AXTable, "_get_cell_index", return_value=5)
3233 test_context.patch_object(AXTable, "_find_ancestor_table", return_value=mock_table)
3234 test_context.patch_object(Atspi.Table, "get_row_at_index", side_effect=raise_glib_error)
3235 test_context.patch_object(
3236 debug,
3237 "print_message",
3238 new=essential_modules["orca.debug"].print_message,
3239 )
3240 result = AXTable.get_cell_coordinates_from_table(mock_cell)
3241 assert result == expected_result
3242 essential_modules["orca.debug"].print_message.assert_called()
3243
3244 def test_get_cell_coordinates_from_table_no_table(self, test_context: OrcaTestContext) -> None:
3245 """Test AXTable.get_cell_coordinates_from_table with no table found."""
3246
3247 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3248 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
3249 from orca.ax_table import AXTable
3250
3251 test_context.patch_object(AXTable, "_get_cell_index", return_value=5)
3252 test_context.patch_object(AXTable, "_find_ancestor_table", return_value=None)
3253 result = AXTable.get_cell_coordinates_from_table(mock_cell)
3254 assert result == (-1, -1)
3255 essential_modules["orca.debug"].print_tokens.assert_called()
3256
3257 def test_get_cell_coordinates_from_table_cell_without_support(
3258 self,
3259 test_context: OrcaTestContext,
3260 ) -> None:
3261 """Test AXTable._get_cell_coordinates_from_table_cell without table cell support."""
3262
3263 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3264 self._setup_dependencies(test_context)
3265 from orca.ax_object import AXObject
3266 from orca.ax_table import AXTable
3267
3268 test_context.patch_object(AXObject, "supports_table_cell", return_value=False)
3269 result = AXTable._get_cell_coordinates_from_table_cell(mock_cell)
3270 assert result == (-1, -1)
3271
3272 def test_get_cell_coordinates_from_table_cell_with_glib_error(
3273 self,
3274 test_context: OrcaTestContext,
3275 ) -> None:
3276 """Test AXTable._get_cell_coordinates_from_table_cell handles GLib.GError."""
3277
3278 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3279 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
3280 from orca import debug
3281 from orca.ax_object import AXObject
3282 from orca.ax_table import AXTable
3283
3284 def raise_glib_error(obj):
3285 raise GLib.GError("Test error")
3286
3287 test_context.patch_object(AXObject, "supports_table_cell", return_value=True)
3288 test_context.patch_object(Atspi.TableCell, "get_position", side_effect=raise_glib_error)
3289 test_context.patch_object(
3290 debug,
3291 "print_message",
3292 new=essential_modules["orca.debug"].print_message,
3293 )
3294 result = AXTable._get_cell_coordinates_from_table_cell(mock_cell)
3295 assert result == (-1, -1)
3296 essential_modules["orca.debug"].print_message.assert_called()
3297
3298 def test_get_cell_coordinates_from_table_cell_failed_result(
3299 self,
3300 test_context: OrcaTestContext,
3301 ) -> None:
3302 """Test AXTable._get_cell_coordinates_from_table_cell with failed result."""
3303
3304 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3305 self._setup_dependencies(test_context)
3306 from orca.ax_object import AXObject
3307 from orca.ax_table import AXTable
3308
3309 test_context.patch_object(AXObject, "supports_table_cell", return_value=True)
3310 test_context.patch_object(Atspi.TableCell, "get_position", return_value=(False, 2, 3))
3311 result = AXTable._get_cell_coordinates_from_table_cell(mock_cell)
3312 assert result == (-1, -1)
3313
3314 def test_get_cell_coordinates_from_attribute_with_none_cell(
3315 self,
3316 test_context: OrcaTestContext,
3317 ) -> None:
3318 """Test AXTable._get_cell_coordinates_from_attribute with None cell."""
3319
3320 self._setup_dependencies(test_context)
3321 from orca.ax_table import AXTable
3322
3323 result = AXTable._get_cell_coordinates_from_attribute(None)
3324 assert result == (None, None)
3325
3326 def test_get_cell_coordinates_from_attribute_from_row_ancestor(
3327 self,
3328 test_context: OrcaTestContext,
3329 ) -> None:
3330 """Test AXTable._get_cell_coordinates_from_attribute from row ancestor."""
3331
3332 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3333 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
3334 from orca.ax_object import AXObject
3335 from orca.ax_table import AXTable
3336 from orca.ax_utilities_object import AXUtilitiesObject
3337 from orca.ax_utilities_role import AXUtilitiesRole
3338
3339 mock_row = test_context.Mock(spec=Atspi.Accessible)
3340
3341 def get_attrs(obj):
3342 if obj == mock_cell:
3343 return {"rowindex": "2"}
3344 return {"rowindex": "2", "colindex": "5"}
3345
3346 test_context.patch_object(AXObject, "get_attributes_dict", side_effect=get_attrs)
3347 test_context.patch_object(
3348 AXUtilitiesRole,
3349 "is_table_row",
3350 side_effect=lambda obj: obj == mock_row,
3351 )
3352 test_context.patch_object(AXUtilitiesObject, "find_ancestor", return_value=mock_row)
3353 result = AXTable._get_cell_coordinates_from_attribute(mock_cell)
3354 assert result == ("2", "5")
3355 essential_modules["orca.debug"].print_tokens.assert_called()
3356
3357 def test_get_presentable_sort_order_from_header_not_header(
3358 self,
3359 test_context: OrcaTestContext,
3360 ) -> None:
3361 """Test AXUtilitiesTable.get_presentable_sort_order_from_header with non-header object."""
3362
3363 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3364 self._setup_dependencies(test_context)
3365 from orca.ax_utilities_role import AXUtilitiesRole
3366 from orca.ax_utilities_table import AXUtilitiesTable
3367
3368 test_context.patch_object(AXUtilitiesRole, "is_table_header", return_value=False)
3369 result = AXUtilitiesTable.get_presentable_sort_order_from_header(mock_cell)
3370 assert result == ""
3371
3372 @pytest.mark.parametrize(
3373 "sort_order, include_name, expected_prefix",
3374 [
3375 pytest.param("ascending", False, "", id="ascending_no_name"),
3376 pytest.param("descending", True, "Header Name. ", id="descending_with_name"),
3377 pytest.param("other", False, "", id="other_sort"),
3378 pytest.param("none", False, "", id="none_sort"),
3379 pytest.param("", False, "", id="empty_sort"),
3380 ],
3381 )
3382 def test_get_presentable_sort_order_from_header(
3383 self,
3384 test_context,
3385 sort_order,
3386 include_name,
3387 expected_prefix,
3388 ) -> None:
3389 """Test AXUtilitiesTable.get_presentable_sort_order_from_header."""
3390
3391 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3392 self._setup_dependencies(test_context)
3393 from orca import object_properties
3394 from orca.ax_object import AXObject
3395 from orca.ax_utilities_role import AXUtilitiesRole
3396 from orca.ax_utilities_table import AXUtilitiesTable
3397
3398 test_context.patch_object(AXUtilitiesRole, "is_table_header", return_value=True)
3399 test_context.patch_object(
3400 AXObject,
3401 "get_attribute",
3402 side_effect=lambda obj, attr, default: sort_order,
3403 )
3404 test_context.patch_object(
3405 AXObject,
3406 "get_name",
3407 side_effect=lambda obj: "Header Name" if include_name else "",
3408 )
3409 test_context.patch_object(object_properties, "SORT_ORDER_ASCENDING", new="ascending sort")
3410 test_context.patch_object(object_properties, "SORT_ORDER_DESCENDING", new="descending sort")
3411 test_context.patch_object(object_properties, "SORT_ORDER_OTHER", new="other sort")
3412 result = AXUtilitiesTable.get_presentable_sort_order_from_header(mock_cell, include_name)
3413 if sort_order == "ascending":
3414 assert result == expected_prefix + "ascending sort"
3415 elif sort_order == "descending":
3416 assert result == expected_prefix + "descending sort"
3417 elif sort_order == "other":
3418 assert result == expected_prefix + "other sort"
3419 else:
3420 assert result == ""
3421
3422 def test_get_dynamic_row_header_no_headers_column(self, test_context: OrcaTestContext) -> None:
3423 """Test AXUtilitiesTable.get_dynamic_row_header with no headers column set."""
3424
3425 mock_table = test_context.Mock(spec=Atspi.Accessible)
3426 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3427 self._setup_dependencies(test_context)
3428 from orca.ax_utilities_table import AXUtilitiesTable
3429
3430 mock_table = test_context.Mock(spec=Atspi.Accessible)
3431 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=mock_table)
3432 result = AXUtilitiesTable.get_dynamic_row_header(mock_cell)
3433 assert result is None
3434
3435 def test_get_dynamic_row_header_same_column(self, test_context: OrcaTestContext) -> None:
3436 """Test AXUtilitiesTable.get_dynamic_row_header with cell in same column as headers."""
3437
3438 mock_table = test_context.Mock(spec=Atspi.Accessible)
3439 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3440 self._setup_dependencies(test_context)
3441 from orca.ax_table import AXTable
3442 from orca.ax_utilities_table import AXUtilitiesTable
3443
3444 mock_table = test_context.Mock(spec=Atspi.Accessible)
3445 AXUtilitiesTable.DYNAMIC_ROW_HEADERS_COLUMN[hash(mock_table)] = 2
3446 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=mock_table)
3447 test_context.patch_object(AXTable, "get_cell_coordinates", return_value=(1, 2))
3448 result = AXUtilitiesTable.get_dynamic_row_header(mock_cell)
3449 assert result is None
3450
3451 def test_get_dynamic_row_header_different_column(self, test_context: OrcaTestContext) -> None:
3452 """Test AXUtilitiesTable.get_dynamic_row_header with cell in different column."""
3453
3454 mock_table = test_context.Mock(spec=Atspi.Accessible)
3455 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3456 self._setup_dependencies(test_context)
3457 from orca.ax_table import AXTable
3458 from orca.ax_utilities_table import AXUtilitiesTable
3459
3460 mock_table = test_context.Mock(spec=Atspi.Accessible)
3461 mock_header_cell = test_context.Mock(spec=Atspi.Accessible)
3462 AXUtilitiesTable.DYNAMIC_ROW_HEADERS_COLUMN[hash(mock_table)] = 0
3463 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=mock_table)
3464 test_context.patch_object(AXTable, "get_cell_coordinates", return_value=(1, 2))
3465 test_context.patch_object(
3466 AXTable,
3467 "get_cell_at",
3468 side_effect=lambda table, row, col: mock_header_cell,
3469 )
3470 result = AXUtilitiesTable.get_dynamic_row_header(mock_cell)
3471 assert result == mock_header_cell
3472
3473 def test_get_dynamic_column_header_no_headers_row(self, test_context: OrcaTestContext) -> None:
3474 """Test AXUtilitiesTable.get_dynamic_column_header with no headers row set."""
3475
3476 mock_table = test_context.Mock(spec=Atspi.Accessible)
3477 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3478 self._setup_dependencies(test_context)
3479 from orca.ax_utilities_table import AXUtilitiesTable
3480
3481 mock_table = test_context.Mock(spec=Atspi.Accessible)
3482 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=mock_table)
3483 result = AXUtilitiesTable.get_dynamic_column_header(mock_cell)
3484 assert result is None
3485
3486 def test_get_dynamic_column_header_same_row(self, test_context: OrcaTestContext) -> None:
3487 """Test AXUtilitiesTable.get_dynamic_column_header with cell in same row as headers."""
3488
3489 mock_table = test_context.Mock(spec=Atspi.Accessible)
3490 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3491 self._setup_dependencies(test_context)
3492 from orca.ax_table import AXTable
3493 from orca.ax_utilities_table import AXUtilitiesTable
3494
3495 mock_table = test_context.Mock(spec=Atspi.Accessible)
3496 AXUtilitiesTable.DYNAMIC_COLUMN_HEADERS_ROW[hash(mock_table)] = 0
3497 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=mock_table)
3498 test_context.patch_object(AXTable, "get_cell_coordinates", return_value=(0, 2))
3499 result = AXUtilitiesTable.get_dynamic_column_header(mock_cell)
3500 assert result is None
3501
3502 def test_get_dynamic_column_header_different_row(self, test_context: OrcaTestContext) -> None:
3503 """Test AXUtilitiesTable.get_dynamic_column_header with cell in different row."""
3504
3505 mock_table = test_context.Mock(spec=Atspi.Accessible)
3506 mock_cell = test_context.Mock(spec=Atspi.Accessible)
3507 self._setup_dependencies(test_context)
3508 from orca.ax_table import AXTable
3509 from orca.ax_utilities_table import AXUtilitiesTable
3510
3511 mock_table = test_context.Mock(spec=Atspi.Accessible)
3512 mock_header_cell = test_context.Mock(spec=Atspi.Accessible)
3513 AXUtilitiesTable.DYNAMIC_COLUMN_HEADERS_ROW[hash(mock_table)] = 0
3514 test_context.patch_object(AXUtilitiesTable, "get_table", return_value=mock_table)
3515 test_context.patch_object(AXTable, "get_cell_coordinates", return_value=(1, 2))
3516 test_context.patch_object(
3517 AXTable,
3518 "get_cell_at",
3519 side_effect=lambda table, row, col: mock_header_cell,
3520 )
3521 result = AXUtilitiesTable.get_dynamic_column_header(mock_cell)
3522 assert result == mock_header_cell
3523
3524 def test_set_dynamic_row_headers_column(self, test_context: OrcaTestContext) -> None:
3525 """Test AXUtilitiesTable.set_dynamic_row_headers_column."""
3526
3527 mock_table = test_context.Mock(spec=Atspi.Accessible)
3528 self._setup_dependencies(test_context)
3529 from orca.ax_utilities_table import AXUtilitiesTable
3530
3531 AXUtilitiesTable.set_dynamic_row_headers_column(mock_table, 3)
3532 assert AXUtilitiesTable.DYNAMIC_ROW_HEADERS_COLUMN[hash(mock_table)] == 3
3533
3534 def test_set_dynamic_column_headers_row(self, test_context: OrcaTestContext) -> None:
3535 """Test AXUtilitiesTable.set_dynamic_column_headers_row."""
3536
3537 mock_table = test_context.Mock(spec=Atspi.Accessible)
3538 self._setup_dependencies(test_context)
3539 from orca.ax_utilities_table import AXUtilitiesTable
3540
3541 AXUtilitiesTable.set_dynamic_column_headers_row(mock_table, 1)
3542 assert AXUtilitiesTable.DYNAMIC_COLUMN_HEADERS_ROW[hash(mock_table)] == 1
3543
3544 def test_clear_dynamic_row_headers_column_exists(self, test_context: OrcaTestContext) -> None:
3545 """Test AXUtilitiesTable.clear_dynamic_row_headers_column when entry exists."""
3546
3547 mock_table = test_context.Mock(spec=Atspi.Accessible)
3548 self._setup_dependencies(test_context)
3549 from orca.ax_utilities_table import AXUtilitiesTable
3550
3551 AXUtilitiesTable.DYNAMIC_ROW_HEADERS_COLUMN[hash(mock_table)] = 2
3552 AXUtilitiesTable.clear_dynamic_row_headers_column(mock_table)
3553 assert hash(mock_table) not in AXUtilitiesTable.DYNAMIC_ROW_HEADERS_COLUMN
3554
3555 def test_clear_dynamic_row_headers_column_not_exists(
3556 self,
3557 test_context: OrcaTestContext,
3558 ) -> None:
3559 """Test AXUtilitiesTable.clear_dynamic_row_headers_column when entry does not exist."""
3560
3561 mock_table = test_context.Mock(spec=Atspi.Accessible)
3562 self._setup_dependencies(test_context)
3563 from orca.ax_utilities_table import AXUtilitiesTable
3564
3565 AXUtilitiesTable.clear_dynamic_row_headers_column(mock_table)
3566
3567 def test_clear_dynamic_column_headers_row_exists(self, test_context: OrcaTestContext) -> None:
3568 """Test AXUtilitiesTable.clear_dynamic_column_headers_row when entry exists."""
3569
3570 mock_table = test_context.Mock(spec=Atspi.Accessible)
3571 self._setup_dependencies(test_context)
3572 from orca.ax_utilities_table import AXUtilitiesTable
3573
3574 AXUtilitiesTable.DYNAMIC_COLUMN_HEADERS_ROW[hash(mock_table)] = 1
3575 AXUtilitiesTable.clear_dynamic_column_headers_row(mock_table)
3576 assert hash(mock_table) not in AXUtilitiesTable.DYNAMIC_COLUMN_HEADERS_ROW
3577
3578 def test_clear_dynamic_column_headers_row_not_exists(
3579 self,
3580 test_context: OrcaTestContext,
3581 ) -> None:
3582 """Test AXUtilitiesTable.clear_dynamic_column_headers_row when entry does not exist."""
3583
3584 mock_table = test_context.Mock(spec=Atspi.Accessible)
3585 self._setup_dependencies(test_context)
3586 from orca.ax_utilities_table import AXUtilitiesTable
3587
3588 AXUtilitiesTable.clear_dynamic_column_headers_row(mock_table)