1package selection
2
3import (
4 "image"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9
10 "github.com/bnema/nefercap/internal/ports"
11)
12
13func TestBackdropImageIsOpaqueRGBA(t *testing.T) {
14 // One XRGB8888 pixel is B, G, R, X in memory; the stride has padding.
15 f := ports.Frame{Pixels: []byte{
16 0x10, 0x20, 0x30, 0x00, 0x40, 0x50, 0x60, 0x00, 0xee, 0xee, 0xee, 0xee,
17 0x70, 0x80, 0x90, 0x00, 0xa0, 0xb0, 0xc0, 0x00,
18 }, Width: 2, Height: 2, Stride: 12, Format: ports.XRGB8888}
19 require.NoError(t, f.Validate())
20 img := backdropImage(f)
21 assert.Equal(t, image.Rect(0, 0, 2, 2), img.Bounds())
22 assert.Equal(t, []byte{0x30, 0x20, 0x10, 0xff, 0x60, 0x50, 0x40, 0xff, 0x90, 0x80, 0x70, 0xff, 0xc0, 0xb0, 0xa0, 0xff}, img.Pix)
23}
24
25func TestWithBackdrops(t *testing.T) {
26 small := ports.Frame{Pixels: make([]byte, 4*4*4), Width: 4, Height: 4, Stride: 16, Format: ports.XRGB8888}
27 s, err := New(ports.Screenshot, ports.VideoSettings{}, nil, false).WithBackdrops(map[uint32]ports.Frame{7: small})
28 require.NoError(t, err)
29 assert.Contains(t, s.backdrops, uint32(7))
30
31 huge := ports.Frame{Width: maxBackdrop + 1, Height: 1, Stride: (maxBackdrop + 1) * 4, Format: ports.XRGB8888}
32 _, err = New(ports.Screenshot, ports.VideoSettings{}, nil, false).WithBackdrops(map[uint32]ports.Frame{9: huge})
33 assert.ErrorContains(t, err, "at most 4096", "the caller freezes only outputs CanBackdrop accepts")
34}
35
36func TestCanBackdrop(t *testing.T) {
37 assert.True(t, CanBackdrop(ports.Output{Width: 3840, Height: 2160}))
38 assert.True(t, CanBackdrop(ports.Output{Width: 2160, Height: 4096}))
39 assert.False(t, CanBackdrop(ports.Output{Width: 5120, Height: 2880}))
40 assert.False(t, CanBackdrop(ports.Output{}), "an unknown mode stays live")
41}
42
43// The frozen screen is drawn under a screenshot selection only: a recording
44// selects on the live screen.
45func TestBackdropDrawnOnlyForScreenshots(t *testing.T) {
46 m := newTestModel(t)
47 m.backdrop = image.NewNRGBA(image.Rect(0, 0, 1920, 1080))
48 assert.True(t, drawsBackdrop(m))
49 m.mode = ports.Record
50 assert.False(t, drawsBackdrop(m))
51 m.mode, m.backdrop = ports.Screenshot, nil
52 assert.False(t, drawsBackdrop(m))
53}