-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathicns_test.go
371 lines (356 loc) · 6.01 KB
/
icns_test.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package icns
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"reflect"
"testing"
)
// TestDecode relies on Encode being correct.
// We are testing that an ICNS with a series of icons will only yield the
// largest icon in the series.
func TestDecode(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
input image.Image
want image.Image
}{
{
"valid square icon, exact size",
rect(0, 0, 256, 256),
rect(0, 0, 256, 256),
},
{
"non exact size",
rect(0, 0, 50, 50),
rect(0, 0, 32, 32),
},
}
for _, tt := range tests {
t.Run(tt.desc, func(st *testing.T) {
buf := bytes.NewBuffer(nil)
if err := Encode(buf, tt.input); err != nil {
st.Fatalf("unexpected error while encoding: %v", err)
}
img, err := Decode(buf)
if err != nil {
st.Fatalf("unexpected error: %v", err)
}
if tt.want != nil && !imageCompare(img, tt.want) {
st.Fatalf("decoded image is incorrect")
}
})
}
}
func imageCompare(left, right image.Image) bool {
if left == nil && right == nil {
return true
}
if left == nil && right != nil {
return false
}
if left != nil && right == nil {
return false
}
lb := left.Bounds()
for ii := lb.Min.X; ii <= lb.Max.X; ii++ {
for kk := lb.Min.Y; kk <= lb.Max.Y; kk++ {
lr, lg, lb, la := left.At(ii, kk).RGBA()
rr, rg, rb, ra := right.At(ii, kk).RGBA()
if lr != rr || lg != rg || lb != rb || la != ra {
return false
}
}
}
return true
}
// TestEncode tests for input validation, sanity checks and errors.
// The validity of the encoding is not tested here.
// Super large images are not tested because the resizing takes too
// long for unit testing.
func TestEncode(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
wr io.Writer
img image.Image
wantErr bool
}{
{
"nil image",
io.Discard,
nil,
true,
},
{
"nil writer",
nil,
rect(0, 0, 50, 50),
true,
},
{
"valid sqaure",
io.Discard,
rect(0, 0, 50, 50),
false,
},
{
"valid non-square",
io.Discard,
rect(0, 0, 10, 50),
false,
},
{
"valid non-square, weird dimensions",
io.Discard,
rect(0, 0, 17, 77),
false,
},
{
"invalid zero img",
io.Discard,
rect(0, 0, 0, 0),
true,
},
{
"invalid small img",
io.Discard,
rect(0, 0, 1, 1),
true,
},
{
"valid square not at origin point",
io.Discard,
rect(10, 10, 50, 50),
false,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(st *testing.T) {
err := Encode(tt.wr, tt.img)
if !tt.wantErr && err != nil {
st.Fatalf("unexpected error: %v", err)
}
})
}
}
func TestSizesFromMax(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
from uint
want []uint
}{
{
"small",
100,
[]uint{64, 32, 16},
},
{
"large",
99999,
[]uint{1024, 512, 256, 128, 64, 32, 16},
},
{
"smallest",
0,
[]uint{},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(st *testing.T) {
got := sizesFrom(tt.from)
if !reflect.DeepEqual(got, tt.want) {
st.Errorf("want=%d, got=%d", tt.want, got)
}
})
}
}
func TestBiggestSide(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
img image.Image
want uint
}{
{
"equal",
rect(0, 0, 100, 100),
100,
},
{
"right larger",
rect(0, 0, 50, 100),
100,
},
{
"left larger",
rect(0, 0, 100, 50),
100,
},
{
"off by one",
rect(0, 0, 100, 99),
100,
},
{
"empty",
rect(0, 0, 0, 0),
0,
},
{
"left empty",
rect(0, 0, 0, 10),
10,
},
{
"right empty",
rect(0, 0, 10, 0),
10,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(st *testing.T) {
got := biggestSide(tt.img)
if got != tt.want {
st.Errorf("want=%d, got=%d", tt.want, got)
}
})
}
}
func TestFindNearestSize(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
img image.Image
want uint
}{
{
"small",
rect(0, 0, 100, 100),
64,
},
{
"very large",
rect(0, 0, 123456789, 123456789),
1024,
},
{
"too small",
rect(0, 0, 15, 15),
0,
},
{
"off by one",
rect(0, 0, 33, 33),
32,
},
{
"exact",
rect(0, 0, 256, 256),
256,
},
{
"exact",
rect(0, 0, 1024, 1024),
1024,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(st *testing.T) {
got := findNearestSize(tt.img)
if tt.want != got {
st.Errorf("want=%d, got=%d", tt.want, got)
}
})
}
}
func TestEncodeImage(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
img image.Image
format string
want string
}{
{
"png - png",
_decode(_png(rect(0, 0, 50, 50))),
"png",
"png",
},
{
"default png - png",
_decode(_png(rect(0, 0, 50, 50))),
"",
"png",
},
{
"jpg - jpg",
_decode(_jpg(rect(0, 0, 50, 50))),
"jpeg",
"png",
},
{
"default jpg - png",
_decode(_jpg(rect(0, 0, 50, 50))),
"",
"png",
},
{
"invalid format identifier",
_decode(_jpg(rect(0, 0, 50, 50))),
"asdf",
"png",
},
{
"not actually a jpeg",
_decode(_png(rect(0, 0, 50, 50))),
"jpeg",
"png",
},
}
for _, tt := range tests {
t.Run(tt.desc, func(st *testing.T) {
data, err := encodeImage(tt.img)
if err != nil {
st.Fatalf("encoding image: %v", err)
}
_, f, err := image.Decode(bytes.NewBuffer(data))
if err != nil {
st.Fatalf("decoding iamge: %v", err)
}
if f != tt.want {
st.Fatalf("formats: want=%s, got=%s", tt.want, f)
}
})
}
}
func rect(x0, y0, x1, y1 int) image.Image {
return image.Rect(x0, y0, x1, y1)
}
func _png(img image.Image) io.Reader {
buf := bytes.NewBuffer(nil)
if err := png.Encode(buf, img); err != nil {
panic(fmt.Errorf("encoding png: %w", err))
}
return buf
}
func _jpg(img image.Image) io.Reader {
buf := bytes.NewBuffer(nil)
if err := jpeg.Encode(buf, img, nil); err != nil {
panic(fmt.Errorf("encoding jpeg: %w", err))
}
return buf
}
func _decode(r io.Reader) image.Image {
m, _, err := image.Decode(r)
if err != nil {
panic(fmt.Errorf("decoding image: %w", err))
}
return m
}