turbo-editors/turbo-corepublic Fork 0
28d59854361aeda8541d853093e732126f3d7bff
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 28d59854361aeda8541d853093e732126f3d7bff · k33g · 15h ago
desktop_test.go · 413 lines · 11.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
package ui

import (
	"strings"
	"testing"

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

// addWindows puts n windows on a desktop and returns them.
func addWindows(d *Desktop, n int) []*Window {
	windows := make([]*Window, n)
	for i := range n {
		windows[i] = NewWindow("w", &stubContent{consume: true})
		windows[i].SetBounds(Rect{X: i * 3, Y: i * 2, W: 20, H: 8})
		d.Add(windows[i])
	}
	return windows
}

func TestAddMakesTheNewWindowActiveAndNumbersThemAll(t *testing.T) {
	d := NewDesktop()

	windows := addWindows(d, 3)

	if d.Active() != windows[2] {
		t.Error("the last window added is not the active one")
	}
	for i, w := range windows {
		if got := w.Number(); got != i+1 {
			t.Errorf("window %d has number %d, want %d", i, got, i+1)
		}
	}
	if windows[0].Active() || windows[1].Active() {
		t.Error("more than one window is marked active")
	}
}

func TestRemoveRenumbersTheRest(t *testing.T) {
	d := NewDesktop()
	windows := addWindows(d, 3)

	if !d.Remove(windows[0]) {
		t.Fatal("Remove() = false for a window that was on the desktop")
	}

	if d.Count() != 2 {
		t.Errorf("Count() = %d, want 2", d.Count())
	}
	if got := windows[1].Number(); got != 1 {
		t.Errorf("the remaining windows were not renumbered: got %d, want 1", got)
	}
	if d.Remove(NewWindow("stranger", nil)) {
		t.Error("Remove() = true for a window that was never added")
	}
}

func TestRemovingTheActiveWindowActivatesTheOneBehind(t *testing.T) {
	d := NewDesktop()
	windows := addWindows(d, 2)

	d.Remove(windows[1])

	if d.Active() != windows[0] || !windows[0].Active() {
		t.Error("the window behind did not become active")
	}
}

func TestFocusBringsAWindowToTheFront(t *testing.T) {
	d := NewDesktop()
	windows := addWindows(d, 3)

	if !d.Focus(windows[0]) {
		t.Fatal("Focus() = false for a window on the desktop")
	}

	if d.Active() != windows[0] {
		t.Error("the focused window is not at the front")
	}
	if got := windows[0].Number(); got != 3 {
		t.Errorf("the raised window has number %d, want 3 — it is now last", got)
	}
}

func TestFocusNumber(t *testing.T) {
	d := NewDesktop()
	windows := addWindows(d, 3)

	if !d.FocusNumber(2) {
		t.Fatal("FocusNumber(2) = false")
	}
	if d.Active() != windows[1] {
		t.Error("FocusNumber raised the wrong window")
	}

	for _, n := range []int{0, -1, 4, 99} {
		if d.FocusNumber(n) {
			t.Errorf("FocusNumber(%d) = true, want false", n)
		}
	}
}

func TestNextCyclesThroughTheWindows(t *testing.T) {
	d := NewDesktop()
	windows := addWindows(d, 3) // front to back: 2, 1, 0

	d.Next()
	if d.Active() != windows[1] {
		t.Error("Next() did not bring the window behind forward")
	}
	d.Next()
	if d.Active() != windows[0] {
		t.Error("a second Next() did not carry on round")
	}
	d.Next()
	if d.Active() != windows[2] {
		t.Error("Next() did not wrap back to the start")
	}
}

func TestNextWithFewerThanTwoWindowsDoesNothing(t *testing.T) {
	d := NewDesktop()
	d.Next() // must not panic on an empty desktop

	windows := addWindows(d, 1)
	d.Next()

	if d.Active() != windows[0] {
		t.Error("Next() disturbed a desktop holding one window")
	}
}

func TestDrawPaintsTheBackdropThenTheWindowsBackToFront(t *testing.T) {
	screen, root := newTestScreen(t, 40, 12)
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 0, W: 40, H: 12})

	back := NewWindow("back", nil)
	back.SetBounds(Rect{X: 0, Y: 0, W: 20, H: 6})
	front := NewWindow("front", nil)
	front.SetBounds(Rect{X: 5, Y: 0, W: 20, H: 6})
	d.Add(back)
	d.Add(front)

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

	lines := screenLines(t, screen)
	if !strings.Contains(lines[11], string(desktopPattern)) {
		t.Errorf("the bottom row is %q, want the desktop pattern", lines[11])
	}
	if !strings.Contains(lines[0], "front") {
		t.Errorf("the top row is %q, want the front window's title visible", lines[0])
	}
	if strings.Contains(lines[0], "back") {
		t.Errorf("the top row is %q, want the back window covered", lines[0])
	}
}

func TestAClickRaisesTheWindowItLandsOn(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 0, W: 40, H: 12})
	back := NewWindow("back", &stubContent{consume: true})
	back.SetBounds(Rect{X: 0, Y: 0, W: 10, H: 6})
	front := NewWindow("front", &stubContent{consume: true})
	front.SetBounds(Rect{X: 20, Y: 0, W: 10, H: 6})
	d.Add(back)
	d.Add(front)

	if !d.HandleMouse(clickEvent(3, 3)) {
		t.Fatal("the click was not handled")
	}
	if d.Active() != back {
		t.Error("clicking a window did not bring it to the front")
	}
}

func TestAClickOnTheBareDesktopIsNotHandled(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 0, W: 40, H: 12})
	addWindows(d, 1)

	if d.HandleMouse(clickEvent(38, 11)) {
		t.Error("a click on the backdrop was reported as handled")
	}
}

func TestADraggedWindowKeepsReceivingEventsThatLeaveIt(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 0, W: 60, H: 20})
	w := NewWindow("t", nil)
	w.SetBounds(Rect{X: 0, Y: 0, W: 10, H: 5})
	d.Add(w)

	d.HandleMouse(clickEvent(5, 0)) // grab the title bar
	// The pointer jumps far outside the window, as a fast drag does.
	d.HandleMouse(tcell.NewEventMouse(40, 12, tcell.Button1, tcell.ModNone))

	if got := w.Bounds(); got.X != 35 || got.Y != 12 {
		t.Errorf("Bounds() = %+v, want the window to have followed the pointer", got)
	}
}

func TestKeysGoToTheActiveWindow(t *testing.T) {
	d := NewDesktop()
	back := &stubContent{consume: true}
	front := &stubContent{consume: true}
	d.Add(NewWindow("back", back))
	d.Add(NewWindow("front", front))

	d.HandleKey(runeEvent('x'))

	if front.keys != 1 || back.keys != 0 {
		t.Errorf("the active window saw %d keys and the other %d, want 1 and 0", front.keys, back.keys)
	}
}

func TestKeysOnAnEmptyDesktopAreNotHandled(t *testing.T) {
	if NewDesktop().HandleKey(runeEvent('x')) {
		t.Error("an empty desktop claimed a key")
	}
}

func TestTilePutsEveryWindowInItsOwnCell(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 1, W: 80, H: 22})
	windows := addWindows(d, 4)

	d.Tile()

	for i, a := range windows {
		for _, b := range windows[i+1:] {
			if !a.Bounds().Intersect(b.Bounds()).IsEmpty() {
				t.Errorf("tiled windows %+v and %+v overlap", a.Bounds(), b.Bounds())
			}
		}
	}
}

func TestTileOnAnEmptyDesktopDoesNothing(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 0, W: 20, H: 10})
	d.Tile() // must not panic
}

func TestCascadeOffsetsEachWindowAndKeepsItsTitleVisible(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 1, W: 80, H: 22})
	windows := addWindows(d, 3)

	d.Cascade()

	for i := 1; i < len(windows); i++ {
		previous, current := windows[i-1].Bounds(), windows[i].Bounds()
		if current.X <= previous.X || current.Y <= previous.Y {
			t.Errorf("window %d at %+v is not offset from %+v", i, current, previous)
		}
	}
}

func TestMaximizeFillsTheDesktop(t *testing.T) {
	d := NewDesktop()
	area := Rect{X: 0, Y: 1, W: 80, H: 22}
	d.SetBounds(area)
	w := addWindows(d, 1)[0]

	d.ToggleMaximize(w)

	if got := w.Bounds(); got != area {
		t.Errorf("Bounds() = %+v, want the whole desktop %+v", got, area)
	}
}

func TestAWindowFollowsTheDesktopWhenTheTerminalGrows(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 1, W: 80, H: 23})
	w := NewWindow("t", nil)
	w.SetBounds(Rect{X: 0, Y: 1, W: 78, H: 23}) // filling it, less the shadow
	d.Add(w)

	d.SetBounds(Rect{X: 0, Y: 1, W: 100, H: 30})

	if got := w.Bounds(); got != (Rect{X: 0, Y: 1, W: 98, H: 30}) {
		t.Errorf("Bounds() = %+v, want the window grown by the same amount as the desktop", got)
	}
}

func TestAWindowFollowsTheDesktopWhenTheTerminalShrinks(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 1, W: 80, H: 23})
	w := NewWindow("t", nil)
	w.SetBounds(Rect{X: 0, Y: 1, W: 78, H: 23})
	d.Add(w)

	d.SetBounds(Rect{X: 0, Y: 1, W: 60, H: 15})

	if got := w.Bounds(); got != (Rect{X: 0, Y: 1, W: 58, H: 15}) {
		t.Errorf("Bounds() = %+v, want the window shrunk with the desktop", got)
	}
}

func TestACascadedWindowKeepsItsOffsetWhileFollowingTheFarCorner(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 1, W: 80, H: 23})
	w := NewWindow("t", nil)
	w.SetBounds(Rect{X: 4, Y: 5, W: 74, H: 19}) // offset from the top left
	d.Add(w)

	d.SetBounds(Rect{X: 0, Y: 1, W: 100, H: 30})

	got := w.Bounds()
	if got.X != 4 || got.Y != 5 {
		t.Errorf("Bounds() = %+v, want the top-left corner left alone", got)
	}
	if got.Right() != 98 || got.Bottom() != 31 {
		t.Errorf("Bounds() = %+v, want the far corner to have kept its distance", got)
	}
}

func TestAWindowThatDoesNotGrowIsOnlyMoved(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 1, W: 80, H: 23})
	w := NewWindow("t", nil)
	w.SetGrow(GrowNone)
	w.SetBounds(Rect{X: 60, Y: 15, W: 20, H: 8})
	d.Add(w)

	d.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 12})

	got := w.Bounds()
	if got.W != 20 {
		t.Errorf("Bounds() = %+v, want its width kept", got)
	}
	if got.Right() > 40 || got.Bottom() > 13 {
		t.Errorf("Bounds() = %+v, want it pulled back inside the smaller desktop", got)
	}
}

func TestNoWindowEndsUpLargerThanTheDesktop(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 1, W: 80, H: 23})
	w := NewWindow("t", nil)
	w.SetGrow(GrowNone) // it does not follow, so only the clamp can save it
	w.SetBounds(Rect{X: 0, Y: 1, W: 78, H: 23})
	d.Add(w)

	d.SetBounds(Rect{X: 0, Y: 1, W: 30, H: 10})

	got := w.Bounds()
	if got.W > 30 || got.H > 10 {
		t.Errorf("Bounds() = %+v, want it held to the desktop's own size", got)
	}
}

func TestAWindowIsNotShrunkBelowItsMinimumWhileTheDesktopCanHoldIt(t *testing.T) {
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 1, W: 80, H: 23})
	w := NewWindow("t", nil)
	w.SetBounds(Rect{X: 0, Y: 1, W: 20, H: 6})
	d.Add(w)

	// Shrinking by more than the window is wide would leave it at nothing.
	d.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 20})

	got := w.Bounds()
	if got.W < MinWindowWidth || got.H < MinWindowHeight {
		t.Errorf("Bounds() = %+v, want at least the minimum window size", got)
	}
}

func TestOnATerminalSmallerThanTheMinimumTheDesktopWins(t *testing.T) {
	// A window may be no larger than the desktop even when that means going
	// below the size it could be dragged down to: overflowing a terminal this
	// small helps nobody, and the painter clips the rest away anyway.
	d := NewDesktop()
	d.SetBounds(Rect{X: 0, Y: 1, W: 80, H: 23})
	w := NewWindow("t", nil)
	w.SetBounds(Rect{X: 0, Y: 1, W: 78, H: 23})
	d.Add(w)

	tiny := Rect{X: 0, Y: 1, W: 4, H: 2}
	d.SetBounds(tiny)

	if got := w.Bounds(); got.W > tiny.W || got.H > tiny.H {
		t.Errorf("Bounds() = %+v, want it to fit inside %+v", got, tiny)
	}
}

func TestALayoutThatChangesNothingLeavesTheWindowsAlone(t *testing.T) {
	// SetBounds runs on every turn of the event loop, so it must be inert when
	// the desktop has not moved. A window the user resized by hand would
	// otherwise creep.
	d := NewDesktop()
	area := Rect{X: 0, Y: 1, W: 80, H: 23}
	d.SetBounds(area)
	w := NewWindow("t", nil)
	w.SetBounds(Rect{X: 5, Y: 5, W: 30, H: 10})
	d.Add(w)

	for range 10 {
		d.SetBounds(area)
	}

	if got := w.Bounds(); got != (Rect{X: 5, Y: 5, W: 30, H: 10}) {
		t.Errorf("Bounds() = %+v, want it untouched", got)
	}
}

func TestADocumentWindowFollowsBothEdgesByDefault(t *testing.T) {
	if got := NewWindow("t", nil).Grow(); got != GrowBoth {
		t.Errorf("Grow() = %v, want GrowBoth", got)
	}
}