1package render
2
3import (
4 "testing"
5
6 "github.com/bnema/nefergui/internal/css"
7 "github.com/bnema/nefergui/internal/layout"
8)
9
10func rectCommands(n int) []layout.Command {
11 cmds := make([]layout.Command, n)
12 for i := range cmds {
13 cmds[i] = layout.Command{Op: "rect", Rect: layout.Rect{X: 1, Y: float64(i), W: 10, H: 1}, Color: css.Color{A: 1}, Opacity: 1}
14 }
15 return cmds
16}
17
18func TestQuadCapacityIsSufficientAndExactWithoutCulling(t *testing.T) {
19 glyphs := glyphCommand(t, "office")
20 n := 0
21 for _, r := range glyphs.Runs {
22 n += len(r.Glyphs)
23 }
24 cmds := []layout.Command{{Op: "clip-push", Rect: layout.Rect{W: 200, H: 100}}, rectCommands(1)[0], glyphs, {Op: "clip-pop"}}
25 if got := quadCapacity(cmds); got != n+1 {
26 t.Fatalf("capacity %d, want %d", got, n+1)
27 }
28 p, err := NewPreparer(256, 2)
29 if err != nil {
30 t.Fatal(err)
31 }
32 f, err := p.Prepare(cmds, 1, 200, 100)
33 if err != nil {
34 t.Fatal(err)
35 }
36 if len(f.Quads) == 0 || len(f.Quads) > n+1 || cap(f.Quads) != n+1 {
37 t.Fatalf("len=%d cap=%d, want cap %d", len(f.Quads), cap(f.Quads), n+1)
38 }
39}
40
41// Frames are detached: a later Prepare must not alias or overwrite an earlier
42// frame that is still queued, blocked or superseded.
43func TestSuccessiveFramesAreDetached(t *testing.T) {
44 p, err := NewPreparer(256, 2)
45 if err != nil {
46 t.Fatal(err)
47 }
48 a, err := p.Prepare(rectCommands(4), 1, 100, 100)
49 if err != nil {
50 t.Fatal(err)
51 }
52 want := append([]Quad(nil), a.Quads...)
53 b, err := p.Prepare(rectCommands(3), 2, 100, 100)
54 if err != nil {
55 t.Fatal(err)
56 }
57 if len(a.Quads) != 4 || len(b.Quads) != 3 || &a.Quads[0] == &b.Quads[0] {
58 t.Fatalf("frames alias: %d %d", len(a.Quads), len(b.Quads))
59 }
60 for i := range want {
61 if a.Quads[i] != want[i] {
62 t.Fatalf("earlier frame quad %d overwritten", i)
63 }
64 }
65 // Appending to a returned frame must not write into another frame.
66 b.Quads = append(b.Quads[:len(b.Quads):len(b.Quads)], Quad{Op: "x"})
67 if a.Quads[0] != want[0] {
68 t.Fatal("append leaked into earlier frame")
69 }
70}
71
72func TestFailedPrepareReturnsNoPartialFrame(t *testing.T) {
73 p, err := NewPreparer(256, 2)
74 if err != nil {
75 t.Fatal(err)
76 }
77 cmds := append(rectCommands(8), layout.Command{Op: "bogus"})
78 f, err := p.Prepare(cmds, 1, 100, 100)
79 if err == nil || f.Quads != nil || f.Uploads != nil {
80 t.Fatalf("partial frame on failure: %v %+v", err, f)
81 }
82 if f, err = p.Prepare(nil, 1, 100, 100); err != nil || f.Quads != nil {
83 t.Fatalf("empty frame: %v %+v", err, f)
84 }
85}
86
87// Preallocation must not disturb owed-upload accounting across frames whose
88// quads were sized differently.
89func TestPreallocatedFramesKeepUploadDebt(t *testing.T) {
90 cmd := glyphCommand(t, "office")
91 p, err := NewPreparer(256, 2)
92 if err != nil {
93 t.Fatal(err)
94 }
95 first, err := p.Prepare([]layout.Command{cmd}, 1, 200, 100)
96 if err != nil || len(first.Uploads) == 0 {
97 t.Fatalf("first: %v %d", err, len(first.Uploads))
98 }
99 // Superseding frame with no glyphs still resends the owed pages.
100 second, err := p.Prepare(rectCommands(2), 1, 200, 100)
101 if err != nil || len(second.Uploads) == 0 {
102 t.Fatalf("debt lost: %v %d", err, len(second.Uploads))
103 }
104 p.Submitted(first.Uploads)
105 if f, err := p.Prepare(rectCommands(2), 1, 200, 100); err != nil || len(f.Uploads) == 0 {
106 t.Fatalf("stale ack cleared debt: %v %d", err, len(f.Uploads))
107 }
108}
109
110// Box-only frames must cost one Quads allocation regardless of command count.
111func TestPrepareQuadAllocationsDoNotScaleWithCommands(t *testing.T) {
112 p, err := NewPreparer(256, 2)
113 if err != nil {
114 t.Fatal(err)
115 }
116 cmds := rectCommands(512)
117 allocs := testing.AllocsPerRun(20, func() {
118 if _, err := p.Prepare(cmds, 1, 100, 1000); err != nil {
119 t.Fatal(err)
120 }
121 })
122 if allocs > 2 {
123 t.Fatalf("%v allocs for 512 rects, want a small constant", allocs)
124 }
125}
126
127func BenchmarkPrepareBoxes(b *testing.B) {
128 p, err := NewPreparer(256, 2)
129 if err != nil {
130 b.Fatal(err)
131 }
132 cmds := rectCommands(512)
133 b.ReportAllocs()
134 b.ResetTimer()
135 for i := 0; i < b.N; i++ {
136 if f, err := p.Prepare(cmds, 1, 100, 1000); err != nil || len(f.Quads) != 512 {
137 b.Fatal(err)
138 }
139 }
140}