-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_layer_test.go
More file actions
173 lines (140 loc) · 4.19 KB
/
context_layer_test.go
File metadata and controls
173 lines (140 loc) · 4.19 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
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
package gg
import (
"testing"
)
// TestPushPopLayer tests basic layer push/pop functionality.
func TestPushPopLayer(t *testing.T) {
dc := NewContext(100, 100)
originalPixmap := dc.pixmap
dc.PushLayer(BlendNormal, 1.0)
if dc.pixmap == originalPixmap {
t.Error("PushLayer should create a new pixmap")
}
if dc.layerStack == nil {
t.Error("PushLayer should initialize layer stack")
}
if len(dc.layerStack.layers) != 1 {
t.Errorf("Expected 1 layer, got %d", len(dc.layerStack.layers))
}
if dc.basePixmap == nil {
t.Error("PushLayer should save base pixmap")
}
dc.PopLayer()
if dc.pixmap != originalPixmap {
t.Error("PopLayer should restore original pixmap")
}
if len(dc.layerStack.layers) != 0 {
t.Errorf("Expected 0 layers after pop, got %d", len(dc.layerStack.layers))
}
if dc.basePixmap != nil {
t.Error("PopLayer should clear base pixmap")
}
}
// TestNestedLayers tests nested push/pop operations.
func TestNestedLayers(t *testing.T) {
dc := NewContext(100, 100)
dc.PushLayer(BlendNormal, 1.0)
layer1Pixmap := dc.pixmap
dc.PushLayer(BlendMultiply, 0.5)
layer2Pixmap := dc.pixmap
if layer1Pixmap == layer2Pixmap {
t.Error("Nested layers should have different pixmaps")
}
if len(dc.layerStack.layers) != 2 {
t.Errorf("Expected 2 layers, got %d", len(dc.layerStack.layers))
}
dc.PopLayer()
if dc.pixmap != layer1Pixmap {
t.Error("PopLayer should restore to layer 1 pixmap")
}
if len(dc.layerStack.layers) != 1 {
t.Errorf("Expected 1 layer after first pop, got %d", len(dc.layerStack.layers))
}
dc.PopLayer()
if len(dc.layerStack.layers) != 0 {
t.Errorf("Expected 0 layers after second pop, got %d", len(dc.layerStack.layers))
}
}
// TestLayerCompositing tests layer compositing with SetPixel.
func TestLayerCompositing(t *testing.T) {
dc := NewContext(10, 10)
dc.ClearWithColor(White)
dc.PushLayer(BlendNormal, 1.0)
dc.SetPixel(5, 5, RGBA{R: 1, G: 0, B: 0, A: 1})
layerPixel := dc.pixmap.GetPixel(5, 5)
if layerPixel.R != 1.0 {
t.Fatalf("Layer pixel should be red, got R=%f", layerPixel.R)
}
dc.PopLayer()
pixel := dc.pixmap.GetPixel(5, 5)
tolerance := 0.1
if abs(pixel.R-1.0) > tolerance {
t.Errorf("Expected R ~1.0, got %f", pixel.R)
}
}
// TestPopWithoutPush tests that PopLayer doesn't crash when no layer is pushed.
func TestPopWithoutPush(t *testing.T) {
dc := NewContext(100, 100)
dc.PopLayer() // Should not crash
if dc.pixmap == nil {
t.Error("PopLayer without PushLayer should not modify pixmap")
}
}
// TestLayerOpacityClamping tests that opacity is clamped to [0, 1].
func TestLayerOpacityClamping(t *testing.T) {
dc := NewContext(100, 100)
dc.PushLayer(BlendNormal, -0.5)
if dc.layerStack.layers[0].opacity != 0 {
t.Errorf("Expected opacity 0, got %f", dc.layerStack.layers[0].opacity)
}
dc.PopLayer()
dc.PushLayer(BlendNormal, 1.5)
if dc.layerStack.layers[0].opacity != 1 {
t.Errorf("Expected opacity 1, got %f", dc.layerStack.layers[0].opacity)
}
dc.PopLayer()
dc.PushLayer(BlendNormal, 0.7)
if dc.layerStack.layers[0].opacity != 0.7 {
t.Errorf("Expected opacity 0.7, got %f", dc.layerStack.layers[0].opacity)
}
dc.PopLayer()
}
// TestLayerClearTransparent tests that new layers start transparent.
func TestLayerClearTransparent(t *testing.T) {
dc := NewContext(10, 10)
dc.ClearWithColor(White)
dc.PushLayer(BlendNormal, 1.0)
pixel := dc.pixmap.GetPixel(5, 5)
if pixel.A != 0 {
t.Errorf("New layer should be transparent, got alpha %f", pixel.A)
}
dc.PopLayer()
}
// TestMultipleLayerCycles tests multiple push/pop cycles.
func TestMultipleLayerCycles(t *testing.T) {
dc := NewContext(50, 50)
for i := 0; i < 5; i++ {
dc.PushLayer(BlendNormal, 1.0)
dc.SetPixel(25, 25, RGBA{R: float64(i) / 5.0, G: 0, B: 0, A: 1})
dc.PopLayer()
}
if len(dc.layerStack.layers) != 0 {
t.Errorf("Expected 0 layers after cycles, got %d", len(dc.layerStack.layers))
}
if dc.basePixmap != nil {
t.Error("Expected basePixmap to be nil after all layers popped")
}
}
// TestSetBlendMode tests the SetBlendMode method.
func TestSetBlendMode(t *testing.T) {
dc := NewContext(100, 100)
dc.SetBlendMode(BlendMultiply)
dc.SetBlendMode(BlendScreen)
dc.SetBlendMode(BlendOverlay)
}
func abs(x float64) float64 {
if x < 0 {
return -x
}
return x
}