forked from ungerik/go-cairo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurface_test.go
161 lines (138 loc) · 3.78 KB
/
surface_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
// Package cairo wraps the c cairographics library.
package cairo
import (
"errors"
"os"
"testing"
)
func TestSurfaceCreation(t *testing.T) {
surface := NewSurface(800, 800)
status := surface.GetStatus()
if status != StatusSuccess {
t.Errorf("Expected status %v, got %v\n", StatusSuccess, status)
}
format := surface.GetFormat()
if format != FormatARGB32 {
t.Errorf("Expected format %v, got %v\n", FormatARGB32, format)
}
content := surface.GetContent()
if content != ContentColorAlpha {
t.Errorf("Expected content type %v, got %v\n", ContentColorAlpha, content)
}
expected := 800 * 4
stride := surface.GetStride()
if stride != expected {
t.Errorf("Expected stride %d, got %d\n", expected, stride)
}
width, height := surface.GetWidth(), surface.GetHeight()
if width != 800 {
t.Errorf("Expected width %d, got %d\n", 800, width)
}
if height != 800 {
t.Errorf("Expected height %d, got %d\n", 800, height)
}
}
func TestSurfaceFromPng(t *testing.T) {
surface, err := NewSurfaceFromPNG("testdata/gopher.png")
if err != nil {
t.Errorf("Unable to create png. Error: %v\n", err)
}
status := surface.GetStatus()
if status != StatusSuccess {
t.Errorf("Expected status %v, got %v\n", StatusSuccess, status)
}
format := surface.GetFormat()
if format != FormatRGB24 {
t.Errorf("Expected format %v, got %v\n", FormatRGB24, format)
}
content := surface.GetContent()
if content != ContentColor {
t.Errorf("Expected content type %v, got %v\n", ContentColor, content)
}
expected := 666 * 4
stride := surface.GetStride()
if stride != expected {
t.Errorf("Expected stride %d, got %d\n", expected, stride)
}
width, height := surface.GetWidth(), surface.GetHeight()
if width != 666 {
t.Errorf("Expected width %d, got %d\n", 666, width)
}
if height != 915 {
t.Errorf("Expected height %d, got %d\n", 915, height)
}
}
func TestFinish(t *testing.T) {
surface := NewSurface(800, 800)
context := NewContext(surface)
context.Rectangle(0, 0, 100, 100)
context.Stroke()
status := surface.GetStatus()
if status != StatusSuccess {
t.Errorf("Expected status %q, got %q\n", StatusSuccess, status)
}
surface.Finish()
context.Rectangle(0, 0, 100, 100)
context.Stroke()
status = surface.GetStatus()
if status != StatusSurfaceFinished {
t.Errorf("Expected status %q, got %q\n", StatusSurfaceFinished, status)
}
}
func TestWritePng(t *testing.T) {
path := "testdata/temp.png"
surface := NewSurface(800, 800)
err := surface.WriteToPNG(path)
if err != nil {
t.Errorf("Unable to save image. Error: %s\n", err)
}
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
t.Errorf("File was not saved.")
}
os.Remove(path)
}
func TestGetData(t *testing.T) {
surface := NewSurface(10, 10)
context := NewContext(surface)
context.SetSourceRGB(1, 0, 0)
context.Rectangle(0, 0, 10, 10)
context.Fill()
data, err := surface.GetData()
if err != nil {
t.Errorf("Unable to get image data. Error: %s\n", err)
}
expected := 10 * 10 * 4
dataLength := len(data)
if dataLength != expected {
t.Errorf("Expected data length of %d, got %d\n", expected, dataLength)
}
// blue
if data[0] != 0 {
t.Errorf("Expected data[0] to be %d, got %d\n", 0, data[0])
}
// green
if data[1] != 0 {
t.Errorf("Expected data[0] to be %d, got %d\n", 0, data[1])
}
// red
if data[2] != 255 {
t.Errorf("Expected data[0] to be %d, got %d\n", 255, data[2])
}
// alpha
if data[3] != 255 {
t.Errorf("Expected data[0] to be %d, got %d\n", 255, data[3])
}
}
func TestSetData(t *testing.T) {
surface := NewSurface(10, 10)
data := make([]byte, 10*10*4)
data[99] = 199
surface.SetData(data)
gotData, err := surface.GetData()
if err != nil {
t.Errorf("Unable to get image data. Error: %s\n", err)
}
if gotData[99] != 199 {
t.Errorf("Expected data[99] to be %d, got %d\n", 199, data[99])
}
}