| skipped 44 lines |
45 | 45 | | // count is the number of times the callback was called. |
46 | 46 | | count int |
47 | 47 | | |
| 48 | + | // useSetCallback when set to true instructs the test to set the callback |
| 49 | + | // via button.SetCallback instead of button.New or button.NewFromChunks. |
| 50 | + | useSetCallback bool |
| 51 | + | |
48 | 52 | | // mu protects the tracker. |
49 | 53 | | mu sync.Mutex |
50 | 54 | | } |
| skipped 12 lines |
63 | 67 | | return nil |
64 | 68 | | } |
65 | 69 | | |
| 70 | + | // event represents a terminal event for tests. |
| 71 | + | type event struct { |
| 72 | + | ev terminalapi.Event |
| 73 | + | meta *widgetapi.EventMeta |
| 74 | + | } |
| 75 | + | |
66 | 76 | | func TestButton(t *testing.T) { |
67 | 77 | | tests := []struct { |
68 | | - | desc string |
69 | | - | text string |
| 78 | + | desc string |
| 79 | + | |
| 80 | + | // Only one of these must be specified. |
| 81 | + | text string // Calls New() as the constructor. |
| 82 | + | textChunks []*TextChunk // Calls NewFromChunks() as the constructor. |
| 83 | + | |
70 | 84 | | callback *callbackTracker |
71 | 85 | | opts []Option |
72 | | - | events []terminalapi.Event |
| 86 | + | events []*event |
73 | 87 | | canvas image.Rectangle |
74 | 88 | | meta *widgetapi.Meta |
75 | 89 | | |
| skipped 8 lines |
84 | 98 | | wantCallbackErr bool |
85 | 99 | | }{ |
86 | 100 | | { |
87 | | - | desc: "New fails with nil callback", |
| 101 | + | desc: "New fails with negative keyUpDelay", |
| 102 | + | callback: &callbackTracker{}, |
| 103 | + | opts: []Option{ |
| 104 | + | KeyUpDelay(-1 * time.Second), |
| 105 | + | }, |
| 106 | + | canvas: image.Rect(0, 0, 1, 1), |
| 107 | + | text: "hello", |
| 108 | + | meta: &widgetapi.Meta{Focused: false}, |
| 109 | + | wantNewErr: true, |
| 110 | + | }, |
| 111 | + | { |
| 112 | + | desc: "New fails with zero Height", |
| 113 | + | callback: &callbackTracker{}, |
| 114 | + | opts: []Option{ |
| 115 | + | Height(0), |
| 116 | + | }, |
| 117 | + | canvas: image.Rect(0, 0, 1, 1), |
| 118 | + | text: "hello", |
| 119 | + | meta: &widgetapi.Meta{Focused: false}, |
| 120 | + | wantNewErr: true, |
| 121 | + | }, |
| 122 | + | { |
| 123 | + | desc: "New fails with zero Width", |
| 124 | + | callback: &callbackTracker{}, |
| 125 | + | opts: []Option{ |
| 126 | + | Width(0), |
| 127 | + | }, |
| 128 | + | canvas: image.Rect(0, 0, 1, 1), |
| 129 | + | text: "hello", |
| 130 | + | meta: &widgetapi.Meta{Focused: false}, |
| 131 | + | wantNewErr: true, |
| 132 | + | }, |
| 133 | + | { |
| 134 | + | desc: "New fails with negative textHorizontalPadding", |
| 135 | + | callback: &callbackTracker{}, |
| 136 | + | opts: []Option{ |
| 137 | + | TextHorizontalPadding(-1), |
| 138 | + | }, |
| 139 | + | canvas: image.Rect(0, 0, 1, 1), |
| 140 | + | text: "hello", |
| 141 | + | meta: &widgetapi.Meta{Focused: false}, |
| 142 | + | wantNewErr: true, |
| 143 | + | }, |
| 144 | + | { |
| 145 | + | desc: "New fails when duplicate Key and GlobalKey are specified", |
| 146 | + | callback: &callbackTracker{}, |
| 147 | + | opts: []Option{ |
| 148 | + | Key('a'), |
| 149 | + | GlobalKey('a'), |
| 150 | + | }, |
88 | 151 | | canvas: image.Rect(0, 0, 1, 1), |
| 152 | + | text: "hello", |
| 153 | + | meta: &widgetapi.Meta{Focused: false}, |
89 | 154 | | wantNewErr: true, |
90 | 155 | | }, |
91 | 156 | | { |
92 | | - | desc: "New fails with negative keyUpDelay", |
| 157 | + | desc: "New fails when duplicate Keys and GlobalKeys are specified", |
| 158 | + | callback: &callbackTracker{}, |
| 159 | + | opts: []Option{ |
| 160 | + | Keys('a'), |
| 161 | + | GlobalKeys('a'), |
| 162 | + | }, |
| 163 | + | canvas: image.Rect(0, 0, 1, 1), |
| 164 | + | text: "hello", |
| 165 | + | meta: &widgetapi.Meta{Focused: false}, |
| 166 | + | wantNewErr: true, |
| 167 | + | }, |
| 168 | + | { |
| 169 | + | desc: "NewFromChunks fails with negative keyUpDelay", |
| 170 | + | textChunks: []*TextChunk{ |
| 171 | + | NewChunk("text"), |
| 172 | + | }, |
93 | 173 | | callback: &callbackTracker{}, |
94 | 174 | | opts: []Option{ |
95 | 175 | | KeyUpDelay(-1 * time.Second), |
96 | 176 | | }, |
97 | 177 | | canvas: image.Rect(0, 0, 1, 1), |
| 178 | + | meta: &widgetapi.Meta{Focused: false}, |
98 | 179 | | wantNewErr: true, |
99 | 180 | | }, |
100 | 181 | | { |
101 | | - | desc: "New fails with zero Height", |
| 182 | + | desc: "NewFromChunks fails with zero Height", |
| 183 | + | textChunks: []*TextChunk{ |
| 184 | + | NewChunk("text"), |
| 185 | + | }, |
102 | 186 | | callback: &callbackTracker{}, |
103 | 187 | | opts: []Option{ |
104 | 188 | | Height(0), |
105 | 189 | | }, |
106 | 190 | | canvas: image.Rect(0, 0, 1, 1), |
| 191 | + | meta: &widgetapi.Meta{Focused: false}, |
107 | 192 | | wantNewErr: true, |
108 | 193 | | }, |
109 | 194 | | { |
110 | | - | desc: "New fails with zero Width", |
| 195 | + | desc: "NewFromChunks fails with zero Width", |
| 196 | + | textChunks: []*TextChunk{ |
| 197 | + | NewChunk("text"), |
| 198 | + | }, |
111 | 199 | | callback: &callbackTracker{}, |
112 | 200 | | opts: []Option{ |
113 | 201 | | Width(0), |
114 | 202 | | }, |
115 | 203 | | canvas: image.Rect(0, 0, 1, 1), |
| 204 | + | meta: &widgetapi.Meta{Focused: false}, |
| 205 | + | wantNewErr: true, |
| 206 | + | }, |
| 207 | + | { |
| 208 | + | desc: "NewFromChunks fails with negative textHorizontalPadding", |
| 209 | + | textChunks: []*TextChunk{ |
| 210 | + | NewChunk("text"), |
| 211 | + | }, |
| 212 | + | callback: &callbackTracker{}, |
| 213 | + | opts: []Option{ |
| 214 | + | TextHorizontalPadding(-1), |
| 215 | + | }, |
| 216 | + | canvas: image.Rect(0, 0, 1, 1), |
| 217 | + | meta: &widgetapi.Meta{Focused: false}, |
| 218 | + | wantNewErr: true, |
| 219 | + | }, |
| 220 | + | { |
| 221 | + | desc: "NewFromChunks fails when duplicate Key and GlobalKey are specified", |
| 222 | + | callback: &callbackTracker{}, |
| 223 | + | opts: []Option{ |
| 224 | + | Key('a'), |
| 225 | + | GlobalKey('a'), |
| 226 | + | }, |
| 227 | + | canvas: image.Rect(0, 0, 1, 1), |
| 228 | + | textChunks: []*TextChunk{ |
| 229 | + | NewChunk("text"), |
| 230 | + | }, |
| 231 | + | meta: &widgetapi.Meta{Focused: false}, |
| 232 | + | wantNewErr: true, |
| 233 | + | }, |
| 234 | + | { |
| 235 | + | desc: "NewFromChunks fails when duplicate Keys and GlobalKeys are specified", |
| 236 | + | callback: &callbackTracker{}, |
| 237 | + | opts: []Option{ |
| 238 | + | Keys('a'), |
| 239 | + | GlobalKeys('a'), |
| 240 | + | }, |
| 241 | + | canvas: image.Rect(0, 0, 1, 1), |
| 242 | + | textChunks: []*TextChunk{ |
| 243 | + | NewChunk("text"), |
| 244 | + | }, |
| 245 | + | meta: &widgetapi.Meta{Focused: false}, |
| 246 | + | wantNewErr: true, |
| 247 | + | }, |
| 248 | + | { |
| 249 | + | desc: "NewFromChunks fails with zero chunks", |
| 250 | + | textChunks: []*TextChunk{}, |
| 251 | + | callback: &callbackTracker{}, |
| 252 | + | opts: []Option{ |
| 253 | + | TextHorizontalPadding(-1), |
| 254 | + | }, |
| 255 | + | canvas: image.Rect(0, 0, 1, 1), |
| 256 | + | meta: &widgetapi.Meta{Focused: false}, |
| 257 | + | wantNewErr: true, |
| 258 | + | }, |
| 259 | + | { |
| 260 | + | desc: "NewFromChunks fails with an empty chunk", |
| 261 | + | textChunks: []*TextChunk{ |
| 262 | + | NewChunk(""), |
| 263 | + | }, |
| 264 | + | callback: &callbackTracker{}, |
| 265 | + | opts: []Option{ |
| 266 | + | TextHorizontalPadding(-1), |
| 267 | + | }, |
| 268 | + | canvas: image.Rect(0, 0, 1, 1), |
| 269 | + | meta: &widgetapi.Meta{Focused: false}, |
116 | 270 | | wantNewErr: true, |
117 | 271 | | }, |
118 | 272 | | { |
| skipped 1 lines |
120 | 274 | | callback: &callbackTracker{}, |
121 | 275 | | text: "hello", |
122 | 276 | | canvas: image.Rect(0, 0, 1, 1), |
| 277 | + | meta: &widgetapi.Meta{Focused: false}, |
123 | 278 | | wantDrawErr: true, |
124 | 279 | | }, |
125 | 280 | | { |
| skipped 1 lines |
127 | 282 | | callback: &callbackTracker{}, |
128 | 283 | | text: "hello", |
129 | 284 | | canvas: image.Rect(0, 0, 8, 4), |
| 285 | + | meta: &widgetapi.Meta{Focused: false}, |
130 | 286 | | want: func(size image.Point) *faketerm.Terminal { |
131 | 287 | | ft := faketerm.MustNew(size) |
132 | 288 | | cvs := testcanvas.MustNew(ft.Area()) |
| skipped 17 lines |
150 | 306 | | wantCallback: &callbackTracker{}, |
151 | 307 | | }, |
152 | 308 | | { |
| 309 | + | desc: "draws button without a shadow in up state", |
| 310 | + | callback: &callbackTracker{}, |
| 311 | + | opts: []Option{ |
| 312 | + | DisableShadow(), |
| 313 | + | }, |
| 314 | + | text: "hello", |
| 315 | + | canvas: image.Rect(0, 0, 8, 4), |
| 316 | + | meta: &widgetapi.Meta{Focused: false}, |
| 317 | + | want: func(size image.Point) *faketerm.Terminal { |
| 318 | + | ft := faketerm.MustNew(size) |
| 319 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 320 | + | |
| 321 | + | // Button. |
| 322 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 8, 4), 'x', cell.BgColor(cell.ColorNumber(117))) |
| 323 | + | |
| 324 | + | // Text. |
| 325 | + | testdraw.MustText(cvs, "hello", image.Point{1, 1}, |
| 326 | + | draw.TextCellOpts( |
| 327 | + | cell.FgColor(cell.ColorBlack), |
| 328 | + | cell.BgColor(cell.ColorNumber(117))), |
| 329 | + | ) |
| 330 | + | |
| 331 | + | testcanvas.MustApply(cvs, ft) |
| 332 | + | return ft |
| 333 | + | }, |
| 334 | + | wantCallback: &callbackTracker{}, |
| 335 | + | }, |
| 336 | + | { |
153 | 337 | | desc: "draws button in down state due to a mouse event", |
154 | 338 | | callback: &callbackTracker{}, |
155 | 339 | | text: "hello", |
156 | 340 | | canvas: image.Rect(0, 0, 8, 4), |
157 | | - | events: []terminalapi.Event{ |
158 | | - | &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 341 | + | meta: &widgetapi.Meta{Focused: false}, |
| 342 | + | events: []*event{ |
| 343 | + | { |
| 344 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 345 | + | meta: &widgetapi.EventMeta{}, |
| 346 | + | }, |
159 | 347 | | }, |
160 | 348 | | want: func(size image.Point) *faketerm.Terminal { |
161 | 349 | | ft := faketerm.MustNew(size) |
| skipped 15 lines |
177 | 365 | | wantCallback: &callbackTracker{}, |
178 | 366 | | }, |
179 | 367 | | { |
180 | | - | desc: "mouse triggered the callback", |
| 368 | + | desc: "draws button in down state without a shadow", |
| 369 | + | callback: &callbackTracker{}, |
| 370 | + | opts: []Option{ |
| 371 | + | DisableShadow(), |
| 372 | + | }, |
| 373 | + | text: "hello", |
| 374 | + | canvas: image.Rect(0, 0, 8, 4), |
| 375 | + | meta: &widgetapi.Meta{Focused: false}, |
| 376 | + | events: []*event{ |
| 377 | + | { |
| 378 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 379 | + | meta: &widgetapi.EventMeta{}, |
| 380 | + | }, |
| 381 | + | }, |
| 382 | + | want: func(size image.Point) *faketerm.Terminal { |
| 383 | + | ft := faketerm.MustNew(size) |
| 384 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 385 | + | |
| 386 | + | // Button. |
| 387 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 8, 4), 'x', cell.BgColor(cell.ColorNumber(117))) |
| 388 | + | |
| 389 | + | // Text. |
| 390 | + | testdraw.MustText(cvs, "hello", image.Point{1, 1}, |
| 391 | + | draw.TextCellOpts( |
| 392 | + | cell.FgColor(cell.ColorBlack), |
| 393 | + | cell.BgColor(cell.ColorNumber(117))), |
| 394 | + | ) |
| 395 | + | |
| 396 | + | testcanvas.MustApply(cvs, ft) |
| 397 | + | return ft |
| 398 | + | }, |
| 399 | + | wantCallback: &callbackTracker{}, |
| 400 | + | }, |
| 401 | + | { |
| 402 | + | desc: "mouse triggered a button with nil callback", |
| 403 | + | callback: nil, |
| 404 | + | text: "hello", |
| 405 | + | canvas: image.Rect(0, 0, 8, 4), |
| 406 | + | meta: &widgetapi.Meta{Focused: false}, |
| 407 | + | events: []*event{ |
| 408 | + | { |
| 409 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 410 | + | meta: &widgetapi.EventMeta{}, |
| 411 | + | }, |
| 412 | + | { |
| 413 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
| 414 | + | meta: &widgetapi.EventMeta{}, |
| 415 | + | }, |
| 416 | + | }, |
| 417 | + | want: func(size image.Point) *faketerm.Terminal { |
| 418 | + | ft := faketerm.MustNew(size) |
| 419 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 420 | + | |
| 421 | + | // Shadow. |
| 422 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(1, 1, 8, 4), 's', cell.BgColor(cell.ColorNumber(240))) |
| 423 | + | |
| 424 | + | // Button. |
| 425 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 7, 3), 'x', cell.BgColor(cell.ColorNumber(117))) |
| 426 | + | |
| 427 | + | // Text. |
| 428 | + | testdraw.MustText(cvs, "hello", image.Point{1, 1}, |
| 429 | + | draw.TextCellOpts( |
| 430 | + | cell.FgColor(cell.ColorBlack), |
| 431 | + | cell.BgColor(cell.ColorNumber(117))), |
| 432 | + | ) |
| 433 | + | |
| 434 | + | testcanvas.MustApply(cvs, ft) |
| 435 | + | return ft |
| 436 | + | }, |
| 437 | + | }, |
| 438 | + | { |
| 439 | + | desc: "mouse triggered a callback set via the constructor", |
181 | 440 | | callback: &callbackTracker{}, |
182 | 441 | | text: "hello", |
183 | 442 | | canvas: image.Rect(0, 0, 8, 4), |
184 | | - | events: []terminalapi.Event{ |
185 | | - | &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
186 | | - | &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
| 443 | + | meta: &widgetapi.Meta{Focused: false}, |
| 444 | + | events: []*event{ |
| 445 | + | { |
| 446 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 447 | + | meta: &widgetapi.EventMeta{}, |
| 448 | + | }, |
| 449 | + | { |
| 450 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
| 451 | + | meta: &widgetapi.EventMeta{}, |
| 452 | + | }, |
187 | 453 | | }, |
188 | 454 | | want: func(size image.Point) *faketerm.Terminal { |
189 | 455 | | ft := faketerm.MustNew(size) |
| skipped 21 lines |
211 | 477 | | }, |
212 | 478 | | }, |
213 | 479 | | { |
| 480 | + | desc: "mouse triggered a callback set via SetCallback", |
| 481 | + | callback: &callbackTracker{ |
| 482 | + | useSetCallback: true, |
| 483 | + | }, |
| 484 | + | text: "hello", |
| 485 | + | canvas: image.Rect(0, 0, 8, 4), |
| 486 | + | meta: &widgetapi.Meta{Focused: false}, |
| 487 | + | events: []*event{ |
| 488 | + | { |
| 489 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 490 | + | meta: &widgetapi.EventMeta{}, |
| 491 | + | }, |
| 492 | + | { |
| 493 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
| 494 | + | meta: &widgetapi.EventMeta{}, |
| 495 | + | }, |
| 496 | + | }, |
| 497 | + | want: func(size image.Point) *faketerm.Terminal { |
| 498 | + | ft := faketerm.MustNew(size) |
| 499 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 500 | + | |
| 501 | + | // Shadow. |
| 502 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(1, 1, 8, 4), 's', cell.BgColor(cell.ColorNumber(240))) |
| 503 | + | |
| 504 | + | // Button. |
| 505 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 7, 3), 'x', cell.BgColor(cell.ColorNumber(117))) |
| 506 | + | |
| 507 | + | // Text. |
| 508 | + | testdraw.MustText(cvs, "hello", image.Point{1, 1}, |
| 509 | + | draw.TextCellOpts( |
| 510 | + | cell.FgColor(cell.ColorBlack), |
| 511 | + | cell.BgColor(cell.ColorNumber(117))), |
| 512 | + | ) |
| 513 | + | |
| 514 | + | testcanvas.MustApply(cvs, ft) |
| 515 | + | return ft |
| 516 | + | }, |
| 517 | + | wantCallback: &callbackTracker{ |
| 518 | + | called: true, |
| 519 | + | count: 1, |
| 520 | + | useSetCallback: true, |
| 521 | + | }, |
| 522 | + | }, |
| 523 | + | { |
214 | 524 | | desc: "draws button in down state due to a keyboard event, callback triggered", |
215 | 525 | | callback: &callbackTracker{}, |
216 | 526 | | text: "hello", |
| skipped 1 lines |
218 | 528 | | Key(keyboard.KeyEnter), |
219 | 529 | | }, |
220 | 530 | | canvas: image.Rect(0, 0, 8, 4), |
221 | | - | events: []terminalapi.Event{ |
222 | | - | &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 531 | + | meta: &widgetapi.Meta{Focused: false}, |
| 532 | + | events: []*event{ |
| 533 | + | { |
| 534 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 535 | + | meta: &widgetapi.EventMeta{Focused: true}, |
| 536 | + | }, |
| 537 | + | }, |
| 538 | + | want: func(size image.Point) *faketerm.Terminal { |
| 539 | + | ft := faketerm.MustNew(size) |
| 540 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 541 | + | |
| 542 | + | // Button. |
| 543 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(1, 1, 8, 4), 'x', cell.BgColor(cell.ColorNumber(117))) |
| 544 | + | |
| 545 | + | // Text. |
| 546 | + | testdraw.MustText(cvs, "hello", image.Point{2, 2}, |
| 547 | + | draw.TextCellOpts( |
| 548 | + | cell.FgColor(cell.ColorBlack), |
| 549 | + | cell.BgColor(cell.ColorNumber(117))), |
| 550 | + | ) |
| 551 | + | |
| 552 | + | testcanvas.MustApply(cvs, ft) |
| 553 | + | return ft |
| 554 | + | }, |
| 555 | + | wantCallback: &callbackTracker{ |
| 556 | + | called: true, |
| 557 | + | count: 1, |
| 558 | + | }, |
| 559 | + | }, |
| 560 | + | { |
| 561 | + | desc: "ignores keyboard event configured with Key when not focused", |
| 562 | + | callback: &callbackTracker{}, |
| 563 | + | text: "hello", |
| 564 | + | opts: []Option{ |
| 565 | + | Key(keyboard.KeyEnter), |
| 566 | + | }, |
| 567 | + | canvas: image.Rect(0, 0, 8, 4), |
| 568 | + | meta: &widgetapi.Meta{Focused: false}, |
| 569 | + | events: []*event{ |
| 570 | + | { |
| 571 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 572 | + | meta: &widgetapi.EventMeta{Focused: false}, |
| 573 | + | }, |
| 574 | + | }, |
| 575 | + | want: func(size image.Point) *faketerm.Terminal { |
| 576 | + | ft := faketerm.MustNew(size) |
| 577 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 578 | + | |
| 579 | + | // Shadow. |
| 580 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(1, 1, 8, 4), 's', cell.BgColor(cell.ColorNumber(240))) |
| 581 | + | |
| 582 | + | // Button. |
| 583 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 7, 3), 'x', cell.BgColor(cell.ColorNumber(117))) |
| 584 | + | |
| 585 | + | // Text. |
| 586 | + | testdraw.MustText(cvs, "hello", image.Point{1, 1}, |
| 587 | + | draw.TextCellOpts( |
| 588 | + | cell.FgColor(cell.ColorBlack), |
| 589 | + | cell.BgColor(cell.ColorNumber(117))), |
| 590 | + | ) |
| 591 | + | |
| 592 | + | testcanvas.MustApply(cvs, ft) |
| 593 | + | return ft |
| 594 | + | }, |
| 595 | + | wantCallback: &callbackTracker{ |
| 596 | + | called: false, |
| 597 | + | count: 0, |
| 598 | + | }, |
| 599 | + | }, |
| 600 | + | |
| 601 | + | { |
| 602 | + | desc: "draws button in down state due to a keyboard event when multiple keys are specified", |
| 603 | + | callback: &callbackTracker{}, |
| 604 | + | text: "hello", |
| 605 | + | opts: []Option{ |
| 606 | + | Keys(keyboard.KeyEnter, keyboard.KeyTab), |
| 607 | + | }, |
| 608 | + | canvas: image.Rect(0, 0, 8, 4), |
| 609 | + | meta: &widgetapi.Meta{Focused: false}, |
| 610 | + | events: []*event{ |
| 611 | + | { |
| 612 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyTab}, |
| 613 | + | meta: &widgetapi.EventMeta{Focused: true}, |
| 614 | + | }, |
| 615 | + | }, |
| 616 | + | want: func(size image.Point) *faketerm.Terminal { |
| 617 | + | ft := faketerm.MustNew(size) |
| 618 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 619 | + | |
| 620 | + | // Button. |
| 621 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(1, 1, 8, 4), 'x', cell.BgColor(cell.ColorNumber(117))) |
| 622 | + | |
| 623 | + | // Text. |
| 624 | + | testdraw.MustText(cvs, "hello", image.Point{2, 2}, |
| 625 | + | draw.TextCellOpts( |
| 626 | + | cell.FgColor(cell.ColorBlack), |
| 627 | + | cell.BgColor(cell.ColorNumber(117))), |
| 628 | + | ) |
| 629 | + | |
| 630 | + | testcanvas.MustApply(cvs, ft) |
| 631 | + | return ft |
| 632 | + | }, |
| 633 | + | wantCallback: &callbackTracker{ |
| 634 | + | called: true, |
| 635 | + | count: 1, |
| 636 | + | }, |
| 637 | + | }, |
| 638 | + | { |
| 639 | + | desc: "draws button in down state due to a keyboard event when single global key is specified", |
| 640 | + | callback: &callbackTracker{}, |
| 641 | + | text: "hello", |
| 642 | + | opts: []Option{ |
| 643 | + | GlobalKey(keyboard.KeyTab), |
| 644 | + | }, |
| 645 | + | canvas: image.Rect(0, 0, 8, 4), |
| 646 | + | meta: &widgetapi.Meta{Focused: false}, |
| 647 | + | events: []*event{ |
| 648 | + | { |
| 649 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyTab}, |
| 650 | + | meta: &widgetapi.EventMeta{}, |
| 651 | + | }, |
| 652 | + | }, |
| 653 | + | want: func(size image.Point) *faketerm.Terminal { |
| 654 | + | ft := faketerm.MustNew(size) |
| 655 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 656 | + | |
| 657 | + | // Button. |
| 658 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(1, 1, 8, 4), 'x', cell.BgColor(cell.ColorNumber(117))) |
| 659 | + | |
| 660 | + | // Text. |
| 661 | + | testdraw.MustText(cvs, "hello", image.Point{2, 2}, |
| 662 | + | draw.TextCellOpts( |
| 663 | + | cell.FgColor(cell.ColorBlack), |
| 664 | + | cell.BgColor(cell.ColorNumber(117))), |
| 665 | + | ) |
| 666 | + | |
| 667 | + | testcanvas.MustApply(cvs, ft) |
| 668 | + | return ft |
| 669 | + | }, |
| 670 | + | wantCallback: &callbackTracker{ |
| 671 | + | called: true, |
| 672 | + | count: 1, |
| 673 | + | }, |
| 674 | + | }, |
| 675 | + | { |
| 676 | + | desc: "draws button in down state due to a keyboard event when multiple global keys are specified", |
| 677 | + | callback: &callbackTracker{}, |
| 678 | + | text: "hello", |
| 679 | + | opts: []Option{ |
| 680 | + | GlobalKeys(keyboard.KeyEnter, keyboard.KeyTab), |
| 681 | + | }, |
| 682 | + | canvas: image.Rect(0, 0, 8, 4), |
| 683 | + | meta: &widgetapi.Meta{Focused: false}, |
| 684 | + | events: []*event{ |
| 685 | + | { |
| 686 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyTab}, |
| 687 | + | meta: &widgetapi.EventMeta{}, |
| 688 | + | }, |
223 | 689 | | }, |
224 | 690 | | want: func(size image.Point) *faketerm.Terminal { |
225 | 691 | | ft := faketerm.MustNew(size) |
| skipped 22 lines |
248 | 714 | | callback: &callbackTracker{}, |
249 | 715 | | text: "hello", |
250 | 716 | | canvas: image.Rect(0, 0, 8, 4), |
251 | | - | events: []terminalapi.Event{ |
252 | | - | &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 717 | + | meta: &widgetapi.Meta{Focused: false}, |
| 718 | + | events: []*event{ |
| 719 | + | { |
| 720 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 721 | + | meta: &widgetapi.EventMeta{}, |
| 722 | + | }, |
253 | 723 | | }, |
254 | 724 | | want: func(size image.Point) *faketerm.Terminal { |
255 | 725 | | ft := faketerm.MustNew(size) |
| skipped 18 lines |
274 | 744 | | wantCallback: &callbackTracker{}, |
275 | 745 | | }, |
276 | 746 | | { |
| 747 | + | desc: "keyboard event triggers a button with nil callback", |
| 748 | + | callback: nil, |
| 749 | + | text: "hello", |
| 750 | + | opts: []Option{ |
| 751 | + | Key(keyboard.KeyEnter), |
| 752 | + | }, |
| 753 | + | timeSince: func(time.Time) time.Duration { |
| 754 | + | return 200 * time.Millisecond |
| 755 | + | }, |
| 756 | + | canvas: image.Rect(0, 0, 8, 4), |
| 757 | + | meta: &widgetapi.Meta{Focused: false}, |
| 758 | + | events: []*event{ |
| 759 | + | { |
| 760 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 761 | + | meta: &widgetapi.EventMeta{Focused: true}, |
| 762 | + | }, |
| 763 | + | }, |
| 764 | + | want: func(size image.Point) *faketerm.Terminal { |
| 765 | + | ft := faketerm.MustNew(size) |
| 766 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 767 | + | |
| 768 | + | // Button. |
| 769 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(1, 1, 8, 4), 'x', cell.BgColor(cell.ColorNumber(117))) |
| 770 | + | |
| 771 | + | // Text. |
| 772 | + | testdraw.MustText(cvs, "hello", image.Point{2, 2}, |
| 773 | + | draw.TextCellOpts( |
| 774 | + | cell.FgColor(cell.ColorBlack), |
| 775 | + | cell.BgColor(cell.ColorNumber(117))), |
| 776 | + | ) |
| 777 | + | |
| 778 | + | testcanvas.MustApply(cvs, ft) |
| 779 | + | return ft |
| 780 | + | }, |
| 781 | + | }, |
| 782 | + | { |
277 | 783 | | desc: "keyboard event triggers the button, trigger time didn't expire so button is down", |
278 | 784 | | callback: &callbackTracker{}, |
279 | 785 | | text: "hello", |
| skipped 4 lines |
284 | 790 | | return 200 * time.Millisecond |
285 | 791 | | }, |
286 | 792 | | canvas: image.Rect(0, 0, 8, 4), |
287 | | - | events: []terminalapi.Event{ |
288 | | - | &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 793 | + | meta: &widgetapi.Meta{Focused: false}, |
| 794 | + | events: []*event{ |
| 795 | + | { |
| 796 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 797 | + | meta: &widgetapi.EventMeta{Focused: true}, |
| 798 | + | }, |
289 | 799 | | }, |
290 | 800 | | want: func(size image.Point) *faketerm.Terminal { |
291 | 801 | | ft := faketerm.MustNew(size) |
| skipped 29 lines |
321 | 831 | | return 200 * time.Millisecond |
322 | 832 | | }, |
323 | 833 | | canvas: image.Rect(0, 0, 8, 4), |
324 | | - | events: []terminalapi.Event{ |
325 | | - | &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 834 | + | meta: &widgetapi.Meta{Focused: false}, |
| 835 | + | events: []*event{ |
| 836 | + | { |
| 837 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 838 | + | meta: &widgetapi.EventMeta{Focused: true}, |
| 839 | + | }, |
326 | 840 | | }, |
327 | 841 | | want: func(size image.Point) *faketerm.Terminal { |
328 | 842 | | ft := faketerm.MustNew(size) |
| skipped 32 lines |
361 | 875 | | return 200 * time.Millisecond |
362 | 876 | | }, |
363 | 877 | | canvas: image.Rect(0, 0, 8, 4), |
364 | | - | events: []terminalapi.Event{ |
365 | | - | &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
366 | | - | &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
367 | | - | &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 878 | + | meta: &widgetapi.Meta{Focused: false}, |
| 879 | + | events: []*event{ |
| 880 | + | { |
| 881 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 882 | + | meta: &widgetapi.EventMeta{Focused: true}, |
| 883 | + | }, |
| 884 | + | { |
| 885 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 886 | + | meta: &widgetapi.EventMeta{Focused: true}, |
| 887 | + | }, |
| 888 | + | { |
| 889 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 890 | + | meta: &widgetapi.EventMeta{Focused: true}, |
| 891 | + | }, |
368 | 892 | | }, |
369 | 893 | | want: func(size image.Point) *faketerm.Terminal { |
370 | 894 | | ft := faketerm.MustNew(size) |
| skipped 25 lines |
396 | 920 | | callback: &callbackTracker{}, |
397 | 921 | | text: "hello", |
398 | 922 | | canvas: image.Rect(0, 0, 8, 4), |
399 | | - | events: []terminalapi.Event{ |
400 | | - | &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
401 | | - | &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
402 | | - | &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
403 | | - | &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
| 923 | + | meta: &widgetapi.Meta{Focused: false}, |
| 924 | + | events: []*event{ |
| 925 | + | { |
| 926 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 927 | + | meta: &widgetapi.EventMeta{}, |
| 928 | + | }, |
| 929 | + | { |
| 930 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
| 931 | + | meta: &widgetapi.EventMeta{}, |
| 932 | + | }, |
| 933 | + | { |
| 934 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 935 | + | meta: &widgetapi.EventMeta{}, |
| 936 | + | }, |
| 937 | + | { |
| 938 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
| 939 | + | meta: &widgetapi.EventMeta{}, |
| 940 | + | }, |
404 | 941 | | }, |
405 | 942 | | want: func(size image.Point) *faketerm.Terminal { |
406 | 943 | | ft := faketerm.MustNew(size) |
| skipped 27 lines |
434 | 971 | | }, |
435 | 972 | | text: "hello", |
436 | 973 | | canvas: image.Rect(0, 0, 8, 4), |
437 | | - | events: []terminalapi.Event{ |
438 | | - | &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
439 | | - | &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
| 974 | + | meta: &widgetapi.Meta{Focused: false}, |
| 975 | + | events: []*event{ |
| 976 | + | { |
| 977 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 978 | + | meta: &widgetapi.EventMeta{}, |
| 979 | + | }, |
| 980 | + | { |
| 981 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonRelease}, |
| 982 | + | meta: &widgetapi.EventMeta{}, |
| 983 | + | }, |
440 | 984 | | }, |
441 | 985 | | wantCallbackErr: true, |
442 | 986 | | }, |
| skipped 11 lines |
454 | 998 | | return 200 * time.Millisecond |
455 | 999 | | }, |
456 | 1000 | | canvas: image.Rect(0, 0, 8, 4), |
457 | | - | events: []terminalapi.Event{ |
458 | | - | &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 1001 | + | meta: &widgetapi.Meta{Focused: false}, |
| 1002 | + | events: []*event{ |
| 1003 | + | { |
| 1004 | + | ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter}, |
| 1005 | + | meta: &widgetapi.EventMeta{Focused: true}, |
| 1006 | + | }, |
459 | 1007 | | }, |
460 | 1008 | | wantCallbackErr: true, |
461 | 1009 | | }, |
| skipped 2 lines |
464 | 1012 | | callback: &callbackTracker{}, |
465 | 1013 | | text: "hello", |
466 | 1014 | | canvas: image.Rect(0, 0, 8, 2), |
| 1015 | + | meta: &widgetapi.Meta{Focused: false}, |
467 | 1016 | | want: func(size image.Point) *faketerm.Terminal { |
468 | 1017 | | ft := faketerm.MustNew(size) |
469 | 1018 | | cvs := testcanvas.MustNew(ft.Area()) |
| skipped 21 lines |
491 | 1040 | | callback: &callbackTracker{}, |
492 | 1041 | | text: "h", |
493 | 1042 | | canvas: image.Rect(0, 0, 4, 2), |
| 1043 | + | meta: &widgetapi.Meta{Focused: false}, |
494 | 1044 | | want: func(size image.Point) *faketerm.Terminal { |
495 | 1045 | | ft := faketerm.MustNew(size) |
496 | 1046 | | cvs := testcanvas.MustNew(ft.Area()) |
| skipped 24 lines |
521 | 1071 | | TextColor(cell.ColorRed), |
522 | 1072 | | }, |
523 | 1073 | | canvas: image.Rect(0, 0, 8, 4), |
| 1074 | + | meta: &widgetapi.Meta{Focused: false}, |
524 | 1075 | | want: func(size image.Point) *faketerm.Terminal { |
525 | 1076 | | ft := faketerm.MustNew(size) |
526 | 1077 | | cvs := testcanvas.MustNew(ft.Area()) |
| skipped 24 lines |
551 | 1102 | | FillColor(cell.ColorRed), |
552 | 1103 | | }, |
553 | 1104 | | canvas: image.Rect(0, 0, 8, 4), |
| 1105 | + | meta: &widgetapi.Meta{Focused: false}, |
554 | 1106 | | want: func(size image.Point) *faketerm.Terminal { |
555 | 1107 | | ft := faketerm.MustNew(size) |
556 | 1108 | | cvs := testcanvas.MustNew(ft.Area()) |
| skipped 24 lines |
581 | 1133 | | ShadowColor(cell.ColorRed), |
582 | 1134 | | }, |
583 | 1135 | | canvas: image.Rect(0, 0, 8, 4), |
| 1136 | + | meta: &widgetapi.Meta{Focused: false}, |
584 | 1137 | | want: func(size image.Point) *faketerm.Terminal { |
585 | 1138 | | ft := faketerm.MustNew(size) |
586 | 1139 | | cvs := testcanvas.MustNew(ft.Area()) |
| skipped 16 lines |
603 | 1156 | | }, |
604 | 1157 | | wantCallback: &callbackTracker{}, |
605 | 1158 | | }, |
| 1159 | + | { |
| 1160 | + | desc: "draws button with text chunks and custom fill color in up state", |
| 1161 | + | callback: &callbackTracker{}, |
| 1162 | + | opts: []Option{ |
| 1163 | + | FillColor(cell.ColorBlue), |
| 1164 | + | FocusedFillColor(cell.ColorYellow), |
| 1165 | + | PressedFillColor(cell.ColorRed), |
| 1166 | + | DisableShadow(), |
| 1167 | + | }, |
| 1168 | + | textChunks: []*TextChunk{ |
| 1169 | + | NewChunk( |
| 1170 | + | "h", |
| 1171 | + | TextCellOpts(cell.FgColor(cell.ColorBlack)), |
| 1172 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorWhite)), |
| 1173 | + | PressedTextCellOpts(cell.FgColor(cell.ColorGreen)), |
| 1174 | + | ), |
| 1175 | + | NewChunk( |
| 1176 | + | "ello", |
| 1177 | + | TextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1178 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1179 | + | PressedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1180 | + | ), |
| 1181 | + | }, |
| 1182 | + | canvas: image.Rect(0, 0, 8, 4), |
| 1183 | + | meta: &widgetapi.Meta{Focused: false}, |
| 1184 | + | want: func(size image.Point) *faketerm.Terminal { |
| 1185 | + | ft := faketerm.MustNew(size) |
| 1186 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 1187 | + | |
| 1188 | + | // Button. |
| 1189 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 8, 4), 'x', cell.BgColor(cell.ColorBlue)) |
| 1190 | + | |
| 1191 | + | // Text. |
| 1192 | + | testdraw.MustText(cvs, "h", image.Point{1, 1}, |
| 1193 | + | draw.TextCellOpts( |
| 1194 | + | cell.FgColor(cell.ColorBlack), |
| 1195 | + | cell.BgColor(cell.ColorBlue)), |
| 1196 | + | ) |
| 1197 | + | testdraw.MustText(cvs, "ello", image.Point{2, 1}, |
| 1198 | + | draw.TextCellOpts( |
| 1199 | + | cell.FgColor(cell.ColorMagenta), |
| 1200 | + | cell.BgColor(cell.ColorBlue)), |
| 1201 | + | ) |
| 1202 | + | |
| 1203 | + | testcanvas.MustApply(cvs, ft) |
| 1204 | + | return ft |
| 1205 | + | }, |
| 1206 | + | wantCallback: &callbackTracker{}, |
| 1207 | + | }, |
| 1208 | + | { |
| 1209 | + | desc: "draws button with text chunks and custom fill color in focused up state", |
| 1210 | + | callback: &callbackTracker{}, |
| 1211 | + | opts: []Option{ |
| 1212 | + | FillColor(cell.ColorBlue), |
| 1213 | + | FocusedFillColor(cell.ColorYellow), |
| 1214 | + | PressedFillColor(cell.ColorRed), |
| 1215 | + | DisableShadow(), |
| 1216 | + | }, |
| 1217 | + | textChunks: []*TextChunk{ |
| 1218 | + | NewChunk( |
| 1219 | + | "h", |
| 1220 | + | TextCellOpts(cell.FgColor(cell.ColorBlack)), |
| 1221 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorWhite)), |
| 1222 | + | PressedTextCellOpts(cell.FgColor(cell.ColorGreen)), |
| 1223 | + | ), |
| 1224 | + | NewChunk( |
| 1225 | + | "ello", |
| 1226 | + | TextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1227 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1228 | + | PressedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1229 | + | ), |
| 1230 | + | }, |
| 1231 | + | canvas: image.Rect(0, 0, 8, 4), |
| 1232 | + | meta: &widgetapi.Meta{Focused: true}, |
| 1233 | + | want: func(size image.Point) *faketerm.Terminal { |
| 1234 | + | ft := faketerm.MustNew(size) |
| 1235 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 1236 | + | |
| 1237 | + | // Button. |
| 1238 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 8, 4), 'x', cell.BgColor(cell.ColorYellow)) |
| 1239 | + | |
| 1240 | + | // Text. |
| 1241 | + | testdraw.MustText(cvs, "h", image.Point{1, 1}, |
| 1242 | + | draw.TextCellOpts( |
| 1243 | + | cell.FgColor(cell.ColorWhite), |
| 1244 | + | cell.BgColor(cell.ColorYellow)), |
| 1245 | + | ) |
| 1246 | + | testdraw.MustText(cvs, "ello", image.Point{2, 1}, |
| 1247 | + | draw.TextCellOpts( |
| 1248 | + | cell.FgColor(cell.ColorMagenta), |
| 1249 | + | cell.BgColor(cell.ColorYellow)), |
| 1250 | + | ) |
| 1251 | + | |
| 1252 | + | testcanvas.MustApply(cvs, ft) |
| 1253 | + | return ft |
| 1254 | + | }, |
| 1255 | + | wantCallback: &callbackTracker{}, |
| 1256 | + | }, |
| 1257 | + | { |
| 1258 | + | desc: "draws button with text chunks in up state, focused colors default to regular colors", |
| 1259 | + | callback: &callbackTracker{}, |
| 1260 | + | opts: []Option{ |
| 1261 | + | FillColor(cell.ColorBlue), |
| 1262 | + | PressedFillColor(cell.ColorRed), |
| 1263 | + | DisableShadow(), |
| 1264 | + | }, |
| 1265 | + | textChunks: []*TextChunk{ |
| 1266 | + | NewChunk( |
| 1267 | + | "h", |
| 1268 | + | TextCellOpts(cell.FgColor(cell.ColorBlack)), |
| 1269 | + | PressedTextCellOpts(cell.FgColor(cell.ColorGreen)), |
| 1270 | + | ), |
| 1271 | + | NewChunk( |
| 1272 | + | "ello", |
| 1273 | + | TextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1274 | + | PressedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1275 | + | ), |
| 1276 | + | }, |
| 1277 | + | canvas: image.Rect(0, 0, 8, 4), |
| 1278 | + | meta: &widgetapi.Meta{Focused: true}, |
| 1279 | + | want: func(size image.Point) *faketerm.Terminal { |
| 1280 | + | ft := faketerm.MustNew(size) |
| 1281 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 1282 | + | |
| 1283 | + | // Button. |
| 1284 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 8, 4), 'x', cell.BgColor(cell.ColorBlue)) |
| 1285 | + | |
| 1286 | + | // Text. |
| 1287 | + | testdraw.MustText(cvs, "h", image.Point{1, 1}, |
| 1288 | + | draw.TextCellOpts( |
| 1289 | + | cell.FgColor(cell.ColorBlack), |
| 1290 | + | cell.BgColor(cell.ColorBlue)), |
| 1291 | + | ) |
| 1292 | + | testdraw.MustText(cvs, "ello", image.Point{2, 1}, |
| 1293 | + | draw.TextCellOpts( |
| 1294 | + | cell.FgColor(cell.ColorMagenta), |
| 1295 | + | cell.BgColor(cell.ColorBlue)), |
| 1296 | + | ) |
| 1297 | + | |
| 1298 | + | testcanvas.MustApply(cvs, ft) |
| 1299 | + | return ft |
| 1300 | + | }, |
| 1301 | + | wantCallback: &callbackTracker{}, |
| 1302 | + | }, |
| 1303 | + | { |
| 1304 | + | desc: "draws button with text chunks and custom fill color in down state", |
| 1305 | + | events: []*event{ |
| 1306 | + | { |
| 1307 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 1308 | + | meta: &widgetapi.EventMeta{}, |
| 1309 | + | }, |
| 1310 | + | }, |
| 1311 | + | callback: &callbackTracker{}, |
| 1312 | + | opts: []Option{ |
| 1313 | + | FillColor(cell.ColorBlue), |
| 1314 | + | FocusedFillColor(cell.ColorYellow), |
| 1315 | + | PressedFillColor(cell.ColorRed), |
| 1316 | + | DisableShadow(), |
| 1317 | + | }, |
| 1318 | + | textChunks: []*TextChunk{ |
| 1319 | + | NewChunk( |
| 1320 | + | "h", |
| 1321 | + | TextCellOpts(cell.FgColor(cell.ColorBlack)), |
| 1322 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorWhite)), |
| 1323 | + | PressedTextCellOpts(cell.FgColor(cell.ColorGreen)), |
| 1324 | + | ), |
| 1325 | + | NewChunk( |
| 1326 | + | "ello", |
| 1327 | + | TextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1328 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1329 | + | PressedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1330 | + | ), |
| 1331 | + | }, |
| 1332 | + | canvas: image.Rect(0, 0, 8, 4), |
| 1333 | + | meta: &widgetapi.Meta{Focused: false}, |
| 1334 | + | want: func(size image.Point) *faketerm.Terminal { |
| 1335 | + | ft := faketerm.MustNew(size) |
| 1336 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 1337 | + | |
| 1338 | + | // Button. |
| 1339 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 8, 4), 'x', cell.BgColor(cell.ColorRed)) |
| 1340 | + | |
| 1341 | + | // Text. |
| 1342 | + | testdraw.MustText(cvs, "h", image.Point{1, 1}, |
| 1343 | + | draw.TextCellOpts( |
| 1344 | + | cell.FgColor(cell.ColorGreen), |
| 1345 | + | cell.BgColor(cell.ColorRed)), |
| 1346 | + | ) |
| 1347 | + | testdraw.MustText(cvs, "ello", image.Point{2, 1}, |
| 1348 | + | draw.TextCellOpts( |
| 1349 | + | cell.FgColor(cell.ColorMagenta), |
| 1350 | + | cell.BgColor(cell.ColorRed)), |
| 1351 | + | ) |
| 1352 | + | |
| 1353 | + | testcanvas.MustApply(cvs, ft) |
| 1354 | + | return ft |
| 1355 | + | }, |
| 1356 | + | wantCallback: &callbackTracker{}, |
| 1357 | + | }, |
| 1358 | + | { |
| 1359 | + | desc: "draws button with text chunks and custom fill color in down focused state (focus has no impact)", |
| 1360 | + | events: []*event{ |
| 1361 | + | { |
| 1362 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 1363 | + | meta: &widgetapi.EventMeta{}, |
| 1364 | + | }, |
| 1365 | + | }, |
| 1366 | + | callback: &callbackTracker{}, |
| 1367 | + | opts: []Option{ |
| 1368 | + | FillColor(cell.ColorBlue), |
| 1369 | + | FocusedFillColor(cell.ColorYellow), |
| 1370 | + | PressedFillColor(cell.ColorRed), |
| 1371 | + | DisableShadow(), |
| 1372 | + | }, |
| 1373 | + | textChunks: []*TextChunk{ |
| 1374 | + | NewChunk( |
| 1375 | + | "h", |
| 1376 | + | TextCellOpts(cell.FgColor(cell.ColorBlack)), |
| 1377 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorWhite)), |
| 1378 | + | PressedTextCellOpts(cell.FgColor(cell.ColorGreen)), |
| 1379 | + | ), |
| 1380 | + | NewChunk( |
| 1381 | + | "ello", |
| 1382 | + | TextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1383 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1384 | + | PressedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1385 | + | ), |
| 1386 | + | }, |
| 1387 | + | canvas: image.Rect(0, 0, 8, 4), |
| 1388 | + | meta: &widgetapi.Meta{Focused: true}, |
| 1389 | + | want: func(size image.Point) *faketerm.Terminal { |
| 1390 | + | ft := faketerm.MustNew(size) |
| 1391 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 1392 | + | |
| 1393 | + | // Button. |
| 1394 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 8, 4), 'x', cell.BgColor(cell.ColorRed)) |
| 1395 | + | |
| 1396 | + | // Text. |
| 1397 | + | testdraw.MustText(cvs, "h", image.Point{1, 1}, |
| 1398 | + | draw.TextCellOpts( |
| 1399 | + | cell.FgColor(cell.ColorGreen), |
| 1400 | + | cell.BgColor(cell.ColorRed)), |
| 1401 | + | ) |
| 1402 | + | testdraw.MustText(cvs, "ello", image.Point{2, 1}, |
| 1403 | + | draw.TextCellOpts( |
| 1404 | + | cell.FgColor(cell.ColorMagenta), |
| 1405 | + | cell.BgColor(cell.ColorRed)), |
| 1406 | + | ) |
| 1407 | + | |
| 1408 | + | testcanvas.MustApply(cvs, ft) |
| 1409 | + | return ft |
| 1410 | + | }, |
| 1411 | + | wantCallback: &callbackTracker{}, |
| 1412 | + | }, |
| 1413 | + | { |
| 1414 | + | desc: "draws button with text chunks in down satte, pressed colors default to regular colors", |
| 1415 | + | events: []*event{ |
| 1416 | + | { |
| 1417 | + | ev: &terminalapi.Mouse{Position: image.Point{0, 0}, Button: mouse.ButtonLeft}, |
| 1418 | + | meta: &widgetapi.EventMeta{}, |
| 1419 | + | }, |
| 1420 | + | }, |
| 1421 | + | callback: &callbackTracker{}, |
| 1422 | + | opts: []Option{ |
| 1423 | + | FillColor(cell.ColorBlue), |
| 1424 | + | FocusedFillColor(cell.ColorYellow), |
| 1425 | + | DisableShadow(), |
| 1426 | + | }, |
| 1427 | + | textChunks: []*TextChunk{ |
| 1428 | + | NewChunk( |
| 1429 | + | "h", |
| 1430 | + | TextCellOpts(cell.FgColor(cell.ColorBlack)), |
| 1431 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorWhite)), |
| 1432 | + | ), |
| 1433 | + | NewChunk( |
| 1434 | + | "ello", |
| 1435 | + | TextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1436 | + | FocusedTextCellOpts(cell.FgColor(cell.ColorMagenta)), |
| 1437 | + | ), |
| 1438 | + | }, |
| 1439 | + | canvas: image.Rect(0, 0, 8, 4), |
| 1440 | + | meta: &widgetapi.Meta{Focused: false}, |
| 1441 | + | want: func(size image.Point) *faketerm.Terminal { |
| 1442 | + | ft := faketerm.MustNew(size) |
| 1443 | + | cvs := testcanvas.MustNew(ft.Area()) |
| 1444 | + | |
| 1445 | + | // Button. |
| 1446 | + | testcanvas.MustSetAreaCells(cvs, image.Rect(0, 0, 8, 4), 'x', cell.BgColor(cell.ColorBlue)) |
| 1447 | + | |
| 1448 | + | // Text. |
| 1449 | + | testdraw.MustText(cvs, "h", image.Point{1, 1}, |
| 1450 | + | draw.TextCellOpts( |
| 1451 | + | cell.FgColor(cell.ColorBlack), |
| 1452 | + | cell.BgColor(cell.ColorBlue)), |
| 1453 | + | ) |
| 1454 | + | testdraw.MustText(cvs, "ello", image.Point{2, 1}, |
| 1455 | + | draw.TextCellOpts( |
| 1456 | + | cell.FgColor(cell.ColorMagenta), |
| 1457 | + | cell.BgColor(cell.ColorBlue)), |
| 1458 | + | ) |
| 1459 | + | |
| 1460 | + | testcanvas.MustApply(cvs, ft) |
| 1461 | + | return ft |
| 1462 | + | }, |
| 1463 | + | wantCallback: &callbackTracker{}, |
| 1464 | + | }, |
606 | 1465 | | } |
607 | 1466 | | |
608 | 1467 | | buttonRune = 'x' |
| skipped 10 lines |
619 | 1478 | | var cFn CallbackFn |
620 | 1479 | | if gotCallback == nil { |
621 | 1480 | | cFn = nil |
| 1481 | + | } else if gotCallback.useSetCallback { |
| 1482 | + | // Set an no-op callback via the constructor. |
| 1483 | + | // It will be updated to the real one via SetCallback. |
| 1484 | + | cFn = func() error { return nil } |
622 | 1485 | | } else { |
623 | 1486 | | cFn = gotCallback.callback |
624 | 1487 | | } |
625 | | - | b, err := New(tc.text, cFn, tc.opts...) |
626 | | - | if (err != nil) != tc.wantNewErr { |
627 | | - | t.Errorf("New => unexpected error: %v, wantNewErr: %v", err, tc.wantNewErr) |
| 1488 | + | |
| 1489 | + | if tc.text != "" && tc.textChunks != nil { |
| 1490 | + | t.Fatalf("cannot specify both text and textChunks in the testdata") |
| 1491 | + | } |
| 1492 | + | |
| 1493 | + | var btn *Button |
| 1494 | + | if tc.textChunks != nil { |
| 1495 | + | b, err := NewFromChunks(tc.textChunks, cFn, tc.opts...) |
| 1496 | + | if (err != nil) != tc.wantNewErr { |
| 1497 | + | t.Errorf("NewFromChunks => unexpected error: %v, wantNewErr: %v", err, tc.wantNewErr) |
| 1498 | + | } |
| 1499 | + | if err != nil { |
| 1500 | + | return |
| 1501 | + | } |
| 1502 | + | btn = b |
| 1503 | + | } else { |
| 1504 | + | b, err := New(tc.text, cFn, tc.opts...) |
| 1505 | + | if (err != nil) != tc.wantNewErr { |
| 1506 | + | t.Errorf("New => unexpected error: %v, wantNewErr: %v", err, tc.wantNewErr) |
| 1507 | + | } |
| 1508 | + | if err != nil { |
| 1509 | + | return |
| 1510 | + | } |
| 1511 | + | btn = b |
628 | 1512 | | } |
629 | | - | if err != nil { |
630 | | - | return |
| 1513 | + | |
| 1514 | + | if gotCallback != nil && gotCallback.useSetCallback { |
| 1515 | + | btn.SetCallback(gotCallback.callback) |
631 | 1516 | | } |
632 | 1517 | | |
633 | 1518 | | { |
| skipped 2 lines |
636 | 1521 | | if err != nil { |
637 | 1522 | | t.Fatalf("canvas.New => unexpected error: %v", err) |
638 | 1523 | | } |
639 | | - | err = b.Draw(c, tc.meta) |
| 1524 | + | err = btn.Draw(c, tc.meta) |
640 | 1525 | | if (err != nil) != tc.wantDrawErr { |
641 | 1526 | | t.Errorf("Draw => unexpected error: %v, wantDrawErr: %v", err, tc.wantDrawErr) |
642 | 1527 | | } |
| skipped 2 lines |
645 | 1530 | | } |
646 | 1531 | | } |
647 | 1532 | | |
648 | | - | for i, ev := range tc.events { |
649 | | - | switch e := ev.(type) { |
| 1533 | + | for i, event := range tc.events { |
| 1534 | + | switch e := event.ev.(type) { |
650 | 1535 | | case *terminalapi.Mouse: |
651 | | - | err := b.Mouse(e) |
| 1536 | + | err := btn.Mouse(e, event.meta) |
652 | 1537 | | // Only the last event in test cases is the one that triggers the callback. |
653 | 1538 | | if i == len(tc.events)-1 { |
654 | 1539 | | if (err != nil) != tc.wantCallbackErr { |
| skipped 9 lines |
664 | 1549 | | } |
665 | 1550 | | |
666 | 1551 | | case *terminalapi.Keyboard: |
667 | | - | err := b.Keyboard(e) |
| 1552 | + | err := btn.Keyboard(e, event.meta) |
668 | 1553 | | // Only the last event in test cases is the one that triggers the callback. |
669 | 1554 | | if i == len(tc.events)-1 { |
670 | 1555 | | if (err != nil) != tc.wantCallbackErr { |
| skipped 9 lines |
680 | 1565 | | } |
681 | 1566 | | |
682 | 1567 | | default: |
683 | | - | t.Fatalf("unsupported event type: %T", ev) |
| 1568 | + | t.Fatalf("unsupported event type: %T", event.ev) |
684 | 1569 | | } |
685 | 1570 | | } |
686 | 1571 | | |
| skipped 2 lines |
689 | 1574 | | t.Fatalf("canvas.New => unexpected error: %v", err) |
690 | 1575 | | } |
691 | 1576 | | |
692 | | - | err = b.Draw(c, tc.meta) |
| 1577 | + | err = btn.Draw(c, tc.meta) |
693 | 1578 | | if (err != nil) != tc.wantDrawErr { |
694 | 1579 | | t.Errorf("Draw => unexpected error: %v, wantDrawErr: %v", err, tc.wantDrawErr) |
695 | 1580 | | } |
| skipped 82 lines |
778 | 1663 | | }, |
779 | 1664 | | }, |
780 | 1665 | | { |
781 | | - | desc: "custom width specified", |
| 1666 | + | desc: "custom width specified with default padding", |
782 | 1667 | | text: "hello", |
783 | 1668 | | opts: []Option{ |
784 | 1669 | | Width(10), |
785 | 1670 | | }, |
786 | 1671 | | want: widgetapi.Options{ |
| 1672 | + | MinimumSize: image.Point{13, 4}, |
| 1673 | + | MaximumSize: image.Point{13, 4}, |
| 1674 | + | WantKeyboard: widgetapi.KeyScopeNone, |
| 1675 | + | WantMouse: widgetapi.MouseScopeGlobal, |
| 1676 | + | }, |
| 1677 | + | }, |
| 1678 | + | { |
| 1679 | + | desc: "custom width specified with custom padding", |
| 1680 | + | text: "hello", |
| 1681 | + | opts: []Option{ |
| 1682 | + | Width(10), |
| 1683 | + | TextHorizontalPadding(0), |
| 1684 | + | }, |
| 1685 | + | want: widgetapi.Options{ |
787 | 1686 | | MinimumSize: image.Point{11, 4}, |
788 | 1687 | | MaximumSize: image.Point{11, 4}, |
789 | 1688 | | WantKeyboard: widgetapi.KeyScopeNone, |
790 | 1689 | | WantMouse: widgetapi.MouseScopeGlobal, |
791 | 1690 | | }, |
792 | 1691 | | }, |
793 | | - | |
| 1692 | + | { |
| 1693 | + | desc: "without shadow or padding", |
| 1694 | + | text: "hello", |
| 1695 | + | opts: []Option{ |
| 1696 | + | Width(10), |
| 1697 | + | TextHorizontalPadding(0), |
| 1698 | + | DisableShadow(), |
| 1699 | + | }, |
| 1700 | + | want: widgetapi.Options{ |
| 1701 | + | MinimumSize: image.Point{10, 3}, |
| 1702 | + | MaximumSize: image.Point{10, 3}, |
| 1703 | + | WantKeyboard: widgetapi.KeyScopeNone, |
| 1704 | + | WantMouse: widgetapi.MouseScopeGlobal, |
| 1705 | + | }, |
| 1706 | + | }, |
794 | 1707 | | { |
795 | | - | desc: "doesn't want keyboard by default", |
| 1708 | + | desc: "doesn't want keyboard by default without any keys", |
796 | 1709 | | text: "hello", |
797 | 1710 | | want: widgetapi.Options{ |
798 | 1711 | | MinimumSize: image.Point{8, 4}, |
| skipped 3 lines |
802 | 1715 | | }, |
803 | 1716 | | }, |
804 | 1717 | | { |
805 | | - | desc: "registers for focused keyboard events", |
| 1718 | + | desc: "registers for keyboard events when Key used", |
806 | 1719 | | text: "hello", |
807 | 1720 | | opts: []Option{ |
808 | 1721 | | Key(keyboard.KeyEnter), |
| skipped 1 lines |
810 | 1723 | | want: widgetapi.Options{ |
811 | 1724 | | MinimumSize: image.Point{8, 4}, |
812 | 1725 | | MaximumSize: image.Point{8, 4}, |
813 | | - | WantKeyboard: widgetapi.KeyScopeFocused, |
| 1726 | + | WantKeyboard: widgetapi.KeyScopeGlobal, |
814 | 1727 | | WantMouse: widgetapi.MouseScopeGlobal, |
815 | 1728 | | }, |
816 | 1729 | | }, |
817 | 1730 | | { |
818 | | - | desc: "registers for global keyboard events", |
| 1731 | + | desc: "registers for keyboard events when Keys used", |
| 1732 | + | text: "hello", |
| 1733 | + | opts: []Option{ |
| 1734 | + | Keys(keyboard.KeyEnter, keyboard.KeyTab), |
| 1735 | + | }, |
| 1736 | + | want: widgetapi.Options{ |
| 1737 | + | MinimumSize: image.Point{8, 4}, |
| 1738 | + | MaximumSize: image.Point{8, 4}, |
| 1739 | + | WantKeyboard: widgetapi.KeyScopeGlobal, |
| 1740 | + | WantMouse: widgetapi.MouseScopeGlobal, |
| 1741 | + | }, |
| 1742 | + | }, |
| 1743 | + | { |
| 1744 | + | desc: "registers for keyboard events when GlobalKey used", |
819 | 1745 | | text: "hello", |
820 | 1746 | | opts: []Option{ |
821 | 1747 | | GlobalKey(keyboard.KeyEnter), |
| 1748 | + | }, |
| 1749 | + | want: widgetapi.Options{ |
| 1750 | + | MinimumSize: image.Point{8, 4}, |
| 1751 | + | MaximumSize: image.Point{8, 4}, |
| 1752 | + | WantKeyboard: widgetapi.KeyScopeGlobal, |
| 1753 | + | WantMouse: widgetapi.MouseScopeGlobal, |
| 1754 | + | }, |
| 1755 | + | }, |
| 1756 | + | { |
| 1757 | + | desc: "registers for keyboard events when GlobalKeys used", |
| 1758 | + | text: "hello", |
| 1759 | + | opts: []Option{ |
| 1760 | + | GlobalKeys(keyboard.KeyEnter, keyboard.KeyTab), |
822 | 1761 | | }, |
823 | 1762 | | want: widgetapi.Options{ |
824 | 1763 | | MinimumSize: image.Point{8, 4}, |
| skipped 24 lines |