turbo-editors/turbo-corepublic Fork 0
d662cebdb65b319885da903daf7eff9ab1bfbb78
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on d662cebdb65b319885da903daf7eff9ab1bfbb78 · k33g · 19h ago
dialog_test.go · 465 lines · 13.0 KBGo Blame HistoryRaw
  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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package ui

import (
	"strings"
	"testing"

	"github.com/gdamore/tcell/v2"
)

// testDialog builds a dialog with a label, a field and two buttons, laid out
// as the file dialogs actually are.
func testDialog() (*Dialog, *InputLine, *Button, *Button) {
	d := NewDialog("Open a File")
	d.SetBounds(Rect{X: 10, Y: 5, W: 40, H: 8})

	label := NewLabel("Name:")
	label.SetBounds(d.Layout(2, 2, 10, 1))
	field := NewInputLine("main.go")
	field.SetBounds(d.Layout(2, 3, 34, 1))
	ok := NewButton("~O~K", func() { d.Close(ResultOK) })
	ok.Default = true
	ok.SetBounds(d.Layout(10, 6, ButtonWidth("~O~K"), 1))
	cancel := NewButton("~C~ancel", func() { d.Close(ResultCancel) })
	cancel.SetBounds(d.Layout(22, 6, ButtonWidth("~C~ancel"), 1))

	d.Add(label)
	d.Add(field)
	d.Add(ok)
	d.Add(cancel)
	return d, field, ok, cancel
}

func TestADialogStartsOpenWithTheFirstFocusableControlFocused(t *testing.T) {
	d, field, _, _ := testDialog()

	if d.Done() {
		t.Error("Done() = true on a new dialog")
	}
	if d.Result() != ResultNone {
		t.Errorf("Result() = %v, want ResultNone", d.Result())
	}
	if d.Focused() != Focusable(field) {
		t.Error("the focus is not on the field; the label should have been skipped")
	}
	if !field.Focused() {
		t.Error("the focused control was not told it has the focus")
	}
}

func TestTabWalksTheFocusRingSkippingLabels(t *testing.T) {
	d, field, ok, cancel := testDialog()

	d.HandleKey(keyEvent(tcell.KeyTab, 0, tcell.ModNone))
	if d.Focused() != Focusable(ok) {
		t.Error("Tab did not move the focus to the OK button")
	}

	d.HandleKey(keyEvent(tcell.KeyTab, 0, tcell.ModNone))
	if d.Focused() != Focusable(cancel) {
		t.Error("Tab did not move on to Cancel")
	}

	d.HandleKey(keyEvent(tcell.KeyTab, 0, tcell.ModNone))
	if d.Focused() != Focusable(field) {
		t.Error("Tab did not wrap round to the field, skipping the label")
	}

	d.HandleKey(keyEvent(tcell.KeyBacktab, 0, tcell.ModNone))
	if d.Focused() != Focusable(cancel) {
		t.Error("Shift-Tab did not walk the ring backwards")
	}
}

func TestEscapeCancelsTheDialog(t *testing.T) {
	d, _, _, _ := testDialog()

	d.HandleKey(keyEvent(tcell.KeyEscape, 0, tcell.ModNone))

	if !d.Done() || d.Result() != ResultCancel {
		t.Errorf("Result() = %v, want ResultCancel", d.Result())
	}
}

func TestEnterPressesTheDefaultButtonFromAnywhere(t *testing.T) {
	d, _, _, _ := testDialog()
	// The focus is on the input line, which does nothing with Enter.

	d.HandleKey(keyEvent(tcell.KeyEnter, 0, tcell.ModNone))

	if d.Result() != ResultOK {
		t.Errorf("Result() = %v, want the default button to have been pressed", d.Result())
	}
}

func TestAltLetterPressesTheMatchingButton(t *testing.T) {
	d, _, _, _ := testDialog()

	d.HandleKey(keyEvent(tcell.KeyRune, 'c', tcell.ModAlt))

	if d.Result() != ResultCancel {
		t.Errorf("Result() = %v, want Alt-C to have pressed Cancel", d.Result())
	}
}

func TestTypingReachesTheFocusedField(t *testing.T) {
	d, field, _, _ := testDialog()

	d.HandleKey(runeEvent('!'))

	if got := field.Text(); got != "main.go!" {
		t.Errorf("the field holds %q, want the character to have reached it", got)
	}
}

func TestADialogSwallowsEveryKey(t *testing.T) {
	d, _, _, _ := testDialog()

	// A modal dialog must not let anything through to the editor behind it,
	// even a key none of its controls wants.
	if !d.HandleKey(keyEvent(tcell.KeyF9, 0, tcell.ModNone)) {
		t.Error("a key escaped the modal dialog")
	}
}

func TestADialogSwallowsClicksThatMissIt(t *testing.T) {
	d, _, _, _ := testDialog()

	if !d.HandleMouse(clickEvent(0, 0)) {
		t.Error("a click outside the dialog was passed through to what is behind it")
	}
}

func TestClickingAControlFocusesIt(t *testing.T) {
	d, _, _, cancel := testDialog()

	bounds := cancel.Bounds()
	d.HandleMouse(clickEvent(bounds.X+1, bounds.Y))

	if d.Result() != ResultCancel {
		t.Errorf("Result() = %v, want the clicked button to have been pressed", d.Result())
	}
}

func TestOnKeyIsOfferedTheKeyFirst(t *testing.T) {
	d, field, _, _ := testDialog()
	seen := 0
	d.OnKey = func(ev *tcell.EventKey) bool {
		seen++
		return ev.Key() == tcell.KeyF1
	}

	if !d.HandleKey(keyEvent(tcell.KeyF1, 0, tcell.ModNone)) {
		t.Error("OnKey claimed the key but the dialog reported it unhandled")
	}
	d.HandleKey(runeEvent('z'))

	if seen != 2 {
		t.Errorf("OnKey saw %d keys, want 2", seen)
	}
	if got := field.Text(); got != "main.goz" {
		t.Errorf("the field holds %q, want the key OnKey declined to have reached it", got)
	}
}

func TestSetFocusRefusesAControlThatCannotTakeIt(t *testing.T) {
	d, field, _, _ := testDialog()

	d.SetFocus(0) // the label
	if d.Focused() != Focusable(field) {
		t.Error("the focus moved onto a label")
	}

	d.SetFocus(99)
	if d.Focused() != Focusable(field) {
		t.Error("the focus moved onto an index out of range")
	}
}

func TestADialogDrawsItsFrameTitleAndControls(t *testing.T) {
	screen, root := newTestScreen(t, 60, 16)
	d, _, _, _ := testDialog()

	d.Draw(root, testTheme(t))
	screen.Show()

	lines := screenLines(t, screen)
	if !strings.Contains(lines[5], "Open a File") {
		t.Errorf("row 5 = %q, want the dialog's title", lines[5])
	}
	if !strings.Contains(lines[5], "╔") {
		t.Errorf("row 5 = %q, want a double frame", lines[5])
	}
	if !strings.Contains(lines[7], "Name:") {
		t.Errorf("row 7 = %q, want the label", lines[7])
	}
	if !strings.Contains(lines[8], "main.go") {
		t.Errorf("row 8 = %q, want the field's content", lines[8])
	}
	if !strings.Contains(lines[11], "OK") || !strings.Contains(lines[11], "Cancel") {
		t.Errorf("row 11 = %q, want both buttons", lines[11])
	}
}

func TestAnEmptyDialogHandlesKeysWithoutPanicking(t *testing.T) {
	d := NewDialog("Empty")
	d.SetBounds(Rect{X: 0, Y: 0, W: 20, H: 5})

	d.HandleKey(keyEvent(tcell.KeyTab, 0, tcell.ModNone))
	d.HandleKey(keyEvent(tcell.KeyEnter, 0, tcell.ModNone))

	if d.Focused() != nil {
		t.Error("Focused() returned a control from an empty dialog")
	}
}

func TestListBoxSelectionAndScrolling(t *testing.T) {
	items := make([]string, 30)
	for i := range items {
		items[i] = string(rune('a' + i%26))
	}
	l := NewListBox(items)
	l.SetBounds(Rect{X: 0, Y: 0, W: 10, H: 5})
	l.SetFocused(true)

	l.HandleKey(keyEvent(tcell.KeyDown, 0, tcell.ModNone))
	if l.Selected() != 1 {
		t.Errorf("Selected() = %d, want 1", l.Selected())
	}

	l.HandleKey(keyEvent(tcell.KeyEnd, 0, tcell.ModNone))
	if l.Selected() != 29 {
		t.Errorf("Selected() = %d, want the last item", l.Selected())
	}

	l.HandleKey(keyEvent(tcell.KeyPgUp, 0, tcell.ModNone))
	if l.Selected() != 24 {
		t.Errorf("Selected() = %d, want a page back", l.Selected())
	}

	l.HandleKey(keyEvent(tcell.KeyHome, 0, tcell.ModNone))
	if l.Selected() != 0 {
		t.Errorf("Selected() = %d, want the first item", l.Selected())
	}
}

func TestListBoxClampsTheSelectionToTheList(t *testing.T) {
	l := NewListBox([]string{"one", "two"})
	l.SetFocused(true)

	l.Select(99)
	if l.Selected() != 1 {
		t.Errorf("Selected() = %d, want the last item", l.Selected())
	}
	l.Select(-5)
	if l.Selected() != 0 {
		t.Errorf("Selected() = %d, want the first item", l.Selected())
	}
}

func TestAnEmptyListBoxHasNoSelection(t *testing.T) {
	l := NewListBox(nil)
	l.SetFocused(true)

	if got := l.Selected(); got != -1 {
		t.Errorf("Selected() = %d, want -1", got)
	}
	if got := l.SelectedItem(); got != "" {
		t.Errorf("SelectedItem() = %q, want empty", got)
	}
	l.HandleKey(keyEvent(tcell.KeyDown, 0, tcell.ModNone)) // must not panic
	l.Choose()
}

func TestListBoxReportsSelectionsAndChoices(t *testing.T) {
	var selected, chosen []int
	l := NewListBox([]string{"a", "b", "c"})
	l.SetBounds(Rect{X: 0, Y: 0, W: 10, H: 3})
	l.SetFocused(true)
	l.OnSelect = func(i int) { selected = append(selected, i) }
	l.OnChoose = func(i int) { chosen = append(chosen, i) }

	l.HandleKey(keyEvent(tcell.KeyDown, 0, tcell.ModNone))
	l.HandleKey(keyEvent(tcell.KeyEnter, 0, tcell.ModNone))

	if len(selected) != 1 || selected[0] != 1 {
		t.Errorf("OnSelect saw %v, want [1]", selected)
	}
	if len(chosen) != 1 || chosen[0] != 1 {
		t.Errorf("OnChoose saw %v, want [1]", chosen)
	}
}

func TestSetItemsResetsTheView(t *testing.T) {
	l := NewListBox([]string{"a", "b", "c"})
	l.SetBounds(Rect{X: 0, Y: 0, W: 10, H: 2})
	l.SetFocused(true)
	l.Select(2)

	l.SetItems([]string{"x", "y"})

	if l.Selected() != 0 {
		t.Errorf("Selected() = %d, want the first of the new items", l.Selected())
	}
	if l.top != 0 {
		t.Errorf("the view is scrolled to %d, want the top", l.top)
	}
}

func TestListBoxMouseSelectsThenChooses(t *testing.T) {
	chosen := 0
	l := NewListBox([]string{"a", "b", "c"})
	l.SetBounds(Rect{X: 0, Y: 2, W: 10, H: 3})
	l.SetFocused(true)
	l.OnChoose = func(int) { chosen++ }

	l.HandleMouse(clickEvent(1, 3)) // the second line
	if l.Selected() != 1 {
		t.Fatalf("Selected() = %d, want 1", l.Selected())
	}
	if chosen != 0 {
		t.Error("a first click on a line opened it; it should only select")
	}

	l.HandleMouse(clickEvent(1, 3)) // the same line again
	if chosen != 1 {
		t.Error("a second click on the selected line did not open it")
	}
}

func TestListBoxWheelScrolls(t *testing.T) {
	l := NewListBox([]string{"a", "b", "c"})
	l.SetBounds(Rect{X: 0, Y: 0, W: 10, H: 3})

	l.HandleMouse(tcell.NewEventMouse(1, 1, tcell.WheelDown, tcell.ModNone))
	if l.Selected() != 1 {
		t.Errorf("Selected() = %d after a wheel-down, want 1", l.Selected())
	}

	l.HandleMouse(tcell.NewEventMouse(1, 1, tcell.WheelUp, tcell.ModNone))
	if l.Selected() != 0 {
		t.Errorf("Selected() = %d after a wheel-up, want 0", l.Selected())
	}
}

func TestListBoxDrawsItsItemsAndScrollBar(t *testing.T) {
	screen, root := newTestScreen(t, 20, 6)
	l := NewListBox([]string{"alpha", "beta", "gamma", "delta", "epsilon", "zeta"})
	l.SetBounds(Rect{X: 0, Y: 0, W: 12, H: 4})
	l.SetFocused(true)

	l.Draw(root, testTheme(t))
	screen.Show()

	lines := screenLines(t, screen)
	if !strings.HasPrefix(lines[0], "alpha") {
		t.Errorf("row 0 = %q, want the first item", lines[0])
	}
	if !strings.Contains(lines[0], "▲") {
		t.Errorf("row 0 = %q, want the scroll bar's up arrow at the right", lines[0])
	}
	if !strings.Contains(lines[3], "▼") {
		t.Errorf("row 3 = %q, want the scroll bar's down arrow", lines[3])
	}
}

func TestListBoxScrollsToKeepTheSelectionVisible(t *testing.T) {
	l := NewListBox([]string{"a", "b", "c", "d", "e", "f", "g", "h"})
	l.SetBounds(Rect{X: 0, Y: 0, W: 10, H: 3})
	l.SetFocused(true)

	l.Select(7)
	l.scrollToSelection()

	if l.top != 5 {
		t.Errorf("the view is scrolled to %d, want 5 so the last item shows", l.top)
	}
}

func TestTheArrowsReachAListBoxBeforeTheyMoveTheFocus(t *testing.T) {
	// A dialog that grabbed the arrows for its focus ring would leave every
	// list in it — the theme picker, the window list — unusable by keyboard.
	d := NewDialog("Theme")
	d.SetBounds(Rect{X: 0, Y: 0, W: 40, H: 12})

	list := NewListBox([]string{"one", "two", "three"})
	list.SetBounds(d.Layout(2, 2, 30, 5))
	ok := NewButton("~O~K", func() { d.Close(ResultOK) })
	ok.SetBounds(d.Layout(2, 9, ButtonWidth("~O~K"), 1))
	d.Add(list)
	d.Add(ok)

	d.HandleKey(keyEvent(tcell.KeyDown, 0, tcell.ModNone))

	if got := list.Selected(); got != 1 {
		t.Errorf("Selected() = %d, want the arrow to have walked the list", got)
	}
	if d.Focused() != Focusable(list) {
		t.Error("the arrow moved the focus off the list instead of walking it")
	}
}

func TestTheArrowsStillWalkTheRingWhenTheFocusedControlDeclinesThem(t *testing.T) {
	d := NewDialog("Confirm")
	d.SetBounds(Rect{X: 0, Y: 0, W: 40, H: 8})

	yes := NewButton("~Y~es", nil)
	yes.SetBounds(d.Layout(2, 5, ButtonWidth("~Y~es"), 1))
	no := NewButton("~N~o", nil)
	no.SetBounds(d.Layout(12, 5, ButtonWidth("~N~o"), 1))
	d.Add(yes)
	d.Add(no)

	d.HandleKey(keyEvent(tcell.KeyDown, 0, tcell.ModNone))

	if d.Focused() != Focusable(no) {
		t.Error("the arrow did not move the focus between buttons, which have no use for it")
	}
}

func TestMovingADialogTakesItsControlsWithIt(t *testing.T) {
	// Controls are placed in screen coordinates when the dialog is built, so
	// moving the frame on its own would leave every one of them behind.
	d, field, ok, _ := testDialog()
	fieldOffset := field.Bounds().X - d.Bounds().X
	buttonOffset := ok.Bounds().Y - d.Bounds().Y

	d.MoveTo(30, 12)

	if got := d.Bounds(); got.X != 30 || got.Y != 12 {
		t.Fatalf("Bounds() = %+v, want the dialog at (30, 12)", got)
	}
	if got := field.Bounds().X - d.Bounds().X; got != fieldOffset {
		t.Errorf("the field is %d from the frame, want %d", got, fieldOffset)
	}
	if got := ok.Bounds().Y - d.Bounds().Y; got != buttonOffset {
		t.Errorf("the button is %d from the frame, want %d", got, buttonOffset)
	}
}

func TestMovingADialogNowhereChangesNothing(t *testing.T) {
	d, field, _, _ := testDialog()
	before := field.Bounds()

	d.MoveTo(d.Bounds().X, d.Bounds().Y)

	if got := field.Bounds(); got != before {
		t.Errorf("the field moved to %+v from %+v", got, before)
	}
}

func TestCenterInPutsADialogBackInTheMiddle(t *testing.T) {
	d, field, _, _ := testDialog()
	fieldOffset := field.Bounds().X - d.Bounds().X
	screen := Rect{W: 120, H: 40}

	d.CenterIn(screen)

	want := d.Bounds().CenteredIn(screen)
	if got := d.Bounds(); got.X != want.X || got.Y != want.Y {
		t.Errorf("Bounds() = %+v, want it centred at %+v", got, want)
	}
	if got := field.Bounds().X - d.Bounds().X; got != fieldOffset {
		t.Errorf("the field is %d from the frame, want %d", got, fieldOffset)
	}
}