File: test_ax_text.py

package info (click to toggle)
orca 49.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,532 kB
  • sloc: python: 98,331; javascript: 281; sh: 64; xml: 27; makefile: 5
file content (2817 lines) | stat: -rw-r--r-- 113,870 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
# Unit tests for ax_text.py methods.
#
# Copyright 2025 Igalia, S.L.
# Author: Joanmarie Diggs <jdiggs@igalia.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.

# pylint: disable=too-many-public-methods
# pylint: disable=wrong-import-position
# pylint: disable=protected-access
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
# pylint: disable=import-outside-toplevel
# pylint: disable=too-many-lines
# pylint: disable=too-many-locals

"""Unit tests for ax_text.py methods."""

from __future__ import annotations

from typing import TYPE_CHECKING, Generator

import gi
import pytest

gi.require_version("Atspi", "2.0")
from gi.repository import Atspi, GLib

if TYPE_CHECKING:
    from .orca_test_context import OrcaTestContext
    from unittest.mock import MagicMock

class MockRect:  # pylint: disable=too-few-public-methods
    """Mock rectangle class for testing."""

    def __init__(self, x: int = 0, y: int = 0, width: int = 0, height: int = 0) -> None:
        self.x = x
        self.y = y
        self.width = width
        self.height = height


@pytest.mark.unit
class TestAXTextAttribute:
    """Test AXTextAttribute enum methods."""

    def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
        """Set up mocks for ax_text dependencies."""

        additional_modules = [
            "locale",
            "orca.colornames",
            "orca.ax_utilities_role",
            "orca.ax_utilities_state",
        ]
        essential_modules = test_context.setup_shared_dependencies(additional_modules)

        locale_mock = essential_modules["locale"]
        locale_mock.localeconv = test_context.Mock(return_value={"decimal_point": "."})

        colornames_mock = essential_modules["orca.colornames"]
        colornames_mock.COLOR_NAMES = {"#ff0000": "red", "#00ff00": "green"}
        colornames_mock.rgb_string_to_color_name = test_context.Mock(
            side_effect=lambda color: colornames_mock.COLOR_NAMES.get(color, color)
        )
        colornames_mock.normalize_rgb_string = test_context.Mock(side_effect=lambda color: color)

        debug_mock = essential_modules["orca.debug"]
        debug_mock.LEVEL_INFO = 800
        debug_mock.debugLevel = 0
        debug_mock.debug = test_context.Mock()
        debug_mock.print_message = test_context.Mock()

        messages_mock = essential_modules["orca.messages"]
        messages_mock.TEXT_ATTRIBUTE_NAMES = {
            "bg-color": "Background Color",
            "size": "size",
            "language": "Language",
            "weight": "Weight",
            "family-name": "Family Name",
        }
        messages_mock.pixel_count = test_context.Mock(
            side_effect=lambda count: f"{count} pixel{'s' if count != 1.0 else ''}"
        )

        settings_mock = essential_modules["orca.settings"]
        settings_mock.speakTextAttributes = True
        settings_mock.useColorNames = True

        text_attribute_names_mock = essential_modules["orca.text_attribute_names"]
        text_attribute_names_mock.attribute_names = {
            "bg-color": "Background Color",
            "size": "size",
            "language": "Language",
            "weight": "Weight",
            "family-name": "Family Name",
        }
        text_attribute_names_mock.attribute_values = test_context.Mock()
        text_attribute_names_mock.attribute_values.get = test_context.Mock(
            side_effect=lambda value, default: default
        )

        ax_object_mock = essential_modules["orca.ax_object"]
        ax_object_class_mock = test_context.Mock()
        ax_object_class_mock.supports_text = test_context.Mock(return_value=True)
        ax_object_mock.AXObject = ax_object_class_mock

        ax_utilities_role_mock = essential_modules["orca.ax_utilities_role"]
        role_class_mock = test_context.Mock()
        ax_utilities_role_mock.AXUtilitiesRole = role_class_mock

        ax_utilities_state_mock = essential_modules["orca.ax_utilities_state"]
        state_class_mock = test_context.Mock()
        ax_utilities_state_mock.AXUtilitiesState = state_class_mock

        return essential_modules

    def test_from_string_with_valid_attribute(self, test_context: OrcaTestContext) -> None:
        """Test AXTextAttribute.from_string with valid attribute name."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        result = AXTextAttribute.from_string("bg-color")
        assert result == AXTextAttribute.BG_COLOR

    def test_from_string_with_invalid_attribute(self, test_context: OrcaTestContext) -> None:
        """Test AXTextAttribute.from_string with invalid attribute name."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        result = AXTextAttribute.from_string("invalid-attribute")
        assert result is None

    def test_from_localized_string_with_valid_attribute(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXTextAttribute.from_localized_string with valid localized name."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        result = AXTextAttribute.from_localized_string("Background Color")
        assert result == AXTextAttribute.BG_COLOR

    def test_from_localized_string_with_invalid_attribute(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXTextAttribute.from_localized_string with invalid localized name."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        result = AXTextAttribute.from_localized_string("Invalid Localized")
        assert result is None

    def test_get_attribute_name(self, test_context: OrcaTestContext) -> None:
        """Test AXTextAttribute.get_attribute_name."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        result = AXTextAttribute.BG_COLOR.get_attribute_name()
        assert result == "bg-color"

    def test_get_localized_name_with_translation(self, test_context: OrcaTestContext) -> None:
        """Test AXTextAttribute.get_localized_name with available translation."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        result = AXTextAttribute.BG_COLOR.get_localized_name()
        assert result == "Background Color"

    def test_get_localized_name_without_translation(self, test_context: OrcaTestContext) -> None:
        """Test AXTextAttribute.get_localized_name without available translation."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        result = AXTextAttribute.INDENT.get_localized_name()
        assert result == "indent"

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "pixel_int_multiple",
                "attribute_name": "INDENT",
                "input_value": "12px",
                "expected_result": "12 pixels",
            },
            {
                "id": "pixel_int_singular",
                "attribute_name": "INDENT",
                "input_value": "1px",
                "expected_result": "1 pixel",
            },
            {
                "id": "pixel_int_zero",
                "attribute_name": "INDENT",
                "input_value": "0px",
                "expected_result": "0 pixels",
            },
            {
                "id": "pixel_float_multiple",
                "attribute_name": "INDENT",
                "input_value": "12.5px",
                "expected_result": "12.5 pixels",
            },
            {
                "id": "pixel_float_singular",
                "attribute_name": "INDENT",
                "input_value": "1.0px",
                "expected_result": "1.0 pixel",
            },
            {
                "id": "pixel_float_zero",
                "attribute_name": "INDENT",
                "input_value": "0.0px",
                "expected_result": "0.0 pixels",
            },
            {
                "id": "color_red",
                "attribute_name": "BG_COLOR",
                "input_value": "#ff0000",
                "expected_result": "red",
            },
            {
                "id": "moz_prefix_removal",
                "attribute_name": "JUSTIFICATION",
                "input_value": "left-moz",
                "expected_result": "left",
            },
            {
                "id": "justify_to_fill",
                "attribute_name": "JUSTIFICATION",
                "input_value": "justify",
                "expected_result": "fill",
            },
            {
                "id": "family_name_cleanup",
                "attribute_name": "FAMILY_NAME",
                "input_value": "Arial, sans-serif",
                "expected_result": "Arial",
            },
            {
                "id": "regular_value",
                "attribute_name": "LANGUAGE",
                "input_value": "en-US",
                "expected_result": "en-US",
            },
            {
                "id": "none_value",
                "attribute_name": "LANGUAGE",
                "input_value": None,
                "expected_result": "",
            },
        ],
        ids=lambda case: case["id"],
    )
    def test_get_localized_value_scenarios(
        self,
        test_context: OrcaTestContext,
        case: dict,
    ) -> None:
        """Test AXTextAttribute.get_localized_value with various scenarios."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        attribute = getattr(AXTextAttribute, case["attribute_name"])
        result = attribute.get_localized_value(case["input_value"])
        assert result == case["expected_result"]

    @pytest.mark.parametrize(
        "case",
        [
            {"id": "bg_color_true", "attribute": "BG_COLOR", "expected": True},
            {"id": "bg_full_height_false", "attribute": "BG_FULL_HEIGHT", "expected": False},
        ],
        ids=lambda case: case["id"],
    )
    def test_should_present_by_default(self, test_context: OrcaTestContext, case: dict) -> None:
        """Test AXTextAttribute.should_present_by_default."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        attr = getattr(AXTextAttribute, case["attribute"])
        result = attr.should_present_by_default()
        assert result == case["expected"]

    @pytest.mark.parametrize(
        "case",
        [
            {"id": "bg_color_zero", "attribute": "BG_COLOR", "value": "0", "expected": True},
            {"id": "bg_color_zero_px", "attribute": "BG_COLOR", "value": "0px", "expected": True},
            {"id": "bg_color_none", "attribute": "BG_COLOR", "value": "none", "expected": True},
            {"id": "bg_color_empty", "attribute": "BG_COLOR", "value": "", "expected": True},
            {"id": "bg_color_null", "attribute": "BG_COLOR", "value": None, "expected": True},
            {"id": "scale_one", "attribute": "SCALE", "value": "1.0", "expected": True},
            {
                "id": "text_position_baseline",
                "attribute": "TEXT_POSITION",
                "value": "baseline",
                "expected": True,
            },
            {"id": "weight_400", "attribute": "WEIGHT", "value": "400", "expected": True},
            {
                "id": "bg_color_bold_false",
                "attribute": "BG_COLOR",
                "value": "bold",
                "expected": False,
            },
        ],
        ids=lambda case: case["id"],
    )
    def test_value_is_default(self, test_context: OrcaTestContext, case: dict) -> None:
        """Test AXTextAttribute.value_is_default."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        attr = getattr(AXTextAttribute, case["attribute"])
        result = attr.value_is_default(case["value"])
        assert result == case["expected"]


@pytest.mark.unit
class TestAXText:
    """Test AXText class methods."""

    def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
        """Set up mocks for ax_text dependencies."""

        additional_modules = [
            "locale",
            "orca.colornames",
            "orca.ax_utilities_role",
            "orca.ax_utilities_state",
        ]
        essential_modules = test_context.setup_shared_dependencies(additional_modules)

        locale_mock = essential_modules["locale"]
        locale_mock.localeconv = test_context.Mock(return_value={"decimal_point": "."})

        colornames_mock = essential_modules["orca.colornames"]
        colornames_mock.COLOR_NAMES = {"#ff0000": "red", "#00ff00": "green"}
        colornames_mock.rgb_string_to_color_name = test_context.Mock(
            side_effect=lambda color: colornames_mock.COLOR_NAMES.get(color, color)
        )
        colornames_mock.normalize_rgb_string = test_context.Mock(side_effect=lambda color: color)

        debug_mock = essential_modules["orca.debug"]
        debug_mock.LEVEL_INFO = 800
        debug_mock.debugLevel = 0
        debug_mock.debug = test_context.Mock()
        debug_mock.print_message = test_context.Mock()

        messages_mock = essential_modules["orca.messages"]
        messages_mock.TEXT_ATTRIBUTE_NAMES = {
            "bg-color": "Background Color",
            "size": "size",
            "language": "Language",
            "weight": "Weight",
            "family-name": "Family Name",
        }
        messages_mock.pixel_count = test_context.Mock(
            side_effect=lambda count: f"{count} pixel{'s' if count != 1.0 else ''}"
        )

        settings_mock = essential_modules["orca.settings"]
        settings_mock.speakTextAttributes = True
        settings_mock.useColorNames = True

        text_attribute_names_mock = essential_modules["orca.text_attribute_names"]
        text_attribute_names_mock.attribute_names = {
            "bg-color": "Background Color",
            "size": "size",
            "language": "Language",
            "weight": "Weight",
            "family-name": "Family Name",
        }
        text_attribute_names_mock.attribute_values = test_context.Mock()
        text_attribute_names_mock.attribute_values.get = test_context.Mock(
            side_effect=lambda value, default: default
        )

        ax_object_mock = essential_modules["orca.ax_object"]
        ax_object_class_mock = test_context.Mock()
        ax_object_class_mock.supports_text = test_context.Mock(return_value=True)
        ax_object_mock.AXObject = ax_object_class_mock

        ax_utilities_role_mock = essential_modules["orca.ax_utilities_role"]
        role_class_mock = test_context.Mock()
        ax_utilities_role_mock.AXUtilitiesRole = role_class_mock

        ax_utilities_state_mock = essential_modules["orca.ax_utilities_state"]
        state_class_mock = test_context.Mock()
        ax_utilities_state_mock.AXUtilitiesState = state_class_mock

        return essential_modules

    def test_is_eoc_with_embedded_object_character(self, test_context: OrcaTestContext) -> None:
        """Test AXText.is_eoc with embedded object character."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        result = AXText.is_eoc("\ufffc")
        assert result is True

    def test_is_eoc_with_regular_character(self, test_context: OrcaTestContext) -> None:
        """Test AXText.is_eoc with regular character."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        result = AXText.is_eoc("a")
        assert result is False

    def test_character_at_offset_is_eoc_with_eoc(self, test_context: OrcaTestContext) -> None:
        """Test AXText.character_at_offset_is_eoc with embedded object character."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText, "get_character_at_offset", return_value=("\ufffc", 5, 6)
        )
        result = AXText.character_at_offset_is_eoc(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result is True

    def test_character_at_offset_is_eoc_without_eoc(self, test_context: OrcaTestContext) -> None:
        """Test AXText.character_at_offset_is_eoc without embedded object character."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText, "get_character_at_offset", return_value=("a", 5, 6)
        )
        result = AXText.character_at_offset_is_eoc(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result is False

    def test_is_whitespace_or_empty_with_whitespace(self, test_context: OrcaTestContext) -> None:
        """Test AXText.is_whitespace_or_empty with whitespace text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_all_text", return_value="   \t\n  ")
        result = AXText.is_whitespace_or_empty(test_context.Mock(spec=Atspi.Accessible))
        assert result is True

    def test_is_whitespace_or_empty_with_content(self, test_context: OrcaTestContext) -> None:
        """Test AXText.is_whitespace_or_empty with actual content."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_all_text", return_value="Hello world")
        result = AXText.is_whitespace_or_empty(test_context.Mock(spec=Atspi.Accessible))
        assert result is False

    def test_has_presentable_text_with_word_characters(self, test_context: OrcaTestContext) -> None:
        """Test AXText.has_presentable_text with word characters."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_all_text", return_value="Hello123")
        result = AXText.has_presentable_text(test_context.Mock(spec=Atspi.Accessible))
        assert result is True

    def test_has_presentable_text_without_word_characters(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.has_presentable_text without word characters."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_all_text", return_value="!@#$%^&*()")
        result = AXText.has_presentable_text(test_context.Mock(spec=Atspi.Accessible))
        assert result is False

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "successful",
                "should_raise_error": False,
                "expected_result": 50,
                "expected_debug_method": "print_tokens",
            },
            {
                "id": "glib_error",
                "should_raise_error": True,
                "expected_result": -1,
                "expected_debug_method": "print_message",
            },
        ],
        ids=lambda case: case["id"],
    )
    def test_get_caret_offset(
        self,
        test_context: OrcaTestContext,
        case: dict,
    ) -> None:
        """Test AXText.get_caret_offset successful case and GLib.GError handling."""

        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        if case["should_raise_error"]:

            def raise_glib_error(_obj) -> None:
                raise GLib.GError("Test error")

            test_context.patch_object(Atspi.Text, "get_caret_offset", new=raise_glib_error)
        else:
            test_context.patch_object(Atspi.Text, "get_caret_offset", return_value=50)

        essential_modules["orca.debug"].print_tokens = test_context.Mock()
        essential_modules["orca.debug"].print_message = test_context.Mock()
        result = AXText.get_caret_offset(test_context.Mock(spec=Atspi.Accessible))
        assert result == case["expected_result"]
        getattr(essential_modules["orca.debug"], case["expected_debug_method"]).assert_called()

    def test_set_caret_offset_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText.set_caret_offset successful case."""

        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(Atspi.Text, "set_caret_offset", return_value=True)
        essential_modules["orca.debug"].print_tokens = test_context.Mock()
        result = AXText.set_caret_offset(test_context.Mock(spec=Atspi.Accessible), 25)
        assert result is True
        essential_modules["orca.debug"].print_tokens.assert_called()

    def test_set_caret_offset_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText.set_caret_offset handles GLib.GError."""

        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        def raise_glib_error(_obj, _offset) -> None:
            raise GLib.GError("Test error")

        test_context.patch_object(Atspi.Text, "set_caret_offset", new=raise_glib_error)
        essential_modules["orca.debug"].print_message = test_context.Mock()
        result = AXText.set_caret_offset(test_context.Mock(spec=Atspi.Accessible), 25)
        assert result is False
        essential_modules["orca.debug"].print_message.assert_called()

    def test_set_caret_offset_to_start(self, test_context: OrcaTestContext) -> None:
        """Test AXText.set_caret_offset_to_start."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "set_caret_offset", return_value=True)
        result = AXText.set_caret_offset_to_start(test_context.Mock(spec=Atspi.Accessible))
        assert result is True

    def test_set_caret_offset_to_end(self, test_context: OrcaTestContext) -> None:
        """Test AXText.set_caret_offset_to_end."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=100)
        test_context.patch_object(AXText, "set_caret_offset", return_value=True)
        result = AXText.set_caret_offset_to_end(test_context.Mock(spec=Atspi.Accessible))
        assert result is True

    def test_has_selected_text_without_selection(self, test_context: OrcaTestContext) -> None:
        """Test AXText.has_selected_text without text selection."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "_get_n_selections", return_value=0)
        result = AXText.has_selected_text(test_context.Mock(spec=Atspi.Accessible))
        assert result is False

    def test_get_n_selections_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText._get_n_selections successful case."""

        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(Atspi.Text, "get_n_selections", return_value=2)
        essential_modules["orca.debug"].print_tokens = test_context.Mock()
        result = AXText._get_n_selections(test_context.Mock(spec=Atspi.Accessible))
        assert result == 2
        essential_modules["orca.debug"].print_tokens.assert_called()

    def test_get_n_selections_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText._get_n_selections handles GLib.GError."""

        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        def raise_glib_error(_obj) -> None:
            raise GLib.GError("Test error")

        test_context.patch_object(Atspi.Text, "get_n_selections", new=raise_glib_error)
        essential_modules["orca.debug"].print_message = test_context.Mock()
        result = AXText._get_n_selections(test_context.Mock(spec=Atspi.Accessible))
        assert result == 0
        essential_modules["orca.debug"].print_message.assert_called()

    def test_get_cached_selected_text_without_cache(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_cached_selected_text without cached data."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.Mock(spec=Atspi.Accessible).hash = test_context.Mock(return_value=54321)
        AXText.CACHED_TEXT_SELECTION.clear()
        result = AXText.get_cached_selected_text(test_context.Mock(spec=Atspi.Accessible))
        assert result == ("", 0, 0)

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "fully_contained",
                "rect1": MockRect(x=10, y=10, width=20, height=20),
                "rect2": MockRect(x=5, y=5, width=30, height=30),
                "expected": True,
            },
            {
                "id": "not_contained",
                "rect1": MockRect(x=0, y=0, width=20, height=20),
                "rect2": MockRect(x=5, y=5, width=10, height=10),
                "expected": False,
            },
            {
                "id": "exactly_same",
                "rect1": MockRect(x=10, y=10, width=20, height=20),
                "rect2": MockRect(x=10, y=10, width=20, height=20),
                "expected": True,
            },
        ],
        ids=lambda case: case["id"],
    )
    def test_rect_is_fully_contained_in(self, test_context: OrcaTestContext, case: dict) -> None:
        """Test AXText._rect_is_fully_contained_in."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        result = AXText._rect_is_fully_contained_in(case["rect1"], case["rect2"])
        assert result is case["expected"]

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "line_inside_clip",
                "line_rect": MockRect(y=10, height=10),
                "clip_rect": MockRect(y=5, height=30),
                "expected": 0,
            },
            {
                "id": "line_above_clip",
                "line_rect": MockRect(y=0, height=10),
                "clip_rect": MockRect(y=15, height=20),
                "expected": -1,
            },
            {
                "id": "line_below_clip",
                "line_rect": MockRect(y=40, height=10),
                "clip_rect": MockRect(y=5, height=20),
                "expected": 1,
            },
        ],
        ids=lambda case: case["id"],
    )
    def test_line_comparison(self, test_context: OrcaTestContext, case: dict) -> None:
        """Test AXText._line_comparison."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        result = AXText._line_comparison(case["line_rect"], case["clip_rect"])
        assert result == case["expected"]

    @pytest.mark.parametrize(
        "case",
        [
            {"id": "successful", "should_raise_error": False, "expected_result": ("hello", 5, 10)},
            {"id": "glib_error", "should_raise_error": True, "expected_result": ("", 0, 0)},
        ],
        ids=lambda case: case["id"],
    )
    def test_get_word_at_offset(
        self,
        test_context: OrcaTestContext,
        case: dict,
    ) -> None:
        """Test AXText.get_word_at_offset successful case and GLib.GError handling."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)
        test_context.patch_object(AXText, "get_caret_offset", return_value=7)

        if case["should_raise_error"]:

            def mock_get_string_at_offset(_obj, _offset, _granularity) -> None:
                raise GLib.GError("Test error")

            test_context.patch(
                "gi.repository.Atspi.Text.get_string_at_offset", new=mock_get_string_at_offset
            )
        else:
            mock_result = test_context.Mock()
            mock_result.content = "hello"
            mock_result.start_offset = 5
            mock_result.end_offset = 10
            test_context.patch(
                "gi.repository.Atspi.Text.get_string_at_offset",
                side_effect=lambda obj, offset, granularity: mock_result,
            )

        result = AXText.get_word_at_offset(test_context.Mock(spec=Atspi.Accessible))
        assert result == case["expected_result"]

    def test_get_all_text_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_all_text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=25)
        test_context.patch(
            "gi.repository.Atspi.Text.get_text", return_value="This is some test text"
        )
        result = AXText.get_all_text(test_context.Mock(spec=Atspi.Accessible))
        assert result == "This is some test text"

    def test_get_all_text_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_all_text handles GLib.GError exceptions."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=25)

        def mock_get_text(_obj, _start, _end) -> None:
            raise GLib.GError("Test error")

        test_context.patch("gi.repository.Atspi.Text.get_text", new=mock_get_text)
        result = AXText.get_all_text(test_context.Mock(spec=Atspi.Accessible))
        assert result == ""

    def test_get_selected_text_with_selection(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_selected_text with text selected."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText, "get_selected_ranges", return_value=[(5, 10), (15, 20)]
        )
        test_context.patch_object(
            AXText, "get_substring", side_effect=lambda obj, start, end: f"text{start}-{end}"
        )
        result = AXText.get_selected_text(test_context.Mock(spec=Atspi.Accessible))
        assert result == ("text5-10 text15-20", 5, 20)

    def test_get_selected_text_without_selection(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_selected_text without text selected."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_selected_ranges", return_value=[])
        result = AXText.get_selected_text(test_context.Mock(spec=Atspi.Accessible))
        assert result == ("", 0, 0)

    def test_get_text_attributes_at_offset_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_text_attributes_at_offset."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)
        test_context.patch_object(AXText, "get_caret_offset", return_value=5)
        test_context.patch_object(
            AXText,
            "get_text_attributes_at_offset",
            return_value=({"font-family": "Arial", "font-size": "12pt"}, 0, 10),
        )
        result = AXText.get_text_attributes_at_offset(test_context.Mock(spec=Atspi.Accessible))
        assert result == ({"font-family": "Arial", "font-size": "12pt"}, 0, 10)

    def test_get_text_attributes_at_offset_with_glib_error(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.get_text_attributes_at_offset handles GLib.GError exceptions."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)
        test_context.patch_object(AXText, "get_caret_offset", return_value=5)
        test_context.patch_object(AXText, "get_character_count", return_value=20)

        def mock_get_attribute_run(_obj, _offset, include_defaults=True) -> None:
            raise GLib.GError("Test error")

        test_context.patch(
            "gi.repository.Atspi.Text.get_attribute_run", new=mock_get_attribute_run
        )
        result = AXText.get_text_attributes_at_offset(test_context.Mock(spec=Atspi.Accessible))
        assert result == ({}, 0, 20)

    def test_get_all_text_attributes_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_all_text_attributes."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)
        test_context.patch_object(AXText, "get_character_count", return_value=20)

        def mock_get_text_attributes_at_offset(_obj, offset) -> tuple[dict[str, str], int, int]:
            if offset < 10:
                return ({"font-weight": "bold"}, 0, 10)
            return ({"font-style": "italic"}, 10, 20)

        test_context.patch_object(
            AXText, "get_text_attributes_at_offset", new=mock_get_text_attributes_at_offset
        )
        result = AXText.get_all_text_attributes(test_context.Mock(spec=Atspi.Accessible))
        assert len(result) == 2
        assert result[0] == (0, 10, {"font-weight": "bold"})
        assert result[1] == (10, 20, {"font-style": "italic"})

    def test_supports_paragraph_iteration_true(self, test_context: OrcaTestContext) -> None:
        """Test AXText.supports_paragraph_iteration returns True."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText, "get_paragraph_at_offset", return_value=("This is a paragraph.", 0, 20)
        )
        result = AXText.supports_paragraph_iteration(test_context.Mock(spec=Atspi.Accessible))
        assert result is True

    def test_iter_character_with_valid_text(self, test_context: OrcaTestContext) -> None:
        """Test AXText.iter_character with valid text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_caret_offset", return_value=0)
        test_context.patch_object(AXText, "get_character_count", return_value=3)

        def mock_get_character_at_offset(_obj, offset) -> tuple[str, int, int]:
            chars = [("a", 0, 1), ("b", 1, 2), ("c", 2, 3)]
            if 0 <= offset < 3:
                return chars[offset]
            return ("", 0, 0)

        test_context.patch_object(
            AXText, "get_character_at_offset", new=mock_get_character_at_offset
        )
        result = list(AXText.iter_character(test_context.Mock(spec=Atspi.Accessible)))
        assert result == [("a", 0, 1), ("b", 1, 2), ("c", 2, 3)]

    def test_iter_character_with_empty_text(self, test_context: OrcaTestContext) -> None:
        """Test AXText.iter_character with empty text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_caret_offset", return_value=0)
        test_context.patch_object(AXText, "get_character_count", return_value=0)
        result = list(AXText.iter_character(test_context.Mock(spec=Atspi.Accessible)))
        assert not result

    def test_iter_word_with_valid_text(self, test_context: OrcaTestContext) -> None:
        """Test AXText.iter_word with valid text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_caret_offset", return_value=0)
        test_context.patch_object(AXText, "get_character_count", return_value=11)

        def mock_get_word_at_offset(_obj, offset) -> tuple[str, int, int]:
            if offset is None:
                offset = 0
            if 0 <= offset < 5:
                return ("hello", 0, 5)
            if 5 <= offset < 11:
                return (" world", 5, 11)
            return ("", 0, 0)

        test_context.patch_object(AXText, "get_word_at_offset", new=mock_get_word_at_offset)
        result = list(AXText.iter_word(test_context.Mock(spec=Atspi.Accessible)))
        assert result == [("hello", 0, 5), (" world", 5, 11)]

    def test_iter_line_with_valid_text(self, test_context: OrcaTestContext) -> None:
        """Test AXText.iter_line with valid text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_caret_offset", return_value=0)
        test_context.patch_object(AXText, "get_character_count", return_value=20)

        def mock_get_line_at_offset(_obj, offset) -> tuple[str, int, int]:
            if offset is None:
                offset = 0
            if 0 <= offset < 10:
                return ("First line", 0, 10)
            if 10 <= offset < 20:
                return ("Second line", 10, 20)
            return ("", 0, 0)

        test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset)
        result = list(AXText.iter_line(test_context.Mock(spec=Atspi.Accessible)))
        assert result == [("First line", 0, 10), ("Second line", 10, 20)]

    def test_iter_line_prevents_infinite_loop_when_get_next_line_returns_same_position(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test that iter_line prevents infinite loops when get_next_line doesn't advance."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_caret_offset", return_value=0)

        def mock_get_line_at_offset(_obj, offset) -> tuple[str, int, int]:
            if offset is None:
                offset = 0
            return ("Test line", 0, 10)

        def mock_get_next_line(_obj, _offset) -> tuple[str, int, int]:
            return ("Next line", 0, 10)

        test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset)
        test_context.patch_object(AXText, "get_next_line", new=mock_get_next_line)

        result = list(AXText.iter_line(test_context.Mock(spec=Atspi.Accessible)))
        assert result == [("Test line", 0, 10)]

    def test_iter_line_skips_duplicate_when_offset_at_end(
        self, test_context: OrcaTestContext
    ) -> None:
        """Ensure iter_line doesn't yield the same line when starting at its end offset."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        # get_line_at_offset returns the same first line even when called at end
        test_context.patch_object(
            AXText,
            "get_line_at_offset",
            new=lambda _obj, _offset: ("First line", 0, 10),
        )

        # get_next_line advances to a distinct second line only once
        def mock_get_next_line(_obj, offset) -> tuple[str, int, int]:
            if offset == 0:
                return ("Second line", 10, 20)
            return ("", 0, 0)

        test_context.patch_object(AXText, "get_next_line", new=mock_get_next_line)

        result = list(AXText.iter_line(test_context.Mock(spec=Atspi.Accessible), 10))
        assert result == [("Second line", 10, 20)]

    def test_iter_sentence_prevents_infinite_loop_when_get_next_sentence_returns_same_position(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test that iter_sentence prevents infinite loops if get_next_sentence doesn't advance."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_caret_offset", return_value=0)

        def mock_get_sentence_at_offset(_obj, offset) -> tuple[str, int, int]:
            if offset is None:
                offset = 0
            return ("Test sentence", 0, 15)

        def mock_get_next_sentence(_obj, _offset) -> tuple[str, int, int]:
            return ("Next sentence", 0, 15)

        test_context.patch_object(AXText, "get_sentence_at_offset", new=mock_get_sentence_at_offset)
        test_context.patch_object(AXText, "get_next_sentence", new=mock_get_next_sentence)

        result = list(AXText.iter_sentence(test_context.Mock(spec=Atspi.Accessible)))
        assert result == [("Test sentence", 0, 15)]

    def test_iter_sentence_skips_duplicate_when_offset_at_end(
        self, test_context: OrcaTestContext
    ) -> None:
        """Ensure iter_sentence doesn't yield the same sentence when starting at its end."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        # get_sentence_at_offset returns the same first sentence even at its end
        test_context.patch_object(
            AXText,
            "get_sentence_at_offset",
            new=lambda _obj, _offset: ("First sentence.", 0, 12),
        )

        # get_next_sentence advances to a distinct second sentence only once
        def mock_get_next_sentence(_obj, offset) -> tuple[str, int, int]:
            if offset == 0:
                return ("Second sentence.", 12, 25)
            return ("", 0, 0)

        test_context.patch_object(AXText, "get_next_sentence", new=mock_get_next_sentence)

        result = list(AXText.iter_sentence(test_context.Mock(spec=Atspi.Accessible), 12))
        assert result == [("Second sentence.", 12, 25)]

    def test_iter_sentence_with_valid_text(self, test_context: OrcaTestContext) -> None:
        """Test AXText.iter_sentence with valid text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_caret_offset", return_value=0)
        test_context.patch_object(AXText, "get_character_count", return_value=25)

        def mock_get_sentence_at_offset(_obj, offset) -> tuple[str, int, int]:
            if offset is None:
                offset = 0
            if 0 <= offset < 12:
                return ("First sentence.", 0, 12)
            if 12 <= offset < 25:
                return ("Second sentence.", 12, 25)
            return ("", 0, 0)

        test_context.patch_object(
            AXText, "get_sentence_at_offset", new=mock_get_sentence_at_offset
        )
        result = list(AXText.iter_sentence(test_context.Mock(spec=Atspi.Accessible)))
        assert result == [("First sentence.", 0, 12), ("Second sentence.", 12, 25)]

    def test_iter_paragraph_with_valid_text(self, test_context: OrcaTestContext) -> None:
        """Test AXText.iter_paragraph with valid text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_caret_offset", return_value=0)
        test_context.patch_object(AXText, "get_character_count", return_value=30)

        def mock_get_paragraph_at_offset(_obj, offset) -> tuple[str, int, int]:
            if offset is None:
                offset = 0
            if 0 <= offset < 15:
                return ("First paragraph.", 0, 15)
            if 15 <= offset < 30:
                return ("Second paragraph.", 15, 30)
            return ("", 0, 0)

        test_context.patch_object(
            AXText, "get_paragraph_at_offset", new=mock_get_paragraph_at_offset
        )
        result = list(AXText.iter_paragraph(test_context.Mock(spec=Atspi.Accessible)))
        assert result == [("First paragraph.", 0, 15), ("Second paragraph.", 15, 30)]

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "offset_successful",
                "method_type": "offset",
                "offset_result": 25,
                "char_count": 30,
                "expected_result": 25,
                "should_raise_error": False,
                "debug_method": "print_tokens",
                "x_coord": 100,
                "y_coord": 200,
            },
            {
                "id": "offset_glib_error",
                "method_type": "offset",
                "offset_result": -1,
                "char_count": 30,
                "expected_result": -1,
                "should_raise_error": True,
                "debug_method": "print_message",
                "x_coord": 100,
                "y_coord": 200,
            },
            {
                "id": "character_valid",
                "method_type": "character",
                "offset_result": 5,
                "char_count": 10,
                "expected_result": ("a", 5, 6),
                "should_raise_error": False,
                "debug_method": None,
                "x_coord": 100,
                "y_coord": 200,
            },
            {
                "id": "character_invalid_offset",
                "method_type": "character",
                "offset_result": -1,
                "char_count": 10,
                "expected_result": ("", 0, 0),
                "should_raise_error": False,
                "debug_method": None,
                "x_coord": 100,
                "y_coord": 200,
            },
            {
                "id": "word_valid",
                "method_type": "word",
                "offset_result": 5,
                "char_count": 10,
                "expected_result": ("hello", 3, 8),
                "should_raise_error": False,
                "debug_method": None,
                "x_coord": 100,
                "y_coord": 200,
            },
            {
                "id": "line_valid",
                "method_type": "line",
                "offset_result": 5,
                "char_count": 20,
                "expected_result": ("This is a line", 0, 14),
                "should_raise_error": False,
                "debug_method": None,
                "x_coord": 100,
                "y_coord": 200,
            },
            {
                "id": "sentence_valid",
                "method_type": "sentence",
                "offset_result": 5,
                "char_count": 20,
                "expected_result": ("This is a sentence.", 0, 19),
                "should_raise_error": False,
                "debug_method": None,
                "x_coord": 100,
                "y_coord": 200,
            },
            {
                "id": "paragraph_valid",
                "method_type": "paragraph",
                "offset_result": 5,
                "char_count": 25,
                "expected_result": ("This is a paragraph.", 0, 20),
                "should_raise_error": False,
                "debug_method": None,
                "x_coord": 100,
                "y_coord": 200,
            },
        ],
        ids=lambda case: case["id"],
    )
    def test_get_at_point_methods_scenarios(  # pylint: disable=too-many-locals
        self,
        test_context: OrcaTestContext,
        case: dict,
    ) -> None:
        """Test AXText get_*_at_point methods with various scenarios."""

        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        mock_obj = test_context.Mock(spec=Atspi.Accessible)
        result: int | tuple[str, int, int] | None = None

        if case["method_type"] == "offset":
            if case["should_raise_error"]:

                def raise_glib_error(_obj, _x, _y, _coord_type) -> None:
                    raise GLib.GError("Test error")

                test_context.patch_object(
                    Atspi.Text, "get_offset_at_point", new=raise_glib_error
                )
            else:
                test_context.patch_object(
                    Atspi.Text,
                    "get_offset_at_point",
                    side_effect=lambda obj, x, y, coord_type: case["offset_result"],
                )
            essential_modules["orca.debug"].print_tokens = test_context.Mock()
            essential_modules["orca.debug"].print_message = test_context.Mock()
            result = AXText.get_offset_at_point(mock_obj, case["x_coord"], case["y_coord"])
            if case["debug_method"]:
                getattr(essential_modules["orca.debug"], case["debug_method"]).assert_called()
        else:
            test_context.patch_object(
                AXText, "get_offset_at_point", side_effect=lambda obj, x, y: case["offset_result"]
            )
            test_context.patch_object(
                AXText, "get_character_count", side_effect=lambda obj: case["char_count"]
            )

            if case["method_type"] == "character":
                if case["offset_result"] == -1:
                    result = AXText.get_character_at_point(
                        mock_obj, case["x_coord"], case["y_coord"]
                    )
                else:
                    test_context.patch_object(
                        AXText,
                        "get_character_at_offset",
                        side_effect=lambda obj, offset: case["expected_result"],
                    )
                    result = AXText.get_character_at_point(
                        mock_obj, case["x_coord"], case["y_coord"]
                    )
            elif case["method_type"] == "word":
                test_context.patch_object(
                    AXText,
                    "get_word_at_offset",
                    side_effect=lambda obj, offset: case["expected_result"]
                )
                result = AXText.get_word_at_point(mock_obj, case["x_coord"], case["y_coord"])
            elif case["method_type"] == "line":
                test_context.patch_object(
                    AXText,
                    "get_line_at_offset",
                    side_effect=lambda obj, offset: case["expected_result"]
                )
                result = AXText.get_line_at_point(mock_obj, case["x_coord"], case["y_coord"])
            elif case["method_type"] == "sentence":
                test_context.patch_object(
                    AXText,
                    "get_sentence_at_offset",
                    side_effect=lambda obj, offset: case["expected_result"]
                )
                result = AXText.get_sentence_at_point(mock_obj, case["x_coord"], case["y_coord"])
            elif case["method_type"] == "paragraph":
                test_context.patch_object(
                    AXText,
                    "get_paragraph_at_offset",
                    side_effect=lambda obj, offset: case["expected_result"]
                )
                result = AXText.get_paragraph_at_point(mock_obj, case["x_coord"], case["y_coord"])

        assert result == case["expected_result"]

    def test_string_has_spelling_error_with_invalid_spelling(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.string_has_spelling_error with spelling error."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText,
            "get_text_attributes_at_offset",
            return_value=({"invalid": "spelling"}, 0, 10),
        )
        result = AXText.string_has_spelling_error(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result is True

    def test_string_has_spelling_error_with_text_spelling(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.string_has_spelling_error with text-spelling misspelled."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText,
            "get_text_attributes_at_offset",
            return_value=({"text-spelling": "misspelled"}, 0, 10),
        )
        result = AXText.string_has_spelling_error(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result is True

    def test_string_has_spelling_error_with_underline_error(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.string_has_spelling_error with underline error."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText,
            "get_text_attributes_at_offset",
            return_value=({"underline": "spelling"}, 0, 10),
        )
        result = AXText.string_has_spelling_error(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result is True

    def test_string_has_spelling_error_without_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText.string_has_spelling_error without spelling error."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText,
            "get_text_attributes_at_offset",
            return_value=({"font-size": "12pt"}, 0, 10),
        )
        result = AXText.string_has_spelling_error(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result is False

    def test_string_has_grammar_error_with_invalid_grammar(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.string_has_grammar_error with grammar error."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText,
            "get_text_attributes_at_offset",
            return_value=({"invalid": "grammar"}, 0, 10),
        )
        result = AXText.string_has_grammar_error(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result is True

    def test_string_has_grammar_error_with_underline_grammar(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.string_has_grammar_error with underline grammar."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText,
            "get_text_attributes_at_offset",
            return_value=({"underline": "grammar"}, 0, 10),
        )
        result = AXText.string_has_grammar_error(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result is True

    def test_string_has_grammar_error_without_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText.string_has_grammar_error without grammar error."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText,
            "get_text_attributes_at_offset",
            return_value=({"font-size": "12pt"}, 0, 10),
        )
        result = AXText.string_has_grammar_error(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result is False

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "start_with_selection",
                "method_name": "get_selection_start_offset",
                "has_selection": True,
                "expected_result": 5,
            },
            {
                "id": "start_without_selection",
                "method_name": "get_selection_start_offset",
                "has_selection": False,
                "expected_result": -1,
            },
            {
                "id": "end_with_selection",
                "method_name": "get_selection_end_offset",
                "has_selection": True,
                "expected_result": 20,
            },
            {
                "id": "end_without_selection",
                "method_name": "get_selection_end_offset",
                "has_selection": False,
                "expected_result": -1,
            },
        ],
        ids=lambda case: case["id"],
    )
    def test_selection_offset_methods(
        self,
        test_context: OrcaTestContext,
        case: dict,
    ) -> None:
        """Test AXText selection offset methods with and without selection."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        selected_ranges = [(5, 10), (15, 20)] if case["has_selection"] else []
        test_context.patch_object(AXText, "get_selected_ranges", return_value=selected_ranges)

        method = getattr(AXText, case["method_name"])
        result = method(test_context.Mock(spec=Atspi.Accessible))
        assert result == case["expected_result"]

    def test_is_all_text_selected_with_full_selection(self, test_context: OrcaTestContext) -> None:
        """Test AXText.is_all_text_selected with full text selected."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)
        test_context.patch_object(AXText, "get_selected_ranges", return_value=[(0, 20)])
        result = AXText.is_all_text_selected(test_context.Mock(spec=Atspi.Accessible))
        assert result is True

    def test_is_all_text_selected_with_partial_selection(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.is_all_text_selected with partial selection."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)
        test_context.patch_object(AXText, "get_selected_ranges", return_value=[(5, 15)])
        result = AXText.is_all_text_selected(test_context.Mock(spec=Atspi.Accessible))
        assert result is False

    def test_is_all_text_selected_without_selection(self, test_context: OrcaTestContext) -> None:
        """Test AXText.is_all_text_selected without selection."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)
        test_context.patch_object(AXText, "get_selected_ranges", return_value=[])
        result = AXText.is_all_text_selected(test_context.Mock(spec=Atspi.Accessible))
        assert result is False

    def test_clear_all_selected_text(self, test_context: OrcaTestContext) -> None:
        """Test AXText.clear_all_selected_text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        removed_selections = []

        def mock_remove_selection(_obj, selection_number) -> None:
            removed_selections.append(selection_number)

        test_context.patch_object(AXText, "_get_n_selections", return_value=3)
        test_context.patch_object(AXText, "_remove_selection", new=mock_remove_selection)
        AXText.clear_all_selected_text(test_context.Mock(spec=Atspi.Accessible))
        assert removed_selections == [0, 1, 2]

    def test_remove_selection(self, test_context: OrcaTestContext) -> None:
        """Test AXText._remove_selection."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)
        mock_remove_selection = test_context.Mock()
        test_context.patch(
            "gi.repository.Atspi.Text.remove_selection", new=mock_remove_selection
        )
        mock_accessible = test_context.Mock(spec=Atspi.Accessible)
        AXText._remove_selection(mock_accessible, 0)
        mock_remove_selection.assert_called_once_with(mock_accessible, 0)

    def test_remove_selection_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText._remove_selection handles GLib.GError."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)

        def mock_remove_selection(obj, selection_number) -> None:
            raise GLib.GError("Test error")

        test_context.patch(
            "gi.repository.Atspi.Text.remove_selection", new=mock_remove_selection
        )

        AXText._remove_selection(test_context.Mock(spec=Atspi.Accessible), 0)

    def test_get_selected_ranges_with_valid_selections(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_selected_ranges with valid selections."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "_get_n_selections", return_value=2)

        def mock_get_selection(_obj, selection_num) -> None:
            results = [
                test_context.Mock(start_offset=5, end_offset=10),
                test_context.Mock(start_offset=15, end_offset=20),
            ]
            return results[selection_num]

        test_context.patch(
            "gi.repository.Atspi.Text.get_selection", new=mock_get_selection
        )
        result = AXText.get_selected_ranges(test_context.Mock(spec=Atspi.Accessible))
        assert result == [(5, 10), (15, 20)]

    def test_get_selected_ranges_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_selected_ranges handles GLib.GError."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "_get_n_selections", return_value=1)

        def mock_get_selection(obj, selection_num) -> None:
            raise GLib.GError("Test error")

        test_context.patch(
            "gi.repository.Atspi.Text.get_selection", new=mock_get_selection
        )
        result = AXText.get_selected_ranges(test_context.Mock(spec=Atspi.Accessible))
        assert not result

    def test_add_new_selection_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText._add_new_selection successful case."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)
        test_context.patch(
            "gi.repository.Atspi.Text.add_selection", return_value=True
        )
        result = AXText._add_new_selection(test_context.Mock(spec=Atspi.Accessible), 5, 10)
        assert result is True

    def test_add_new_selection_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText._add_new_selection handles GLib.GError."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)

        def mock_add_selection(obj, start, end) -> None:
            raise GLib.GError("Test error")

        test_context.patch(
            "gi.repository.Atspi.Text.add_selection", new=mock_add_selection
        )
        result = AXText._add_new_selection(test_context.Mock(spec=Atspi.Accessible), 5, 10)
        assert result is False

    def test_update_existing_selection_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText._update_existing_selection successful case."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)
        test_context.patch(
            "gi.repository.Atspi.Text.set_selection", return_value=True
        )
        result = AXText._update_existing_selection(
            test_context.Mock(spec=Atspi.Accessible), 5, 10, 0
        )
        assert result is True

    def test_update_existing_selection_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText._update_existing_selection handles GLib.GError."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)

        def mock_set_selection(obj, num, start, end) -> None:
            raise GLib.GError("Test error")

        test_context.patch(
            "gi.repository.Atspi.Text.set_selection", new=mock_set_selection
        )
        result = AXText._update_existing_selection(
            test_context.Mock(spec=Atspi.Accessible), 5, 10, 0
        )
        assert result is False

    def test_set_selected_text_with_existing_selection(self, test_context: OrcaTestContext) -> None:
        """Test AXText.set_selected_text with existing selection."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "_get_n_selections", return_value=1)
        test_context.patch_object(
            AXText, "_update_existing_selection", return_value=True
        )
        test_context.patch_object(AXText, "get_substring", return_value="test")
        test_context.patch_object(AXText, "get_selected_text", return_value=("test", 5, 10))
        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        essential_modules["orca.debug"].debugLevel = 0
        result = AXText.set_selected_text(test_context.Mock(spec=Atspi.Accessible), 5, 10)
        assert result is True

    def test_set_selected_text_with_new_selection(self, test_context: OrcaTestContext) -> None:
        """Test AXText.set_selected_text with new selection."""

        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "_get_n_selections", return_value=0)
        test_context.patch_object(AXText, "_add_new_selection", return_value=True)
        test_context.patch_object(AXText, "get_substring", return_value="test")
        test_context.patch_object(AXText, "get_selected_text", return_value=("test", 5, 10))
        essential_modules["orca.debug"].debugLevel = 0
        result = AXText.set_selected_text(test_context.Mock(spec=Atspi.Accessible), 5, 10)
        assert result is True

    def test_update_cached_selected_text(self, test_context: OrcaTestContext) -> None:
        """Test AXText.update_cached_selected_text."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(
            AXText, "get_selected_text", return_value=("cached text", 5, 15)
        )
        AXText.CACHED_TEXT_SELECTION.clear()
        mock_accessible = test_context.Mock(spec=Atspi.Accessible)
        AXText.update_cached_selected_text(mock_accessible)
        mock_hash = hash(mock_accessible)
        assert mock_hash in AXText.CACHED_TEXT_SELECTION
        assert AXText.CACHED_TEXT_SELECTION[mock_hash] == ("cached text", 5, 15)

    def test_get_character_rect_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_character_rect successful case."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)
        test_context.patch_object(AXText, "get_caret_offset", return_value=5)
        mock_rect = test_context.Mock(x=10, y=20, width=5, height=12)
        test_context.patch(
            "gi.repository.Atspi.Text.get_character_extents",
            side_effect=lambda obj, offset, coord_type: mock_rect,
        )
        result = AXText.get_character_rect(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result == mock_rect

    def test_get_character_rect_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_character_rect handles GLib.GError."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)
        test_context.patch_object(AXText, "get_caret_offset", return_value=5)

        def mock_get_character_extents(obj, offset, coord_type) -> None:
            raise GLib.GError("Test error")

        test_context.patch(
            "gi.repository.Atspi.Text.get_character_extents", new=mock_get_character_extents
        )
        result = AXText.get_character_rect(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result.x == 0 and result.y == 0

    def test_get_range_rect_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_range_rect successful case."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)
        mock_rect = test_context.Mock(x=10, y=20, width=50, height=12)
        test_context.patch(
            "gi.repository.Atspi.Text.get_range_extents",
            side_effect=lambda obj, start, end, coord_type: mock_rect,
        )
        result = AXText.get_range_rect(test_context.Mock(spec=Atspi.Accessible), 5, 15)
        assert result == mock_rect

    def test_get_range_rect_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_range_rect handles GLib.GError."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=True)

        def mock_get_range_extents(obj, start, end, coord_type) -> None:
            raise GLib.GError("Test error")

        test_context.patch(
            "gi.repository.Atspi.Text.get_range_extents", new=mock_get_range_extents
        )
        result = AXText.get_range_rect(test_context.Mock(spec=Atspi.Accessible), 5, 15)
        assert result.x == 0 and result.y == 0

    def test_scroll_substring_to_point_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText.scroll_substring_to_point successful case."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)
        test_context.patch(
            "gi.repository.Atspi.Text.scroll_substring_to_point",
            return_value=True,
        )
        result = AXText.scroll_substring_to_point(
            test_context.Mock(spec=Atspi.Accessible), 100, 200, 5, 15
        )
        assert result is True

    def test_scroll_substring_to_point_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText.scroll_substring_to_point handles GLib.GError."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)

        def mock_scroll_substring_to_point(obj, start, end, coord_type, x, y) -> None:
            raise GLib.GError("Test error")

        test_context.patch(
            "gi.repository.Atspi.Text.scroll_substring_to_point", new=mock_scroll_substring_to_point
        )
        result = AXText.scroll_substring_to_point(
            test_context.Mock(spec=Atspi.Accessible), 100, 200, 5, 15
        )
        assert result is False

    def test_scroll_substring_to_location_successful(self, test_context: OrcaTestContext) -> None:
        """Test AXText.scroll_substring_to_location successful case."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)
        mock_scroll_type = test_context.Mock()

        def mock_scroll_success(_obj, _start, _end, _location) -> bool:
            return True

        test_context.patch(
            "gi.repository.Atspi.Text.scroll_substring_to", new=mock_scroll_success
        )
        mock_accessible = test_context.Mock(spec=Atspi.Accessible)
        result = AXText.scroll_substring_to_location(mock_accessible, mock_scroll_type, 5, 15)
        assert result is True

    def test_scroll_substring_to_location_with_glib_error(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.scroll_substring_to_location handles GLib.GError."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)

        def mock_scroll_substring_to(obj, start, end, location) -> None:
            raise GLib.GError("Test error")

        mock_scroll_type = test_context.Mock()
        scroll_attr = "gi.repository.Atspi.Text.scroll_substring_to"
        test_context.patch(scroll_attr, new=mock_scroll_substring_to)
        mock_accessible = test_context.Mock(spec=Atspi.Accessible)
        result = AXText.scroll_substring_to_location(mock_accessible, mock_scroll_type, 5, 15)
        assert result is False

    def test_get_visible_lines(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_visible_lines."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        clip_rect = test_context.Mock(x=0, y=0, width=100, height=50)
        test_context.patch_object(
            AXText, "find_first_visible_line", return_value=("First line", 0, 10)
        )

        def mock_iter_line(_obj, _offset) -> Generator[tuple[str, int, int], None, None]:
            lines = [("Second line", 10, 20), ("Third line", 20, 30)]
            yield from lines

        test_context.patch_object(AXText, "iter_line", new=mock_iter_line)
        test_context.patch_object(
            AXText,
            "get_range_rect",
            side_effect=lambda obj, start, end: test_context.Mock(x=0, y=10, width=50, height=10),
        )
        test_context.patch_object(AXText, "_line_comparison", return_value=0)
        result = AXText.get_visible_lines(test_context.Mock(spec=Atspi.Accessible), clip_rect)
        assert len(result) >= 1
        assert result[0] == ("First line", 0, 10)

    def test_find_first_visible_line_at_start(self, test_context: OrcaTestContext) -> None:
        """Test AXText.find_first_visible_line when first line is at start."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        clip_rect = test_context.Mock(x=0, y=0, width=100, height=50)
        test_context.patch_object(AXText, "get_character_count", return_value=100)
        test_context.patch_object(
            AXText, "get_line_at_offset", return_value=("First line", 0, 10)
        )
        result = AXText.find_first_visible_line(test_context.Mock(spec=Atspi.Accessible), clip_rect)
        assert result == ("First line", 0, 10)

    def test_find_last_visible_line_at_end(self, test_context: OrcaTestContext) -> None:
        """Test AXText.find_last_visible_line when last line is at end."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        clip_rect = test_context.Mock(x=0, y=0, width=100, height=50)
        test_context.patch_object(AXText, "get_character_count", return_value=100)
        test_context.patch_object(
            AXText, "get_line_at_offset", return_value=("Last line", 90, 100)
        )
        result = AXText.find_last_visible_line(test_context.Mock(spec=Atspi.Accessible), clip_rect)
        assert result == ("Last line", 90, 100)

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "successful",
                "should_raise_error": False,
                "expected_result": ("This is a line", 0, 14),
            },
            {"id": "glib_error", "should_raise_error": True, "expected_result": ("", 0, 0)},
        ],
        ids=lambda case: case["id"],
    )
    def test_get_line_at_offset(
        self,
        test_context: OrcaTestContext,
        case: dict,
    ) -> None:
        """Test AXText.get_line_at_offset successful case and GLib.GError handling."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)
        test_context.patch_object(AXText, "get_caret_offset", return_value=7)

        if case["should_raise_error"]:

            def mock_get_string_at_offset(_obj, _offset, _granularity) -> None:
                raise GLib.GError("Test error")

            test_context.patch(
                "gi.repository.Atspi.Text.get_string_at_offset", new=mock_get_string_at_offset
            )
        else:
            mock_result = test_context.Mock()
            mock_result.content = "This is a line"
            mock_result.start_offset = 0
            mock_result.end_offset = 14
            test_context.patch(
                "gi.repository.Atspi.Text.get_string_at_offset",
                side_effect=lambda obj, offset, granularity: mock_result,
            )

        result = AXText.get_line_at_offset(test_context.Mock(spec=Atspi.Accessible))
        assert result == case["expected_result"]

    def test_get_line_at_offset_chromium_fallback(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_line_at_offset with Chromium fallback for invalid result."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)
        test_context.patch_object(AXText, "get_caret_offset", return_value=20)
        call_count = 0

        def mock_get_string_at_offset(_obj, _offset, _granularity):
            nonlocal call_count
            call_count += 1
            mock_result = test_context.Mock()
            if call_count == 1:
                mock_result.content = ""
                mock_result.start_offset = -1
                mock_result.end_offset = -1
            else:
                mock_result.content = "Valid line"
                mock_result.start_offset = 10
                mock_result.end_offset = 20
            return mock_result

        test_context.patch(
            "gi.repository.Atspi.Text.get_string_at_offset", new=mock_get_string_at_offset
        )
        result = AXText.get_line_at_offset(test_context.Mock(spec=Atspi.Accessible), 20)
        assert result == ("Valid line", 10, 20)

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "successful",
                "should_raise_error": False,
                "expected_result": ("This is a sentence.", 0, 19),
            },
            {"id": "glib_error", "should_raise_error": True, "expected_result": ("", 0, 0)},
        ],
        ids=lambda case: case["id"],
    )
    def test_get_sentence_at_offset(
        self,
        test_context: OrcaTestContext,
        case: dict,
    ) -> None:
        """Test AXText.get_sentence_at_offset successful case and GLib.GError handling."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=25)
        test_context.patch_object(AXText, "get_caret_offset", return_value=7)

        if case["should_raise_error"]:

            def mock_get_string_at_offset(_obj, _offset, _granularity) -> None:
                raise GLib.GError("Test error")

            test_context.patch(
                "gi.repository.Atspi.Text.get_string_at_offset", new=mock_get_string_at_offset
            )
            test_context.patch(
                "gi.repository.Atspi.Text.get_text", return_value=""
            )
        else:
            mock_result = test_context.Mock()
            mock_result.content = "This is a sentence."
            mock_result.start_offset = 0
            mock_result.end_offset = 19
            test_context.patch(
                "gi.repository.Atspi.Text.get_string_at_offset",
                side_effect=lambda obj, offset, granularity: mock_result,
            )

        result = AXText.get_sentence_at_offset(test_context.Mock(spec=Atspi.Accessible))
        assert result == case["expected_result"]

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "successful",
                "should_raise_error": False,
                "expected_result": ("This is a paragraph.", 0, 20),
            },
            {"id": "glib_error", "should_raise_error": True, "expected_result": ("", 0, 0)},
        ],
        ids=lambda case: case["id"],
    )
    def test_get_paragraph_at_offset(
        self,
        test_context: OrcaTestContext,
        case: dict,
    ) -> None:
        """Test AXText.get_paragraph_at_offset successful case and GLib.GError handling."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=25)
        test_context.patch_object(AXText, "get_caret_offset", return_value=7)

        if case["should_raise_error"]:

            def mock_get_string_at_offset(_obj, _offset, _granularity) -> None:
                raise GLib.GError("Test error")

            test_context.patch(
                "gi.repository.Atspi.Text.get_string_at_offset", new=mock_get_string_at_offset
            )
        else:
            mock_result = test_context.Mock()
            mock_result.content = "This is a paragraph."
            mock_result.start_offset = 0
            mock_result.end_offset = 20
            test_context.patch(
                "gi.repository.Atspi.Text.get_string_at_offset",
                side_effect=lambda obj, offset, granularity: mock_result,
            )

        result = AXText.get_paragraph_at_offset(test_context.Mock(spec=Atspi.Accessible))
        assert result == case["expected_result"]

    def test_get_character_at_offset_invalid_offset(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_character_at_offset with invalid offset."""

        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        essential_modules["orca.debug"].print_message = test_context.Mock()
        test_context.patch(
            "gi.repository.Atspi.Text.get_character_count", return_value=10
        )
        result = AXText.get_character_at_offset(test_context.Mock(spec=Atspi.Accessible), 15)
        assert result == ("", 0, 0)
        essential_modules["orca.debug"].print_message.assert_called()

    def test_get_substring_with_end_offset_minus_one(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_substring with end_offset=-1."""

        essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch_object(AXText, "get_character_count", return_value=20)
        test_context.patch_object(
            Atspi.Text, "get_text", return_value="substring"
        )
        essential_modules["orca.debug"].print_tokens = test_context.Mock()
        result = AXText.get_substring(test_context.Mock(spec=Atspi.Accessible), 5, -1)
        assert result == "substring"

    def test_supports_paragraph_iteration_false(self, test_context: OrcaTestContext) -> None:
        """Test AXText.supports_paragraph_iteration returns False."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=False)
        result = AXText.supports_paragraph_iteration(test_context.Mock(spec=Atspi.Accessible))
        assert result is False

    def test_is_whitespace_or_empty_no_text_support(self, test_context: OrcaTestContext) -> None:
        """Test AXText.is_whitespace_or_empty with no text support."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText
        from orca.ax_object import AXObject

        test_context.patch_object(AXObject, "supports_text", return_value=False)
        result = AXText.is_whitespace_or_empty(test_context.Mock(spec=Atspi.Accessible))
        assert result is True

    @pytest.mark.parametrize(
        "case",
        [
            {"id": "matching_locale", "attribute_value": "en", "expected": True},
            {"id": "non_matching_locale", "attribute_value": "fr", "expected": False},
        ],
        ids=lambda case: case["id"],
    )
    def test_value_is_default_language_attribute(
        self, test_context: OrcaTestContext, case: dict
    ) -> None:
        """Test AXTextAttribute.value_is_default for LANGUAGE attribute."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXTextAttribute

        test_context.patch("locale.getlocale", return_value=("en_US", "UTF-8"))
        result = AXTextAttribute.LANGUAGE.value_is_default(case["attribute_value"])
        assert result is case["expected"]

    @pytest.mark.parametrize(
        "case",
        [
            {
                "id": "next_character_successful",
                "direction": "next",
                "unit_type": "character",
                "setup_params": {
                    "caret_offset": 0,
                    "count": 5,
                    "data": {"0": ("a", 0, 1), "1": ("b", 1, 2), "2": ("c", 2, 3)},
                },
                "method_params": {},
                "expected": ("b", 1, 2),
            },
            {
                "id": "next_character_with_offset",
                "direction": "next",
                "unit_type": "character",
                "setup_params": {"count": 5, "data": {"1": ("b", 1, 2), "2": ("c", 2, 3)}},
                "method_params": {"offset": 1},
                "expected": ("c", 2, 3),
            },
            {
                "id": "next_character_no_current",
                "direction": "next",
                "unit_type": "character",
                "setup_params": {"caret_offset": 5, "data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "next_character_at_end",
                "direction": "next",
                "unit_type": "character",
                "setup_params": {"caret_offset": 4, "count": 5, "data": {"4": ("z", 4, 5)}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "next_word_successful",
                "direction": "next",
                "unit_type": "word",
                "setup_params": {
                    "caret_offset": 0,
                    "count": 15,
                    "word_data": {"0-5": ("first", 0, 5), "6-11": ("second", 6, 11)},
                },
                "method_params": {},
                "expected": ("second", 6, 11),
            },
            {
                "id": "next_word_with_offset",
                "direction": "next",
                "unit_type": "word",
                "setup_params": {
                    "count": 15,
                    "word_data": {"0-5": ("first", 0, 5), "6-11": ("second", 6, 11)},
                },
                "method_params": {"offset": 2},
                "expected": ("second", 6, 11),
            },
            {
                "id": "next_word_no_current",
                "direction": "next",
                "unit_type": "word",
                "setup_params": {"caret_offset": 5, "word_data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "next_word_at_end",
                "direction": "next",
                "unit_type": "word",
                "setup_params": {
                    "caret_offset": 10,
                    "count": 15,
                    "word_data": {"10-15": ("last", 10, 15)},
                },
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "next_line_successful",
                "direction": "next",
                "unit_type": "line",
                "setup_params": {
                    "caret_offset": 0,
                    "count": 25,
                    "line_data": {"0-10": ("First line", 0, 10), "11-20": ("Second line", 11, 20)},
                },
                "method_params": {},
                "expected": ("Second line", 11, 20),
            },
            {
                "id": "next_line_with_offset",
                "direction": "next",
                "unit_type": "line",
                "setup_params": {
                    "count": 25,
                    "line_data": {"0-10": ("First line", 0, 10), "11-20": ("Second line", 11, 20)},
                },
                "method_params": {"offset": 5},
                "expected": ("Second line", 11, 20),
            },
            {
                "id": "next_line_no_current",
                "direction": "next",
                "unit_type": "line",
                "setup_params": {"caret_offset": 5, "line_data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "next_line_at_end",
                "direction": "next",
                "unit_type": "line",
                "setup_params": {
                    "caret_offset": 15,
                    "count": 25,
                    "line_data": {"15-25": ("Last line", 15, 25)},
                },
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "next_sentence_successful",
                "direction": "next",
                "unit_type": "sentence",
                "setup_params": {
                    "caret_offset": 0,
                    "count": 30,
                    "sentence_data": {
                        "0-10": ("First sentence.", 0, 10),
                        "11-25": ("Second sentence.", 11, 25),
                    },
                },
                "method_params": {},
                "expected": ("Second sentence.", 11, 25),
            },
            {
                "id": "next_sentence_with_offset",
                "direction": "next",
                "unit_type": "sentence",
                "setup_params": {
                    "count": 30,
                    "sentence_data": {
                        "0-10": ("First sentence.", 0, 10),
                        "11-25": ("Second sentence.", 11, 25),
                    },
                },
                "method_params": {"offset": 5},
                "expected": ("Second sentence.", 11, 25),
            },
            {
                "id": "next_sentence_no_current",
                "direction": "next",
                "unit_type": "sentence",
                "setup_params": {"caret_offset": 5, "sentence_data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "next_sentence_at_end",
                "direction": "next",
                "unit_type": "sentence",
                "setup_params": {
                    "caret_offset": 20,
                    "count": 30,
                    "sentence_data": {"20-30": ("Last sentence.", 20, 30)},
                },
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "next_paragraph_successful",
                "direction": "next",
                "unit_type": "paragraph",
                "setup_params": {
                    "caret_offset": 0,
                    "count": 40,
                    "paragraph_data": {
                        "0-15": ("First paragraph.", 0, 15),
                        "16-30": ("Second paragraph.", 16, 30),
                    },
                },
                "method_params": {},
                "expected": ("Second paragraph.", 16, 30),
            },
            {
                "id": "next_paragraph_with_offset",
                "direction": "next",
                "unit_type": "paragraph",
                "setup_params": {
                    "count": 40,
                    "paragraph_data": {
                        "0-15": ("First paragraph.", 0, 15),
                        "16-30": ("Second paragraph.", 16, 30),
                    },
                },
                "method_params": {"offset": 8},
                "expected": ("Second paragraph.", 16, 30),
            },
            {
                "id": "next_paragraph_no_current",
                "direction": "next",
                "unit_type": "paragraph",
                "setup_params": {"caret_offset": 5, "paragraph_data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "next_paragraph_at_end",
                "direction": "next",
                "unit_type": "paragraph",
                "setup_params": {
                    "caret_offset": 25,
                    "count": 40,
                    "paragraph_data": {"25-40": ("Last paragraph.", 25, 40)},
                },
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_character_successful",
                "direction": "previous",
                "unit_type": "character",
                "setup_params": {"caret_offset": 2, "data": {"1": ("a", 1, 2), "2": ("b", 2, 3)}},
                "method_params": {},
                "expected": ("a", 1, 2),
            },
            {
                "id": "previous_character_with_offset",
                "direction": "previous",
                "unit_type": "character",
                "setup_params": {"data": {"0": ("a", 0, 1), "1": ("b", 1, 2)}},
                "method_params": {"offset": 1},
                "expected": ("a", 0, 1),
            },
            {
                "id": "previous_character_no_current",
                "direction": "previous",
                "unit_type": "character",
                "setup_params": {"caret_offset": 5, "data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_character_at_start",
                "direction": "previous",
                "unit_type": "character",
                "setup_params": {"caret_offset": 0, "data": {"0": ("a", 0, 1)}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_word_successful",
                "direction": "previous",
                "unit_type": "word",
                "setup_params": {
                    "caret_offset": 8,
                    "word_data": {"0-4": ("first", 0, 4), "5-11": ("second", 5, 11)},
                },
                "method_params": {},
                "expected": ("first", 0, 4),
            },
            {
                "id": "previous_word_with_offset",
                "direction": "previous",
                "unit_type": "word",
                "setup_params": {"word_data": {"0-4": ("first", 0, 4), "5-11": ("second", 5, 11)}},
                "method_params": {"offset": 8},
                "expected": ("first", 0, 4),
            },
            {
                "id": "previous_word_no_current",
                "direction": "previous",
                "unit_type": "word",
                "setup_params": {"caret_offset": 5, "word_data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_word_at_start",
                "direction": "previous",
                "unit_type": "word",
                "setup_params": {"caret_offset": 0, "word_data": {"0-4": ("first", 0, 4)}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_line_successful",
                "direction": "previous",
                "unit_type": "line",
                "setup_params": {
                    "caret_offset": 15,
                    "line_data": {"0-10": ("First line", 0, 10), "11-20": ("Second line", 11, 20)},
                },
                "method_params": {},
                "expected": ("First line", 0, 10),
            },
            {
                "id": "previous_line_with_offset",
                "direction": "previous",
                "unit_type": "line",
                "setup_params": {
                    "line_data": {"0-10": ("First line", 0, 10), "11-20": ("Second line", 11, 20)}
                },
                "method_params": {"offset": 15},
                "expected": ("First line", 0, 10),
            },
            {
                "id": "previous_line_no_current",
                "direction": "previous",
                "unit_type": "line",
                "setup_params": {"caret_offset": 5, "count": 10, "line_data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_line_at_start",
                "direction": "previous",
                "unit_type": "line",
                "setup_params": {"caret_offset": 0, "line_data": {"0-10": ("First line", 0, 10)}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_sentence_successful",
                "direction": "previous",
                "unit_type": "sentence",
                "setup_params": {
                    "caret_offset": 20,
                    "sentence_data": {
                        "0-15": ("First sentence.", 0, 15),
                        "16-30": ("Second sentence.", 16, 30),
                    },
                },
                "method_params": {},
                "expected": ("First sentence.", 0, 15),
            },
            {
                "id": "previous_sentence_with_offset",
                "direction": "previous",
                "unit_type": "sentence",
                "setup_params": {
                    "sentence_data": {
                        "0-15": ("First sentence.", 0, 15),
                        "16-30": ("Second sentence.", 16, 30),
                    }
                },
                "method_params": {"offset": 20},
                "expected": ("First sentence.", 0, 15),
            },
            {
                "id": "previous_sentence_no_current",
                "direction": "previous",
                "unit_type": "sentence",
                "setup_params": {"caret_offset": 5, "sentence_data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_sentence_at_start",
                "direction": "previous",
                "unit_type": "sentence",
                "setup_params": {
                    "caret_offset": 0,
                    "sentence_data": {"0-15": ("First sentence.", 0, 15)},
                },
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_paragraph_successful",
                "direction": "previous",
                "unit_type": "paragraph",
                "setup_params": {
                    "caret_offset": 20,
                    "paragraph_data": {
                        "0-15": ("First paragraph.", 0, 15),
                        "16-30": ("Second paragraph.", 16, 30),
                    },
                },
                "method_params": {},
                "expected": ("First paragraph.", 0, 15),
            },
            {
                "id": "previous_paragraph_with_offset",
                "direction": "previous",
                "unit_type": "paragraph",
                "setup_params": {
                    "paragraph_data": {
                        "0-15": ("First paragraph.", 0, 15),
                        "16-30": ("Second paragraph.", 16, 30),
                    }
                },
                "method_params": {"offset": 20},
                "expected": ("First paragraph.", 0, 15),
            },
            {
                "id": "previous_paragraph_no_current",
                "direction": "previous",
                "unit_type": "paragraph",
                "setup_params": {"caret_offset": 5, "paragraph_data": {}},
                "method_params": {},
                "expected": ("", 0, 0),
            },
            {
                "id": "previous_paragraph_at_start",
                "direction": "previous",
                "unit_type": "paragraph",
                "setup_params": {
                    "caret_offset": 0,
                    "paragraph_data": {"0-15": ("First paragraph.", 0, 15)},
                },
                "method_params": {},
                "expected": ("", 0, 0),
            },
        ],
        ids=lambda case: case["id"],
    )
    def test_get_next_text_unit_scenarios(  # pylint: disable=too-many-branches,too-many-statements
        self,
        test_context: OrcaTestContext,
        case: dict,
    ) -> None:
        """Test AXText.get_next_* and get_previous_* methods with various scenarios."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        setup_params = case["setup_params"]
        method_params = case["method_params"]
        direction = case["direction"]
        unit_type = case["unit_type"]
        expected = case["expected"]

        if "caret_offset" in setup_params:
            test_context.patch_object(
                AXText, "get_caret_offset", side_effect=lambda obj: setup_params["caret_offset"]
            )
        if "count" in setup_params:
            test_context.patch_object(
                AXText, "get_character_count", side_effect=lambda obj: setup_params["count"]
            )

        if unit_type == "character":

            def mock_get_character_at_offset(_obj, offset) -> tuple[str, int, int]:
                return setup_params["data"].get(str(offset), ("", 0, 0))

            test_context.patch_object(
                AXText, "get_character_at_offset", new=mock_get_character_at_offset
            )

            mock_obj = test_context.Mock(spec=Atspi.Accessible)
            if direction == "next":
                if method_params:
                    result = AXText.get_next_character(mock_obj, **method_params)
                else:
                    result = AXText.get_next_character(mock_obj)
            else:
                if method_params:
                    result = AXText.get_previous_character(mock_obj, **method_params)
                else:
                    result = AXText.get_previous_character(mock_obj)
        elif unit_type == "word":

            def mock_get_word_at_offset(_obj, offset) -> tuple[str, int, int]:
                for range_key, data in setup_params.get("word_data", {}).items():
                    if "-" in range_key:
                        start, end = map(int, range_key.split("-"))
                        if start <= offset <= end:
                            return data
                return ("", 0, 0)

            test_context.patch_object(AXText, "get_word_at_offset", new=mock_get_word_at_offset)

            mock_obj = test_context.Mock(spec=Atspi.Accessible)
            if direction == "next":
                if method_params:
                    result = AXText.get_next_word(mock_obj, **method_params)
                else:
                    result = AXText.get_next_word(mock_obj)
            else:
                if method_params:
                    result = AXText.get_previous_word(mock_obj, **method_params)
                else:
                    result = AXText.get_previous_word(mock_obj)
        elif unit_type == "line":

            def mock_get_line_at_offset(_obj, offset) -> tuple[str, int, int]:
                for range_key, data in setup_params.get("line_data", {}).items():
                    if "-" in range_key:
                        start, end = map(int, range_key.split("-"))
                        if start <= offset <= end:
                            return data
                return ("", 0, 0)

            test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset)

            mock_obj = test_context.Mock(spec=Atspi.Accessible)
            if direction == "next":
                if method_params:
                    result = AXText.get_next_line(mock_obj, **method_params)
                else:
                    result = AXText.get_next_line(mock_obj)
            else:
                if method_params:
                    result = AXText.get_previous_line(mock_obj, **method_params)
                else:
                    result = AXText.get_previous_line(mock_obj)
        elif unit_type == "sentence":

            def mock_get_sentence_at_offset(_obj, offset) -> tuple[str, int, int]:
                for range_key, data in setup_params.get("sentence_data", {}).items():
                    if "-" in range_key:
                        start, end = map(int, range_key.split("-"))
                        if start <= offset <= end:
                            return data
                return ("", 0, 0)

            test_context.patch_object(
                AXText, "get_sentence_at_offset", new=mock_get_sentence_at_offset
            )

            mock_obj = test_context.Mock(spec=Atspi.Accessible)
            if direction == "next":
                if method_params:
                    result = AXText.get_next_sentence(mock_obj, **method_params)
                else:
                    result = AXText.get_next_sentence(mock_obj)
            else:
                if method_params:
                    result = AXText.get_previous_sentence(mock_obj, **method_params)
                else:
                    result = AXText.get_previous_sentence(mock_obj)
        elif unit_type == "paragraph":

            def mock_get_paragraph_at_offset(_obj, offset) -> tuple[str, int, int]:
                for range_key, data in setup_params.get("paragraph_data", {}).items():
                    if "-" in range_key:
                        start, end = map(int, range_key.split("-"))
                        if start <= offset <= end:
                            return data
                return ("", 0, 0)

            test_context.patch_object(
                AXText, "get_paragraph_at_offset", new=mock_get_paragraph_at_offset
            )

            mock_obj = test_context.Mock(spec=Atspi.Accessible)
            if direction == "next":
                if method_params:
                    result = AXText.get_next_paragraph(mock_obj, **method_params)
                else:
                    result = AXText.get_next_paragraph(mock_obj)
            else:
                if method_params:
                    result = AXText.get_previous_paragraph(mock_obj, **method_params)
                else:
                    result = AXText.get_previous_paragraph(mock_obj)
        else:
            result = ("", 0, 0)

        assert result == expected

    def test_get_character_at_offset_with_empty_text(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_character_at_offset when text is empty."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        test_context.patch(
            "gi.repository.Atspi.Text.get_character_count", return_value=0
        )
        result = AXText.get_character_at_offset(test_context.Mock(spec=Atspi.Accessible), 0)
        assert result == ("", 0, 0)

    def test_get_character_at_offset_with_none_offset(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_character_at_offset when offset is None."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        mock_obj = test_context.Mock(spec=Atspi.Accessible)
        test_context.patch(
            "gi.repository.Atspi.Text.get_character_count", return_value=10
        )
        test_context.patch_object(AXText, "get_caret_offset", return_value=5)

        mock_result = test_context.Mock()
        mock_result.content = "a"
        mock_result.start_offset = 5
        mock_result.end_offset = 6
        test_context.patch(
            "gi.repository.Atspi.Text.get_string_at_offset",
            side_effect=lambda obj, offset, granularity: mock_result,
        )

        result = AXText.get_character_at_offset(mock_obj, None)
        assert result == ("a", 5, 6)

    def test_get_character_at_offset_with_glib_error(self, test_context: OrcaTestContext) -> None:
        """Test AXText.get_character_at_offset when GLib.GError occurs."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        def raise_glib_error(_obj, _offset, _granularity):
            raise GLib.GError("Test error")

        test_context.patch(
            "gi.repository.Atspi.Text.get_character_count", return_value=10
        )
        test_context.patch(
            "gi.repository.Atspi.Text.get_string_at_offset", new=raise_glib_error
        )

        result = AXText.get_character_at_offset(test_context.Mock(spec=Atspi.Accessible), 5)
        assert result == ("", 0, 0)

    def test_get_previous_line_at_end_with_newline_sets_correct_start(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.get_previous_line sets start when fallback line ends with newline."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        mock_obj = test_context.Mock(spec=Atspi.Accessible)

        # Mock get_character_count to return 12 (end of text)
        test_context.patch_object(AXText, "get_character_count", return_value=12)

        # Mock get_line_at_offset for the sequence
        def mock_get_line_at_offset(_obj, offset):
            if offset == 12:  # At end of text - empty line
                return ("", 12, 12)
            if offset == 11:  # One character back - line ending with newline
                return ("Hello World\n", 0, 12)
            if offset == 10:  # Previous line search from (offset-1)-1 = 10
                return ("Previous Line", 0, 11)
            return ("", 0, 0)

        test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset)

        # Test: when at end with empty line and fallback has newline
        result = AXText.get_previous_line(mock_obj, 12)

        # The current line becomes "Hello World\n" with start=11
        assert result == ("Previous Line", 0, 11)

    def test_get_previous_line_at_end_of_text_fallback_to_normal_behavior(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.get_previous_line at end when fallback doesn't end with newline."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        mock_obj = test_context.Mock(spec=Atspi.Accessible)

        # Mock get_character_count to return 11 (end of text)
        test_context.patch_object(AXText, "get_character_count", return_value=11)

        # Mock get_line_at_offset scenarios
        def mock_get_line_at_offset(_obj, offset):
            if offset == 11:  # At end of text
                return ("", 11, 11)
            if offset == 10:  # One character back - no newline
                return ("Hello World", 5, 15)  # start > 0 so it continues
            if offset == 4:  # Previous line lookup from start-1 = 4
                return ("Previous Line", 0, 5)
            return ("", 0, 0)

        test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset)

        # Test: should use normal behavior when fallback line doesn't end with newline
        result = AXText.get_previous_line(mock_obj, 11)
        assert result == ("Previous Line", 0, 5)

    def test_get_previous_line_empty_current_line_returns_empty(
        self, test_context: OrcaTestContext
    ) -> None:
        """Test AXText.get_previous_line returns empty when current_line is empty."""

        self._setup_dependencies(test_context)
        from orca.ax_text import AXText

        mock_obj = test_context.Mock(spec=Atspi.Accessible)

        # Mock get_character_count to return 5 (not at end)
        test_context.patch_object(AXText, "get_character_count", return_value=5)

        # Mock get_line_at_offset to return empty line not at end of text
        def mock_get_line_at_offset(_obj, _offset):
            return ("", 3, 3)

        test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset)

        # Test: when current_line is empty (and not at end), should return empty
        result = AXText.get_previous_line(mock_obj, 3)
        assert result == ("", 0, 0)