-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbutton_test.go
More file actions
279 lines (252 loc) · 9.34 KB
/
Copy pathbutton_test.go
File metadata and controls
279 lines (252 loc) · 9.34 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
package vtui
import (
"testing"
"github.com/unxed/vtinput"
)
func TestButton_OnClick(t *testing.T) {
b := NewButton(0, 0, "OK")
clicked := false
b.OnClick = func() { clicked = true }
// Test KeyDown Space
b.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_SPACE})
if !clicked {
t.Error("Button should be clicked on Space")
}
clicked = false
// Test KeyDown Return (Buttons SHOULD still handle Return)
b.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_RETURN})
if !clicked {
t.Error("Button should be clicked on Return")
}
clicked = false
// Mouse down only presses the button visually; release performs the click.
b.ProcessMouse(&vtinput.InputEvent{Type: vtinput.MouseEventType, KeyDown: true, ButtonState: vtinput.FromLeft1stButtonPressed})
if clicked {
t.Error("Button should not click on mouse down")
}
if !b.mousePressed {
t.Error("Button should be visually pressed on mouse down")
}
b.ProcessMouse(&vtinput.InputEvent{Type: vtinput.MouseEventType, ButtonState: 0})
if !clicked {
t.Error("Button should be clicked on mouse release")
}
if b.mousePressed || b.mouseArmed {
t.Error("Button should reset its mouse state after release")
}
}
func TestButton_MouseReleaseOutsideCancelsClick(t *testing.T) {
b := NewButton(2, 1, "OK")
clicked := false
b.OnClick = func() { clicked = true }
b.ProcessMouse(&vtinput.InputEvent{
Type: vtinput.MouseEventType, KeyDown: true,
ButtonState: vtinput.FromLeft1stButtonPressed,
MouseX: 3, MouseY: 1,
})
b.ProcessMouse(&vtinput.InputEvent{
Type: vtinput.MouseEventType, ButtonState: vtinput.FromLeft1stButtonPressed,
MouseEventFlags: vtinput.MouseMoved,
MouseX: 20, MouseY: 5,
})
if b.mousePressed {
t.Error("Button should not look pressed while pointer is outside")
}
b.ProcessMouse(&vtinput.InputEvent{
Type: vtinput.MouseEventType, ButtonState: 0,
MouseX: 20, MouseY: 5,
})
if clicked {
t.Error("Button should not click when released outside")
}
}
func TestButton_DragBackInsideRestoresPressedStateAndClicks(t *testing.T) {
b := NewButton(2, 1, "OK")
clicked := false
b.OnClick = func() { clicked = true }
b.ProcessMouse(&vtinput.InputEvent{Type: vtinput.MouseEventType, KeyDown: true, ButtonState: vtinput.FromLeft1stButtonPressed, MouseX: 3, MouseY: 1})
b.ProcessMouse(&vtinput.InputEvent{Type: vtinput.MouseEventType, ButtonState: vtinput.FromLeft1stButtonPressed, MouseEventFlags: vtinput.MouseMoved, MouseX: 20, MouseY: 5})
b.ProcessMouse(&vtinput.InputEvent{Type: vtinput.MouseEventType, ButtonState: vtinput.FromLeft1stButtonPressed, MouseEventFlags: vtinput.MouseMoved, MouseX: 3, MouseY: 1})
if !b.mousePressed {
t.Error("Button should look pressed again after dragging back inside")
}
b.ProcessMouse(&vtinput.InputEvent{Type: vtinput.MouseEventType, ButtonState: 0, MouseX: 3, MouseY: 1})
if !clicked {
t.Error("Button should click when released inside after dragging back")
}
}
func TestButton_HotkeyParsing(t *testing.T) {
b := NewButton(0, 0, "Sa&ve")
// Check that the constructor correctly extracted 'v' (lowercase)
if b.GetHotkey() != 'v' {
t.Errorf("Expected hotkey 'v', got %c", b.GetHotkey())
}
// The brackets must not leak into the caption exposed to the outside.
if b.GetCaption() != "Save" {
t.Errorf("Expected caption %q, got %q", "Save", b.GetCaption())
}
if b.GetText() != "[ Sa&ve ]" {
t.Errorf("Expected raw text %q, got %q", "[ Sa&ve ]", b.GetText())
}
node := b.SemanticNode(&SemanticContext{Width: 80, Height: 25})
if node["text"] != "Save" {
t.Errorf("Expected semantic text %q, got %v", "Save", node["text"])
}
if node["hotkey"] != "v" {
t.Errorf("Expected semantic hotkey %q, got %v", "v", node["hotkey"])
}
// A later SetText must re-decorate the text and refresh both the caption
// and the hotkey.
b.SetText("&Close")
if b.GetCaption() != "Close" || b.GetText() != "[ &Close ]" {
t.Errorf("Expected the caption to follow SetText, got %q and %q", b.GetCaption(), b.GetText())
}
if b.GetHotkey() != 'c' {
t.Errorf("Expected hotkey 'c' after SetText, got %c", b.GetHotkey())
}
}
// A host can reach the embedded ScreenObject.SetText directly to give a
// button raw, already-decorated text without Button's own "[ Text ]"
// wrapping — f4's settings center does this for its search prev/next arrow
// buttons ("[←]"/"[→]"), a 3-rune cleanText shorter than the 4-rune frame
// (bracket, space, space, bracket) DisplayObject's ear-carving assumes.
// That used to panic with a negative-length slice; it must instead just
// render the raw text without ears.
func TestButton_ShortRawTextDoesNotPanic(t *testing.T) {
SetDefaultPalette()
scr := NewSilentScreenBuf()
scr.AllocBuf(10, 1)
b := NewButton(0, 0, "placeholder")
b.ScreenObject.SetText("[←]")
b.Show(scr) // must not panic
if got := ScreenRow(scr, 0, 0, 2); got != "[←]" {
t.Errorf("expected the raw text drawn as-is, got %q", got)
}
}
func TestButton_DefaultUsesHighlightStyleWhenUnfocused(t *testing.T) {
SetDefaultPalette()
scr := NewSilentScreenBuf()
scr.AllocBuf(20, 3)
normal := NewButton(0, 0, "Normal")
defaultButton := NewButton(0, 1, "Default")
defaultButton.IsDefault = true
normal.Show(scr)
defaultButton.Show(scr)
checkCell(t, scr, 2, 0, 'N', Palette[ColDialogButton])
checkCell(t, scr, 2, 1, 'D', Palette[ColDialogHighlightButton])
defaultButton.SetFocus(true)
defaultButton.Show(scr)
checkCell(t, scr, 2, 1, 'D', Palette[ColDialogSelectedButton])
}
func dialogWithButtons(t *testing.T, build func(w *BaseWindow) []*Button) (*ScreenBuf, []*Button) {
t.Helper()
SetDefaultPalette()
scr := NewSilentScreenBuf()
scr.AllocBuf(40, 10)
w := NewBaseWindow(0, 0, 39, 9, "Dialog")
buttons := build(w)
w.Show(scr)
return scr, buttons
}
// A dialog that flags no default button still presses its first button on
// Enter; that button has to look like the default one (f4 #320).
func TestDialog_ButtonEnterFallsBackToIsDrawnAsDefault(t *testing.T) {
scr, buttons := dialogWithButtons(t, func(w *BaseWindow) []*Button {
// An input field takes the initial focus, so no button is drawn as the
// focused one.
w.AddItem(NewEdit(2, 1, 10, ""))
first := NewButton(2, 2, "First")
first.OnClick = func() {}
second := NewButton(2, 4, "Second")
second.OnClick = func() {}
w.AddItem(first)
w.AddItem(second)
return []*Button{first, second}
})
if !buttons[0].IsEnterDefault() || buttons[1].IsEnterDefault() {
t.Fatalf("default flags = %v, %v; want the first only", buttons[0].IsEnterDefault(), buttons[1].IsEnterDefault())
}
checkCell(t, scr, 4, 2, 'F', Palette[ColDialogHighlightButton])
checkCell(t, scr, 4, 4, 'S', Palette[ColDialogButton])
if buttons[0].IsDefault {
t.Fatal("the flag the dialog owns was overwritten")
}
}
func TestDialog_FlaggedDefaultButtonWinsOverTheFallback(t *testing.T) {
_, buttons := dialogWithButtons(t, func(w *BaseWindow) []*Button {
first := NewButton(2, 2, "First")
first.OnClick = func() {}
second := NewButton(2, 4, "Second")
second.OnClick = func() {}
second.IsDefault = true
w.AddItem(first)
w.AddItem(second)
return []*Button{first, second}
})
if buttons[0].IsEnterDefault() || !buttons[1].IsEnterDefault() {
t.Fatalf("default flags = %v, %v; want the flagged second only", buttons[0].IsEnterDefault(), buttons[1].IsEnterDefault())
}
}
func TestDialog_FallbackSkipsDisabledAndFollowsTheDialog(t *testing.T) {
scr := NewSilentScreenBuf()
scr.AllocBuf(40, 10)
SetDefaultPalette()
w := NewBaseWindow(0, 0, 39, 9, "Dialog")
first := NewButton(2, 2, "First")
first.OnClick = func() {}
first.SetDisabled(true)
second := NewButton(2, 4, "Second")
second.OnClick = func() {}
w.AddItem(first)
w.AddItem(second)
w.Show(scr)
if first.IsEnterDefault() || !second.IsEnterDefault() {
t.Fatalf("with the first disabled the second must be the default: %v, %v", first.IsEnterDefault(), second.IsEnterDefault())
}
// The dialog then flags a default of its own: the fallback goes away.
first.SetDisabled(false)
first.IsDefault = true
w.Show(scr)
if !first.IsEnterDefault() || second.IsEnterDefault() {
t.Fatalf("after flagging the first: %v, %v", first.IsEnterDefault(), second.IsEnterDefault())
}
}
// What is drawn as the default is what Enter presses, whatever the layout.
func TestDialog_DrawnDefaultIsWhatEnterPresses(t *testing.T) {
SetDefaultPalette()
scr := NewSilentScreenBuf()
scr.AllocBuf(40, 10)
w := NewBaseWindow(0, 0, 39, 9, "Dialog")
w.AddItem(NewEdit(2, 1, 10, "")) // holds the focus, so Enter is left for the dialog
pressed := ""
names := []string{"A", "B", "C"}
var buttons []*Button
for i, name := range names {
name := name
b := NewButton(2, 2+i, name)
b.OnClick = func() { pressed = name }
buttons = append(buttons, b)
}
// One of them lives in a nested group, the way rows of buttons often do.
inner := NewGroup(0, 0, 10, 3)
inner.AddItem(buttons[1])
w.AddItem(buttons[0])
w.AddItem(inner)
w.AddItem(buttons[2])
w.Show(scr)
drawn := ""
for i, b := range buttons {
if b.IsEnterDefault() {
if drawn != "" {
t.Fatalf("two buttons are drawn as the default: %s and %s", drawn, names[i])
}
drawn = names[i]
}
}
if !w.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_RETURN}) {
t.Fatal("Enter did nothing")
}
if pressed == "" || pressed != drawn {
t.Fatalf("Enter pressed %q, the default is drawn on %q", pressed, drawn)
}
}