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.motion = c.spring(viewSpring(100, 0), t0)
22 ws.shift = 100
23 m.switchMotion = c.spring(workspaceSpring(100, 0), t0)
24 m.switchOff = 100
25 sc.rects = make(map[WindowID]rectMotion, 20)
26 for i := range 20 {
27 sp := viewSpring(50, 0)
28 rm := rectMotion{
29 x: c.spring(sp, t0), y: c.spring(sp, t0),
30 w: c.spring(sp, t0), h: c.spring(sp, t0),
31 dx: 50, dy: 50, dw: 50, dh: 50,
32 fade: c.spring(levelSpring(1, 0), t0), df: 1,
33 dim: c.spring(levelSpring(0.5, 0), t0), ddim: 0.5,
34 scale: true,
35 }
36 if i%2 == 0 {
37 rm.leaving, rm.left = true, Placement{ID: WindowID(100 + i), Rect: Rect{W: 10, H: 10}}
38 }
39 sc.rects[WindowID(100+i)] = rm
40 }
41
42 // A stash peek's veil moves alone (a navigation): dim-only motions,
43 // some going below zero, as an offset from Stash.Dim.
44 for i := range 4 {
45 var rm rectMotion
46 from := 0.5
47 if i%2 == 1 {
48 from = -0.5
49 }
50 rm.dim, rm.ddim = c.spring(levelSpring(from, 0), t0), from
51 sc.rects[WindowID(200+i)] = rm
52 }
53
54 now := t0.Add(10 * time.Millisecond)
55 for name, only := range map[string]*screen{"all": nil, "only": sc} {
56 if n := testing.AllocsPerRun(100, func() { c.animate(now, only) }); n != 0 {
57 t.Errorf("animate(%s) allocs = %v, want 0", name, n)
58 }
59 }
60 if !ws.motion.on || !m.switchMotion.on || len(sc.rects) != 24 {
61 t.Fatalf("motions settled: camera %v switch %v rects %d", ws.motion.on, m.switchMotion.on, len(sc.rects))
62 }
63}