1package core
2
3import (
4 "testing"
5 "time"
6)
7
8// TestAnimateAllocations guards animate: with a camera, a switch and 20 rect
9// motions running (fade, dim and scale components, half of them leaving) it
10// allocates nothing.
11func TestAnimateAllocations(t *testing.T) {
12 c, ic := indicatorCore(t)
13 c.cfg.Animations.On = true
14 sc := c.cur()
15 t0 := ic.now
16 m := sc.mon
17 m.AddWindow(1)
18 m.AddWindow(2)
19
20 ws := m.Workspaces[0]
21 ws.view.motion = c.spring(viewSpring(100, 0), t0)
22 ws.view.off = 100
23 m.switchView.motion = c.spring(workspaceSpring(100, 0), t0)
24 m.switchView.off = 100
25 ws.stashView.motion = c.spring(workspaceSpring(0.5, 0), t0)
26 ws.stashView.off = 0.5
27 sc.rects = make(map[WindowID]rectMotion, 20)
28 for i := range 20 {
29 sp := viewSpring(50, 0)
30 rm := rectMotion{
31 x: c.spring(sp, t0), y: c.spring(sp, t0),
32 w: c.spring(sp, t0), h: c.spring(sp, t0),
33 dx: 50, dy: 50, dw: 50, dh: 50,
34 fade: c.spring(levelSpring(1, 0), t0), df: 1,
35 dim: c.spring(levelSpring(0.5, 0), t0), ddim: 0.5,
36 scale: true,
37 }
38 if i%2 == 0 {
39 rm.leaving, rm.left = true, Placement{ID: WindowID(100 + i), Rect: Rect{W: 10, H: 10}}
40 }
41 sc.rects[WindowID(100+i)] = rm
42 }
43
44 // A stash peek's veil moves alone (a navigation): dim-only motions,
45 // some going below zero, as an offset from Stash.Dim.
46 for i := range 4 {
47 var rm rectMotion
48 from := 0.5
49 if i%2 == 1 {
50 from = -0.5
51 }
52 rm.dim, rm.ddim = c.spring(levelSpring(from, 0), t0), from
53 sc.rects[WindowID(200+i)] = rm
54 }
55
56 now := t0.Add(10 * time.Millisecond)
57 for name, only := range map[string]*screen{"all": nil, "only": sc} {
58 if n := testing.AllocsPerRun(100, func() { c.animate(now, only) }); n != 0 {
59 t.Errorf("animate(%s) allocs = %v, want 0", name, n)
60 }
61 }
62 if !ws.view.motion.on || !ws.stashView.motion.on || !m.switchView.motion.on || len(sc.rects) != 24 {
63 t.Fatalf("motions settled: camera %v switch %v rects %d", ws.view.motion.on, m.switchView.motion.on, len(sc.rects))
64 }
65}