-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathAccessibilityHierarchyParserTests.swift
More file actions
155 lines (117 loc) · 6.39 KB
/
Copy pathAccessibilityHierarchyParserTests.swift
File metadata and controls
155 lines (117 loc) · 6.39 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
//
// Copyright 2019 Square Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import AccessibilitySnapshot
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.parseAccessibilityElements(
in: gridView,
userInterfaceLayoutDirectionProvider: TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .leftToRight),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .phone)
).map { $0.description }
XCTAssertEqual(ltrElements, ["A", "B", "C", "D"])
let rtlElements = parser.parseAccessibilityElements(
in: gridView,
userInterfaceLayoutDirectionProvider: TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .rightToLeft),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .phone)
).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.parseAccessibilityElements(
in: gridView,
userInterfaceLayoutDirectionProvider:
TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .leftToRight),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .pad)
).map { $0.description }
// on pad elements are sorted horizontally
XCTAssertEqual(padElements, ["A", "B", "C", "D"])
let phoneElements = parser.parseAccessibilityElements(
in: gridView,
userInterfaceLayoutDirectionProvider:
TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .leftToRight),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .phone)
).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.parseAccessibilityElements(
in: gridView,
userInterfaceLayoutDirectionProvider:
TestUserInterfaceLayoutDirectionProvider(userInterfaceLayoutDirection: .leftToRight),
userInterfaceIdiomProvider: TestUserInterfaceIdiomProvider(userInterfaceIdiom: .pad)
).map { $0.description }
// Now pad elements are sorted vertically and then left to right
XCTAssertEqual(padAgain, ["C", "D", "B", "A"])
}
}
// MARK: -
private struct TestUserInterfaceLayoutDirectionProvider: UserInterfaceLayoutDirectionProviding {
var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection
}
private struct TestUserInterfaceIdiomProvider: UserInterfaceIdiomProviding {
var userInterfaceIdiom: UIUserInterfaceIdiom
}