-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawframe_test.go
More file actions
208 lines (176 loc) · 4.97 KB
/
Copy pathdrawframe_test.go
File metadata and controls
208 lines (176 loc) · 4.97 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package microui
import (
"testing"
"github.com/user/microui-go/types"
)
func TestDrawFrame_CustomCallback(t *testing.T) {
callCount := 0
customDrawFrame := func(ui *UI, rect types.Rect, colorID int) {
callCount++
}
ui := New(Config{
DrawFrame: customDrawFrame,
})
ui.BeginFrame()
ui.BeginWindow("Test", types.Rect{X: 0, Y: 0, W: 200, H: 150})
ui.LayoutRow(1, []int{-1}, 0)
ui.Button("Click") // Should trigger draw_frame
ui.EndWindow()
ui.EndFrame()
if callCount == 0 {
t.Error("Custom DrawFrame callback was never called")
}
}
func TestDrawFrame_DefaultProducesCommands(t *testing.T) {
ui := New(Config{})
ui.BeginFrame()
ui.BeginWindow("Test", types.Rect{X: 0, Y: 0, W: 200, H: 150})
ui.LayoutRow(1, []int{-1}, 0)
ui.Button("Click")
ui.EndWindow()
ui.EndFrame()
// Should have rect commands from default draw_frame
hasRect := false
ui.commands.Each(func(cmd Command) {
if cmd.Kind == CmdRect {
hasRect = true
}
})
if !hasRect {
t.Error("Default DrawFrame should produce CmdRect commands")
}
}
func TestDrawFrame_ColorIDsProvided(t *testing.T) {
colorIDs := make(map[int]bool)
customDrawFrame := func(ui *UI, rect types.Rect, colorID int) {
colorIDs[colorID] = true
}
ui := New(Config{
DrawFrame: customDrawFrame,
})
ui.BeginFrame()
ui.BeginWindow("Test", types.Rect{X: 0, Y: 0, W: 200, H: 150})
ui.LayoutRow(1, []int{-1}, 0)
ui.Button("Normal") // Button color
ui.EndWindow()
ui.EndFrame()
// Should have received button color ID
if !colorIDs[ColorButton] && !colorIDs[ColorBase] {
t.Error("DrawFrame should receive control color IDs")
}
}
func TestDrawFrame_ColorConstants(t *testing.T) {
// Verify color constants are defined correctly (matching C microui ordering)
expectedValues := map[int]string{
ColorText: "ColorText",
ColorBorder: "ColorBorder",
ColorWindowBG: "ColorWindowBG",
ColorTitleBG: "ColorTitleBG",
ColorTitleText: "ColorTitleText",
ColorPanelBG: "ColorPanelBG",
ColorButton: "ColorButton",
ColorButtonHover: "ColorButtonHover",
ColorButtonFocus: "ColorButtonFocus",
ColorBase: "ColorBase",
ColorBaseHover: "ColorBaseHover",
ColorBaseFocus: "ColorBaseFocus",
ColorScrollBase: "ColorScrollBase",
ColorScrollThumb: "ColorScrollThumb",
}
// Check that each constant has a unique value
seen := make(map[int]string)
for value, name := range expectedValues {
if existing, ok := seen[value]; ok {
t.Errorf("Color constant %s has same value as %s (%d)", name, existing, value)
}
seen[value] = name
}
// Check constants are in expected order (iota)
if ColorText != 0 {
t.Errorf("ColorText should be 0, got %d", ColorText)
}
}
func TestDrawFrame_GetColorByID(t *testing.T) {
ui := New(Config{})
// Test that getColorByID returns appropriate colors
tests := []struct {
colorID int
name string
}{
{ColorButton, "ColorButton"},
{ColorButtonHover, "ColorButtonHover"},
{ColorBase, "ColorBase"},
{ColorBaseHover, "ColorBaseHover"},
{ColorBaseFocus, "ColorBaseFocus"},
{ColorScrollBase, "ColorScrollBase"},
{ColorScrollThumb, "ColorScrollThumb"},
{ColorTitleBG, "ColorTitleBG"},
{ColorWindowBG, "ColorWindowBG"},
{ColorPanelBG, "ColorPanelBG"},
{ColorText, "ColorText"},
}
for _, tt := range tests {
c := ui.GetColorByID(tt.colorID)
if c == nil {
t.Errorf("GetColorByID(%s) returned nil", tt.name)
}
}
}
func TestDrawFrame_NilCallbackUsesDefault(t *testing.T) {
ui := New(Config{
DrawFrame: nil, // Explicitly nil
})
// Should not panic and should use default
ui.BeginFrame()
ui.BeginWindow("Test", types.Rect{X: 0, Y: 0, W: 200, H: 150})
ui.LayoutRow(1, []int{-1}, 0)
ui.Button("Click")
ui.EndWindow()
ui.EndFrame()
// Should have rect commands from default draw_frame
hasRect := false
ui.commands.Each(func(cmd Command) {
if cmd.Kind == CmdRect {
hasRect = true
}
})
if !hasRect {
t.Error("Default DrawFrame (from nil config) should produce CmdRect commands")
}
}
func TestDrawFrame_CalledWithCorrectRect(t *testing.T) {
var capturedRect types.Rect
customDrawFrame := func(ui *UI, rect types.Rect, colorID int) {
if colorID == ColorButton {
capturedRect = rect
}
}
ui := New(Config{
DrawFrame: customDrawFrame,
})
ui.BeginFrame()
ui.BeginWindow("Test", types.Rect{X: 0, Y: 0, W: 200, H: 150})
ui.LayoutRow(1, []int{100}, 30)
ui.Button("Click")
ui.EndWindow()
ui.EndFrame()
// Button should have been rendered with non-zero dimensions
if capturedRect.W == 0 || capturedRect.H == 0 {
t.Errorf("DrawFrame should be called with valid rect, got %+v", capturedRect)
}
}
func TestDrawFrame_PublicMethod(t *testing.T) {
callCount := 0
customDrawFrame := func(ui *UI, rect types.Rect, colorID int) {
callCount++
}
ui := New(Config{
DrawFrame: customDrawFrame,
})
// Test that DrawFrame public method works
testRect := types.Rect{X: 10, Y: 20, W: 100, H: 50}
ui.DrawFrame(testRect, ColorButton)
if callCount != 1 {
t.Errorf("DrawFrame method should call callback once, got %d calls", callCount)
}
}