-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwindow_test.go
304 lines (256 loc) · 7.9 KB
/
window_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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package winman_test
import (
"fmt"
"strings"
"testing"
"unicode/utf8"
"github.com/epiclabs-io/winman"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type ScreenMonitor struct {
screen tcell.SimulationScreen
contents []tcell.SimCell
width int
height int
}
func assertPanic(t *testing.T, f func()) {
defer func() {
if recover() == nil {
t.Errorf("Expected this call to panic")
}
}()
f()
}
func (sm *ScreenMonitor) Sync() {
sm.screen.Sync()
sm.contents, sm.width, sm.height = sm.screen.GetContents()
}
func (sm *ScreenMonitor) Char(x, y int) string {
return string(sm.contents[x+y*sm.width].Runes)
}
func (sm *ScreenMonitor) Line(x, y, width int) string {
b := strings.Builder{}
s := x + y*sm.width
for i := 0; i < width; i++ {
b.WriteString(string(sm.contents[i+s].Runes))
}
return b.String()
}
type BoringPrimitive struct {
*tview.Box
Symbol rune
clickCount int
}
func NewBoringPrimitive(Symbol rune) *BoringPrimitive {
return &BoringPrimitive{
Box: tview.NewBox(),
Symbol: Symbol,
}
}
func (bp *BoringPrimitive) Draw(screen tcell.Screen) {
px, py, width, height := bp.GetRect()
for x := px; x < px+width; x++ {
for y := py; y < py+height; y++ {
screen.SetContent(x, y, bp.Symbol, nil, tcell.StyleDefault)
}
}
}
func (bp *BoringPrimitive) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
return func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
x, y := event.Position()
if bp.InRect(x, y) {
bp.clickCount++
return true, nil
}
return false, nil
}
}
type Position struct {
x int
y int
}
type WindowTest struct {
wnd *winman.WindowBase
buttonClicks []Position
lines []string
}
var priv = NewBoringPrimitive('@')
var wtests = []WindowTest{
{winman.NewWindow().SetRoot(priv), nil, []string{` ┌─────────────┐ `, ` │@@@@@@@@@@@@@│ `}},
{winman.NewWindow().SetRoot(priv).AddButton(&winman.Button{
Symbol: 'A',
Alignment: winman.ButtonLeft,
}), []Position{{3, 0}}, []string{` ┌[A]──────────┐ `, ` │@@@@@@@@@@@@@│ `}},
{winman.NewWindow().SetRoot(priv).AddButton(&winman.Button{
Symbol: 'B',
Alignment: winman.ButtonRight,
}), []Position{{13, 0}}, []string{` ┌──────────[B]┐ `, ` │@@@@@@@@@@@@@│ `}},
{winman.NewWindow().SetRoot(priv).AddButton(&winman.Button{
Symbol: 'C',
Alignment: winman.ButtonRight,
}).AddButton(&winman.Button{
Symbol: 'D',
Alignment: winman.ButtonLeft,
}).AddButton(&winman.Button{
Symbol: 'E',
Alignment: winman.ButtonRight,
}).AddButton(&winman.Button{
Symbol: 'F',
Alignment: winman.ButtonLeft,
}), []Position{{13, 0}, {3, 0}, {10, 0}, {6, 0}}, []string{` ┌[D][F]─[E][C]┐ `, ` │@@@@@@@@@@@@@│ `}},
}
func TestWindow(t *testing.T) {
screen := tcell.NewSimulationScreen("UTF-8")
screen.SetSize(80, 24)
screen.Init()
sm := &ScreenMonitor{screen: screen}
for num, wt := range wtests {
t.Run(fmt.Sprintf("Window %d", num), func(t *testing.T) {
// Start with a new screen
screen.Clear()
//Position window
wt.wnd.SetRect(1, 0, 15, 10)
//Draw this window on the screen
wt.wnd.Draw(screen)
sm.Sync() // sync so the screen is readable now
// 1.- Check whether the first lines of the window have rendered correctly:
for y, expectedLine := range wt.lines {
line := sm.Line(0, y, utf8.RuneCountInString(expectedLine))
if line != expectedLine {
t.Fatalf("Wrong rendering. Expected %q, got %q", expectedLine, line)
}
}
// 2.- Test that GetButton() returns nil for out of range button numbers:
btn := wt.wnd.GetButton(-1)
if btn != nil {
t.Fatalf("Expected GetButton() to return nil, got %v", btn)
}
btn = wt.wnd.GetButton(1000)
if btn != nil {
t.Fatalf("Expected GetButton() to return nil, got %v", btn)
}
// 3.- Set up window button handlers and click all cells in the screen to see
// if handlers are called.
var clickedButton int
clickCounter := make(map[int]int)
//var focusedPrimitive tview.Primitive
setFocus := func(p tview.Primitive) {
//focusedPrimitive = p
}
for i := 0; i < wt.wnd.ButtonCount(); i++ {
func(i int) {
button := wt.wnd.GetButton(i)
button.OnClick = func() {
clickedButton = i
clickCounter[i] = clickCounter[i] + 1
}
}(i)
}
windowMouseHandler := wt.wnd.MouseHandler()
// The following code virtually clicks each cell of the entire screen
// As a consequence, each window button should be clicked exactly once.
priv.clickCount = 0
sw, sh := screen.Size()
for x := 0; x < sw; x++ {
for y := 0; y < sh; y++ {
clickedButton = -1
event := tcell.NewEventMouse(x, y, tcell.Button1, tcell.ModNone)
windowMouseHandler(tview.MouseLeftClick, event, setFocus)
if clickedButton != -1 {
expectedPos := wt.buttonClicks[clickedButton]
if x != expectedPos.x || y != expectedPos.y {
t.Fatalf("Expected window button to handle click to (%d,%d), got (%d,%d)", expectedPos.x, expectedPos.y, x, y)
}
}
}
}
if len(clickCounter) != wt.wnd.ButtonCount() {
t.Fatalf("Expected only %d different buttons to be clicked, got %d", wt.wnd.ButtonCount(), len(clickCounter))
}
for i, clicks := range clickCounter {
if clicks != 1 {
t.Fatalf("Expected each button to be clicked exactly once. Got %d clicks in button %d", clicks, i)
}
}
_, _, width, height := priv.GetRect()
if priv.clickCount != width*height {
t.Fatalf("Expected root primitive to have received exactly %d clicks, got %d", width*height, priv.clickCount)
}
wt.wnd.SetBorder(false)
for x := 0; x < sw; x++ {
for y := 0; y < sh; y++ {
clickedButton = -1
event := tcell.NewEventMouse(x, y, tcell.Button1, tcell.ModNone)
windowMouseHandler(tview.MouseLeftClick, event, setFocus)
if clickedButton != -1 {
t.Fatalf("Expected no window button to be clicked, since border is turned off")
}
}
}
})
}
}
func delegate(p tview.Primitive) {
p.Focus(delegate)
}
func TestFocusDelegation(t *testing.T) {
root := NewBoringPrimitive('%')
wnd := winman.NewWindow()
hasFocus := wnd.HasFocus()
if hasFocus == true {
t.Fatal("Expected a newly created window without root to not have focus, since Focus() was not called")
}
// set focus and check if windows shows as focused
wnd.Focus(delegate)
hasFocus = wnd.HasFocus()
if hasFocus == false {
t.Fatal("Expected to have focus")
}
wnd.SetRoot(root)
hasFocus = wnd.HasFocus()
if hasFocus == true {
t.Fatalf("Expected not to have focus, since root does not have focus")
}
// set focus. Focus should be passed on to root and retained.
wnd.Focus(delegate)
hasFocus = wnd.HasFocus()
if hasFocus == false {
t.Fatal("Expected window to have focus")
}
hasFocus = root.HasFocus()
if hasFocus == false {
t.Fatal("Expected root to have focus")
}
}
func TestWindowSettings(t *testing.T) {
wnd := winman.NewWindow()
if wnd.IsModal() {
t.Fatal("Expected window to be non-modal by default")
}
wnd.SetModal(true)
if !wnd.IsModal() {
t.Fatal("Expected window to be modal after setting modal to true")
}
if wnd.IsResizable() {
t.Fatal("Expected window to be non-resizable by default")
}
wnd.SetResizable(true)
if !wnd.IsResizable() {
t.Fatal("Expected window to be resizable after setting resizable to true")
}
if wnd.IsDraggable() {
t.Fatal("Expected window to be non-draggable by default")
}
wnd.SetDraggable(true)
if !wnd.IsDraggable() {
t.Fatal("Expected window to be draggable after setting draggable to true")
}
if wnd.GetTitle() != "" {
t.Fatal("Expected window to not have a title by default")
}
wnd.SetTitle("Hello")
if wnd.GetTitle() != "Hello" {
t.Fatalf("Expected window to have the expecte title, got %s", wnd.GetTitle())
}
}