1package flash
2
3import (
4 "context"
5 "os"
6 "testing"
7 "time"
8
9 "github.com/bnema/neferclient"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12
13 "github.com/bnema/nefercap/internal/ports"
14)
15
16func TestLayerConfigWholeOutput(t *testing.T) {
17 c := layerConfig("DP-1", ports.Region{})
18 assert.Equal(t, "DP-1", c.Output)
19 assert.Equal(t, neferclient.AnchorTop|neferclient.AnchorBottom|neferclient.AnchorLeft|neferclient.AnchorRight, c.Anchors)
20 assert.Equal(t, [4]int32{}, c.Margin)
21 assert.Zero(t, c.Width)
22 assert.Zero(t, c.Height)
23 assert.Equal(t, neferclient.KeyboardNone, c.Keyboard)
24 assert.NotNil(t, c.InputRects, "an empty non-nil input region lets clicks through")
25 assert.Empty(t, c.InputRects)
26}
27
28func TestLayerConfigRegion(t *testing.T) {
29 c := layerConfig("DP-1", ports.Region{X: 10, Y: 20, Width: 300, Height: 200})
30 assert.Equal(t, neferclient.AnchorTop|neferclient.AnchorLeft, c.Anchors)
31 assert.Equal(t, [4]int32{20, 0, 0, 10}, c.Margin)
32 assert.Equal(t, int32(300), c.Width)
33 assert.Equal(t, int32(200), c.Height)
34 assert.NotNil(t, c.InputRects)
35 assert.Empty(t, c.InputRects)
36}
37
38func TestFadeSteps(t *testing.T) {
39 ctx, cancel := context.WithCancel(context.Background())
40 defer cancel()
41 f := &fade{done: cancel}
42 start := time.Unix(100, 0)
43 assert.Equal(t, "s0", f.class(start), "the fade starts with the first frame")
44 assert.Equal(t, "s0", f.class(start.Add(step-time.Millisecond)))
45 assert.Equal(t, "s1", f.class(start.Add(step)))
46 assert.Equal(t, "s9", f.class(start.Add(9*step)))
47 assert.NoError(t, ctx.Err())
48 assert.False(t, f.ended)
49 assert.Equal(t, "s9", f.class(start.Add(10*step)), "past the end the faintest step stays")
50 assert.Equal(t, "s9", f.class(start.Add(time.Hour)))
51 assert.ErrorIs(t, ctx.Err(), context.Canceled, "the fade ends the run")
52 assert.True(t, f.ended)
53 assert.Equal(t, "s0", (&fade{start: start, done: cancel}).class(start.Add(-time.Second)), "a reading before start never underflows")
54}
55
56func TestShowRequiresOutputName(t *testing.T) {
57 err := Show(context.Background(), ports.Output{ID: 3}, ports.Region{})
58 require.ErrorIs(t, err, ports.ErrOutputNotFound)
59}
60
61// TestSmokeShow shows the real flash. It needs a Wayland compositor with
62// layer-shell and Vulkan, so it runs only when NEFERCAP_FLASH_OUTPUT names the
63// wl_output to show it on (for example HEADLESS-1 on a NeferWL headless
64// compositor).
65func TestSmokeShow(t *testing.T) {
66 name := os.Getenv("NEFERCAP_FLASH_OUTPUT")
67 if name == "" {
68 t.Skip("set NEFERCAP_FLASH_OUTPUT to the output name on a Wayland session")
69 }
70 began := time.Now()
71 require.NoError(t, Show(context.Background(), ports.Output{ID: 1, Name: name}, ports.Region{X: 40, Y: 40, Width: 320, Height: 200}))
72 assert.Less(t, time.Since(began), Limit+time.Second)
73}