1package wayland
2
3import (
4 "fmt"
5 "strings"
6 "testing"
7 "time"
8
9 "github.com/bnema/go-wayland-bindings/server/wayland"
10 "github.com/bnema/zerowrap"
11)
12
13// --debug=wayland-damage logs one "client damage" entry per fresh commit
14// with the damage in buffer pixels; without it nothing is logged.
15func TestClientDamageLog(t *testing.T) {
16 for _, on := range []bool{true, false} {
17 t.Run(fmt.Sprintf("LogDamage=%v", on), func(t *testing.T) {
18 var out lockedBuffer
19 log := zerowrap.New(zerowrap.Config{Level: "debug", Format: "json", Output: &out}).WithField("component", "wayland")
20 s, events, _, contents, dir := contentServerWith(t, Options{LogDamage: on}, log)
21 c := protocolClient(t, s, dir)
22 // A mapped window (its 1×1 first buffer is not under test), then
23 // the 16×16 buffer the commits below attach.
24 _, surf, _ := surfaceMapperAt(t, c, events, 4)()
25 drainContents(contents)
26 buf := shmBufferSized(t, c, 16, 16)
27 commit := func(rects ...[4]int32) {
28 t.Helper()
29 requestProtocol(t, c, surf, wayland.SurfaceRequestAttach, buf, int32(0), int32(0))
30 for _, r := range rects {
31 requestProtocol(t, c, surf, wayland.SurfaceRequestDamageBuffer, r[0], r[1], r[2], r[3])
32 }
33 requestProtocol(t, c, surf, wayland.SurfaceRequestCommit)
34 if err := c.Roundtrip(); err != nil {
35 t.Fatal(err)
36 }
37 select {
38 case <-contents:
39 case <-time.After(2 * time.Second):
40 t.Fatal("no content")
41 }
42 }
43 // First buffer: a new size, full. Then a small rect, then one
44 // covering the whole buffer.
45 commit([4]int32{0, 0, 4, 4})
46 commit([4]int32{2, 3, 4, 5})
47 commit([4]int32{0, 0, 16, 16})
48 var damage []string
49 for _, l := range strings.Split(strings.TrimSpace(out.String()), "\n") {
50 if strings.Contains(l, `"client damage"`) {
51 damage = append(damage, l)
52 }
53 }
54 if !on {
55 if len(damage) != 0 {
56 t.Fatalf("logged with LogDamage off: %q", damage)
57 }
58 return
59 }
60 // The mapping commit logged one entry of its own.
61 if len(damage) != 4 {
62 t.Fatalf("%d client damage entries, want 4 (mapping commit and 3 commits): %q", len(damage), damage)
63 }
64 damage = damage[1:]
65 for i, want := range []string{`"full_buffer":true`, `"full_buffer":false`, `"full_buffer":true`} {
66 if !strings.Contains(damage[i], want) {
67 t.Errorf("entry %d lacks %s: %s", i, want, damage[i])
68 }
69 }
70 for _, want := range []string{`"component":"wayland"`, `"bw":16`, `"bh":16`, `"rects":1`, `"window":`, `"first_rects":[{"x":2,"y":3,"w":4,"h":5}]`} {
71 if !strings.Contains(damage[1], want) {
72 t.Errorf("entry 1 lacks %s: %s", want, damage[1])
73 }
74 }
75 })
76 }
77}