1package core_test
2
3import (
4 "context"
5 "image"
6 "reflect"
7 "slices"
8 "testing"
9 "time"
10
11 "github.com/bnema/neferwl/internal/core"
12 "github.com/bnema/neferwl/internal/ports"
13)
14
15type multiRig struct {
16 client chan ports.ClientEvent
17 input chan ports.InputEvent
18 output chan ports.OutputEvent
19 reload chan ports.ConfigChanged
20 commands chan ports.ClientCommand
21 scenes chan []ports.Scene
22 spawn chan ports.SpawnRequest
23 state chan ports.State
24 cfg ports.Config
25}
26
27var (
28 left = ports.OutputInfo{Name: "DP-1", Make: "Acme", Model: "A", Serial: "1", Width: 200, Height: 100}
29 right = ports.OutputInfo{Name: "DP-2", Make: "Acme", Model: "B", Serial: "2", Width: 400, Height: 200}
30 third = ports.OutputInfo{Name: "DP-3", Make: "Acme", Model: "C", Serial: "3", Width: 300, Height: 100}
31)
32
33// startMulti runs core with Alt as Cmd, no border and no gaps, and plugs
34// the given outputs in order.
35func startMulti(t *testing.T, edit func(*ports.Config), outs ...ports.OutputInfo) *multiRig {
36 t.Helper()
37 return startRig(t, false, edit, outs...)
38}
39
40// startRig is startMulti; terminal enables the configured automatic-open policy.
41func startRig(t *testing.T, terminal bool, edit func(*ports.Config), outs ...ports.OutputInfo) *multiRig {
42 t.Helper()
43 return startRigWith(t, core.Options{Terminal: terminal}, edit, outs...)
44}
45
46// startRigWith is startRig with explicit core options.
47func startRigWith(t *testing.T, opts core.Options, edit func(*ports.Config), outs ...ports.OutputInfo) *multiRig {
48 t.Helper()
49 cfg := altCmdDefaults()
50 cfg.Border.Width = 0
51 if edit != nil {
52 edit(&cfg)
53 }
54 r := &multiRig{
55 client: make(chan ports.ClientEvent, 16), input: make(chan ports.InputEvent, 16),
56 output: make(chan ports.OutputEvent, 4), reload: make(chan ports.ConfigChanged, 4),
57 commands: make(chan ports.ClientCommand, 1024), scenes: make(chan []ports.Scene, 1), spawn: make(chan ports.SpawnRequest, 16), state: make(chan ports.State, 1), cfg: cfg,
58 }
59 c, err := core.New(cfg, core.Channels{Client: r.client, Input: r.input, Output: r.output, Config: r.reload, Commands: r.commands, Scenes: r.scenes, Spawn: r.spawn, State: r.state}, opts)
60 if err != nil {
61 t.Fatal(err)
62 }
63 ctx, cancel := context.WithCancel(context.Background())
64 done := make(chan error, 1)
65 go func() { done <- c.Run(ctx) }()
66 t.Cleanup(func() { cancel(); <-done })
67 for _, o := range outs {
68 r.plug(t, o)
69 }
70 return r
71}
72
73func (r *multiRig) plug(t *testing.T, o ports.OutputInfo) []ports.Scene {
74 t.Helper()
75 r.output <- ports.OutputAdded{Info: o}
76 return receive(t, r.scenes)
77}
78
79func (r *multiRig) key(t *testing.T, sym string, mods ports.Mods) []ports.Scene {
80 t.Helper()
81 r.input <- ports.KeyEvent{Keysym: sym, Mods: mods, Pressed: true}
82 s := receive(t, r.scenes)
83 r.input <- ports.KeyEvent{Keysym: sym, Mods: mods}
84 return s
85}
86
87func (r *multiRig) mapWindow(t *testing.T, id ports.WindowID) []ports.Scene {
88 t.Helper()
89 r.client <- ports.WindowMapped{ID: id}
90 return receive(t, r.scenes)
91}
92
93// shown returns the visible window IDs of each output, by connector.
94func shown(set []ports.Scene) map[string][]ports.WindowID {
95 out := map[string][]ports.WindowID{}
96 for _, s := range set {
97 out[s.Output] = []ports.WindowID{}
98 for _, w := range s.Windows {
99 if !w.Hidden {
100 out[s.Output] = append(out[s.Output], w.ID)
101 }
102 }
103 }
104 return out
105}
106
107func lastOutputs(t *testing.T, ch chan ports.ClientCommand) ports.SetOutputs {
108 t.Helper()
109 var last ports.SetOutputs
110 found := false
111 for len(ch) > 0 {
112 if v, ok := (<-ch).(ports.SetOutputs); ok {
113 last, found = v, true
114 }
115 }
116 if !found {
117 t.Fatal("no SetOutputs")
118 }
119 return last
120}
121
122func TestOutputsLeftToRight(t *testing.T) {
123 r := startMulti(t, func(c *ports.Config) {
124 c.Outputs = []ports.OutputConfig{{Name: "DP-2", Scale: 2}, {Name: "DP-1"}}
125 }, left, right)
126 out := lastOutputs(t, r.commands)
127 // Config order wins: DP-2 (200x100 logical at scale 2), then DP-1.
128 if len(out.Outputs) != 2 || out.Outputs[0].Info.Name != "DP-2" || out.Outputs[0].X != 0 || out.Outputs[0].Width != 200 ||
129 out.Outputs[1].Info.Name != "DP-1" || out.Outputs[1].X != 200 || out.Focused != "DP-1" {
130 t.Fatalf("%+v", out)
131 }
132}
133
134func TestLayoutPerOutput(t *testing.T) {
135 both(t, func(t *testing.T, animated bool) {
136 r := startLanding(t, animated, func(c *ports.Config) {
137 c.Layout.MaxColumns = 2
138 c.Layout.Outputs = []ports.OutputLayout{{Output: "Acme B 2", MaxColumns: 4, Overflow: "fixed"}}
139 }, left, right)
140 layoutPerOutput(t, r)
141 })
142}
143
144func layoutPerOutput(t *testing.T, r *landRig) {
145 r.mapLanded(t, 1)
146 r.keyLanded(t, "Right", ports.ModAlt|ports.ModCtrl)
147 var set []ports.Scene
148 for id := ports.WindowID(2); id <= 6; id++ {
149 set = r.mapLanded(t, id)
150 }
151 // DP-2: four columns of 100; the fifth window splits the last one
152 // (fixed) instead of scrolling.
153 if got := shown(set); len(got["DP-2"]) != 5 {
154 t.Fatal(got)
155 }
156 for _, s := range set {
157 for _, w := range s.Windows {
158 // DP-1 keeps the default: one window of two columns fills it.
159 if want := map[string]int{"DP-1": 200, "DP-2": 100}[s.Output]; w.Rect.W != want {
160 t.Fatalf("%s: %+v", s.Output, w)
161 }
162 }
163 }
164}
165
166// widths returns the window widths on one output.
167func widths(set []ports.Scene, output string) []int {
168 var out []int
169 for _, s := range set {
170 for _, w := range s.Windows {
171 if s.Output == output {
172 out = append(out, w.Rect.W)
173 }
174 }
175 }
176 return out
177}
178
179func TestLayoutPerOutputFollowsMonitorAndReload(t *testing.T) {
180 both(t, func(t *testing.T, animated bool) {
181 r := startLanding(t, animated, func(c *ports.Config) {
182 c.Layout.MaxColumns = 1
183 c.Layout.Outputs = []ports.OutputLayout{{Output: "Acme B 2", LayoutRules: ports.LayoutRules{MaxColumns: 2}}}
184 }, right)
185 layoutPerOutputFollowsMonitorAndReload(t, r)
186 })
187}
188
189func layoutPerOutputFollowsMonitorAndReload(t *testing.T, r *landRig) {
190 r.mapLanded(t, 1)
191 if got := widths(r.mapLanded(t, 2), "DP-2"); !slices.Equal(got, []int{200, 200}) {
192 t.Fatal(got)
193 }
194 // Another monitor on DP-2: the key rule no longer applies.
195 other := right
196 other.Serial = "3"
197 if got := widths(r.land(r.plug(t, other)), "DP-2"); !slices.Equal(got, []int{400, 400}) {
198 t.Fatal(got)
199 }
200 // Back to the first monitor, then a reload drops the rule.
201 r.land(r.plug(t, right))
202 cfg := r.cfg
203 cfg.Layout.Outputs = nil
204 r.reload <- ports.ConfigChanged{Config: cfg}
205 if got := widths(r.land(receive(t, r.scenes)), "DP-2"); !slices.Equal(got, []int{400, 400}) {
206 t.Fatal(got)
207 }
208}
209
210func TestExplicitOutputPositionAndAutomaticFallback(t *testing.T) {
211 r := startMulti(t, func(c *ports.Config) {
212 c.Outputs = []ports.OutputConfig{{Name: "DP-1"}, {Name: "DP-2", Pos: &image.Point{X: 100, Y: 40}}}
213 }, left, right)
214 out := lastOutputs(t, r.commands)
215 if len(out.Outputs) != 2 || out.Outputs[0].X != 500 || out.Outputs[0].Y != 0 || out.Outputs[1].X != 100 || out.Outputs[1].Y != 40 {
216 t.Fatalf("unexpected positions: %+v", out.Outputs)
217 }
218 r.cfg.Outputs[1].Pos = &image.Point{X: -200, Y: 12}
219 r.reload <- ports.ConfigChanged{Config: r.cfg}
220 _ = receive(t, r.scenes)
221 out = lastOutputs(t, r.commands)
222 if out.Outputs[0].X != 200 || out.Outputs[1].X != -200 || out.Outputs[1].Y != 12 {
223 t.Fatalf("unexpected positions after reload: %+v", out.Outputs)
224 }
225}
226
227func TestRelativeOutputPlacement(t *testing.T) {
228 r := startMulti(t, func(c *ports.Config) {
229 c.Outputs = []ports.OutputConfig{
230 {Name: "DP-1"},
231 {Name: "DP-2", Anchor: ports.OutputAnchor{Relation: ports.RelationBelow, To: "DP-1", Offset: 30}},
232 {Name: "DP-3", Anchor: ports.OutputAnchor{Relation: ports.RelationRightOf, To: "DP-2", Offset: -10}},
233 }
234 }, left, right, third)
235 positions := func() [][2]int {
236 t.Helper()
237 var got [][2]int
238 for _, o := range lastOutputs(t, r.commands).Outputs {
239 got = append(got, [2]int{o.X, o.Y})
240 }
241 return got
242 }
243 // DP-1 is 200x100, DP-2 400x200 logical.
244 if got, want := positions(), [][2]int{{0, 0}, {30, 100}, {430, 90}}; !slices.Equal(got, want) {
245 t.Fatalf("positions %v, want %v", got, want)
246 }
247 // A reload with another offset moves the output.
248 r.cfg.Outputs[1].Anchor.Offset = -50
249 r.reload <- ports.ConfigChanged{Config: r.cfg}
250 _ = receive(t, r.scenes)
251 if got, want := positions(), [][2]int{{0, 0}, {-50, 100}, {350, 90}}; !slices.Equal(got, want) {
252 t.Fatalf("positions after offset reload %v, want %v", got, want)
253 }
254 // Scaling the reference down moves the output below it.
255 r.cfg.Outputs[0].Scale = 2
256 r.reload <- ports.ConfigChanged{Config: r.cfg}
257 _ = receive(t, r.scenes)
258 if got, want := positions(), [][2]int{{0, 0}, {-50, 50}, {350, 40}}; !slices.Equal(got, want) {
259 t.Fatalf("positions after scale reload %v, want %v", got, want)
260 }
261}
262
263func TestRelativeOutputWithoutReferenceIsAutomatic(t *testing.T) {
264 r := startMulti(t, func(c *ports.Config) {
265 c.Outputs = []ports.OutputConfig{{Name: "DP-2", Anchor: ports.OutputAnchor{Relation: ports.RelationRightOf, To: "DP-1", Offset: 25}}}
266 }, right)
267 if out := lastOutputs(t, r.commands).Outputs; len(out) != 1 || out[0].X != 0 || out[0].Y != 0 {
268 t.Fatalf("without reference: %+v", out)
269 }
270 // Plugging the reference restores the relation (DP-1 is 200 wide).
271 r.plug(t, left)
272 out := lastOutputs(t, r.commands).Outputs
273 if len(out) != 2 || out[0].Info.Name != "DP-2" || out[1].Info.Name != "DP-1" {
274 t.Fatalf("outputs %+v", out)
275 }
276 if out[0].X != 200 || out[0].Y != 25 || out[1].X != 0 || out[1].Y != 0 {
277 t.Fatalf("placement %+v", out)
278 }
279}
280
281func TestWindowsOpenOnFocusedOutput(t *testing.T) {
282 r := startMulti(t, nil, left, right)
283 r.mapWindow(t, 1)
284 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
285 set := r.mapWindow(t, 2)
286 if got := shown(set); len(got["DP-1"]) != 1 || got["DP-1"][0] != 1 || len(got["DP-2"]) != 1 || got["DP-2"][0] != 2 {
287 t.Fatal(got)
288 }
289 // Only the focused output draws a focused window.
290 for _, s := range set {
291 for _, w := range s.Windows {
292 if w.Focused != (s.Output == "DP-2") {
293 t.Fatalf("%s: %+v", s.Output, w)
294 }
295 }
296 }
297}
298
299func TestFocusColumnCrossesOutputs(t *testing.T) {
300 r := startMulti(t, nil, left, right)
301 r.mapWindow(t, 1)
302 // The only column is the edge: focus moves to the right output.
303 r.key(t, "Right", ports.ModAlt)
304 r.mapWindow(t, 2)
305 for len(r.commands) > 0 {
306 <-r.commands
307 }
308 r.key(t, "Left", ports.ModAlt)
309 var focus ports.WindowID
310 for len(r.commands) > 0 {
311 if v, ok := (<-r.commands).(ports.FocusWindow); ok {
312 focus = v.ID
313 }
314 }
315 if focus != 1 {
316 t.Fatal(focus)
317 }
318}
319
320func TestPointerCrossesOutputs(t *testing.T) {
321 both(t, func(t *testing.T, animated bool) {
322 pointerCrossesOutputs(t, startLanding(t, animated, nil, left, right))
323 })
324}
325
326func pointerCrossesOutputs(t *testing.T, r *landRig) {
327 r.mapLanded(t, 1)
328 r.keyLanded(t, "Right", ports.ModAlt|ports.ModCtrl)
329 r.mapLanded(t, 2)
330 for len(r.commands) > 0 {
331 <-r.commands
332 }
333 // DP-2 starts at x 200: global (250, 50) is (50, 50) on window 2.
334 r.input <- ports.PointerMotion{X: 250, Y: 50}
335 if v := command(t, r.commands); v != (ports.PointerFocus{ID: 2, X: 50, Y: 50}) {
336 t.Fatal(v)
337 }
338 // Back on DP-1, then below it (100 high): off every output, y stays on DP-1.
339 r.input <- ports.PointerMotion{X: 10, Y: 50}
340 r.input <- ports.PointerMotion{X: 10, Y: 150}
341 for {
342 if v, ok := command(t, r.commands).(ports.PointerMotionTo); ok && v.Y == 99 {
343 if v.ID != 1 || v.X != 10 {
344 t.Fatal(v)
345 }
346 break
347 }
348 }
349 // A click focuses the output under the pointer.
350 r.input <- ports.PointerButton{Button: 0x110, Pressed: true}
351 set := receive(t, r.scenes)
352 for _, s := range set {
353 for _, w := range s.Windows {
354 if w.Focused != (w.ID == 1) {
355 t.Fatalf("%s: %+v", s.Output, w)
356 }
357 }
358 }
359}
360
361func TestMoveWorkspaceToMonitor(t *testing.T) {
362 r := startMulti(t, nil, left, right)
363 r.mapWindow(t, 1)
364 set := r.key(t, "Right", ports.ModAlt|ports.ModCtrl|ports.ModShift)
365 if got := shown(set); len(got["DP-1"]) != 0 || len(got["DP-2"]) != 1 {
366 t.Fatal(got)
367 }
368 // It is DP-2's now: unplugging DP-1 does not move it.
369 r.output <- ports.OutputRemoved{Name: "DP-1"}
370 set = receive(t, r.scenes)
371 if len(set) != 1 || set[0].Output != "DP-2" || len(shown(set)["DP-2"]) != 1 {
372 t.Fatal(shown(set))
373 }
374}
375
376func TestMonitorActionsFollowGeometry(t *testing.T) {
377 r := startMulti(t, func(c *ports.Config) {
378 c.Outputs = []ports.OutputConfig{
379 {Name: "DP-1"},
380 {Name: "DP-2", Anchor: ports.OutputAnchor{Relation: ports.RelationBelow, To: "DP-1"}},
381 }
382 // Without a default bind; these replace move-workspace-prev/next.
383 c.Binds["Cmd+Ctrl+Shift+Up"] = string(core.ActionMoveWorkspaceToMonitorUp)
384 c.Binds["Cmd+Ctrl+Shift+Down"] = string(core.ActionMoveWorkspaceToMonitorDown)
385 }, left, right)
386 // SetOutputs is sent only when the focus or layout changed.
387 current := lastOutputs(t, r.commands).Focused
388 focused := func() string {
389 for len(r.commands) > 0 {
390 if v, ok := (<-r.commands).(ports.SetOutputs); ok {
391 current = v.Focused
392 }
393 }
394 return current
395 }
396 cmdCtrl := ports.ModAlt | ports.ModCtrl
397 // DP-1 is focused and has no neighbor left, right or up.
398 r.mapWindow(t, 1)
399 for _, k := range []string{"Left", "Right", "Up"} {
400 r.key(t, k, cmdCtrl)
401 if got := focused(); got != "DP-1" {
402 t.Fatalf("%s moved focus to %s", k, got)
403 }
404 }
405 r.key(t, "Down", cmdCtrl)
406 if got := focused(); got != "DP-2" {
407 t.Fatalf("focus-monitor-down: %s", got)
408 }
409 r.key(t, "k", cmdCtrl)
410 if got := focused(); got != "DP-1" {
411 t.Fatalf("focus-monitor-up (vim twin): %s", got)
412 }
413 // The workspace goes to the monitor below and focus follows it.
414 set := r.key(t, "Down", cmdCtrl|ports.ModShift)
415 if got := shown(set); len(got["DP-1"]) != 0 || len(got["DP-2"]) != 1 {
416 t.Fatal(got)
417 }
418 set = r.key(t, "Up", cmdCtrl|ports.ModShift)
419 if got := shown(set); len(got["DP-1"]) != 1 || len(got["DP-2"]) != 0 {
420 t.Fatalf("move-workspace-to-monitor-up: %v", got)
421 }
422}
423
424func TestUnplugMovesWorkspacesAndReplugReturnsThem(t *testing.T) {
425 both(t, func(t *testing.T, animated bool) {
426 r := startLanding(t, animated, nil, left, right)
427 r.mapLanded(t, 1)
428 r.keyLanded(t, "Right", ports.ModAlt|ports.ModCtrl)
429 r.mapLanded(t, 2)
430 // Unplug DP-2 while it is focused: its workspace joins DP-1 below its own.
431 r.output <- ports.OutputRemoved{Name: "DP-2"}
432 set := r.land(receive(t, r.scenes))
433 if len(set) != 1 || len(shown(set)["DP-1"]) != 1 || shown(set)["DP-1"][0] != 1 {
434 t.Fatal(shown(set))
435 }
436 // Cmd+2 shows the guest.
437 r.input <- ports.KeyEvent{Keysym: "2", Keycode: 3, Mods: ports.ModAlt, Pressed: true}
438 set = r.land(receive(t, r.scenes))
439 r.input <- ports.KeyEvent{Keysym: "2", Keycode: 3, Mods: ports.ModAlt}
440 if got := shown(set)["DP-1"]; len(got) != 1 || got[0] != 2 {
441 t.Fatal(got)
442 }
443 // Replugged while on screen: the guest stays until the user leaves it.
444 set = r.land(r.plug(t, right))
445 if got := shown(set); len(got["DP-1"]) != 1 || got["DP-1"][0] != 2 || len(got["DP-2"]) != 0 {
446 t.Fatal(got)
447 }
448 r.input <- ports.KeyEvent{Keysym: "1", Keycode: 2, Mods: ports.ModAlt, Pressed: true}
449 set = r.land(receive(t, r.scenes))
450 if got := shown(set); len(got["DP-1"]) != 1 || got["DP-1"][0] != 1 || len(got["DP-2"]) != 1 || got["DP-2"][0] != 2 {
451 t.Fatal(got)
452 }
453 })
454}
455
456func TestReplugReturnsHiddenGuestAtOnce(t *testing.T) {
457 r := startMulti(t, nil, left, right)
458 r.mapWindow(t, 1)
459 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
460 r.mapWindow(t, 2)
461 r.key(t, "Left", ports.ModAlt|ports.ModCtrl)
462 r.output <- ports.OutputRemoved{Name: "DP-2"}
463 receive(t, r.scenes)
464 // Not on screen: it goes home as soon as DP-2 is back.
465 set := r.plug(t, right)
466 if got := shown(set); len(got["DP-2"]) != 1 || got["DP-2"][0] != 2 {
467 t.Fatal(got)
468 }
469}
470
471func TestNamedWorkspaceHomeChangeAppliesLive(t *testing.T) {
472 r := startMulti(t, func(c *ports.Config) {
473 c.Workspaces = []ports.WorkspaceConfig{{Name: "web", Monitor: "DP-1"}}
474 c.Binds["Alt+w"] = "workspace web"
475 }, left, right)
476 r.key(t, "w", ports.ModAlt)
477 r.mapWindow(t, 1)
478 r.key(t, "w", ports.ModAlt)
479 r.cfg.Workspaces[0].Monitor = "DP-2"
480 r.reload <- ports.ConfigChanged{Config: r.cfg}
481 receive(t, r.scenes)
482 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
483 if got := shown(r.key(t, "w", ports.ModAlt)); len(got["DP-1"]) != 0 || len(got["DP-2"]) != 1 {
484 t.Fatal(got)
485 }
486}
487
488func TestNamedWorkspaceOnItsMonitor(t *testing.T) {
489 r := startMulti(t, func(c *ports.Config) {
490 c.Workspaces = []ports.WorkspaceConfig{{Name: "web", Monitor: "DP-2"}}
491 c.Binds["Alt+w"] = "workspace web"
492 }, left)
493 // DP-2 is not plugged: web lives on DP-1 as a guest.
494 r.key(t, "w", ports.ModAlt)
495 r.mapWindow(t, 1)
496 r.key(t, "w", ports.ModAlt)
497 // DP-2 arrives: web goes home (not on screen).
498 r.plug(t, right)
499 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
500 set := r.key(t, "w", ports.ModAlt)
501 if got := shown(set); len(got["DP-2"]) != 1 || got["DP-2"][0] != 1 {
502 t.Fatal(got)
503 }
504}
505
506func TestLayersStayOnTheirOutput(t *testing.T) {
507 r := startMulti(t, nil, left, right)
508 bar := ports.LayerSurface{ID: 9, Layer: ports.LayerTop, Anchor: ports.AnchorTop | ports.AnchorLeft | ports.AnchorRight, Height: 10, ExclusiveZone: 10, Output: "DP-2"}
509 r.client <- ports.LayerChanged{Layers: []ports.LayerSurface{bar}}
510 set := receive(t, r.scenes)
511 for _, s := range set {
512 if (len(s.Layers) == 1) != (s.Output == "DP-2") {
513 t.Fatalf("%s: %+v", s.Output, s.Layers)
514 }
515 }
516 if set[1].Layers[0].Rect != (ports.Rect{W: 400, H: 10}) {
517 t.Fatal(set[1].Layers)
518 }
519}
520
521func TestScaleBindTargetsFocusedOutput(t *testing.T) {
522 r := startMulti(t, func(c *ports.Config) { c.Binds["Alt+equal"] = "scale-up" }, left, right)
523 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
524 set := r.key(t, "equal", ports.ModAlt)
525 if set[0].Scale != 1 || set[1].Scale == 1 {
526 t.Fatal(set[0].Scale, set[1].Scale)
527 }
528}
529
530func TestLastOutputUnpluggedKeepsWindows(t *testing.T) {
531 r := startMulti(t, nil, left)
532 r.mapWindow(t, 1)
533 r.output <- ports.OutputRemoved{Name: "DP-1"}
534 if set := receive(t, r.scenes); len(set) != 1 || set[0].Output != "" {
535 t.Fatal(shown(set))
536 }
537 if out := lastOutputs(t, r.commands); len(out.Outputs) != 0 {
538 t.Fatalf("%+v", out)
539 }
540 // Another monitor takes the windows over.
541 set := r.plug(t, right)
542 if got := shown(set); len(got) != 1 || len(got["DP-2"]) != 1 || got["DP-2"][0] != 1 {
543 t.Fatal(got)
544 }
545}
546
547func TestModeChangeKeepsScreen(t *testing.T) {
548 r := startMulti(t, nil, left, right)
549 r.mapWindow(t, 1)
550 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
551 r.mapWindow(t, 2)
552 // A new mode on DP-2 is only a new size: nothing moves, focus stays.
553 bigger := right
554 bigger.Width, bigger.Height = 800, 400
555 set := r.plug(t, bigger)
556 if got := shown(set); len(got["DP-2"]) != 1 || got["DP-2"][0] != 2 || len(got["DP-1"]) != 1 {
557 t.Fatal(got)
558 }
559 if out := lastOutputs(t, r.commands); out.Focused != "DP-2" || out.Outputs[1].Width != 800 {
560 t.Fatalf("%+v", out)
561 }
562}
563
564func TestPrimaryOutputGetsFocusAtStartup(t *testing.T) {
565 r := startMulti(t, func(c *ports.Config) {
566 c.Outputs = []ports.OutputConfig{{Name: "DP-1"}, {Name: "DP-2", Primary: true}}
567 }, left, right)
568 out := lastOutputs(t, r.commands)
569 // Placed right of DP-1, but focused.
570 if out.Focused != "DP-2" || out.Outputs[1].Info.Name != "DP-2" || !out.Outputs[1].Primary || out.Outputs[0].Primary {
571 t.Fatalf("%+v", out)
572 }
573 set := r.mapWindow(t, 1)
574 if got := shown(set); len(got["DP-2"]) != 1 {
575 t.Fatal(got)
576 }
577}
578
579func TestMoveColumnCrossesOutputs(t *testing.T) {
580 r := startMulti(t, nil, left, right)
581 r.mapWindow(t, 1)
582 r.mapWindow(t, 2)
583 // Window 2 is the right column of DP-1: moving right first hits the
584 // edge, then the column goes to DP-2 and focus follows it.
585 set := r.key(t, "Right", ports.ModAlt|ports.ModShift)
586 if got := shown(set); len(got["DP-1"]) != 1 || got["DP-1"][0] != 1 || len(got["DP-2"]) != 1 || got["DP-2"][0] != 2 {
587 t.Fatal(got)
588 }
589 if out := lastOutputs(t, r.commands); out.Focused != "DP-2" {
590 t.Fatal(out.Focused)
591 }
592 // Back left (no terminal kept here, so a last column may move): it
593 // lands on the right side of DP-1.
594 set = r.key(t, "Left", ports.ModAlt|ports.ModShift)
595 if got := shown(set)["DP-1"]; len(got) != 2 {
596 t.Fatal(got)
597 }
598 for _, s := range set {
599 for _, w := range s.Windows {
600 if w.ID == 2 && (s.Output != "DP-1" || !w.Focused || w.Rect.X == 0) {
601 t.Fatalf("%s %+v", s.Output, w)
602 }
603 }
604 }
605}
606
607func TestPointerFocusesOutput(t *testing.T) {
608 r := startMulti(t, nil, left, right)
609 r.input <- ports.PointerMotion{X: 250, Y: 50}
610 receive(t, r.scenes)
611 if out := lastOutputs(t, r.commands); out.Focused != "DP-2" {
612 t.Fatal(out.Focused)
613 }
614 // New windows open where the pointer is.
615 if got := shown(r.mapWindow(t, 1)); len(got["DP-2"]) != 1 {
616 t.Fatal(got)
617 }
618}
619
620// The first terminal cannot launch before wayland receives its output layout.
621func TestFirstTerminalFollowsSetOutputs(t *testing.T) {
622 cfg := scrollDefaults()
623 commands := make(chan ports.ClientCommand)
624 // The launcher handoff is deliberately nonblocking. A ready buffered
625 // queue tests ordering without requiring core to wait for this reader.
626 spawn := make(chan ports.SpawnRequest, 1)
627 output := make(chan ports.OutputEvent, 1)
628 scenes := make(chan []ports.Scene, 1)
629 state := make(chan ports.State, 1)
630 c, err := core.New(cfg, core.Channels{Output: output, Commands: commands, Spawn: spawn, Scenes: scenes, State: state}, core.Options{Terminal: true})
631 if err != nil {
632 t.Fatal(err)
633 }
634 ctx, cancel := context.WithCancel(context.Background())
635 done := make(chan error, 1)
636 go func() { done <- c.Run(ctx) }()
637 t.Cleanup(func() { cancel(); receive(t, done) })
638 receive(t, state) // startup finished; next command belongs to the output event
639 output <- ports.OutputAdded{Info: left}
640 select {
641 case v := <-commands:
642 if o, ok := v.(ports.SetOutputs); !ok || len(o.Outputs) != 1 || o.Outputs[0].Info.Name != left.Name {
643 t.Fatalf("first command: %v", v)
644 }
645 case req := <-spawn:
646 t.Fatalf("terminal spawned before SetOutputs: %v", req)
647 case <-time.After(time.Second):
648 t.Fatal("no output command or spawn")
649 }
650 if v, ok := receive(t, commands).(ports.SlotsPending); !ok || !v.Pending {
651 t.Fatalf("expected pending before spawn, got %v", v)
652 }
653 if req := receive(t, spawn); len(req.Argv) == 0 {
654 t.Fatal(req)
655 }
656 receive(t, scenes)
657}
658
659func TestTerminalAutoOpenFirstOnly(t *testing.T) {
660 r := startRig(t, true, nil, left)
661 first := receive(t, r.spawn)
662 if first.Argv[0] != "foot" {
663 t.Fatal(first)
664 }
665 r.key(t, "Next", ports.ModAlt)
666 if len(r.spawn) != 0 {
667 t.Fatal("opened a terminal on the second workspace")
668 }
669}
670
671func TestTerminalAutoOpenOff(t *testing.T) {
672 r := startRig(t, true, func(c *ports.Config) { c.Terminal.AutoOpen = "off" }, left)
673 if len(r.spawn) != 0 {
674 t.Fatal("opened a terminal with auto-open off")
675 }
676 r.key(t, "Return", ports.ModAlt)
677 if req := receive(t, r.spawn); req.Argv[0] != "foot" || len(req.Env) != 0 {
678 t.Fatal(req)
679 }
680}
681
682func TestTerminalAutoOpenFirstSkipsPopulatedInitialWorkspace(t *testing.T) {
683 r := startRig(t, false, nil, left)
684 r.mapWindow(t, 1)
685 r.cfg.Terminal.AutoOpen = "first"
686 r.reload <- ports.ConfigChanged{Config: r.cfg}
687 receive(t, r.scenes)
688 r.key(t, "Next", ports.ModAlt)
689 if len(r.spawn) != 0 {
690 t.Fatal("opened a terminal after the initial workspace opportunity passed")
691 }
692}
693
694func TestTerminalAutoOpenFirstDoesNotTargetLaterWorkspace(t *testing.T) {
695 r := startRig(t, false, nil, left)
696 r.key(t, "Next", ports.ModAlt)
697 r.cfg.Terminal.AutoOpen = "first"
698 r.reload <- ports.ConfigChanged{Config: r.cfg}
699 receive(t, r.scenes)
700 if len(r.spawn) != 0 {
701 t.Fatal("opened the first terminal outside workspace 1")
702 }
703}
704
705func TestTerminalAutoOpenFirstSurvivesReloadBeforeOutput(t *testing.T) {
706 r := startRig(t, true, nil)
707 r.reload <- ports.ConfigChanged{Config: r.cfg}
708 receive(t, r.scenes)
709 r.plug(t, left)
710 if req := receive(t, r.spawn); req.Argv[0] != "foot" {
711 t.Fatal(req)
712 }
713}
714
715func TestTerminalAutoOpenAllThenFirstDoesNotOpenAgain(t *testing.T) {
716 r := startRig(t, true, func(c *ports.Config) { c.Terminal.AutoOpen = "all" }, left)
717 req := receive(t, r.spawn)
718 token := req.Env[0][len(ports.SlotEnv)+1:]
719 r.client <- ports.WindowMapped{ID: 1, Slot: token}
720 receive(t, r.scenes)
721 r.cfg.Terminal.AutoOpen = "first"
722 r.reload <- ports.ConfigChanged{Config: r.cfg}
723 receive(t, r.scenes)
724 r.client <- ports.WindowUnmapped{ID: 1}
725 receive(t, r.scenes)
726 if len(r.spawn) != 0 {
727 t.Fatal("opened a second automatic terminal after all-to-first reload")
728 }
729}
730
731func TestEmptyWorkspaceGetsTerminal(t *testing.T) {
732 r := startRig(t, true, func(c *ports.Config) { c.Terminal.AutoOpen = "all" }, left, right)
733 // One terminal per output, each tagged for its workspace.
734 var reqs []ports.SpawnRequest
735 for len(reqs) < 2 {
736 reqs = append(reqs, receive(t, r.spawn))
737 }
738 if len(r.spawn) != 0 || reqs[0].Argv[0] != "foot" || reqs[0].Env[0] == reqs[1].Env[0] {
739 t.Fatal(reqs, len(r.spawn))
740 }
741 token := func(req ports.SpawnRequest) string { return req.Env[0][len(ports.SlotEnv)+1:] }
742 // The focused screen is DP-2 now; DP-1's terminal still lands on DP-1.
743 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
744 r.client <- ports.WindowMapped{ID: 1, Slot: token(reqs[0])}
745 r.client <- ports.WindowMapped{ID: 2, Slot: token(reqs[1])}
746 receive(t, r.scenes)
747 set := receive(t, r.scenes)
748 if got := shown(set); len(got["DP-1"]) != 1 || len(got["DP-2"]) != 1 || got["DP-1"][0] == got["DP-2"][0] {
749 t.Fatal(got)
750 }
751 // A terminal that exits right after mapping is not respawned at once
752 // (no loop for a broken terminal command).
753 r.client <- ports.WindowUnmapped{ID: 2}
754 receive(t, r.scenes)
755 r.key(t, "Left", ports.ModAlt|ports.ModCtrl)
756 if len(r.spawn) != 0 {
757 t.Fatal("respawned at once")
758 }
759}
760
761// A terminal that exits is not respawned before termRetry has passed on
762// the injected clock, and is once it has.
763func TestTerminalRetryFollowsClock(t *testing.T) {
764 clk := newStepClock(t)
765 r := startRigWith(t, core.Options{Terminal: true, Clock: clk.clock}, func(c *ports.Config) { c.Terminal.AutoOpen = "all" }, left)
766 first := receive(t, r.spawn)
767 r.client <- ports.WindowMapped{ID: 1, Slot: token(t, first)}
768 receive(t, r.scenes)
769 r.client <- ports.WindowUnmapped{ID: 1}
770 receive(t, r.scenes)
771 if len(r.spawn) != 0 {
772 t.Fatal("respawned at once")
773 }
774 // churn empties the workspace again, so fillEmpty runs at the clock's time.
775 churn := func(id ports.WindowID) {
776 r.client <- ports.WindowMapped{ID: id}
777 receive(t, r.scenes)
778 r.client <- ports.WindowUnmapped{ID: id}
779 receive(t, r.scenes)
780 }
781 // termRetry is private to core: stay far below and far above it.
782 clk.advance(time.Second)
783 churn(2)
784 if len(r.spawn) != 0 {
785 t.Fatal("respawned before the retry interval")
786 }
787 clk.advance(time.Minute)
788 churn(3)
789 if req := receive(t, r.spawn); req.Argv[0] != "foot" {
790 t.Fatal(req)
791 }
792}
793
794func TestLastColumnMovesToNeighborOutput(t *testing.T) {
795 r := startRig(t, true, nil, left, right)
796 r.mapWindow(t, 1)
797 set := r.key(t, "Right", ports.ModAlt|ports.ModShift)
798 if got := shown(set); len(got["DP-1"]) != 0 || len(got["DP-2"]) != 1 {
799 t.Fatal(got)
800 }
801}
802
803func TestKeyboardScreenFocusSticksUntilPointerLeaves(t *testing.T) {
804 r := startMulti(t, nil, left, right)
805 r.mapWindow(t, 1)
806 r.input <- ports.PointerMotion{X: 10, Y: 10}
807 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
808 // Jitter on DP-1 keeps DP-2 focused: the next window opens there.
809 r.input <- ports.PointerMotion{X: 11, Y: 10}
810 if got := shown(r.mapWindow(t, 2)); len(got["DP-2"]) != 1 {
811 t.Fatal(got)
812 }
813 // Entering DP-1 again focuses it.
814 r.input <- ports.PointerMotion{X: 250, Y: 10}
815 r.input <- ports.PointerMotion{X: 10, Y: 10}
816 receive(t, r.scenes)
817 if got := shown(r.mapWindow(t, 3)); len(got["DP-1"]) != 2 {
818 t.Fatal(got)
819 }
820}
821
822func TestStateSnapshot(t *testing.T) {
823 both(t, func(t *testing.T, animated bool) {
824 r := startLanding(t, animated, nil, left, right)
825 r.client <- ports.WindowMapped{ID: 1, AppID: "foot", PID: 100}
826 r.land(receive(t, r.scenes))
827 r.keyLanded(t, "Right", ports.ModAlt|ports.ModCtrl)
828 <-r.state // the channel holds the latest snapshot only
829 r.client <- ports.WindowMapped{ID: 2, AppID: "firefox", PID: 200}
830 r.land(receive(t, r.scenes))
831 // Snapshots from before window 2 may still be queued: wait for it.
832 st := receive(t, r.state)
833 for len(st.Windows) < 2 {
834 st = receive(t, r.state)
835 }
836 want := ports.State{
837 Output: "DP-2",
838 Outputs: []ports.OutputState{{Name: "DP-1", Active: 1, Count: 1, WorkspaceID: 1}, {Name: "DP-2", Active: 1, Count: 1, WorkspaceID: 2}},
839 Windows: []ports.WindowState{
840 {ID: 1, AppID: "foot", PID: 100, Output: "DP-1", Workspace: 1, WorkspaceID: 1, Visible: true, Column: 1, Row: 1},
841 {ID: 2, AppID: "firefox", PID: 200, Output: "DP-2", Workspace: 1, WorkspaceID: 2, Visible: true, Column: 1, Row: 1},
842 },
843 }
844 want.Window = &want.Windows[1]
845 if !reflect.DeepEqual(st, want) {
846 t.Fatalf("got %+v\nwant %+v", st, want)
847 }
848 // Moving to workspace 2 of DP-2: two workspaces there, window 2 off screen.
849 r.input <- ports.KeyEvent{Keysym: "2", Keycode: 3, Mods: ports.ModAlt, Pressed: true}
850 r.land(receive(t, r.scenes))
851 st = receive(t, r.state)
852 // The window keeps its workspace ID; the output now shows the new one.
853 if o := st.Outputs[1]; o.Name != "DP-2" || o.Active != 2 || o.Count != 2 || o.WorkspaceID == 0 || o.WorkspaceID == 2 || st.Windows[1].WorkspaceID != 2 || st.Windows[1].Visible || st.Window != nil {
854 t.Fatalf("%+v", st)
855 }
856 })
857}
858
859func TestStateFollowsAppIDAndHiddenWorkspace(t *testing.T) {
860 r := startMulti(t, func(c *ports.Config) {
861 c.Workspaces = []ports.WorkspaceConfig{{Name: "notes"}}
862 c.Binds["Alt+n"] = "workspace notes"
863 }, left)
864 r.client <- ports.WindowMapped{ID: 1, PID: 100}
865 receive(t, r.scenes)
866 <-r.state
867 // An app ID set after map reaches the state.
868 r.client <- ports.WindowAppID{ID: 1, AppID: "foot"}
869 receive(t, r.scenes)
870 // Snapshots from before the app ID may still be queued.
871 for st := receive(t, r.state); st.Windows[0].AppID != "foot"; st = receive(t, r.state) {
872 }
873 // A hidden workspace on screen: no active number, its name, window 1 off screen.
874 r.key(t, "n", ports.ModAlt)
875 st := receive(t, r.state)
876 if st.Outputs[0].Active != 0 || st.Outputs[0].Workspace != "notes" || st.Windows[0].Visible {
877 t.Fatalf("%+v", st)
878 }
879 // The hidden workspace has its own ID, and the window keeps the one of
880 // the workspace it is on.
881 if st.Outputs[0].WorkspaceID == 0 || st.Windows[0].WorkspaceID == 0 || st.Outputs[0].WorkspaceID == st.Windows[0].WorkspaceID {
882 t.Fatalf("workspace IDs: %+v", st)
883 }
884 // A window on the configured workspace carries its name; the ID string
885 // of ext-workspace and the state file is built from it.
886 r.client <- ports.WindowMapped{ID: 2, PID: 200}
887 receive(t, r.scenes)
888 for st = receive(t, r.state); len(st.Windows) < 2; st = receive(t, r.state) {
889 }
890 if st.Windows[0].WorkspaceName != "" || st.Windows[1].WorkspaceName != "notes" || st.Windows[1].WorkspaceID != st.Outputs[0].WorkspaceID {
891 t.Fatalf("workspace names: %+v", st.Windows)
892 }
893}
894
895func TestMoveColumnOntoFullscreenStaysVisible(t *testing.T) {
896 r := startMulti(t, func(c *ports.Config) { c.Layout.Overflow = "fixed" }, left, right)
897 r.mapWindow(t, 1)
898 r.mapWindow(t, 2)
899 // Window 2 goes to DP-2, alone, and goes fullscreen in place.
900 r.key(t, "Right", ports.ModAlt|ports.ModShift)
901 r.key(t, "f", ports.ModAlt|ports.ModShift)
902 r.key(t, "Left", ports.ModAlt|ports.ModCtrl)
903 // Window 1 joins from DP-1: the fullscreen ends so it is visible and focused.
904 set := r.key(t, "Right", ports.ModAlt|ports.ModShift)
905 for _, s := range set {
906 for _, w := range s.Windows {
907 if w.ID == 1 && (s.Output != "DP-2" || w.Hidden || !w.Focused) {
908 t.Fatalf("%s %+v", s.Output, w)
909 }
910 }
911 }
912}
913
914// windowsOf lists the tiled windows of an output in scene order, with the
915// focused one.
916func windowsOf(set []ports.Scene, out string) (ids []ports.WindowID, focused ports.WindowID) {
917 for _, s := range set {
918 if s.Output != out {
919 continue
920 }
921 for _, w := range s.Windows {
922 if !w.Hidden {
923 ids = append(ids, w.ID)
924 }
925 if w.Focused {
926 focused = w.ID
927 }
928 }
929 }
930 return ids, focused
931}
932
933func TestExpelCrossesToFullNeighbor(t *testing.T) {
934 r := startMulti(t, func(c *ports.Config) { c.Layout.Overflow = "fixed" }, left, right)
935 // DP-1 (max 2): [1] [2] [3] in the spiral; consume 3 left gives
936 // [1] [2 3], focus on 3 at the bottom of the edge column.
937 r.mapWindow(t, 1)
938 r.mapWindow(t, 2)
939 r.mapWindow(t, 3)
940 r.key(t, "bracketleft", ports.ModAlt)
941 // DP-2: [4] [5], full too.
942 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
943 r.mapWindow(t, 4)
944 r.mapWindow(t, 5)
945 r.key(t, "Left", ports.ModAlt|ports.ModCtrl)
946 // Expel right at the edge: 3 stacks under 4, the first column of DP-2.
947 set := r.key(t, "bracketright", ports.ModAlt)
948 if got, _ := windowsOf(set, "DP-1"); !reflect.DeepEqual(got, []ports.WindowID{1, 2}) {
949 t.Fatal(got)
950 }
951 got, focused := windowsOf(set, "DP-2")
952 if !reflect.DeepEqual(got, []ports.WindowID{4, 3, 5}) || focused != 3 {
953 t.Fatal(got, focused)
954 }
955 if out := lastOutputs(t, r.commands); out.Focused != "DP-2" {
956 t.Fatal(out.Focused)
957 }
958 // 3 is not in the edge column of DP-2: it stacks into [5].
959 set = r.key(t, "bracketright", ports.ModAlt)
960 if got, focused := windowsOf(set, "DP-2"); !reflect.DeepEqual(got, []ports.WindowID{4, 5, 3}) || focused != 3 {
961 t.Fatal(got, focused)
962 }
963 // Now at the edge with no right monitor: nothing moves.
964 set = r.key(t, "bracketright", ports.ModAlt)
965 if got, focused := windowsOf(set, "DP-2"); !reflect.DeepEqual(got, []ports.WindowID{4, 5, 3}) || focused != 3 {
966 t.Fatal(got, focused)
967 }
968}
969
970func TestExpelOntoFixedFullscreenLeavesIt(t *testing.T) {
971 r := startMulti(t, func(c *ports.Config) { c.Layout.Overflow = "fixed" }, left, right)
972 // DP-1: [1] [2 3], focus on 3.
973 r.mapWindow(t, 1)
974 r.mapWindow(t, 2)
975 r.mapWindow(t, 3)
976 r.key(t, "bracketleft", ports.ModAlt)
977 // DP-2: [4] [5]; 5 goes fullscreen.
978 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
979 r.mapWindow(t, 4)
980 r.mapWindow(t, 5)
981 r.key(t, "f", ports.ModAlt|ports.ModShift)
982 r.key(t, "Left", ports.ModAlt|ports.ModCtrl)
983 // 3 joins as a tile: fullscreen ends so it is seen, focused.
984 set := r.key(t, "bracketright", ports.ModAlt)
985 got, focused := windowsOf(set, "DP-2")
986 if !slices.Contains(got, 3) || !slices.Contains(got, 4) || focused != 3 {
987 t.Fatal(got, focused)
988 }
989}
990
991func TestExplicitOverlappingPositionsAndNegativeY(t *testing.T) {
992 r := startMulti(t, func(c *ports.Config) {
993 c.Outputs = []ports.OutputConfig{{Name: "DP-1", Pos: &image.Point{X: 20, Y: -120}}, {Name: "DP-2", Pos: &image.Point{X: 20, Y: -120}}}
994 }, left, right)
995 out := lastOutputs(t, r.commands)
996 if len(out.Outputs) != 2 || out.Outputs[0].Info.Name != "DP-1" || out.Outputs[1].Info.Name != "DP-2" {
997 t.Fatalf("ordering: %+v", out.Outputs)
998 }
999 for _, o := range out.Outputs {
1000 if o.X != 20 || o.Y != -120 {
1001 t.Fatalf("explicit position: %+v", o)
1002 }
1003 }
1004}
1005
1006func TestTerminalTokenClaimedOnce(t *testing.T) {
1007 // Plug DP-1 first so its request is unambiguously first. Then plug
1008 // DP-2 and focus it before either window maps.
1009 r := startRig(t, true, func(c *ports.Config) { c.Terminal.AutoOpen = "all" }, left)
1010 first := receive(t, r.spawn)
1011 r.plug(t, right)
1012 second := receive(t, r.spawn)
1013 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1014 r.client <- ports.WindowMapped{ID: 41, Slot: token(t, first), Floating: true, Width: 20, Height: 20}
1015 receive(t, r.scenes)
1016 r.client <- ports.WindowMapped{ID: 42, Slot: token(t, first)}
1017 set := receive(t, r.scenes)
1018 if got := shown(set); !slices.Equal(got["DP-1"], []ports.WindowID{42}) || !slices.Equal(got["DP-2"], []ports.WindowID{41}) {
1019 t.Fatalf("first claim: %v", got)
1020 }
1021 // Move back to DP-1: the duplicate must follow focus, not the token.
1022 r.key(t, "Left", ports.ModAlt|ports.ModCtrl)
1023 r.client <- ports.WindowMapped{ID: 43, Slot: token(t, first)}
1024 set = receive(t, r.scenes)
1025 if got := shown(set); !slices.Equal(got["DP-1"], []ports.WindowID{42, 43}) || !slices.Equal(got["DP-2"], []ports.WindowID{41}) {
1026 t.Fatalf("duplicate claim: %v", got)
1027 }
1028 // The other terminal's token remains available despite the first claim.
1029 r.client <- ports.WindowMapped{ID: 44, Slot: token(t, second)}
1030 set = receive(t, r.scenes)
1031 if got := shown(set); !slices.Equal(got["DP-1"], []ports.WindowID{42, 43}) || !slices.Equal(got["DP-2"], []ports.WindowID{44, 41}) {
1032 t.Fatalf("second claim: %v", got)
1033 }
1034}
1035
1036// From a focused float, the first column focus move leaves the float for
1037// the columns, even when the column has no neighbor on that side: it does
1038// not jump to the neighbor monitor.
1039func TestFocusColumnFromFloatStaysOnMonitor(t *testing.T) {
1040 r := startMulti(t, nil, left, right)
1041 r.mapWindow(t, 1)
1042 r.client <- ports.WindowMapped{ID: 2, Floating: true, Width: 20, Height: 10}
1043 receive(t, r.scenes)
1044 set := r.key(t, "Right", ports.ModAlt)
1045 var focused []ports.WindowID
1046 for _, s := range set {
1047 for _, w := range s.Windows {
1048 if w.Focused {
1049 focused = append(focused, w.ID)
1050 if s.Output != "DP-1" {
1051 t.Fatalf("focused %d on %s", w.ID, s.Output)
1052 }
1053 }
1054 }
1055 }
1056 if !slices.Equal(focused, []ports.WindowID{1}) {
1057 t.Fatalf("focused %v", focused)
1058 }
1059}
1060
1061// The stash end to end: binds stash tiles, peeking neighbors are dimmed
1062// and their popups wait, a click selects a peek, and the script state
1063// tells the stash and whether it is hidden.
1064func TestStashEndToEnd(t *testing.T) {
1065 both(t, func(t *testing.T, animated bool) {
1066 r := startLanding(t, animated, func(c *ports.Config) { c.Stash.Gap, c.Stash.Dim = 0, 0.5 }, left)
1067 stashEndToEnd(t, r)
1068 })
1069}
1070
1071func stashEndToEnd(t *testing.T, r *landRig) {
1072 for id := ports.WindowID(1); id <= 3; id++ {
1073 r.mapLanded(t, id)
1074 }
1075 r.keyLanded(t, "s", ports.ModAlt|ports.ModShift) // 3 stashed
1076 r.keyLanded(t, "Left", ports.ModAlt) // stays on 3: its stash's only window
1077 sc := r.keyLanded(t, "s", ports.ModAlt)
1078 if got := shown(sc)["DP-1"]; !slices.Equal(got, []ports.WindowID{1, 2}) {
1079 t.Fatalf("hidden stash: %v", got)
1080 }
1081 r.keyLanded(t, "Left", ports.ModAlt) // tiles: from 2 to 1
1082 sc = r.keyLanded(t, "s", ports.ModAlt|ports.ModShift)
1083 // Stash 3 1: 1 selected in the middle, 3 peeking left, dimmed.
1084 var peek, sel ports.SceneWindow
1085 for _, w := range sc[0].Windows {
1086 switch w.ID {
1087 case 3:
1088 peek = w
1089 case 1:
1090 sel = w
1091 }
1092 }
1093 if peek.Hidden || peek.Dim != 0.5 || !peek.Floating || sel.Dim != 0 || sel.Hidden || !sel.Focused || !sel.Floating {
1094 t.Fatalf("peek %+v selected %+v", peek, sel)
1095 }
1096 // A popup of the peek waits until the peek is selected.
1097 r.client <- ports.PopupRequest{ID: 9, Parent: 3, Positioner: ports.Positioner{Width: 5, Height: 5, AnchorRect: ports.Rect{W: 1, H: 1}}}
1098 r.client <- ports.PopupMapped{ID: 9}
1099 sc = receive(t, r.scenes)
1100 for _, w := range sc[0].Windows {
1101 if w.ID == 9 {
1102 t.Fatalf("popup of a peek drawn: %+v", w)
1103 }
1104 }
1105 // A click on the peek (x 0..19 of 200) selects it.
1106 r.input <- ports.PointerMotion{X: 5, Y: 50}
1107 r.input <- ports.PointerButton{Button: 0x110, Pressed: true}
1108 r.land(receive(t, r.scenes))
1109 st := receive(t, r.state)
1110 for st.Window == nil || st.Window.ID != 3 {
1111 st = receive(t, r.state)
1112 }
1113 byID := map[ports.WindowID]ports.WindowState{}
1114 for _, w := range st.Windows {
1115 byID[w.ID] = w
1116 }
1117 if w := byID[3]; !w.Floating || w.StashIndex != 1 || w.StashCount != 2 || w.Hidden || !w.Visible {
1118 t.Fatalf("window 3 %+v", w)
1119 }
1120 if w := byID[2]; w.Floating || w.StashIndex != 0 || w.Hidden {
1121 t.Fatalf("tile %+v", w)
1122 }
1123 r.input <- ports.PointerButton{Button: 0x110}
1124 r.keyLanded(t, "s", ports.ModAlt)
1125 st = receive(t, r.state)
1126 for !st.Windows[0].Hidden {
1127 st = receive(t, r.state)
1128 }
1129 for _, w := range st.Windows {
1130 if (w.StashIndex > 0) != w.Hidden {
1131 t.Fatalf("after hide %+v", w)
1132 }
1133 }
1134}
1135
1136// stateAfter waits for the published state that ok accepts, or fails:
1137// snapshots from before an event may still be queued.
1138func stateAfter(t *testing.T, ch <-chan ports.State, ok func(ports.State) bool) ports.State {
1139 t.Helper()
1140 deadline := time.After(time.Second)
1141 for {
1142 select {
1143 case st := <-ch:
1144 if ok(st) {
1145 return st
1146 }
1147 case <-deadline:
1148 t.Fatal("no matching state")
1149 return ports.State{}
1150 }
1151 }
1152}
1153
1154// At the ends of the stash, focus moves stay there: no hop to the
1155// neighbor monitor or workspace.
1156func TestStashKeepsFocusAtEdges(t *testing.T) {
1157 r := startMulti(t, nil, left, right)
1158 r.key(t, "Left", ports.ModAlt|ports.ModCtrl) // DP-1
1159 r.mapWindow(t, 1)
1160 r.mapWindow(t, 2)
1161 r.key(t, "s", ports.ModAlt|ports.ModShift)
1162 for _, k := range []string{"Right", "Down"} {
1163 r.key(t, k, ports.ModAlt)
1164 }
1165 // A marker after the keys: its state follows theirs.
1166 r.mapWindow(t, 9)
1167 st := stateAfter(t, r.state, func(st ports.State) bool { return len(st.Windows) == 3 })
1168 if st.Output != "DP-1" || st.Outputs[0].Active != 1 {
1169 t.Fatalf("output %s active %d", st.Output, st.Outputs[0].Active)
1170 }
1171}
1172
1173// Binds act on the dialog shown over a fullscreen window, not on the window:
1174// close closes the dialog, a focus move goes back to the window and keeps it
1175// fullscreen. A dialog of a window on another output opens there.
1176func TestBindsOnDialogOverFullscreen(t *testing.T) {
1177 for _, overflow := range []string{"scroll", "fixed"} {
1178 t.Run(overflow, func(t *testing.T) {
1179 r := startMulti(t, func(c *ports.Config) { c.Layout.Overflow = overflow }, left, right)
1180 r.key(t, "Left", ports.ModAlt|ports.ModCtrl) // DP-1
1181 r.mapWindow(t, 1)
1182 r.mapWindow(t, 2)
1183 r.key(t, "Left", ports.ModAlt)
1184 r.key(t, "f", ports.ModAlt|ports.ModShift) // 1 fullscreen
1185 r.client <- ports.WindowMapped{ID: 9, Floating: true, Width: 10, Height: 10, Parent: 1}
1186 receive(t, r.scenes)
1187 st := stateAfter(t, r.state, func(st ports.State) bool { return st.Window != nil && st.Window.ID == 9 })
1188 if st.Window.ID != 9 {
1189 t.Fatalf("focus %+v, want the dialog", st.Window)
1190 }
1191 for len(r.commands) > 0 {
1192 <-r.commands
1193 }
1194 r.input <- ports.KeyEvent{Keysym: "q", Mods: ports.ModAlt, Pressed: true}
1195 r.input <- ports.KeyEvent{Keysym: "q", Mods: ports.ModAlt}
1196 for {
1197 if v, ok := receive(t, r.commands).(ports.CloseWindow); ok {
1198 if v.ID != 9 {
1199 t.Fatalf("closed %d, want the dialog", v.ID)
1200 }
1201 break
1202 }
1203 }
1204 scenes := r.key(t, "Right", ports.ModAlt)
1205 st = stateAfter(t, r.state, func(st ports.State) bool { return st.Window != nil && st.Window.ID != 9 })
1206 if st.Window.ID != 1 {
1207 t.Fatalf("focus %+v, want the fullscreen window", st.Window)
1208 }
1209 for _, s := range scenes {
1210 for _, w := range s.Windows {
1211 if s.Output == "DP-1" && w.ID == 1 && !w.Fullscreen {
1212 t.Fatalf("window 1 left fullscreen: %+v", w)
1213 }
1214 }
1215 }
1216 // A dialog of the window on DP-1 opens on DP-1 while DP-2 has the focus.
1217 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1218 r.client <- ports.WindowMapped{ID: 10, Floating: true, Width: 10, Height: 10, Parent: 1}
1219 for {
1220 seen := shown(receive(t, r.scenes))
1221 if slices.Contains(seen["DP-2"], 10) {
1222 t.Fatalf("dialog on the focused output: %v", seen)
1223 }
1224 if slices.Contains(seen["DP-1"], 10) {
1225 break
1226 }
1227 }
1228 })
1229 }
1230}
1231
1232// A focused native dialog: focus-column first leaves the dialog for the
1233// tiles below, it does not hop to the neighbor monitor.
1234func TestDialogFocusStaysOnMonitor(t *testing.T) {
1235 r := startMulti(t, nil, left, right)
1236 r.key(t, "Left", ports.ModAlt|ports.ModCtrl) // DP-1
1237 r.mapWindow(t, 1)
1238 r.client <- ports.WindowMapped{ID: 3, Floating: true, Width: 10, Height: 10}
1239 receive(t, r.scenes)
1240 r.key(t, "Right", ports.ModAlt)
1241 st := stateAfter(t, r.state, func(st ports.State) bool { return st.Window == nil || st.Window.ID != 3 })
1242 if st.Output != "DP-1" || st.Window == nil || st.Window.ID != 1 {
1243 t.Fatalf("output %s window %+v", st.Output, st.Window)
1244 }
1245}
1246
1247// A click on a tile behind the shown stash hides the stash and focuses
1248// the tile.
1249func TestStashHidesOnBackgroundClick(t *testing.T) {
1250 both(t, func(t *testing.T, animated bool) {
1251 // Gap 10 at 80%: no peeks, the margins show the tile behind.
1252 stashHidesOnBackgroundClick(t, startLanding(t, animated, func(c *ports.Config) { c.Stash.Gap = 10 }, left))
1253 })
1254}
1255
1256func stashHidesOnBackgroundClick(t *testing.T, r *landRig) {
1257 r.mapLanded(t, 1)
1258 r.mapLanded(t, 2)
1259 r.keyLanded(t, "s", ports.ModAlt|ports.ModShift) // 2 stashed and focused, over tile 1
1260 r.input <- ports.PointerMotion{X: 5, Y: 50}
1261 r.input <- ports.PointerButton{Button: 0x110, Pressed: true}
1262 st := stateAfter(t, r.state, func(st ports.State) bool { return st.Window != nil && st.Window.ID == 1 })
1263 for _, w := range st.Windows {
1264 if w.ID == 2 && !w.Hidden {
1265 t.Fatalf("stash still shown: %+v", w)
1266 }
1267 }
1268}
1269
1270// A peek narrower than its border is still clickable.
1271func TestStashThinPeekClick(t *testing.T) {
1272 // 200 wide, 80%: a 20px margin, 18px of gap, 2px of the peek.
1273 r := startMulti(t, func(c *ports.Config) { c.Stash.Gap, c.Border.Width = 9, 4 }, left)
1274 r.mapWindow(t, 1)
1275 r.mapWindow(t, 2)
1276 r.key(t, "s", ports.ModAlt|ports.ModShift) // 2 stashed
1277 r.key(t, "s", ports.ModAlt)
1278 r.key(t, "Left", ports.ModAlt)
1279 r.key(t, "s", ports.ModAlt|ports.ModShift) // stash 2 1, 2 peeks 2px from the left
1280 r.input <- ports.PointerMotion{X: 1, Y: 50}
1281 for {
1282 if v, ok := (<-r.commands).(ports.PointerFocus); ok {
1283 if v.ID != 2 {
1284 t.Fatalf("pointer focus %+v, want the peek", v)
1285 }
1286 break
1287 }
1288 }
1289}
1290
1291func TestNamedWorkspaceWithoutHomeFollowsFocus(t *testing.T) {
1292 r := startMulti(t, func(c *ports.Config) {
1293 c.Workspaces = []ports.WorkspaceConfig{{Name: "web"}}
1294 c.Binds["Alt+w"] = "workspace web"
1295 }, left, right)
1296 // Called from DP-2, web comes to DP-2.
1297 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1298 r.key(t, "w", ports.ModAlt)
1299 if got := shown(r.mapWindow(t, 1)); len(got["DP-1"]) != 0 || len(got["DP-2"]) != 1 {
1300 t.Fatal(got)
1301 }
1302 if got := shown(r.key(t, "w", ports.ModAlt)); len(got["DP-2"]) != 0 {
1303 t.Fatal(got)
1304 }
1305 // Called from DP-1, it moves there with its window.
1306 r.key(t, "Left", ports.ModAlt|ports.ModCtrl)
1307 if got := shown(r.key(t, "w", ports.ModAlt)); len(got["DP-1"]) != 1 || got["DP-1"][0] != 1 || len(got["DP-2"]) != 0 {
1308 t.Fatal(got)
1309 }
1310}
1311
1312func TestNamedWorkspaceWithHomeFocusesIt(t *testing.T) {
1313 r := startMulti(t, func(c *ports.Config) {
1314 c.Workspaces = []ports.WorkspaceConfig{{Name: "web", Monitor: "DP-1"}}
1315 c.Binds["Alt+w"] = "workspace web"
1316 }, left, right)
1317 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1318 r.key(t, "w", ports.ModAlt)
1319 // The focus went to DP-1: the window opens there.
1320 if got := shown(r.mapWindow(t, 1)); len(got["DP-1"]) != 1 || len(got["DP-2"]) != 0 {
1321 t.Fatal(got)
1322 }
1323}
1324
1325func TestNamedGuestFollowsFocusAndReturnsHome(t *testing.T) {
1326 r := startMulti(t, func(c *ports.Config) {
1327 c.Workspaces = []ports.WorkspaceConfig{{Name: "web", Monitor: "DP-3"}}
1328 c.Binds["Alt+w"] = "workspace web"
1329 }, left, right)
1330 // DP-3 is not plugged: web follows the focus to DP-2.
1331 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1332 r.key(t, "w", ports.ModAlt)
1333 if got := shown(r.mapWindow(t, 1)); len(got["DP-2"]) != 1 {
1334 t.Fatal(got)
1335 }
1336 r.key(t, "w", ports.ModAlt)
1337 // DP-3 arrives: web, not on screen, goes home with no bind.
1338 r.plug(t, third)
1339 st := receive(t, r.state)
1340 for len(st.Outputs) < 3 {
1341 st = receive(t, r.state)
1342 }
1343 if len(st.Windows) != 1 || st.Windows[0].Output != "DP-3" {
1344 t.Fatalf("%+v", st.Windows)
1345 }
1346}
1347
1348func TestNamedGuestOnScreenGoesHomeOnBind(t *testing.T) {
1349 r := startMulti(t, func(c *ports.Config) {
1350 c.Workspaces = []ports.WorkspaceConfig{{Name: "web", Monitor: "DP-3"}}
1351 c.Binds["Alt+w"] = "workspace web"
1352 }, left, right)
1353 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1354 r.key(t, "w", ports.ModAlt)
1355 r.mapWindow(t, 1)
1356 // DP-3 arrives while web is on screen on DP-2: the bind takes it home.
1357 r.plug(t, third)
1358 if got := shown(r.key(t, "w", ports.ModAlt)); len(got["DP-3"]) != 1 || len(got["DP-2"]) != 0 {
1359 t.Fatal(got)
1360 }
1361}
1362
1363func TestNamedOnScreenAtHomeStaysOnBindFromElsewhere(t *testing.T) {
1364 r := startMulti(t, func(c *ports.Config) {
1365 c.Workspaces = []ports.WorkspaceConfig{{Name: "web", Monitor: "DP-1"}}
1366 c.Binds["Alt+w"] = "workspace web"
1367 }, left, right)
1368 r.key(t, "w", ports.ModAlt)
1369 r.mapWindow(t, 1)
1370 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1371 // Reaching web from DP-2 focuses it; it does not hide it.
1372 if got := shown(r.key(t, "w", ports.ModAlt)); len(got["DP-1"]) != 1 {
1373 t.Fatal(got)
1374 }
1375 if got := shown(r.key(t, "w", ports.ModAlt)); len(got["DP-1"]) != 0 {
1376 t.Fatal(got)
1377 }
1378}
1379
1380// From a fullscreen window, focus-column-left/right leaves fullscreen for
1381// the column on that side; at the workspace edge it goes to the neighbor
1382// monitor and the window stays fullscreen.
1383func TestFocusColumnLeavesFullscreenBeforeMonitor(t *testing.T) {
1384 r := startMulti(t, func(c *ports.Config) { c.Layout.Overflow = "fixed" }, left, right)
1385 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1386 r.mapWindow(t, 1)
1387 r.mapWindow(t, 2)
1388 r.key(t, "f", ports.ModAlt|ports.ModShift)
1389 // Right edge: DP-2 has no neighbor there, the game stays fullscreen.
1390 if got, focused := windowsOf(r.key(t, "Right", ports.ModAlt), "DP-2"); !reflect.DeepEqual(got, []ports.WindowID{2}) || focused != 2 {
1391 t.Fatal("edge:", got, focused)
1392 }
1393 // Left: window 1 is there, fullscreen ends.
1394 if got, focused := windowsOf(r.key(t, "Left", ports.ModAlt), "DP-2"); !reflect.DeepEqual(got, []ports.WindowID{1, 2}) || focused != 1 {
1395 t.Fatal("left:", got, focused)
1396 }
1397 // Fullscreen on window 1, the left edge: DP-1 gets the focus and
1398 // window 1 stays fullscreen on DP-2.
1399 r.key(t, "f", ports.ModAlt|ports.ModShift)
1400 set := r.key(t, "Left", ports.ModAlt)
1401 if got, _ := windowsOf(set, "DP-2"); !reflect.DeepEqual(got, []ports.WindowID{1}) {
1402 t.Fatal("monitor edge:", got)
1403 }
1404 if got, _ := windowsOf(r.mapWindow(t, 3), "DP-1"); !reflect.DeepEqual(got, []ports.WindowID{3}) {
1405 t.Fatal("focus not on DP-1:", got)
1406 }
1407}
1408
1409// focusedOutput drains the commands and returns the focused connector,
1410// current when no SetOutputs was sent (it is sent only on a change).
1411func focusedOutput(r *multiRig, current string) string {
1412 for len(r.commands) > 0 {
1413 if v, ok := (<-r.commands).(ports.SetOutputs); ok {
1414 current = v.Focused
1415 }
1416 }
1417 return current
1418}
1419
1420// The column edge crosses to the screen on that side of the geometry, not to
1421// the next one in configuration order.
1422func TestFocusColumnEdgeFollowsGeometryNotConfigOrder(t *testing.T) {
1423 r := startMulti(t, func(c *ports.Config) {
1424 c.Outputs = []ports.OutputConfig{
1425 {Name: "DP-1", Pos: &image.Point{X: 500, Y: 0}},
1426 {Name: "DP-2", Pos: &image.Point{X: 0, Y: 0}},
1427 }
1428 }, left, right)
1429 // DP-1 (x 500..700) is focused first; DP-2 (x 0..400) lies to its left.
1430 current := focusedOutput(r, lastOutputs(t, r.commands).Focused)
1431 r.key(t, "Left", ports.ModAlt|ports.ModCtrl)
1432 if current = focusedOutput(r, current); current != "DP-2" {
1433 t.Fatalf("focus-monitor-left: %s, want DP-2", current)
1434 }
1435 r.mapWindow(t, 1)
1436 r.key(t, "Right", ports.ModAlt)
1437 if got := focusedOutput(r, current); got != "DP-1" {
1438 t.Fatalf("focus-column-right at the right edge: %s, want DP-1", got)
1439 }
1440 r.key(t, "Left", ports.ModAlt)
1441 if got := focusedOutput(r, "DP-1"); got != "DP-2" {
1442 t.Fatalf("focus-column-left at the left edge: %s, want DP-2", got)
1443 }
1444}
1445
1446// With the outputs stacked, a column edge has no neighbor on its side.
1447func TestFocusColumnEdgeStaysOnStackedOutputs(t *testing.T) {
1448 r := startMulti(t, func(c *ports.Config) {
1449 c.Outputs = []ports.OutputConfig{
1450 {Name: "DP-1"},
1451 {Name: "DP-2", Anchor: ports.OutputAnchor{Relation: ports.RelationBelow, To: "DP-1"}},
1452 }
1453 }, left, right)
1454 r.mapWindow(t, 1)
1455 current := focusedOutput(r, lastOutputs(t, r.commands).Focused)
1456 for _, k := range []string{"Left", "Right", "Left"} {
1457 r.key(t, k, ports.ModAlt)
1458 if got := focusedOutput(r, current); got != current {
1459 t.Fatalf("focus-column %s moved focus from %s to %s", k, current, got)
1460 }
1461 }
1462}
1463
1464// A screen turned off by power management is still a neighbor.
1465func TestPoweredOffScreenStaysNeighbor(t *testing.T) {
1466 r := startMulti(t, nil, left, right)
1467 // DP-1 is focused, DP-2 follows it on the right.
1468 current := focusedOutput(r, lastOutputs(t, r.commands).Focused)
1469 r.client <- ports.OutputPower{Output: "DP-2", On: false}
1470 _ = receive(t, r.scenes)
1471 if current != "DP-1" {
1472 t.Fatalf("focused %s, want DP-1", current)
1473 }
1474 if out := lastOutputs(t, r.commands); !slices.Equal(out.Off, []string{"DP-2"}) {
1475 t.Fatalf("off %v, want [DP-2]", out.Off)
1476 }
1477 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1478 if got := focusedOutput(r, current); got != "DP-2" {
1479 t.Fatalf("focus-monitor-right: %s, want the powered-off DP-2", got)
1480 }
1481}
1482
1483func TestRotatedOutputLayout(t *testing.T) {
1484 r := startMulti(t, func(c *ports.Config) {
1485 c.Outputs = []ports.OutputConfig{{Name: left.Name, Transform: 1}}
1486 }, left, right)
1487 out := lastOutputs(t, r.commands)
1488 if len(out.Outputs) != 2 {
1489 t.Fatalf("%+v", out.Outputs)
1490 }
1491 if o := out.Outputs[0]; o.Info.Name != "DP-1" || o.Width != 100 || o.Height != 200 || o.Transform != 1 {
1492 t.Fatalf("DP-1: %+v", o)
1493 }
1494 if o := out.Outputs[1]; o.Info.Name != "DP-2" || o.X != 100 || o.Width != 400 || o.Height != 200 || o.Transform != 0 {
1495 t.Fatalf("DP-2: %+v", o)
1496 }
1497 r.mapWindow(t, 1)
1498 set := r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1499 if len(set) != 2 || set[0].Output != "DP-1" || set[1].Output != "DP-2" {
1500 t.Fatalf("scenes: %+v", set)
1501 }
1502 for _, s := range set {
1503 switch s.Output {
1504 case "DP-1":
1505 if s.OutputWidth != 100 || s.OutputHeight != 200 || s.Transform != 1 {
1506 t.Fatalf("DP-1 scene: %dx%d t=%d", s.OutputWidth, s.OutputHeight, s.Transform)
1507 }
1508 case "DP-2":
1509 if s.OutputWidth != 400 || s.OutputHeight != 200 || s.Transform != 0 {
1510 t.Fatalf("DP-2 scene: %dx%d t=%d", s.OutputWidth, s.OutputHeight, s.Transform)
1511 }
1512 }
1513 }
1514}
1515
1516func TestRotatedOutputNeighbor(t *testing.T) {
1517 r := startMulti(t, func(c *ports.Config) {
1518 c.Outputs = []ports.OutputConfig{
1519 {Name: "DP-1", Transform: 1},
1520 {Name: "DP-2", Anchor: ports.OutputAnchor{Relation: ports.RelationBelow, To: "DP-1"}},
1521 }
1522 }, left, right)
1523 out := lastOutputs(t, r.commands)
1524 if o := out.Outputs[1]; o.Info.Name != "DP-2" || o.Y != 200 {
1525 t.Fatalf("DP-2: %+v", o)
1526 }
1527 r.mapWindow(t, 1)
1528 r.key(t, "Down", ports.ModAlt|ports.ModCtrl)
1529 if got := lastOutputs(t, r.commands).Focused; got != "DP-2" {
1530 t.Fatalf("focus-monitor-down: %s", got)
1531 }
1532}
1533
1534func TestTransformReload(t *testing.T) {
1535 r := startMulti(t, func(c *ports.Config) {
1536 c.Binds["Alt+equal"] = "scale-up"
1537 c.Outputs = []ports.OutputConfig{{Name: "DP-1"}, {Name: "DP-2"}}
1538 }, left, right)
1539 // Scale DP-2 (focus follows the key to it) with the bind first.
1540 r.key(t, "Right", ports.ModAlt|ports.ModCtrl)
1541 set := r.key(t, "equal", ports.ModAlt)
1542 scale := set[1].Scale
1543 if scale == 1 {
1544 t.Fatal("scale-up did not change the scale")
1545 }
1546 if out := lastOutputs(t, r.commands); out.Outputs[0].Width != 200 || out.Outputs[1].X != 200 {
1547 t.Fatalf("%+v", out.Outputs)
1548 }
1549
1550 r.cfg.Outputs[0].Transform = 1
1551 r.reload <- ports.ConfigChanged{Config: r.cfg}
1552 set = receive(t, r.scenes)
1553 out := lastOutputs(t, r.commands)
1554 if o := out.Outputs[0]; o.Width != 100 || o.Height != 200 || o.Transform != 1 {
1555 t.Fatalf("DP-1: %+v", o)
1556 }
1557 if o := out.Outputs[1]; o.X != 100 {
1558 t.Fatalf("DP-2 not re-placed: %+v", o)
1559 }
1560 if set[1].Scale != scale || out.Outputs[1].Scale != scale {
1561 t.Fatalf("scale %v changed to %v by a transform reload", scale, set[1].Scale)
1562 }
1563
1564 // A transform change on the scaled output itself keeps its live scale.
1565 r.cfg.Outputs[1].Transform = 1
1566 r.reload <- ports.ConfigChanged{Config: r.cfg}
1567 set = receive(t, r.scenes)
1568 if set[1].Scale != scale || set[1].Transform != 1 {
1569 t.Fatalf("scale %v transform %d", set[1].Scale, set[1].Transform)
1570 }
1571 // Back to normal.
1572 r.cfg.Outputs[0].Transform = 0
1573 r.reload <- ports.ConfigChanged{Config: r.cfg}
1574 _ = receive(t, r.scenes)
1575 if o := lastOutputs(t, r.commands).Outputs[0]; o.Width != 200 || o.Transform != 0 {
1576 t.Fatalf("DP-1: %+v", o)
1577 }
1578}
1579
1580// A reload that shrinks the layout under a still pointer brings it back onto
1581// an output.
1582func TestTransformReloadClampsPointer(t *testing.T) {
1583 both(t, func(t *testing.T, animated bool) {
1584 transformReloadClampsPointer(t, startLanding(t, animated, func(c *ports.Config) {
1585 c.Outputs = []ports.OutputConfig{{Name: "DP-1"}, {Name: "DP-2"}}
1586 }, left, right))
1587 })
1588}
1589
1590func transformReloadClampsPointer(t *testing.T, r *landRig) {
1591 r.mapLanded(t, 1)
1592 r.keyLanded(t, "Right", ports.ModAlt|ports.ModCtrl)
1593 r.mapLanded(t, 2)
1594 // DP-2 spans x 200..600: (550, 50) is (350, 50) on window 2.
1595 for len(r.commands) > 0 {
1596 <-r.commands
1597 }
1598 r.input <- ports.PointerMotion{X: 550, Y: 50}
1599 if v := command(t, r.commands); v != (ports.PointerFocus{ID: 2, X: 350, Y: 50}) {
1600 t.Fatal(v)
1601 }
1602 // Rotating DP-1 moves DP-2 to x 100..500: 550 is off every output.
1603 r.cfg.Outputs[0].Transform = 1
1604 r.reload <- ports.ConfigChanged{Config: r.cfg}
1605 receive(t, r.scenes)
1606 for len(r.commands) > 0 {
1607 <-r.commands
1608 }
1609 // A rehit (here from a swipe end) points at what lies under the pointer:
1610 // clamped onto DP-1's right edge, window 1, not nothing.
1611 r.input <- ports.SwipeEnd{}
1612 for {
1613 if v, ok := command(t, r.commands).(ports.PointerFocus); ok {
1614 if v.ID != 1 {
1615 t.Fatalf("pointer after reload: %+v", v)
1616 }
1617 break
1618 }
1619 }
1620}