-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathAccessibilityHierarchyParserTests.swift
More file actions
1326 lines (1085 loc) · 49.6 KB
/
AccessibilityHierarchyParserTests.swift
File metadata and controls
1326 lines (1085 loc) · 49.6 KB
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
@testable import AccessibilitySnapshotCore
@testable import AccessibilitySnapshotParser
import UIKit
import XCTest
final class AccessibilityHierarchyParserTests: XCTestCase {
func testUserInterfaceLayoutDirection() {
let gridView = UIView(frame: .init(x: 0, y: 0, width: 20, height: 20))
let elementA = UIView(frame: .init(x: 0, y: 0, width: 10, height: 10))
elementA.isAccessibilityElement = true
elementA.accessibilityLabel = "A"
elementA.accessibilityFrame = elementA.frame
gridView.addSubview(elementA)
let elementB = UIView(frame: .init(x: 10, y: 0, width: 10, height: 10))
elementB.isAccessibilityElement = true
elementB.accessibilityLabel = "B"
elementB.accessibilityFrame = elementB.frame
gridView.addSubview(elementB)
let elementC = UIView(frame: .init(x: 0, y: 10, width: 10, height: 10))
elementC.isAccessibilityElement = true
elementC.accessibilityLabel = "C"
elementC.accessibilityFrame = elementC.frame
gridView.addSubview(elementC)
let elementD = UIView(frame: .init(x: 10, y: 10, width: 10, height: 10))
elementD.isAccessibilityElement = true
elementD.accessibilityLabel = "D"
elementD.accessibilityFrame = elementD.frame
gridView.addSubview(elementD)
let parser = AccessibilityHierarchyParser()
let ltrElements = parser.parseAccessibilityHierarchy(
in: gridView,
userInterfaceLayoutDirectionProvider: TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .leftToRight),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .phone)
).flattenToElements().map { $0.description }
XCTAssertEqual(ltrElements, ["A", "B", "C", "D"])
let rtlElements = parser.parseAccessibilityHierarchy(
in: gridView,
userInterfaceLayoutDirectionProvider: TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .rightToLeft),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .phone)
).flattenToElements().map { $0.description }
XCTAssertEqual(rtlElements, ["B", "A", "D", "C"])
}
func testVerticalSeperation() {
let magicNumber = 8.0 // This is enough to trigger vertical separation for phone but not for pad
let gridView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 20))
let elementA = UIView(frame: .init(x: 0, y: magicNumber, width: 10, height: 10))
elementA.isAccessibilityElement = true
elementA.accessibilityLabel = "A"
elementA.accessibilityFrame = elementA.frame
gridView.addSubview(elementA)
let elementB = UIView(frame: .init(x: 10, y: 0, width: 0, height: 10))
elementB.isAccessibilityElement = true
elementB.accessibilityLabel = "B"
elementB.accessibilityFrame = elementB.frame
gridView.addSubview(elementB)
let elementC = UIView(frame: .init(x: 20, y: -magicNumber, width: 10, height: 10))
elementC.isAccessibilityElement = true
elementC.accessibilityLabel = "C"
elementC.accessibilityFrame = elementC.frame
gridView.addSubview(elementC)
let elementD = UIView(frame: .init(x: 30, y: -magicNumber, width: 10, height: 10))
elementD.isAccessibilityElement = true
elementD.accessibilityLabel = "D"
elementD.accessibilityFrame = elementD.frame
gridView.addSubview(elementD)
let parser = AccessibilityHierarchyParser()
let padElements = parser.parseAccessibilityHierarchy(
in: gridView,
userInterfaceLayoutDirectionProvider:
TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .leftToRight),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .pad)
).flattenToElements().map { $0.description }
// on pad elements are sorted horizontally
XCTAssertEqual(padElements, ["A", "B", "C", "D"])
let phoneElements = parser.parseAccessibilityHierarchy(
in: gridView,
userInterfaceLayoutDirectionProvider:
TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .leftToRight),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .phone)
).flattenToElements().map { $0.description }
// on phone elements are sorted vertically and then left to right
XCTAssertEqual(phoneElements, ["C", "D", "B", "A"])
let padMagicNumber = 25
elementA.accessibilityFrame = .init(x: 0, y: padMagicNumber, width: 10, height: 10)
elementB.accessibilityFrame = .init(x: 10, y: 0, width: 0, height: 10)
elementC.accessibilityFrame = .init(x: 20, y: -padMagicNumber, width: 10, height: 10)
elementD.accessibilityFrame = .init(x: 30, y: -padMagicNumber, width: 10, height: 10)
let padAgain = parser.parseAccessibilityHierarchy(
in: gridView,
userInterfaceLayoutDirectionProvider:
TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .leftToRight),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .pad)
).flattenToElements().map { $0.description }
// Now pad elements are sorted vertically and then left to right
XCTAssertEqual(padAgain, ["C", "D", "B", "A"])
}
// MARK: - Container Hierarchy Tree Tests
func testSemanticGroupWithLabelIsPreserved() {
let rootView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
let container = UIView(frame: .init(x: 0, y: 0, width: 100, height: 50))
container.accessibilityContainerType = .semanticGroup
container.accessibilityLabel = "Group Label"
rootView.addSubview(container)
let element = UIView(frame: .init(x: 10, y: 10, width: 30, height: 30))
element.isAccessibilityElement = true
element.accessibilityLabel = "Element"
element.accessibilityFrame = CGRect(x: 10, y: 10, width: 30, height: 30)
container.addSubview(element)
let parser = AccessibilityHierarchyParser()
let hierarchy = parser.parseAccessibilityHierarchy(in: rootView)
// Should have one container at root level
XCTAssertEqual(hierarchy.count, 1)
// Verify it's a container with correct label
if case let .container(containerInfo, children) = hierarchy.first {
if case let .semanticGroup(label, _, _) = containerInfo.type {
XCTAssertEqual(label, "Group Label")
} else {
XCTFail("Expected semanticGroup container type")
}
XCTAssertEqual(children.count, 1)
// Verify child element
if case let .element(childElement, _) = children.first {
XCTAssertEqual(childElement.description, "Element")
} else {
XCTFail("Expected element child")
}
} else {
XCTFail("Expected container at root level")
}
}
func testSemanticGroupWithoutLabelIsFlattened() {
let rootView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
let container = UIView(frame: .init(x: 0, y: 0, width: 100, height: 50))
container.accessibilityContainerType = .semanticGroup
// No label, value, or identifier
rootView.addSubview(container)
let element = UIView(frame: .init(x: 10, y: 10, width: 30, height: 30))
element.isAccessibilityElement = true
element.accessibilityLabel = "Element"
element.accessibilityFrame = CGRect(x: 10, y: 10, width: 30, height: 30)
container.addSubview(element)
let parser = AccessibilityHierarchyParser()
let hierarchy = parser.parseAccessibilityHierarchy(in: rootView)
// Should have one element at root level (container flattened)
XCTAssertEqual(hierarchy.count, 1)
// Verify it's an element, not a container
if case let .element(elementInfo, _) = hierarchy.first {
XCTAssertEqual(elementInfo.description, "Element")
} else {
XCTFail("Expected element at root level (container should be flattened)")
}
}
func testListContainerIsAlwaysPreserved() {
let rootView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
let listContainer = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
listContainer.accessibilityContainerType = .list
// No label - but should still be preserved
rootView.addSubview(listContainer)
let item1 = UIView(frame: .init(x: 0, y: 0, width: 100, height: 30))
item1.isAccessibilityElement = true
item1.accessibilityLabel = "Item 1"
item1.accessibilityFrame = CGRect(x: 0, y: 0, width: 100, height: 30)
listContainer.addSubview(item1)
let item2 = UIView(frame: .init(x: 0, y: 40, width: 100, height: 30))
item2.isAccessibilityElement = true
item2.accessibilityLabel = "Item 2"
item2.accessibilityFrame = CGRect(x: 0, y: 40, width: 100, height: 30)
listContainer.addSubview(item2)
let parser = AccessibilityHierarchyParser()
let hierarchy = parser.parseAccessibilityHierarchy(in: rootView)
// Should have one list container at root level
XCTAssertEqual(hierarchy.count, 1)
if case let .container(containerInfo, children) = hierarchy.first {
XCTAssertEqual(containerInfo.type, .list)
XCTAssertEqual(children.count, 2)
} else {
XCTFail("Expected list container at root level")
}
}
func testLandmarkContainerIsAlwaysPreserved() {
let rootView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
let landmarkContainer = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
landmarkContainer.accessibilityContainerType = .landmark
rootView.addSubview(landmarkContainer)
let element = UIView(frame: .init(x: 10, y: 10, width: 30, height: 30))
element.isAccessibilityElement = true
element.accessibilityLabel = "Landmark Content"
element.accessibilityFrame = CGRect(x: 10, y: 10, width: 30, height: 30)
landmarkContainer.addSubview(element)
let parser = AccessibilityHierarchyParser()
let hierarchy = parser.parseAccessibilityHierarchy(in: rootView)
XCTAssertEqual(hierarchy.count, 1)
if case let .container(containerInfo, _) = hierarchy.first {
XCTAssertEqual(containerInfo.type, .landmark)
} else {
XCTFail("Expected landmark container at root level")
}
}
func testNestedContainersPreserveHierarchy() {
// Use NestedContainersTestView which mirrors ContainerHierarchyViewController's NestedContainersDemoView
let nestedView = NestedContainersTestView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
let parser = AccessibilityHierarchyParser()
let hierarchy = parser.parseAccessibilityHierarchy(in: nestedView)
// Should have outer container at root
XCTAssertEqual(hierarchy.count, 1)
if case let .container(outerInfo, outerChildren) = hierarchy.first {
if case let .semanticGroup(label, _, _) = outerInfo.type {
XCTAssertEqual(label, "Outer Container")
} else {
XCTFail("Expected semanticGroup container type for outer")
}
// Should have 2 children: "Outer Item" element and inner container
XCTAssertEqual(outerChildren.count, 2)
// Find the outer item element
let outerElements = outerChildren.compactMap { node -> AccessibilityElement? in
if case let .element(element, _) = node { return element }
return nil
}
XCTAssertEqual(outerElements.count, 1)
XCTAssertEqual(outerElements.first?.description, "Outer Item")
// Find the inner container
let innerContainers = outerChildren.compactMap { node -> (AccessibilityContainer, [AccessibilityHierarchy])? in
if case let .container(info, children) = node { return (info, children) }
return nil
}
XCTAssertEqual(innerContainers.count, 1)
if let innerContainer = innerContainers.first?.0,
case let .semanticGroup(label, _, _) = innerContainer.type
{
XCTAssertEqual(label, "Inner Container")
} else {
XCTFail("Expected semanticGroup container type for inner")
}
// Inner container should have 2 element children
if let innerChildren = innerContainers.first?.1 {
let innerElements = innerChildren.compactMap { node -> AccessibilityElement? in
if case let .element(element, _) = node { return element }
return nil
}
XCTAssertEqual(innerElements.count, 2)
XCTAssertEqual(innerElements.map { $0.description }, ["Inner Item 1", "Inner Item 2"])
}
} else {
XCTFail("Expected outer container")
}
// Verify flattening produces correct element order
let flattenedElements = hierarchy.flattenToElements()
XCTAssertEqual(flattenedElements.map { $0.description }, ["Outer Item", "Inner Item 1", "Inner Item 2"])
// Verify flattenToContainers gets both containers
let containers = hierarchy.flattenToContainers()
XCTAssertEqual(containers.count, 2)
let containerLabels = containers.compactMap { container -> String? in
if case let .semanticGroup(label, _, _) = container.type { return label }
return nil
}
XCTAssertEqual(Set(containerLabels), ["Outer Container", "Inner Container"])
}
func testHierarchySortOrder() {
let rootView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
// Add elements in reverse order
let elementC = UIView(frame: .init(x: 0, y: 60, width: 30, height: 30))
elementC.isAccessibilityElement = true
elementC.accessibilityLabel = "C"
elementC.accessibilityFrame = CGRect(x: 0, y: 60, width: 30, height: 30)
rootView.addSubview(elementC)
let elementB = UIView(frame: .init(x: 0, y: 30, width: 30, height: 30))
elementB.isAccessibilityElement = true
elementB.accessibilityLabel = "B"
elementB.accessibilityFrame = CGRect(x: 0, y: 30, width: 30, height: 30)
rootView.addSubview(elementB)
let elementA = UIView(frame: .init(x: 0, y: 0, width: 30, height: 30))
elementA.isAccessibilityElement = true
elementA.accessibilityLabel = "A"
elementA.accessibilityFrame = CGRect(x: 0, y: 0, width: 30, height: 30)
rootView.addSubview(elementA)
let parser = AccessibilityHierarchyParser()
let hierarchy = parser.parseAccessibilityHierarchy(in: rootView)
let flattenedDescriptions = hierarchy.flattenToElements().map { $0.description }
// Should be sorted by position (top to bottom)
XCTAssertEqual(flattenedDescriptions, ["A", "B", "C"])
}
func testContainerChildrenSortOrder() {
let rootView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 200))
let container = UIView(frame: .init(x: 0, y: 0, width: 100, height: 200))
container.accessibilityContainerType = .list
rootView.addSubview(container)
// Add in reverse order
let item3 = UIView(frame: .init(x: 0, y: 120, width: 100, height: 30))
item3.isAccessibilityElement = true
item3.accessibilityLabel = "Third"
item3.accessibilityFrame = CGRect(x: 0, y: 120, width: 100, height: 30)
container.addSubview(item3)
let item1 = UIView(frame: .init(x: 0, y: 0, width: 100, height: 30))
item1.isAccessibilityElement = true
item1.accessibilityLabel = "First"
item1.accessibilityFrame = CGRect(x: 0, y: 0, width: 100, height: 30)
container.addSubview(item1)
let item2 = UIView(frame: .init(x: 0, y: 60, width: 100, height: 30))
item2.isAccessibilityElement = true
item2.accessibilityLabel = "Second"
item2.accessibilityFrame = CGRect(x: 0, y: 60, width: 100, height: 30)
container.addSubview(item2)
let parser = AccessibilityHierarchyParser()
let hierarchy = parser.parseAccessibilityHierarchy(in: rootView)
if case let .container(_, children) = hierarchy.first {
let childDescriptions = children.compactMap { node -> String? in
if case let .element(element, _) = node { return element.description }
return nil
}
// Children should be sorted by position
XCTAssertEqual(childDescriptions, ["First", "Second", "Third"])
} else {
XCTFail("Expected list container")
}
}
func testFlattenToContainers() {
let rootView = UIView(frame: .init(x: 0, y: 0, width: 200, height: 200))
let list = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
list.accessibilityContainerType = .list
list.accessibilityLabel = "My List"
rootView.addSubview(list)
let landmark = UIView(frame: .init(x: 100, y: 0, width: 100, height: 100))
landmark.accessibilityContainerType = .landmark
landmark.accessibilityLabel = "My Landmark"
rootView.addSubview(landmark)
let listItem = UIView(frame: .init(x: 10, y: 10, width: 30, height: 30))
listItem.isAccessibilityElement = true
listItem.accessibilityLabel = "List Item"
listItem.accessibilityFrame = CGRect(x: 10, y: 10, width: 30, height: 30)
list.addSubview(listItem)
let landmarkContent = UIView(frame: .init(x: 110, y: 10, width: 30, height: 30))
landmarkContent.isAccessibilityElement = true
landmarkContent.accessibilityLabel = "Landmark Content"
landmarkContent.accessibilityFrame = CGRect(x: 110, y: 10, width: 30, height: 30)
landmark.addSubview(landmarkContent)
let parser = AccessibilityHierarchyParser()
let hierarchy = parser.parseAccessibilityHierarchy(in: rootView)
let containers = hierarchy.flattenToContainers()
XCTAssertEqual(containers.count, 2)
let hasListContainer = containers.contains {
if case .list = $0.type { return true }
return false
}
let hasLandmarkContainer = containers.contains {
if case .landmark = $0.type { return true }
return false
}
XCTAssertTrue(hasListContainer)
XCTAssertTrue(hasLandmarkContainer)
}
// MARK: - Codable Tests
func testAccessibilityElementCodable() throws {
let element = AccessibilityElement(
description: "Test Button",
label: "Button Label",
value: "Button Value",
traits: [.button, .selected],
identifier: "test-button-id",
hint: "Double tap to activate",
userInputLabels: ["tap button", "press button"],
shape: .frame(CGRect(x: 10, y: 20, width: 100, height: 44)),
activationPoint: CGPoint(x: 60, y: 42),
usesDefaultActivationPoint: true,
customActions: [AccessibilityElement.CustomAction(name: "Delete", image: nil)],
customContent: [],
customRotors: [],
accessibilityLanguage: "en-US",
respondsToUserInteraction: true,
containerContext: nil
)
let encoder = JSONEncoder()
let data = try encoder.encode(element)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityElement.self, from: data)
XCTAssertEqual(decoded.description, element.description)
XCTAssertEqual(decoded.label, element.label)
XCTAssertEqual(decoded.value, element.value)
XCTAssertEqual(decoded.traits, element.traits)
XCTAssertEqual(decoded.identifier, element.identifier)
XCTAssertEqual(decoded.hint, element.hint)
XCTAssertEqual(decoded.userInputLabels, element.userInputLabels)
XCTAssertEqual(decoded.shape, element.shape)
XCTAssertEqual(decoded.activationPoint, element.activationPoint)
XCTAssertEqual(decoded.usesDefaultActivationPoint, element.usesDefaultActivationPoint)
XCTAssertEqual(decoded.customActions.map { $0.name }, element.customActions.map { $0.name })
XCTAssertEqual(decoded.accessibilityLanguage, element.accessibilityLanguage)
XCTAssertEqual(decoded.respondsToUserInteraction, element.respondsToUserInteraction)
}
func testAccessibilityContainerCodable() throws {
let container = AccessibilityContainer(
type: .list,
frame: CGRect(x: 0, y: 0, width: 320, height: 200)
)
let encoder = JSONEncoder()
let data = try encoder.encode(container)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityContainer.self, from: data)
XCTAssertEqual(decoded.type, .list)
XCTAssertEqual(decoded.frame, container.frame)
}
func testAccessibilityHierarchyCodable() throws {
let element1 = AccessibilityElement(
description: "Item 1",
label: "Item 1",
value: nil,
traits: [],
identifier: nil,
hint: nil,
userInputLabels: nil,
shape: .frame(CGRect(x: 0, y: 0, width: 100, height: 44)),
activationPoint: CGPoint(x: 50, y: 22),
usesDefaultActivationPoint: true,
customActions: [],
customContent: [],
customRotors: [],
accessibilityLanguage: nil,
respondsToUserInteraction: false,
containerContext: nil
)
let element2 = AccessibilityElement(
description: "Item 2",
label: "Item 2",
value: nil,
traits: [],
identifier: nil,
hint: nil,
userInputLabels: nil,
shape: .frame(CGRect(x: 0, y: 50, width: 100, height: 44)),
activationPoint: CGPoint(x: 50, y: 72),
usesDefaultActivationPoint: true,
customActions: [],
customContent: [],
customRotors: [],
accessibilityLanguage: nil,
respondsToUserInteraction: false,
containerContext: nil
)
let container = AccessibilityContainer(
type: .list,
frame: CGRect(x: 0, y: 0, width: 100, height: 100)
)
let hierarchy: [AccessibilityHierarchy] = [
.container(container, children: [
.element(element1, traversalIndex: 0),
.element(element2, traversalIndex: 1),
]),
]
let encoder = JSONEncoder()
let data = try encoder.encode(hierarchy)
let decoder = JSONDecoder()
let decoded = try decoder.decode([AccessibilityHierarchy].self, from: data)
XCTAssertEqual(decoded.count, 1)
if case let .container(decodedContainer, children) = decoded.first {
XCTAssertEqual(decodedContainer.type, .list)
XCTAssertEqual(children.count, 2)
if case let .element(child1, index1) = children[0] {
XCTAssertEqual(child1.description, "Item 1")
XCTAssertEqual(index1, 0)
} else {
XCTFail("Expected element child")
}
if case let .element(child2, index2) = children[1] {
XCTAssertEqual(child2.description, "Item 2")
XCTAssertEqual(index2, 1)
} else {
XCTFail("Expected element child")
}
} else {
XCTFail("Expected container at root")
}
}
func testShapeCodableWithPath() throws {
let path = UIBezierPath(roundedRect: CGRect(x: 10, y: 20, width: 100, height: 50), cornerRadius: 8)
let shape = AccessibilityElement.Shape.path(path)
let encoder = JSONEncoder()
let data = try encoder.encode(shape)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityElement.Shape.self, from: data)
if case let .path(decodedPath) = decoded {
XCTAssertEqual(decodedPath.bounds, path.bounds)
} else {
XCTFail("Expected path shape")
}
}
func testContainerTypeCodable() throws {
let types: [AccessibilityContainer.ContainerType] = [
.list,
.landmark,
.tabBar,
.semanticGroup(label: "Test", value: nil, identifier: "test-id"),
.dataTable(rowCount: 3, columnCount: 4),
]
for type in types {
let encoder = JSONEncoder()
let data = try encoder.encode(type)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityContainer.ContainerType.self, from: data)
XCTAssertEqual(decoded, type)
}
}
func testTraitsCodable() throws {
let traits: UIAccessibilityTraits = [.button, .selected, .header, .link]
let encoder = JSONEncoder()
let data = try encoder.encode(traits)
// Verify human-readable format (array of trait names)
let jsonArray = try JSONSerialization.jsonObject(with: data) as! [String]
XCTAssertTrue(jsonArray.contains("button"), "Traits should include 'button'")
XCTAssertTrue(jsonArray.contains("selected"), "Traits should include 'selected'")
XCTAssertTrue(jsonArray.contains("header"), "Traits should include 'header'")
XCTAssertTrue(jsonArray.contains("link"), "Traits should include 'link'")
let decoder = JSONDecoder()
let decoded = try decoder.decode(UIAccessibilityTraits.self, from: data)
XCTAssertEqual(decoded, traits)
}
func testTraitsEmptyEncodesAsEmptyArray() throws {
let traits: UIAccessibilityTraits = []
let encoder = JSONEncoder()
let data = try encoder.encode(traits)
let jsonString = String(data: data, encoding: .utf8)!
XCTAssertEqual(jsonString, "[]", "Empty traits should encode as empty array")
let decoder = JSONDecoder()
let decoded = try decoder.decode(UIAccessibilityTraits.self, from: data)
XCTAssertEqual(decoded, traits)
}
func testShapePathEncodesAsPathElements() throws {
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 100, y: 0))
path.addLine(to: CGPoint(x: 100, y: 50))
path.close()
let shape = AccessibilityElement.Shape.path(path)
let encoder = JSONEncoder()
let data = try encoder.encode(shape)
// Verify round-trip works
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityElement.Shape.self, from: data)
if case let .path(decodedPath) = decoded {
XCTAssertEqual(decodedPath.bounds, path.bounds)
} else {
XCTFail("Expected path shape")
}
}
func testCustomActionWithImageCodable() throws {
// Create a simple red image for testing
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 10, height: 10))
let testImage = renderer.image { context in
UIColor.red.setFill()
context.fill(CGRect(x: 0, y: 0, width: 10, height: 10))
}
let action = AccessibilityElement.CustomAction(name: "Delete", image: testImage)
let encoder = JSONEncoder()
let data = try encoder.encode(action)
// Verify imageData and imageScale are included
let json = try JSONSerialization.jsonObject(with: data) as! [String: Any]
XCTAssertEqual(json["name"] as? String, "Delete")
XCTAssertNotNil(json["imageData"], "Image should be encoded as imageData")
XCTAssertNotNil(json["imageScale"], "Image scale should be encoded")
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityElement.CustomAction.self, from: data)
XCTAssertEqual(decoded.name, "Delete")
XCTAssertNotNil(decoded.image, "Image should be decoded")
XCTAssertEqual(decoded.image?.size, testImage.size, "Image size should be preserved")
XCTAssertEqual(decoded.image?.scale, testImage.scale, "Image scale should be preserved")
}
func testCustomActionWithoutImageCodable() throws {
let action = AccessibilityElement.CustomAction(name: "Edit", image: nil)
let encoder = JSONEncoder()
let data = try encoder.encode(action)
// Verify imageData is not included when image is nil
let json = try JSONSerialization.jsonObject(with: data) as! [String: Any]
XCTAssertEqual(json["name"] as? String, "Edit")
XCTAssertNil(json["imageData"], "imageData should not be present when image is nil")
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityElement.CustomAction.self, from: data)
XCTAssertEqual(decoded.name, "Edit")
XCTAssertNil(decoded.image)
}
// MARK: - Data Table Tests
func testDataTableContainerWithDimensions() {
let rootView = UIView(frame: .init(x: 0, y: 0, width: 200, height: 200))
let dataTable = TestDataTableView(
frame: CGRect(x: 0, y: 0, width: 200, height: 200),
rows: 5,
columns: 4
)
rootView.addSubview(dataTable)
// Add some cells
let cell1 = TestDataTableCell(row: 0, column: 0, label: "A1")
cell1.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
cell1.accessibilityFrame = CGRect(x: 0, y: 0, width: 50, height: 40)
dataTable.addSubview(cell1)
dataTable.cells[CellIndex(row: 0, column: 0)] = cell1
let cell2 = TestDataTableCell(row: 0, column: 1, label: "B1")
cell2.frame = CGRect(x: 50, y: 0, width: 50, height: 40)
cell2.accessibilityFrame = CGRect(x: 50, y: 0, width: 50, height: 40)
dataTable.addSubview(cell2)
dataTable.cells[CellIndex(row: 0, column: 1)] = cell2
let parser = AccessibilityHierarchyParser()
let hierarchy = parser.parseAccessibilityHierarchy(in: rootView)
// Should have one container with dataTable type
XCTAssertEqual(hierarchy.count, 1)
if case let .container(container, children) = hierarchy.first {
if case let .dataTable(rowCount, columnCount) = container.type {
XCTAssertEqual(rowCount, 5)
XCTAssertEqual(columnCount, 4)
} else {
XCTFail("Expected dataTable container type")
}
XCTAssertEqual(children.count, 2)
} else {
XCTFail("Expected dataTable container")
}
}
func testDataTableContainerCodable() throws {
let container = AccessibilityContainer(
type: .dataTable(rowCount: 5, columnCount: 4),
frame: CGRect(x: 0, y: 0, width: 320, height: 200)
)
let encoder = JSONEncoder()
let data = try encoder.encode(container)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityContainer.self, from: data)
if case let .dataTable(rowCount, columnCount) = decoded.type {
XCTAssertEqual(rowCount, 5)
XCTAssertEqual(columnCount, 4)
} else {
XCTFail("Expected dataTable type")
}
}
func testSemanticGroupContainerCodable() throws {
let container = AccessibilityContainer(
type: .semanticGroup(label: "Group Label", value: "Group Value", identifier: "group-id"),
frame: CGRect(x: 0, y: 0, width: 200, height: 100)
)
let encoder = JSONEncoder()
let data = try encoder.encode(container)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityContainer.self, from: data)
if case let .semanticGroup(label, value, identifier) = decoded.type {
XCTAssertEqual(label, "Group Label")
XCTAssertEqual(value, "Group Value")
XCTAssertEqual(identifier, "group-id")
} else {
XCTFail("Expected semanticGroup type")
}
}
func testTabBarContainerCodable() throws {
let container = AccessibilityContainer(
type: .tabBar,
frame: CGRect(x: 0, y: 0, width: 320, height: 49)
)
let encoder = JSONEncoder()
let data = try encoder.encode(container)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityContainer.self, from: data)
XCTAssertEqual(decoded.type, .tabBar)
}
func testLandmarkContainerCodable() throws {
let container = AccessibilityContainer(
type: .landmark,
frame: CGRect(x: 0, y: 0, width: 320, height: 200)
)
let encoder = JSONEncoder()
let data = try encoder.encode(container)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityContainer.self, from: data)
XCTAssertEqual(decoded.type, .landmark)
}
// MARK: - High-Importance Custom Content Tests
func testHighImportanceCustomContentIncludedInDescription() {
let element = AccessibilityElement(
description: "Photo, 42",
label: "Photo",
value: nil,
traits: .image,
identifier: nil,
hint: nil,
userInputLabels: nil,
shape: .frame(.zero),
activationPoint: .zero,
usesDefaultActivationPoint: true,
customActions: [],
customContent: [
.init(label: "Likes", value: "42", isImportant: true),
.init(label: "Comments", value: "5", isImportant: false),
],
customRotors: [],
accessibilityLanguage: nil,
respondsToUserInteraction: false,
containerContext: nil
)
// High-importance content value should be in description (comma-separated)
XCTAssertTrue(element.description.contains(", 42"))
// Default-importance content should NOT be in description
XCTAssertFalse(element.description.contains("5"))
}
func testHighImportanceContentAppearsBeforeTraits() {
let element = AccessibilityElement(
description: "Bailey: beagle, three years. Image.",
label: "Bailey",
value: "beagle",
traits: .image,
identifier: nil,
hint: nil,
userInputLabels: nil,
shape: .frame(.zero),
activationPoint: .zero,
usesDefaultActivationPoint: true,
customActions: [],
customContent: [
.init(label: "Age", value: "three years", isImportant: true),
],
customRotors: [],
accessibilityLanguage: nil,
respondsToUserInteraction: false,
containerContext: nil
)
// Per WWDC21: "Bailey, beagle, three years. Image."
let desc = element.description
let customContentIndex = desc.range(of: "three years")!.lowerBound
let traitIndex = desc.range(of: "Image")!.lowerBound
XCTAssertTrue(customContentIndex < traitIndex)
}
func testMultipleHighImportanceContentItems() {
let element = AccessibilityElement(
description: "Tweet, 100, 25",
label: "Tweet",
value: nil,
traits: [],
identifier: nil,
hint: nil,
userInputLabels: nil,
shape: .frame(.zero),
activationPoint: .zero,
usesDefaultActivationPoint: true,
customActions: [],
customContent: [
.init(label: "Likes", value: "100", isImportant: true),
.init(label: "Retweets", value: "25", isImportant: true),
],
customRotors: [],
accessibilityLanguage: nil,
respondsToUserInteraction: false,
containerContext: nil
)
// Both values should appear
XCTAssertTrue(element.description.contains("100"))
XCTAssertTrue(element.description.contains("25"))
}
func testHighImportanceContentWithEmptyValue() {
let element = AccessibilityElement(
description: "Status, Verified",
label: "Status",
value: nil,
traits: [],
identifier: nil,
hint: nil,
userInputLabels: nil,
shape: .frame(.zero),
activationPoint: .zero,
usesDefaultActivationPoint: true,
customActions: [],
customContent: [
.init(label: "Verified", value: "", isImportant: true),
],
customRotors: [],
accessibilityLanguage: nil,
respondsToUserInteraction: false,
containerContext: nil
)
// Should include label when value is empty
XCTAssertTrue(element.description.contains("Verified"))
}
func testCustomContentCodable() throws {
let content = AccessibilityElement.CustomContent(
label: "Rating",
value: "5 stars",
isImportant: true
)
let encoder = JSONEncoder()
let data = try encoder.encode(content)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityElement.CustomContent.self, from: data)
XCTAssertEqual(decoded.label, "Rating")
XCTAssertEqual(decoded.value, "5 stars")
XCTAssertEqual(decoded.isImportant, true)
}
func testElementWithCustomContentCodable() throws {
let element = AccessibilityElement(
description: "Photo, 42",
label: "Photo",
value: nil,
traits: .image,
identifier: nil,
hint: nil,
userInputLabels: nil,
shape: .frame(.zero),
activationPoint: .zero,
usesDefaultActivationPoint: true,
customActions: [],
customContent: [
.init(label: "Likes", value: "42", isImportant: true),
.init(label: "Comments", value: "5", isImportant: false),
],
customRotors: [],
accessibilityLanguage: nil,
respondsToUserInteraction: false,
containerContext: nil
)
let encoder = JSONEncoder()
let data = try encoder.encode(element)
let decoder = JSONDecoder()
let decoded = try decoder.decode(AccessibilityElement.self, from: data)
XCTAssertEqual(decoded.customContent.count, 2)
XCTAssertEqual(decoded.customContent[0].label, "Likes")
XCTAssertEqual(decoded.customContent[0].value, "42")
XCTAssertEqual(decoded.customContent[0].isImportant, true)
XCTAssertEqual(decoded.customContent[1].label, "Comments")
XCTAssertEqual(decoded.customContent[1].value, "5")
XCTAssertEqual(decoded.customContent[1].isImportant, false)
}
}
// MARK: -
private struct TestUserInterfaceLayoutDirectionProvider: UserInterfaceLayoutDirectionProviding {
var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection