-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgif_test.go
More file actions
142 lines (120 loc) · 2.98 KB
/
Copy pathgif_test.go
File metadata and controls
142 lines (120 loc) · 2.98 KB
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
package ansipx
import (
"image"
"image/color"
"image/gif"
"os"
"testing"
"time"
)
func TestRenderGIFSingleFrame(t *testing.T) {
path := createTestGIF(t, 1, 10)
defer os.Remove(path)
opts := DefaultOptions()
opts.Width = 2
opts.Height = 2
opts.SizeMode = Stretch
frames, err := RenderGIF(path, opts)
if err != nil {
t.Fatalf("RenderGIF failed: %v", err)
}
if len(frames) != 1 {
t.Errorf("expected 1 frame, got %d", len(frames))
}
if len(frames[0].Content) == 0 {
t.Error("frame content is empty")
}
}
func TestRenderGIFMultiFrame(t *testing.T) {
path := createTestGIF(t, 5, 20)
defer os.Remove(path)
opts := DefaultOptions()
opts.Width = 2
opts.Height = 2
opts.SizeMode = Stretch
frames, err := RenderGIF(path, opts)
if err != nil {
t.Fatalf("RenderGIF failed: %v", err)
}
if len(frames) != 5 {
t.Errorf("expected 5 frames, got %d", len(frames))
}
// Check delays (20 centiseconds = 200ms)
for i, f := range frames {
if f.Delay != 200*time.Millisecond {
t.Errorf("frame %d: expected delay 200ms, got %v", i, f.Delay)
}
}
}
func TestRenderGIFMaxFrames(t *testing.T) {
path := createTestGIF(t, 150, 5)
defer os.Remove(path)
opts := DefaultOptions()
opts.Width = 2
opts.Height = 2
opts.SizeMode = Stretch
frames, err := RenderGIF(path, opts)
if err != nil {
t.Fatalf("RenderGIF failed: %v", err)
}
if len(frames) != 100 {
t.Errorf("expected 100 frames (capped), got %d", len(frames))
}
}
func TestRenderGIFZeroDelay(t *testing.T) {
path := createTestGIF(t, 3, 0) // 0 delay
defer os.Remove(path)
opts := DefaultOptions()
opts.Width = 2
opts.Height = 2
opts.SizeMode = Stretch
frames, err := RenderGIF(path, opts)
if err != nil {
t.Fatalf("RenderGIF failed: %v", err)
}
for i, f := range frames {
if f.Delay != 100*time.Millisecond {
t.Errorf("frame %d: zero delay should default to 100ms, got %v", i, f.Delay)
}
}
}
// createTestGIF builds a simple animated GIF with solid-color frames
// and writes it to a temp file. Returns the file path.
func createTestGIF(t *testing.T, numFrames, delayCentiseconds int) string {
t.Helper()
f, err := os.CreateTemp("", "test-*.gif")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
g := &gif.GIF{}
colors := []color.Color{
color.RGBA{R: 255, A: 255},
color.RGBA{G: 255, A: 255},
color.RGBA{B: 255, A: 255},
}
for i := 0; i < numFrames; i++ {
img := image.NewPaletted(image.Rect(0, 0, 4, 4), color.Palette{
color.Transparent,
colors[i%len(colors)],
})
// Fill with the solid color
for y := 0; y < 4; y++ {
for x := 0; x < 4; x++ {
img.SetColorIndex(x, y, 1)
}
}
g.Image = append(g.Image, img)
g.Delay = append(g.Delay, delayCentiseconds)
g.Disposal = append(g.Disposal, gif.DisposalBackground)
}
g.Config = image.Config{
ColorModel: color.Palette{color.Transparent, color.White},
Width: 4,
Height: 4,
}
if err := gif.EncodeAll(f, g); err != nil {
t.Fatalf("failed to encode GIF: %v", err)
}
f.Close()
return f.Name()
}