Repository navigation
Expand file tree
/
Copy pathelement_focus_test.go
More file actions
368 lines (322 loc) · 10.8 KB
/
Copy pathelement_focus_test.go
File metadata and controls
368 lines (322 loc) · 10.8 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
package tui
import (
"fmt"
"slices"
"testing"
)
// --- Hit Testing Tests ---
func TestElement_ElementAt_ReturnsNilForPointOutsideBounds(t *testing.T) {
e := New(WithWidth(10), WithHeight(10))
// Calculate layout so the element has a position
buf := NewBuffer(80, 25)
e.RenderTo(buf, 80, 25)
result := e.ElementAt(50, 50)
if result != nil {
t.Error("ElementAt should return nil for points outside the element's bounds")
}
}
func TestElement_ElementAt_ReturnsSelfForPointInsideBounds(t *testing.T) {
e := New(WithWidth(10), WithHeight(10))
// Calculate layout so the element has a position
buf := NewBuffer(80, 25)
e.RenderTo(buf, 80, 25)
result := e.ElementAt(5, 5)
if result != e {
t.Error("ElementAt should return the element itself when point is inside bounds and no children")
}
}
func TestElement_ElementAt_ReturnsChildForPointInsideChild(t *testing.T) {
parent := New(
WithWidth(100),
WithHeight(100),
WithDirection(Column),
)
child := New(WithWidth(50), WithHeight(50))
parent.AddChild(child)
// Calculate layout
buf := NewBuffer(100, 100)
parent.RenderTo(buf, 100, 100)
// Point inside child bounds (child starts at 0,0 and is 50x50)
result := parent.ElementAt(10, 10)
if result != child {
t.Error("ElementAt should return the child when point is inside child bounds")
}
}
func TestElement_ElementAt_ReturnsDeepestChild(t *testing.T) {
root := New(
WithWidth(100),
WithHeight(100),
)
child1 := New(WithWidth(80), WithHeight(80))
child2 := New(WithWidth(60), WithHeight(60))
grandchild := New(WithWidth(40), WithHeight(40))
child2.AddChild(grandchild)
child1.AddChild(child2)
root.AddChild(child1)
// Calculate layout
buf := NewBuffer(100, 100)
root.RenderTo(buf, 100, 100)
// Point inside grandchild bounds (should be at 0,0)
result := root.ElementAt(10, 10)
if result != grandchild {
t.Error("ElementAt should return the deepest child containing the point")
}
}
func TestElement_ElementAt_LastChildTakesPrecedence(t *testing.T) {
// When children overlap, last child should take precedence (renders on top)
parent := New(
WithWidth(100),
WithHeight(100),
)
// Both children start at 0,0 with same size
child1 := New(WithWidth(50), WithHeight(50))
child2 := New(WithWidth(50), WithHeight(50))
parent.AddChild(child1)
parent.AddChild(child2)
// Calculate layout - both children will be at position (0,0)
buf := NewBuffer(100, 100)
parent.RenderTo(buf, 100, 100)
result := parent.ElementAt(10, 10)
// child2 was added last, so it should take precedence
// However, with default layout (row), they won't overlap.
// Let's just verify we get one of the children
if result != child1 && result != child2 {
t.Error("ElementAt should return one of the children for overlapping bounds")
}
}
func TestElement_ElementAtPoint_ReturnsFocusable(t *testing.T) {
// Test that ElementAtPoint returns a Focusable interface
e := New(WithWidth(10), WithHeight(10))
buf := NewBuffer(80, 25)
e.RenderTo(buf, 80, 25)
result := e.ElementAtPoint(5, 5)
if result == nil {
t.Error("ElementAtPoint should return a non-nil Focusable")
}
// Verify it's the same underlying element
if result.(*Element) != e {
t.Error("ElementAtPoint should return the element wrapped as Focusable")
}
}
func TestElement_ElementAtPoint_ReturnsNilForPointOutsideBounds(t *testing.T) {
e := New(WithWidth(10), WithHeight(10))
buf := NewBuffer(80, 25)
e.RenderTo(buf, 80, 25)
result := e.ElementAtPoint(50, 50)
if result != nil {
t.Error("ElementAtPoint should return nil for points outside bounds")
}
}
// --- HandleEvent Tests ---
func TestElement_HandleEvent_MouseNotHandledByDefault(t *testing.T) {
e := New()
event := MouseEvent{
Button: MouseLeft,
Action: MousePress,
X: 5,
Y: 5,
}
consumed := e.HandleEvent(event)
if consumed {
t.Error("HandleEvent should return false for mouse events (mouse handling is via component HandleMouse)")
}
}
func TestElement_HandleEvent_KeyNotHandledByDefault(t *testing.T) {
e := New()
event := KeyEvent{Key: KeyRune, Rune: 'x'}
consumed := e.HandleEvent(event)
if consumed {
t.Error("HandleEvent should return false for key events (key handling is via component KeyMap)")
}
}
// --- Border style writes while focused ---
// borderWriters are the three ways user code changes a border color.
var borderWriters = map[string]func(e *Element, s Style){
"SetBorderStyle": func(e *Element, s Style) { e.SetBorderStyle(s) },
"Apply WithBorderStyle": func(e *Element, s Style) { e.Apply(WithBorderStyle(s)) },
"SetClass border color": func(e *Element, _ Style) { e.SetClass("border-green") },
}
func TestElement_BorderStyleWriteWhileFocused(t *testing.T) {
red := NewStyle().Foreground(Red)
green := NewStyle().Foreground(Green)
cyan := NewStyle().Foreground(Cyan)
magenta := NewStyle().Foreground(Magenta)
type tc struct {
opts []Option
writeUnfocused bool // write before Focus instead of during
wantFocused Style // visible style after the write, while focused
}
tests := map[string]tc{
"default highlight keeps highlight until blur": {
opts: []Option{WithBorder(BorderSingle), WithBorderStyle(red), WithFocusable(true)},
wantFocused: cyan,
},
"explicit focus style keeps highlight until blur": {
opts: []Option{WithBorder(BorderSingle), WithBorderStyle(red), WithFocusable(true), WithFocusBorderStyle(magenta)},
wantFocused: magenta,
},
"no highlight applies immediately": {
opts: []Option{WithBorder(BorderSingle), WithBorderStyle(red), WithOnFocus(func() {})},
wantFocused: green,
},
"write before focus survives focus and blur": {
opts: []Option{WithBorder(BorderSingle), WithBorderStyle(red), WithFocusable(true)},
writeUnfocused: true,
wantFocused: cyan,
},
}
for name, tt := range tests {
for wname, write := range borderWriters {
t.Run(name+"/"+wname, func(t *testing.T) {
e := New(tt.opts...)
if tt.writeUnfocused {
write(e, green)
}
e.Focus()
if !tt.writeUnfocused {
write(e, green)
}
if got := e.activeBorderStyle(); got != tt.wantFocused {
t.Errorf("focused: visible border = %+v, want %+v", got, tt.wantFocused)
}
if got := e.BorderStyle(); got != green {
t.Errorf("focused: BorderStyle() = %+v, want %+v", got, green)
}
e.Blur()
if got := e.activeBorderStyle(); got != green {
t.Errorf("blurred: visible border = %+v, want %+v", got, green)
}
})
}
}
}
func TestElement_Blur_RefocusInOnBlurKeepsHighlight(t *testing.T) {
red := NewStyle().Foreground(Red)
cyan := NewStyle().Foreground(Cyan)
var e *Element
e = New(WithBorder(BorderSingle), WithBorderStyle(red), WithFocusable(true),
WithOnBlur(func() { e.Focus() }))
e.Focus()
e.Blur()
if !e.IsFocused() {
t.Fatal("onBlur refocus should leave the element focused")
}
if got := e.activeBorderStyle(); got != cyan {
t.Errorf("refocused: visible border = %+v, want %+v", got, cyan)
}
// Drop the handler so the next Blur really blurs.
e.onBlur = nil
e.Blur()
if got := e.activeBorderStyle(); got != red {
t.Errorf("blurred: visible border = %+v, want %+v", got, red)
}
}
func TestElement_SetClassWhileFocused_Clear(t *testing.T) {
red := NewStyle().Foreground(Red)
green := NewStyle().Foreground(Green)
cyan := NewStyle().Foreground(Cyan)
type tc struct {
applyBeforeFocus bool // SetClass("border-green") before Focus instead of during
clearAfterBlur bool // SetClass("") after Blur instead of while focused
wantBlurred Style // visible style right after Blur
}
tests := map[string]tc{
"apply and clear while focused": {wantBlurred: red},
"apply while focused, clear after blur": {clearAfterBlur: true, wantBlurred: green},
"apply before focus, clear while focused": {applyBeforeFocus: true, wantBlurred: red},
"apply before focus, clear after blur": {applyBeforeFocus: true, clearAfterBlur: true, wantBlurred: green},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
e := New(WithBorder(BorderSingle), WithBorderStyle(red), WithFocusable(true))
if tt.applyBeforeFocus {
e.SetClass("border-green")
}
e.Focus()
if !tt.applyBeforeFocus {
e.SetClass("border-green")
}
if !tt.clearAfterBlur {
e.SetClass("")
}
if got := e.activeBorderStyle(); got != cyan {
t.Errorf("focused: visible border = %+v, want %+v", got, cyan)
}
e.Blur()
if got := e.activeBorderStyle(); got != tt.wantBlurred {
t.Errorf("blurred: visible border = %+v, want %+v", got, tt.wantBlurred)
}
if tt.clearAfterBlur {
e.SetClass("")
}
if got := e.activeBorderStyle(); got != red {
t.Errorf("class cleared: visible border = %+v, want %+v", got, red)
}
// A second focus cycle must not have baked the highlight into the base.
e.Focus()
e.Blur()
if got := e.activeBorderStyle(); got != red {
t.Errorf("after second cycle: visible border = %+v, want %+v", got, red)
}
})
}
}
// newScrolledList builds a bordered, scrollable list under a title, scrolled
// down two rows, and renders it:
//
// y=0 title
// y=1 ┌ border
// y=2 r2 (r0, r1 scrolled out above)
// y=3 r3
// y=4 └ border (r4 is clipped here)
func newScrolledList() (root *Element, rows []*Element) {
root = New(WithDirection(Column), WithWidth(12), WithHeight(6))
root.AddChild(New(WithText("title"), WithHeight(1)))
list := New(WithDirection(Column), WithHeight(4), WithWidth(12), WithBorder(BorderSingle), WithScrollable(ScrollVertical))
for i := range 6 {
row := New(WithText(fmt.Sprintf("r%d", i)), WithHeight(1))
rows = append(rows, row)
list.AddChild(row)
}
root.AddChild(list)
buf := NewBuffer(12, 6)
root.RenderTo(buf, 12, 6)
list.ScrollBy(0, 2)
root.RenderTo(buf, 12, 6)
return root, rows
}
func TestElement_HitTestingInsideScrolledContainer(t *testing.T) {
type tc struct {
x, y int
want int // index of the row hit, -1 for none
}
tests := map[string]tc{
"first visible row": {x: 2, y: 2, want: 2},
"second visible row": {x: 2, y: 3, want: 3},
"bottom border clips the next row": {x: 2, y: 4, want: -1},
"top border": {x: 2, y: 1, want: -1},
"title above the list": {x: 2, y: 0, want: -1},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
root, rows := newScrolledList()
hit := root.ElementAt(tt.x, tt.y)
gotAt := slices.Index(rows, hit)
if gotAt != tt.want {
t.Errorf("ElementAt(%d,%d) hit row %d, want %d", tt.x, tt.y, gotAt, tt.want)
}
gotContains := -1
for i, row := range rows {
if row.ContainsPoint(tt.x, tt.y) {
if gotContains != -1 {
t.Errorf("ContainsPoint(%d,%d) true for rows %d and %d", tt.x, tt.y, gotContains, i)
}
gotContains = i
}
}
if gotContains != tt.want {
t.Errorf("ContainsPoint(%d,%d) matched row %d, want %d", tt.x, tt.y, gotContains, tt.want)
}
})
}
}