-
Notifications
You must be signed in to change notification settings - Fork 0
/
gof_test.go
104 lines (89 loc) · 2.28 KB
/
gof_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
package gof
import (
"image"
"testing"
"time"
)
func TestGofPlayerCreation(t *testing.T) {
_, err := New("./assets/test.gif", func(img *image.Image) {})
if err != nil {
t.Errorf("Failed to create GofPlayer instance")
}
}
func TestGofPlayerRender(t *testing.T) {
ch := make(chan string)
gp, _ := New("./assets/test.gif", func(img *image.Image) {
ch <- "rendered"
})
select {
case <-ch:
// Do nothing
case <-time.After(gp.delay):
t.Errorf("Expected render to be called within %s", gp.delay)
}
}
/**
* The below tests rely on the test.gif file specifically which contains a
* single pixel gif that increases it's R value in the RGBA color of the pixel.
* This makes it easy to test if the playback controls are working as expected
*/
func TestGofRenderOrderPlay(t *testing.T) {
ch := make(chan *image.Image)
gp, _ := New("./assets/test.gif", func(img *image.Image) {
ch <- img
})
// Redundant direction set for readability
gp.SetFrameDirection(PLAY)
var prev *image.Image = nil
frame := 0
frameCount := len(gp.frames)
for img := range ch {
frame++
if frame >= frameCount {
// Consider this test done after looping through the full GIF
break
}
if prev == nil {
prev = img
continue
}
r, _, _, _ := (*img).At(0, 0).RGBA()
oldR, _, _, _ := (*prev).At(0, 0).RGBA()
if r <= oldR {
t.Errorf("Expected frames to be played in an increasing order")
}
prev = img
}
}
func TestGofRenderOrderRewind(t *testing.T) {
ch := make(chan *image.Image)
gp, _ := New("./assets/test.gif", func(img *image.Image) {
ch <- img
})
gp.SetFrameDirection(REWIND)
var prev *image.Image = nil
frame := 0
frameCount := len(gp.frames)
for img := range ch {
frame++
if frame >= frameCount {
// Consider this test done after looping through the full GIF
break
}
if prev == nil {
prev = img
continue
}
r, _, _, _ := (*img).At(0, 0).RGBA()
oldR, _, _, _ := (*prev).At(0, 0).RGBA()
/**
* Only enforce this once passed the first two frames
* On reverse, the first frame is still the 0th frame so going from 0th to (len - 1)
* will result in this test failing.
*/
if r >= oldR && frame > 2 { // The first frame is the 0th frame so going in reverse ...
t.Errorf("Expected frames to be played in an decreasing order")
}
prev = img
}
}