1package core
2
3import (
4 "context"
5 "testing"
6 "time"
7
8 "github.com/bnema/neferwl/internal/ports"
9)
10
11// A scene published while a spring runs carries Animating, so its flip wakes
12// core for the next frame; the scene the spring settles on does not, and
13// neither does any scene of an idle output.
14func TestSceneAnimatingFollowsSprings(t *testing.T) {
15 c, ic := indicatorCore(t)
16 c.cfg.Animations.On = true
17 a := c.cur()
18 a.mon.AddWindow(1)
19 a.mon.AddWindow(2)
20 if s := indicatorScene(t, c); s.Animating {
21 t.Fatal("an idle output publishes an animating scene")
22 }
23
24 // A slide starts.
25 w := a.mon.Current()
26 w.view.off = 100
27 w.view.motion = newMotion(viewSpring(w.view.off, 0), ic.now, 1)
28 ic.now = ic.now.Add(10 * time.Millisecond)
29 first := stepScenes(t, c, a)["A"]
30 if !first.Animating {
31 t.Fatal("a scene with a running spring is not animating")
32 }
33 ic.now = ic.now.Add(10 * time.Millisecond)
34 if second := stepScenes(t, c, a)["A"]; !second.Animating || second.Seq == first.Seq {
35 t.Fatalf("running slide: animating=%v Seq %d (was %d)", second.Animating, second.Seq, first.Seq)
36 }
37
38 // The step that lands the spring publishes the last scene: nothing more
39 // is due, so it must not wake core again.
40 ic.now = ic.now.Add(10 * time.Second)
41 last := stepScenes(t, c, a)["A"]
42 if last.Animating {
43 t.Fatal("the scene a spring settles on is animating")
44 }
45 if a.springing() {
46 t.Fatal("bad setup: the spring still runs")
47 }
48 // Idle again: still false, and the Seq is held (no extra recomposition).
49 if err := c.publish(context.Background()); err != nil {
50 t.Fatal(err)
51 }
52 idle := (<-c.ch.Scenes)[0]
53 if idle.Animating || idle.Seq != last.Seq || !idle.SameAs(last) {
54 t.Fatalf("idle publish: animating=%v Seq %d (was %d)", idle.Animating, idle.Seq, last.Seq)
55 }
56}
57
58// stepScenes steps only the screen and returns the scene of each output.
59func stepScenes(t *testing.T, c *Core, only *screen) map[string]ports.Scene {
60 t.Helper()
61 if err := c.step(context.Background(), only); err != nil {
62 t.Fatal(err)
63 }
64 out := map[string]ports.Scene{}
65 for _, s := range <-c.ch.Scenes {
66 out[s.Output] = s
67 }
68 return out
69}
70
71// Animating is per output: a spring on one does not mark the other.
72func TestSceneAnimatingIsPerOutput(t *testing.T) {
73 c, ic := indicatorCore(t)
74 c.cfg.Animations.On = true
75 a := c.cur()
76 c.addScreen(ports.OutputInfo{Name: "B", Width: 300, Height: 200})
77 b := c.screens[1]
78 a.mon.AddWindow(1)
79 b.mon.AddWindow(2)
80 indicatorScene(t, c)
81 w := b.mon.Current()
82 w.view.off = 100
83 w.view.motion = newMotion(viewSpring(w.view.off, 0), ic.now, 1)
84 ic.now = ic.now.Add(10 * time.Millisecond)
85 got := stepScenes(t, c, b)
86 if !got["B"].Animating || got["A"].Animating {
87 t.Fatalf("animating A=%v B=%v, want B only", got["A"].Animating, got["B"].Animating)
88 }
89}
90
91// A spring that settles on the very scene its last running step drew (the
92// tail rounds to the final pixel) must still publish a scene with Animating
93// off, with a fresh Seq: SameAs ignores the flag, and the outputs would
94// otherwise keep the animating scene they show and wake core on every later
95// flip.
96func TestSettledSceneSameAsPreviousClearsAnimating(t *testing.T) {
97 // Commands (rehit) are not read: room for every step.
98 c, ic := indicatorCoreWith(t, make(chan ports.ClientCommand, 4096))
99 c.cfg.Animations.On = true
100 a := c.cur()
101 a.mon.AddWindow(1)
102 a.mon.AddWindow(2)
103 indicatorScene(t, c)
104
105 w := a.mon.Current()
106 w.view.off = 100
107 w.view.motion = newMotion(viewSpring(w.view.off, 0), ic.now, 1)
108 // Step in 1 ms until the spring settles: the snap settles it once it is
109 // within half a pixel of its target, where the last running scene can
110 // draw the same pixels as the settled one.
111 start := ic.now
112 var running, settled ports.Scene
113 for ms := 1; ; ms++ {
114 if ms > 2000 {
115 t.Fatal("the spring never settles")
116 }
117 ic.now = start.Add(time.Duration(ms) * time.Millisecond)
118 settled = stepScenes(t, c, a)["A"]
119 if !a.springing() {
120 break
121 }
122 running = settled
123 }
124 if !running.Animating {
125 t.Fatalf("bad setup: last running scene animating=%v", running.Animating)
126 }
127 if !settled.SameAs(running) {
128 t.Fatal("bad setup: the settled scene differs from the last running one")
129 }
130 if settled.Animating {
131 t.Fatal("the settled scene is animating")
132 }
133 if settled.Seq == running.Seq {
134 t.Fatalf("settled scene kept the animating scene's Seq %d", running.Seq)
135 }
136}