1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
package notify
import (
"bytes"
"image"
"image/color"
"image/png"
"math"
"testing"
"github.com/godbus/dbus/v5"
)
// rawVariant builds the (iiibiiay) struct the bus delivers for image-data.
func rawVariant(w, h, stride int, alpha bool, ch int, data []byte) dbus.Variant {
return dbus.MakeVariant([]interface{}{
int32(w), int32(h), int32(stride), alpha, int32(8), int32(ch), data,
})
}
func TestImageDataFromHints(t *testing.T) {
// 2x1 RGBA: red, green.
rgba := []byte{255, 0, 0, 255, 0, 255, 0, 255}
cases := []struct {
name string
hints map[string]dbus.Variant
wantW int
want bool
}{
{"image-data wins", map[string]dbus.Variant{
"image-data": rawVariant(2, 1, 8, true, 4, rgba),
"icon_data": rawVariant(9, 9, 8, true, 4, rgba),
"image-path": dbus.MakeVariant("/tmp/x.png"),
}, 2, true},
{"image_data alias", map[string]dbus.Variant{
"image_data": rawVariant(2, 1, 8, true, 4, rgba),
}, 2, true},
{"icon_data is not tier 1", map[string]dbus.Variant{
"icon_data": rawVariant(2, 1, 8, true, 4, rgba),
}, 0, false},
{"absent", map[string]dbus.Variant{}, 0, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, ok := ImageDataFromHints(c.hints)
if ok != c.want {
t.Fatalf("ok = %v, want %v", ok, c.want)
}
if ok && got.Width != c.wantW {
t.Fatalf("width = %d, want %d", got.Width, c.wantW)
}
})
}
}
func TestIconDataFromHints(t *testing.T) {
rgba := []byte{255, 0, 0, 255, 0, 255, 0, 255}
if got, ok := IconDataFromHints(map[string]dbus.Variant{
"icon_data": rawVariant(2, 1, 8, true, 4, rgba),
}); !ok || got.Width != 2 {
t.Fatalf("icon_data got %v ok=%v", got, ok)
}
if _, ok := IconDataFromHints(map[string]dbus.Variant{
"image-data": rawVariant(2, 1, 8, true, 4, rgba),
}); ok {
t.Fatal("image-data must not be read as icon_data")
}
}
func TestImagePathFromHints(t *testing.T) {
got, ok := ImagePathFromHints(map[string]dbus.Variant{"image-path": dbus.MakeVariant("/tmp/shot.png")})
if !ok || got != "/tmp/shot.png" {
t.Fatalf("got %q ok=%v", got, ok)
}
if _, ok := ImagePathFromHints(map[string]dbus.Variant{}); ok {
t.Fatal("empty hints must not report a path")
}
}
// checkPixel asserts the RGBA of img at (x,y) matches want.
func checkPixel(t *testing.T, img image.Image, x, y int, want color.RGBA) {
t.Helper()
r, g, b, a := img.At(x, y).RGBA()
if r>>8 != uint32(want.R) || g>>8 != uint32(want.G) || b>>8 != uint32(want.B) || a>>8 != uint32(want.A) {
t.Fatalf("pixel (%d,%d) = %d %d %d %d, want %v", x, y, r>>8, g>>8, b>>8, a>>8, want)
}
}
func TestRawImagePNG(t *testing.T) {
// 2x2 RGBA with a rowstride wider than Width*Channels. The second row is
// the point: if stride were ignored, row 1 would read the row-0 padding.
r := &RawImage{Width: 2, Height: 2, RowStride: 12, HasAlpha: true, BitsPerSample: 8, Channels: 4,
Data: []byte{
255, 0, 0, 255, 0, 255, 0, 255, 9, 9, 9, 9, // row 0: red, green, pad
0, 0, 255, 255, 255, 255, 255, 255, 9, 9, 9, 9, // row 1: blue, white, pad
}}
data, err := r.PNG()
if err != nil {
t.Fatalf("PNG: %v", err)
}
img, err := png.Decode(bytes.NewReader(data))
if err != nil {
t.Fatalf("decode: %v", err)
}
if img.Bounds().Dx() != 2 || img.Bounds().Dy() != 2 {
t.Fatalf("bounds = %v", img.Bounds())
}
checkPixel(t, img, 0, 0, color.RGBA{255, 0, 0, 255})
checkPixel(t, img, 1, 0, color.RGBA{0, 255, 0, 255})
checkPixel(t, img, 0, 1, color.RGBA{0, 0, 255, 255})
checkPixel(t, img, 1, 1, color.RGBA{255, 255, 255, 255})
}
func TestRawImagePNGThreeChannel(t *testing.T) {
// 2x1 RGB, no alpha byte: PNG() must force opaque.
r := &RawImage{Width: 2, Height: 1, RowStride: 6, HasAlpha: false, BitsPerSample: 8, Channels: 3,
Data: []byte{255, 0, 0, 0, 255, 0}}
data, err := r.PNG()
if err != nil {
t.Fatalf("PNG: %v", err)
}
img, err := png.Decode(bytes.NewReader(data))
if err != nil {
t.Fatalf("decode: %v", err)
}
if img.Bounds().Dx() != 2 || img.Bounds().Dy() != 1 {
t.Fatalf("bounds = %v", img.Bounds())
}
checkPixel(t, img, 0, 0, color.RGBA{255, 0, 0, 255})
checkPixel(t, img, 1, 0, color.RGBA{0, 255, 0, 255})
}
func TestRawImagePNGRejectsBadData(t *testing.T) {
if _, err := (&RawImage{Width: 0, Height: 1, Channels: 4, BitsPerSample: 8}).PNG(); err == nil {
t.Fatal("zero width must error")
}
if _, err := (&RawImage{Width: 1, Height: 1, Channels: 2, BitsPerSample: 8}).PNG(); err == nil {
t.Fatal("channels 2 must error")
}
// 2x2 RGBA needs 16 bytes; the slice holds one row.
if _, err := (&RawImage{Width: 2, Height: 2, RowStride: 8, BitsPerSample: 8, Channels: 4,
Data: []byte{0, 0, 0, 255, 255, 255, 255, 255}}).PNG(); err == nil {
t.Fatal("short data must error")
}
// Dimensions straight off the bus: with RowStride 0 the stride*(Height-1)
// product overflows to negative and used to slip past the guard into
// image.NewRGBA. Must error, not panic.
if _, err := (&RawImage{
Width: 1<<31 - 1, Height: 1<<31 - 1, RowStride: 0,
BitsPerSample: 8, Channels: 4, Data: []byte{0, 0, 0, 255},
}).PNG(); err == nil {
t.Fatal("oversized dimensions must error")
}
// Small dimensions but RowStride at MaxInt: need wraps negative and the
// y*stride slice at y=1 panics. Must error, not panic.
if _, err := (&RawImage{
Width: 2, Height: 2, RowStride: math.MaxInt,
BitsPerSample: 8, Channels: 4,
Data: []byte{0, 0, 0, 255, 0, 0, 0, 255},
}).PNG(); err == nil {
t.Fatal("oversized rowstride must error")
}
if _, err := (&RawImage{
Width: 2, Height: 1, RowStride: -1,
BitsPerSample: 8, Channels: 4,
Data: []byte{0, 0, 0, 255, 0, 0, 0, 255},
}).PNG(); err == nil {
t.Fatal("negative rowstride must error")
}
}
|