-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasewindow_test.go
More file actions
223 lines (184 loc) · 5.97 KB
/
basewindow_test.go
File metadata and controls
223 lines (184 loc) · 5.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package vtui
import (
"testing"
"github.com/unxed/vtinput"
)
func TestBaseWindow_ShadowFlag(t *testing.T) {
bw := NewBaseWindow(0, 0, 10, 10, "Title")
if !bw.HasShadow() {
t.Error("BaseWindow (Dialogs/Windows) should have shadows enabled by default")
}
}
func TestBaseWindow_HandleCommand(t *testing.T) {
bw := NewBaseWindow(0, 0, 10, 10, "Command Test")
// Test built-in Window command (CmClose)
if bw.IsDone() {
t.Fatal("Window should not be done initially")
}
bw.HandleCommand(CmClose, nil)
if !bw.IsDone() {
t.Error("CmClose command should close the BaseWindow")
}
}
func TestBaseWindow_AddItem(t *testing.T) {
// Создаем окно 10x5. С учетом рамок, контент занимает 8x3.
bw := NewBaseWindow(0, 0, 10, 5, "Test MinSize")
// Начальный MinW должен быть равен переданному размеру (11 символов: 0..10)
if bw.MinW != 11 {
t.Errorf("Initial MinW is wrong, got %d, want 11", bw.MinW)
}
// Добавляем кнопку, которая выходит за границы окна вправо.
// Кнопка на x=15, её ширина ~10. Конец будет на x=25.
// Окно должно увеличить MinW, чтобы вместить элемент + рамку.
btn := NewButton(15, 3, "Wide")
bw.AddItem(btn)
// x2 кнопки (15 + len("[ Wide ]") - 1) = 22.
// MinW окна = x2 кнопки - bw.X1 + 2 (рамка) = 24.
if bw.MinW < 24 {
t.Errorf("MinW did not update correctly after adding wide item. Expected >= 24, got %d", bw.MinW)
}
}
func TestBaseWindow_DataMapping(t *testing.T) {
type TestData struct {
Name string `vtui:"user_name"`
Admin bool `vtui:"is_admin"`
}
bw := NewBaseWindow(0, 0, 40, 20, "Data Test")
edit := NewEdit(1, 1, 20, "")
edit.SetId("user_name")
bw.AddItem(edit)
chk := NewCheckbox(1, 2, "Admin", false)
chk.SetId("is_admin")
bw.AddItem(chk)
// 1. Test SetData (делегирование в rootGroup)
input := TestData{
Name: "Explorer",
Admin: true,
}
bw.SetData(input)
if edit.GetText() != "Explorer" {
t.Errorf("SetData failed to update Edit: %s", edit.GetText())
}
if chk.State != 1 {
t.Error("SetData failed to update Checkbox")
}
// 2. Test GetData
edit.SetText("NewName")
var output TestData
bw.GetData(&output)
if output.Name != "NewName" {
t.Errorf("GetData failed to retrieve string: %s", output.Name)
}
}
func TestBaseWindow_DataMapping_EdgeCases(t *testing.T) {
bw := NewBaseWindow(0, 0, 10, 10, "Edge Case Test")
edit := NewEdit(0, 0, 5, "")
edit.SetId("field1")
bw.AddItem(edit)
// 1. SetData with incorrect types (should not panic)
bw.SetData(nil)
bw.SetData("not a struct")
bw.SetData(42)
// 2. GetData into non-pointer or non-struct
var target string
bw.GetData(target) // Not a pointer
bw.GetData(&target) // Pointer to non-struct
// 3. Type mismatch
type WrongTypeStruct struct {
Field1 int `vtui:"field1"` // UI is Edit (string), here it's int
}
wrong := WrongTypeStruct{Field1: 123}
bw.SetData(wrong) // Should be ignored as int is not a string
if edit.GetText() != "" {
t.Error("SetData should ignore value when types are incompatible")
}
edit.SetText("NotANumber")
var result WrongTypeStruct
bw.GetData(&result)
if result.Field1 != 0 {
t.Error("GetData should not set field when types are incompatible")
}
}
type broadcastMockElement struct {
ScreenObject
handled bool
}
func (m *broadcastMockElement) HandleBroadcast(cmd int, args any) bool {
if cmd == 42 {
m.handled = true
return true
}
return false
}
func TestBaseWindow_HandleBroadcast_Propagation(t *testing.T) {
bw := NewBaseWindow(0, 0, 10, 10, "Test")
el1 := &broadcastMockElement{}
el2 := &broadcastMockElement{}
bw.AddItem(el1)
bw.AddItem(el2)
res := bw.HandleBroadcast(42, nil)
if !res {
t.Error("BaseWindow should return true if items handled the broadcast")
}
if !el1.handled || !el2.handled {
t.Error("Broadcast was not propagated to all items through rootGroup")
}
}
func TestBaseWindow_Validation_CmDefault(t *testing.T) {
SetDefaultPalette()
fm := FrameManager
fm.Init(NewSilentScreenBuf())
defer fm.Shutdown()
dlg := NewDialog(0, 0, 20, 5, "Enter Test")
edit := NewEdit(1, 1, 10, "wrong")
edit.Validator = &RegexValidator{Pattern: "^correct$"}
dlg.AddItem(edit)
fm.Push(dlg)
// CmDefault обычно вызывается при нажатии Enter
handled := dlg.HandleCommand(CmDefault, nil)
if !handled {
t.Error("Command should be consumed")
}
if dlg.IsDone() {
t.Error("CmDefault should be blocked by validation")
}
}
func TestBaseWindow_NoDownwardCommandRouting(t *testing.T) {
bw := NewBaseWindow(0, 0, 10, 10, "Recursion Test")
itemHandledCommand := false
item := &cmdMockFrame{}
item.onCmd = func(cmd int, args any) bool {
itemHandledCommand = true
return true
}
bw.AddItem(item)
bw.rootGroup.focusIdx = 0
// Вызываем команду на окне.
// Оно не должно пытаться передать её вниз сфокусированному элементу (это делает FrameManager),
// чтобы избежать бесконечной рекурсии, если элемент сам вызывает метод окна.
bw.HandleCommand(CmOK, nil)
if itemHandledCommand {
t.Error("Window passed command down to focused item, risking infinite recursion")
}
}
func TestBaseWindow_PgDnFocus(t *testing.T) {
bw := NewBaseWindow(0, 0, 40, 10, "PgDn Test")
edit := NewEdit(1, 1, 20, "")
btnOk := NewButton(1, 5, "&Ok")
btnOk.IsDefault = true
bw.AddItem(edit)
bw.AddItem(btnOk)
// Ensure initial focus is on edit
bw.rootGroup.setFocus(0)
if !edit.IsFocused() { t.Fatal("Setup failed: edit not focused") }
// Press PgDn
bw.ProcessKey(&vtinput.InputEvent{
Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_NEXT,
})
if !btnOk.IsFocused() {
t.Error("PgDn failed to move focus to the default button")
}
if edit.IsFocused() {
t.Error("Focus remained on the edit field after PgDn")
}
}