-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot_test.go
More file actions
238 lines (215 loc) · 6.77 KB
/
snapshot_test.go
File metadata and controls
238 lines (215 loc) · 6.77 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
package main
import (
"encoding/json"
"strings"
"testing"
)
func TestRawAXValueString(t *testing.T) {
tests := []struct {
name string
val *rawAXValue
want string
}{
{"nil", nil, ""},
{"nil value", &rawAXValue{Type: "string"}, ""},
{"string", &rawAXValue{Type: "string", Value: json.RawMessage(`"hello"`)}, "hello"},
{"number", &rawAXValue{Type: "integer", Value: json.RawMessage(`42`)}, "42"},
{"bool", &rawAXValue{Type: "boolean", Value: json.RawMessage(`true`)}, "true"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.val.String()
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
})
}
}
func TestInteractiveRoles(t *testing.T) {
interactive := []string{"button", "link", "textbox", "checkbox", "radio", "tab", "menuitem"}
for _, r := range interactive {
if !interactiveRoles[r] {
t.Errorf("expected %q to be interactive", r)
}
}
nonInteractive := []string{"heading", "paragraph", "image", "banner", "main", "navigation"}
for _, r := range nonInteractive {
if interactiveRoles[r] {
t.Errorf("expected %q to NOT be interactive", r)
}
}
}
func TestBuildSnapshot(t *testing.T) {
// Simulate raw a11y nodes
nodes := []rawAXNode{
{
NodeID: "root",
Role: &rawAXValue{Value: json.RawMessage(`"WebArea"`)},
Name: &rawAXValue{Value: json.RawMessage(`"Test Page"`)},
ChildIDs: []string{"n1", "n2", "n3"},
BackendDOMNodeID: 1,
},
{
NodeID: "n1",
Role: &rawAXValue{Value: json.RawMessage(`"button"`)},
Name: &rawAXValue{Value: json.RawMessage(`"Submit"`)},
BackendDOMNodeID: 10,
},
{
NodeID: "n2",
Role: &rawAXValue{Value: json.RawMessage(`"textbox"`)},
Name: &rawAXValue{Value: json.RawMessage(`"Email"`)},
BackendDOMNodeID: 20,
Properties: []rawAXProp{
{Name: "focused", Value: &rawAXValue{Value: json.RawMessage(`"true"`)}},
},
},
{
NodeID: "n3",
Ignored: true,
Role: &rawAXValue{Value: json.RawMessage(`"none"`)},
},
{
NodeID: "n4",
Role: &rawAXValue{Value: json.RawMessage(`"generic"`)},
Name: &rawAXValue{Value: json.RawMessage(`""`)},
BackendDOMNodeID: 30,
},
}
flat, refs := buildSnapshot(nodes, "", -1)
// Should have 3 entries: WebArea, button, textbox (ignored + generic + none filtered)
if len(flat) != 3 {
t.Fatalf("expected 3 nodes, got %d: %+v", len(flat), flat)
}
// Check refs map
if refs["e0"] != 1 {
t.Errorf("e0 should map to nodeID 1, got %d", refs["e0"])
}
if refs["e1"] != 10 {
t.Errorf("e1 should map to nodeID 10, got %d", refs["e1"])
}
if refs["e2"] != 20 {
t.Errorf("e2 should map to nodeID 20, got %d", refs["e2"])
}
// Check depth
if flat[0].Depth != 0 {
t.Errorf("root depth should be 0, got %d", flat[0].Depth)
}
if flat[1].Depth != 1 {
t.Errorf("button depth should be 1, got %d", flat[1].Depth)
}
// Check focused property
if !flat[2].Focused {
t.Error("textbox should be focused")
}
}
func TestBuildSnapshotInteractiveFilter(t *testing.T) {
nodes := []rawAXNode{
{
NodeID: "root",
Role: &rawAXValue{Value: json.RawMessage(`"WebArea"`)},
Name: &rawAXValue{Value: json.RawMessage(`"Page"`)},
ChildIDs: []string{"n1", "n2"},
BackendDOMNodeID: 1,
},
{
NodeID: "n1",
Role: &rawAXValue{Value: json.RawMessage(`"heading"`)},
Name: &rawAXValue{Value: json.RawMessage(`"Title"`)},
BackendDOMNodeID: 10,
},
{
NodeID: "n2",
Role: &rawAXValue{Value: json.RawMessage(`"button"`)},
Name: &rawAXValue{Value: json.RawMessage(`"Click me"`)},
BackendDOMNodeID: 20,
},
}
flat, _ := buildSnapshot(nodes, filterInteractive, -1)
if len(flat) != 1 {
t.Fatalf("expected 1 interactive node, got %d: %+v", len(flat), flat)
}
if flat[0].Role != "button" {
t.Errorf("expected button, got %s", flat[0].Role)
}
}
func TestFormatSnapshotText(t *testing.T) {
nodes := []A11yNode{
{Ref: "e0", Role: "WebArea", Name: "Page", Depth: 0},
{Ref: "e1", Role: "button", Name: "Submit", Depth: 1},
{Ref: "e2", Role: "textbox", Name: "Email", Depth: 1, Value: "test@x.com", Focused: true},
{Ref: "e3", Role: "button", Name: "Cancel", Depth: 1, Disabled: true},
}
text := formatSnapshotText(nodes)
if !strings.Contains(text, `e0 WebArea "Page"`) {
t.Error("missing root node")
}
if !strings.Contains(text, ` e1 button "Submit"`) {
t.Error("missing indented button")
}
if !strings.Contains(text, `val="test@x.com"`) {
t.Error("missing value")
}
if !strings.Contains(text, "[focused]") {
t.Error("missing focused flag")
}
if !strings.Contains(text, "[disabled]") {
t.Error("missing disabled flag")
}
}
func TestDiffSnapshot(t *testing.T) {
prev := []A11yNode{
{Ref: "e0", Role: "button", Name: "Submit", NodeID: 10},
{Ref: "e1", Role: "textbox", Name: "Email", NodeID: 20, Value: ""},
{Ref: "e2", Role: "link", Name: "Old Link", NodeID: 30},
}
curr := []A11yNode{
{Ref: "e0", Role: "button", Name: "Submit", NodeID: 10}, // unchanged
{Ref: "e1", Role: "textbox", Name: "Email", NodeID: 20, Value: "hi"}, // changed (value)
{Ref: "e3", Role: "link", Name: "New Link", NodeID: 40}, // added
}
added, changed, removed := diffSnapshot(prev, curr)
if len(added) != 1 || added[0].Name != "New Link" {
t.Errorf("expected 1 added (New Link), got %+v", added)
}
if len(changed) != 1 || changed[0].Name != "Email" {
t.Errorf("expected 1 changed (Email), got %+v", changed)
}
if len(removed) != 1 || removed[0].Name != "Old Link" {
t.Errorf("expected 1 removed (Old Link), got %+v", removed)
}
}
func TestDiffSnapshotEmpty(t *testing.T) {
added, changed, removed := diffSnapshot(nil, nil)
if len(added) != 0 || len(changed) != 0 || len(removed) != 0 {
t.Error("diff of two empty snapshots should be empty")
}
}
func TestBuildSnapshotDepthFilter(t *testing.T) {
nodes := []rawAXNode{
{
NodeID: "root",
Role: &rawAXValue{Value: json.RawMessage(`"WebArea"`)},
Name: &rawAXValue{Value: json.RawMessage(`"Page"`)},
ChildIDs: []string{"n1"},
BackendDOMNodeID: 1,
},
{
NodeID: "n1",
Role: &rawAXValue{Value: json.RawMessage(`"navigation"`)},
Name: &rawAXValue{Value: json.RawMessage(`"Nav"`)},
ChildIDs: []string{"n2"},
BackendDOMNodeID: 10,
},
{
NodeID: "n2",
Role: &rawAXValue{Value: json.RawMessage(`"link"`)},
Name: &rawAXValue{Value: json.RawMessage(`"Home"`)},
BackendDOMNodeID: 20,
},
}
flat, _ := buildSnapshot(nodes, "", 1)
if len(flat) != 2 {
t.Fatalf("expected 2 nodes at depth<=1, got %d: %+v", len(flat), flat)
}
}