detached bcc9a45

prepare_test.go RawHistoryFolder

1package render
2
3import (
4	"image"
5	"math"
6	"strings"
7	"testing"
8
9	"github.com/bnema/nefergui/internal/css"
10	"github.com/bnema/nefergui/internal/layout"
11	"github.com/bnema/nefergui/internal/text"
12)
13
14func TestClipStackAndPhysical(t *testing.T) {
15	p, err := NewPreparer(128, 2)
16	if err != nil {
17		t.Fatal(err)
18	}
19	color := css.Color{R: 1, A: 1}
20	cmds := []layout.Command{
21		{Op: "clip-push", Rect: layout.Rect{X: 1, Y: 2, W: 8, H: 7}},
22		{Op: "clip-push", Rect: layout.Rect{X: 4, Y: 1, W: 10, H: 4}},
23		{Op: "selection", Rect: layout.Rect{W: 20, H: 20}, Color: color, Opacity: .5},
24		{Op: "clip-pop"},
25		{Op: "caret", Rect: layout.Rect{X: 2, Y: 3, W: 1, H: 1}, Color: color, Opacity: 1},
26		{Op: "clip-pop"},
27	}
28	f, err := p.Prepare(cmds, 1.25, 100, 100)
29	if err != nil {
30		t.Fatal(err)
31	}
32	if len(f.Quads) != 2 {
33		t.Fatalf("quads: %+v", f.Quads)
34	}
35	a, b := f.Quads[0], f.Quads[1]
36	if a.Clip != (layout.Rect{X: 5, Y: 3, W: 6, H: 3}) || a.Color.A != .5 || b.Clip != (layout.Rect{X: 1, Y: 3, W: 10, H: 8}) {
37		t.Fatalf("clip/opacity: %+v %+v", a, b)
38	}
39	for _, bad := range [][]layout.Command{{{Op: "clip-pop"}}, {{Op: "clip-push"}}, {{Op: "unrecognized"}}, {{Op: "image", ID: "missing"}}} {
40		if _, err := p.Prepare(bad, 1, 100, 100); err == nil {
41			t.Fatalf("accepted %+v", bad)
42		}
43	}
44}
45
46func TestImageQuad(t *testing.T) {
47	p, err := NewPreparer(128, 2)
48	if err != nil {
49		t.Fatal(err)
50	}
51	img := image.NewNRGBA(image.Rect(0, 0, 2, 2))
52	f, err := p.Prepare([]layout.Command{{Op: "image", Rect: layout.Rect{X: 1, Y: 2, W: 20, H: 10}, Image: img, Opacity: .5, Radii: [4]float64{4, 3, 2, 1}}}, 2, 100, 100)
53	if err != nil || len(f.Quads) != 1 {
54		t.Fatalf("prepare: %+v, %v", f, err)
55	}
56	q := f.Quads[0]
57	if q.Image != img || q.Op != "image" || q.Rect != (layout.Rect{X: 2, Y: 4, W: 40, H: 20}) || q.Clip != (layout.Rect{W: 100, H: 100}) || q.Color.A != .5 || q.Radii != ([4]float64{8, 6, 4, 2}) {
58		t.Fatalf("image quad: %+v", q)
59	}
60}
61
62func TestGlyphRasterAndAtlas(t *testing.T) {
63	catalog, err := text.Load(text.DirectorySource("../../testdata/fonts"))
64	if err != nil {
65		t.Fatal(err)
66	}
67	measured, err := text.NewEngine(catalog).Measure("office سلام 日本語", text.Request{Families: []string{"Noto Sans"}, Size: 20}, 0)
68	if err != nil {
69		t.Fatal(err)
70	}
71	cmd := layout.Command{Op: "text", Color: css.Color{A: 1}, Opacity: 1}
72	for _, line := range measured.Lines {
73		for _, shaped := range line.Runs {
74			r := layout.Run{Face: shaped.Face, FaceID: shaped.Face.ID, Size: 20}
75			for _, g := range shaped.Glyphs {
76				r.Glyphs = append(r.Glyphs, layout.Glyph{ID: uint32(g.ID), X: 6 + g.X, Y: 6 + g.Y})
77			}
78			cmd.Runs = append(cmd.Runs, r)
79		}
80	}
81	p, err := NewPreparer(1024, 2)
82	if err != nil {
83		t.Fatal(err)
84	}
85	for _, scale := range []float64{1, 1.25, 1.5, 2} {
86		f, err := p.Prepare([]layout.Command{cmd}, scale, int(math.Ceil((measured.Width+12)*scale)), 100)
87		if err != nil {
88			t.Fatal(err)
89		}
90		if len(f.Quads) < 10 || len(f.Uploads) == 0 {
91			t.Fatalf("scale %g: %d glyphs %d uploads", scale, len(f.Quads), len(f.Uploads))
92		}
93		p.Submitted(f.Uploads)
94		f, err = p.Prepare([]layout.Command{cmd}, scale, int(math.Ceil((measured.Width+12)*scale)), 100)
95		if err != nil || len(f.Uploads) != 0 {
96			t.Fatalf("scale %g: second frame uploads=%d err=%v", scale, len(f.Uploads), err)
97		}
98	}
99	cmd.Runs[0].FaceID = "wrong"
100	if _, err := p.Prepare([]layout.Command{cmd}, 1, 400, 100); err == nil || !strings.Contains(err.Error(), "mismatched") {
101		t.Fatalf("accepted mismatched face: %v", err)
102	}
103}
104
105func glyphCommand(t *testing.T, str string) layout.Command {
106	t.Helper()
107	catalog, err := text.Load(text.DirectorySource("../../testdata/fonts"))
108	if err != nil {
109		t.Fatal(err)
110	}
111	measured, err := text.NewEngine(catalog).Measure(str, text.Request{Families: []string{"Noto Sans"}, Size: 20}, 0)
112	if err != nil {
113		t.Fatal(err)
114	}
115	cmd := layout.Command{Op: "text", Color: css.Color{A: 1}, Opacity: 1}
116	for _, line := range measured.Lines {
117		for _, shaped := range line.Runs {
118			r := layout.Run{Face: shaped.Face, FaceID: shaped.Face.ID, Size: 20}
119			for _, g := range shaped.Glyphs {
120				r.Glyphs = append(r.Glyphs, layout.Glyph{ID: uint32(g.ID), X: 6 + g.X, Y: 6 + g.Y})
121			}
122			cmd.Runs = append(cmd.Runs, r)
123		}
124	}
125	return cmd
126}
127
128func uploadBytes(us []text.Upload) (n int) {
129	for _, u := range us {
130		n += len(u.Bytes)
131	}
132	return n
133}
134
135func TestUploadsStayOwedUntilSubmitted(t *testing.T) {
136	cmd := glyphCommand(t, "office")
137	p, err := NewPreparer(256, 2)
138	if err != nil {
139		t.Fatal(err)
140	}
141	prep := func() Frame {
142		f, err := p.Prepare([]layout.Command{cmd}, 1, 200, 100)
143		if err != nil {
144			t.Fatal(err)
145		}
146		return f
147	}
148	first := prep()
149	if len(first.Uploads) == 0 {
150		t.Fatal("no uploads")
151	}
152	// No submit (deferred/retried): the same placements are resent, as full pages.
153	retry := prep()
154	if len(retry.Uploads) == 0 || len(retry.Quads) != len(first.Quads) {
155		t.Fatalf("retry uploads=%d quads=%d/%d", len(retry.Uploads), len(retry.Quads), len(first.Quads))
156	}
157	for i := range first.Quads {
158		if first.Quads[i].Glyph != retry.Quads[i].Glyph {
159			t.Fatal("placements moved on retry")
160		}
161	}
162	if got := retry.Uploads[0].Rect; got.Dx() != 256 || got.Dy() != 256 {
163		t.Fatalf("retry rect %v not full page", got)
164	}
165	// Acknowledging a stale frame's uploads is ignored.
166	p.Submitted(first.Uploads)
167	again := prep()
168	if len(again.Uploads) == 0 {
169		t.Fatal("stale acknowledgement discarded owed uploads")
170	}
171	// Successful submit clears the debt; an unchanged frame uploads nothing.
172	p.Submitted(again.Uploads)
173	if f := prep(); len(f.Uploads) != 0 {
174		t.Fatalf("uploads after ack: %d", len(f.Uploads))
175	}
176	// Repeated acknowledgement is harmless.
177	p.Submitted(again.Uploads)
178	if f := prep(); len(f.Uploads) != 0 {
179		t.Fatalf("uploads after repeated ack: %d", len(f.Uploads))
180	}
181}
182
183func TestSupersededFrameKeepsAbandonedAtlasChanges(t *testing.T) {
184	a, b := glyphCommand(t, "office"), glyphCommand(t, "wxyz日本語")
185	p, err := NewPreparer(256, 2)
186	if err != nil {
187		t.Fatal(err)
188	}
189	// Frame 1 is prepared and acknowledged: the GPU mirror holds "office".
190	f, err := p.Prepare([]layout.Command{a}, 1, 400, 100)
191	if err != nil {
192		t.Fatal(err)
193	}
194	p.Submitted(f.Uploads)
195	// Frame 2 adds glyphs but is abandoned before submission.
196	old, err := p.Prepare([]layout.Command{a, b}, 1, 400, 100)
197	if err != nil || len(old.Uploads) == 0 {
198		t.Fatalf("old frame: %v uploads=%d", err, len(old.Uploads))
199	}
200	// Frame 3 does not use frame 2's glyphs, yet must carry its atlas changes.
201	latest, err := p.Prepare([]layout.Command{a}, 1, 400, 100)
202	if err != nil {
203		t.Fatal(err)
204	}
205	if uploadBytes(latest.Uploads) < uploadBytes(old.Uploads) {
206		t.Fatalf("latest uploads %d < abandoned %d", uploadBytes(latest.Uploads), uploadBytes(old.Uploads))
207	}
208	// The abandoned frame's late acknowledgement must not clear the debt.
209	p.Submitted(old.Uploads)
210	if next, err := p.Prepare([]layout.Command{a}, 1, 400, 100); err != nil || len(next.Uploads) == 0 {
211		t.Fatalf("debt lost: %v uploads=%d", err, len(next.Uploads))
212	}
213}
214
215func TestFailedPrepareKeepsOwedUploads(t *testing.T) {
216	cmd := glyphCommand(t, "office")
217	p, err := NewPreparer(256, 2)
218	if err != nil {
219		t.Fatal(err)
220	}
221	if _, err = p.Prepare([]layout.Command{cmd}, 1, 200, 100); err != nil {
222		t.Fatal(err)
223	}
224	if _, err = p.Prepare([]layout.Command{cmd, {Op: "clip-pop"}}, 1, 200, 100); err == nil {
225		t.Fatal("expected failure")
226	}
227	f, err := p.Prepare([]layout.Command{cmd}, 1, 200, 100)
228	if err != nil || len(f.Uploads) == 0 {
229		t.Fatalf("uploads lost after failed prepare: %v %d", err, len(f.Uploads))
230	}
231}
232
233func TestFirstPrepareFailureKeepsUndrainedAtlasChanges(t *testing.T) {
234	cmd := glyphCommand(t, "office")
235	p, err := NewPreparer(256, 2)
236	if err != nil {
237		t.Fatal(err)
238	}
239	// Glyphs are inserted, then the frame fails before uploads are drained.
240	if _, err = p.Prepare([]layout.Command{cmd, {Op: "clip-pop"}}, 1, 200, 100); err == nil {
241		t.Fatal("expected failure")
242	}
243	f, err := p.Prepare([]layout.Command{cmd}, 1, 200, 100)
244	if err != nil || len(f.Uploads) == 0 || len(f.Quads) == 0 {
245		t.Fatalf("undrained changes lost: %v uploads=%d quads=%d", err, len(f.Uploads), len(f.Quads))
246	}
247	p.Submitted(f.Uploads)
248	if g, err := p.Prepare([]layout.Command{cmd}, 1, 200, 100); err != nil || len(g.Uploads) != 0 {
249		t.Fatalf("uploads after ack: %v %d", err, len(g.Uploads))
250	}
251}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 12

↑↓ moveenter openesc close