Skip to content

Commit 5a4586b

Browse files
authored
chore: add some test (#18)
1 parent 774b6ae commit 5a4586b

4 files changed

Lines changed: 271 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package accessibility
2+
3+
import (
4+
"image"
5+
"testing"
6+
)
7+
8+
func TestRectFromInfo(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
info *ElementInfo
12+
want image.Rectangle
13+
}{
14+
{
15+
name: "Basic rectangle",
16+
info: &ElementInfo{
17+
Position: image.Point{X: 10, Y: 20},
18+
Size: image.Point{X: 100, Y: 50},
19+
},
20+
want: image.Rect(10, 20, 110, 70),
21+
},
22+
{
23+
name: "Zero size",
24+
info: &ElementInfo{
25+
Position: image.Point{X: 5, Y: 5},
26+
Size: image.Point{X: 0, Y: 0},
27+
},
28+
want: image.Rect(5, 5, 5, 5),
29+
},
30+
{
31+
name: "Negative position",
32+
info: &ElementInfo{
33+
Position: image.Point{X: -10, Y: -20},
34+
Size: image.Point{X: 30, Y: 40},
35+
},
36+
want: image.Rect(-10, -20, 20, 20),
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
got := rectFromInfo(tt.info)
43+
if got != tt.want {
44+
t.Errorf("rectFromInfo() = %v, want %v", got, tt.want)
45+
}
46+
})
47+
}
48+
}
49+
50+
func TestExpandRectangle(t *testing.T) {
51+
tests := []struct {
52+
name string
53+
rect image.Rectangle
54+
padding int
55+
want image.Rectangle
56+
}{
57+
{
58+
name: "Expand by 5",
59+
rect: image.Rect(10, 10, 20, 20),
60+
padding: 5,
61+
want: image.Rect(5, 5, 25, 25),
62+
},
63+
{
64+
name: "Expand by 0",
65+
rect: image.Rect(10, 10, 20, 20),
66+
padding: 0,
67+
want: image.Rect(10, 10, 20, 20),
68+
},
69+
{
70+
name: "Expand by negative",
71+
rect: image.Rect(10, 10, 20, 20),
72+
padding: -2,
73+
want: image.Rect(12, 12, 18, 18),
74+
},
75+
}
76+
77+
for _, tt := range tests {
78+
t.Run(tt.name, func(t *testing.T) {
79+
got := expandRectangle(tt.rect, tt.padding)
80+
if got != tt.want {
81+
t.Errorf("expandRectangle() = %v, want %v", got, tt.want)
82+
}
83+
})
84+
}
85+
}

internal/config/config_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,60 @@ func TestLoadNonExistentFile(t *testing.T) {
116116
}
117117
}
118118

119+
func TestConfigWithAppSpecificSettings(t *testing.T) {
120+
// Create a config with app-specific settings
121+
cfg := DefaultConfig()
122+
123+
// Add app-specific configurations
124+
cfg.Accessibility.AppConfigs = []AppConfig{
125+
{
126+
BundleID: "com.example.app1",
127+
AdditionalClickable: []string{"CustomButton1", "CustomLink1"},
128+
AdditionalScrollable: []string{"CustomScroll1"},
129+
},
130+
{
131+
BundleID: "com.example.app2",
132+
AdditionalClickable: []string{"CustomButton2"},
133+
AdditionalScrollable: []string{"CustomScroll2", "CustomPanel2"},
134+
},
135+
}
136+
137+
// Test getting clickable roles for specific app
138+
clickableRoles := cfg.GetClickableRolesForApp("com.example.app1")
139+
140+
// Should include both default and app-specific roles
141+
defaultCount := len(cfg.Accessibility.ClickableRoles)
142+
expectedCount := defaultCount + 2 // Default + 2 custom roles
143+
144+
if len(clickableRoles) != expectedCount {
145+
t.Errorf("Expected %d clickable roles, got %d", expectedCount, len(clickableRoles))
146+
}
147+
148+
// Check if app-specific roles are included
149+
found := false
150+
for _, role := range clickableRoles {
151+
if role == "CustomButton1" {
152+
found = true
153+
break
154+
}
155+
}
156+
157+
if !found {
158+
t.Errorf("App-specific role 'CustomButton1' not found in clickable roles")
159+
}
160+
161+
// Test getting scrollable roles for specific app
162+
scrollableRoles := cfg.GetScrollableRolesForApp("com.example.app2")
163+
164+
// Should include both default and app-specific roles
165+
defaultScrollCount := len(cfg.Accessibility.ScrollableRoles)
166+
expectedScrollCount := defaultScrollCount + 2 // Default + 2 custom roles
167+
168+
if len(scrollableRoles) != expectedScrollCount {
169+
t.Errorf("Expected %d scrollable roles, got %d", expectedScrollCount, len(scrollableRoles))
170+
}
171+
}
172+
119173
func TestGetConfigPath(t *testing.T) {
120174
path := GetConfigPath()
121175
if path == "" {

internal/hints/generator_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,114 @@ func TestHintIsVisible(t *testing.T) {
192192
}
193193
}
194194

195+
func TestGenerate(t *testing.T) {
196+
// Create test elements
197+
elements := []*accessibility.TreeNode{
198+
{
199+
Info: &accessibility.ElementInfo{
200+
Position: image.Point{X: 10, Y: 10},
201+
Size: image.Point{X: 20, Y: 20},
202+
Role: "button",
203+
},
204+
},
205+
{
206+
Info: &accessibility.ElementInfo{
207+
Position: image.Point{X: 50, Y: 10},
208+
Size: image.Point{X: 20, Y: 20},
209+
Role: "link",
210+
},
211+
},
212+
{
213+
Info: &accessibility.ElementInfo{
214+
Position: image.Point{X: 10, Y: 50},
215+
Size: image.Point{X: 20, Y: 20},
216+
Role: "checkbox",
217+
},
218+
},
219+
}
220+
221+
tests := []struct {
222+
name string
223+
characters string
224+
style string
225+
maxHints int
226+
elements []*accessibility.TreeNode
227+
wantCount int
228+
wantLabels []string
229+
}{
230+
{
231+
name: "alphabet style",
232+
characters: "asdf",
233+
style: "alphabet",
234+
maxHints: 10,
235+
elements: elements,
236+
wantCount: 3,
237+
wantLabels: []string{"A", "S", "D"},
238+
},
239+
{
240+
name: "numeric style",
241+
characters: "asdf",
242+
style: "numeric",
243+
maxHints: 10,
244+
elements: elements,
245+
wantCount: 3,
246+
wantLabels: []string{"1", "2", "3"},
247+
},
248+
{
249+
name: "limited hints",
250+
characters: "asdf",
251+
style: "alphabet",
252+
maxHints: 2,
253+
elements: elements,
254+
wantCount: 2,
255+
wantLabels: []string{"A", "S"},
256+
},
257+
{
258+
name: "empty elements",
259+
characters: "asdf",
260+
style: "alphabet",
261+
maxHints: 10,
262+
elements: []*accessibility.TreeNode{},
263+
wantCount: 0,
264+
wantLabels: []string{},
265+
},
266+
}
267+
268+
for _, tt := range tests {
269+
t.Run(tt.name, func(t *testing.T) {
270+
generator := NewGenerator(tt.characters, tt.style, tt.maxHints)
271+
hints, err := generator.Generate(tt.elements)
272+
273+
if err != nil {
274+
t.Fatalf("Generate() error = %v", err)
275+
}
276+
277+
if len(hints) != tt.wantCount {
278+
t.Errorf("Generate() returned %d hints, want %d", len(hints), tt.wantCount)
279+
}
280+
281+
// Check labels
282+
for i, hint := range hints {
283+
if i < len(tt.wantLabels) && hint.Label != tt.wantLabels[i] {
284+
t.Errorf("Generate() hint[%d].Label = %s, want %s", i, hint.Label, tt.wantLabels[i])
285+
}
286+
}
287+
288+
// Check positions are centered
289+
for i, hint := range hints {
290+
element := tt.elements[i]
291+
expectedX := element.Info.Position.X + (element.Info.Size.X / 2)
292+
expectedY := element.Info.Position.Y + (element.Info.Size.Y / 2)
293+
294+
if hint.Position.X != expectedX || hint.Position.Y != expectedY {
295+
t.Errorf("Generate() hint[%d].Position = (%d,%d), want (%d,%d)",
296+
i, hint.Position.X, hint.Position.Y, expectedX, expectedY)
297+
}
298+
}
299+
})
300+
}
301+
}
302+
195303
func TestHintCollection(t *testing.T) {
196304
hints := []*Hint{
197305
{Label: "a"},

internal/scroll/controller_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,27 @@ func TestController_BasicOperations(t *testing.T) {
3232
// This is a basic smoke test
3333
_ = controller
3434
}
35+
36+
func TestScrollDirections(t *testing.T) {
37+
// Test that direction constants are defined correctly
38+
directions := []Direction{DirectionUp, DirectionDown, DirectionLeft, DirectionRight}
39+
expectedValues := []int{0, 1, 2, 3}
40+
41+
for i, dir := range directions {
42+
if int(dir) != expectedValues[i] {
43+
t.Errorf("Direction constant %d has unexpected value %d", i, dir)
44+
}
45+
}
46+
}
47+
48+
func TestScrollAmounts(t *testing.T) {
49+
// Test that amount constants are defined correctly
50+
amounts := []ScrollAmount{AmountChar, AmountHalfPage, AmountFullPage}
51+
expectedValues := []int{0, 1, 2}
52+
53+
for i, amount := range amounts {
54+
if int(amount) != expectedValues[i] {
55+
t.Errorf("ScrollAmount constant %d has unexpected value %d", i, amount)
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)