This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
main_test.go
189 lines (164 loc) · 3.31 KB
/
main_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
package thumbnailer
import (
"fmt"
"image"
"image/png"
"os"
"path/filepath"
"testing"
)
var samples = [...]string{
"no_cover.mp4",
"no_sound.mkv",
"no_sound.ogg",
"sample.gif",
"with_sound.avi",
"no_cover.flac",
"no_cover.ogg",
"no_sound.mov",
"no_sound.webm",
"with_sound_vp9.webm",
"sample.jpg",
"with_cover.mp3",
"with_sound.mkv",
"with_sound.ogg",
"no_sound.avi",
"no_sound.mp4",
"no_sound.wmv",
"no_sound_90.mp4",
"no_sound_180.mp4",
"no_sound_270.mp4",
"sample.webp",
"with_sound.mov",
"with_sound.webm",
"no_cover.mp3",
"no_magic.mp3", // No magic numbers
"no_sound.flv",
"sample.png",
"with_cover.flac",
"with_sound.mp4",
"with_sound_90.mp4",
"with_sound_hevc.mp4",
"odd_dimensions.webm", // Unconventional dims for a YUV stream
"alpha.webm",
"start_black.webm", // Check the histogram thumbnailing
"rare_brand.mp4",
"invalid_data.jpg", // Check handling images with some invalid data
"sample.zip",
"sample.rar",
"too small.png",
"exact_thumb_size.jpg",
"meta_segfault.mp4",
// Exif rotation compensation
"jannu_baseline.jpg",
"jannu_h_mirrored.jpg",
"jannu_180.jpg",
"jannu_v_mirrored.jpg",
"jannu_270_h_mirrored.jpg",
"jannu_90.jpg",
"jannu_90_h_mirrored.jpg",
"jannu_270.jpg",
}
var ignore = map[string]bool{
"invalid_data.jpg": true,
"sample.zip": true,
"sample.rar": true,
}
func TestProcess(t *testing.T) {
t.Parallel()
opts := Options{
ThumbDims: Dims{150, 150},
}
for i := range samples {
sample := samples[i]
t.Run(sample, func(t *testing.T) {
t.Parallel()
f := openSample(t, sample)
defer f.Close()
src, thumb, err := Process(f, opts)
if err != nil && err != ErrCantThumbnail {
t.Fatal(err)
}
if err != ErrCantThumbnail {
name := fmt.Sprintf(`%s_thumb.png`, sample)
writeSample(t, name, thumb)
}
t.Logf("src: %v\n", src)
if thumb != nil {
t.Logf("thumb: %v\t\n", thumb.Bounds())
}
})
}
}
func openSample(t *testing.T, name string) *os.File {
t.Helper()
f, err := os.Open(filepath.Join("testdata", name))
if err != nil {
t.Fatal(err)
}
return f
}
func writeSample(t *testing.T, name string, img image.Image) {
t.Helper()
f, err := os.Create(filepath.Join("testdata", name))
if err != nil {
t.Fatal(err)
}
defer f.Close()
png.Encode(f, img)
if err != nil {
t.Fatal(err)
}
}
func TestErrorPassing(t *testing.T) {
t.Parallel()
f := openSample(t, "sample.txt")
defer f.Close()
_, _, err := Process(f, Options{
ThumbDims: Dims{
Width: 150,
Height: 150,
},
})
if err == nil {
t.Fatal(`expected error`)
}
}
func TestSourceAlreadyThumbSize(t *testing.T) {
t.Parallel()
f := openSample(t, "too small.png")
defer f.Close()
_, thumb, err := Process(f, Options{
ThumbDims: Dims{
Width: 150,
Height: 150,
},
})
if err != nil {
t.Fatal(err)
}
dims := thumb.Bounds().Max
if dims.X != 121 {
t.Errorf("unexpected width: 121 : %d", dims.X)
}
if dims.Y != 150 {
t.Errorf("unexpected height: 150: %d", dims.Y)
}
}
func TestUnprocessedLine(t *testing.T) {
t.Parallel()
const sample = "jannu_180.jpg"
f := openSample(t, sample)
defer f.Close()
_, thumb, err := Process(f, Options{
ThumbDims: Dims{
Width: 300,
Height: 300,
},
})
if err != nil {
t.Fatal(err)
}
name := fmt.Sprintf(`%s_to_300x300_thumb.png`, sample)
writeSample(t, name, thumb)
}