Debian Codegraph @

demos/boxes/boxes_test.go

raw symbols SCIP
1 // Copyright 2026 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18 "fmt"
19 "sync"
20 "testing"
21 "time"
22
23 "github.com/gdamore/tcell/v3"
24 "github.com/gdamore/tcell/v3/vt"
25 )
26
27 func TestBoxes(t *testing.T) {
28
29 for _, colors := range []int{0, 8, 16, 88, 256, 1 << 24} {
30 t.Run(fmt.Sprintf("%d_colors)", colors), func(t *testing.T) {
31 mt := vt.NewMockTerm(vt.MockOptColors(colors))
32 scr, err := tcell.NewTerminfoScreenFromTty(mt, tcell.OptColors(colors))
33 if err != nil {
34 t.Fatalf("failed to create screen: %v", err)
35 }
36 tcell.ShimScreen(scr)
37 var wg sync.WaitGroup
38 wg.Add(1)
39 count = 0
40 drawTime = 0
41 interval = time.Microsecond * 10
42
43 go func() {
44 defer wg.Done()
45 main()
46 }()
47
48 time.Sleep(time.Millisecond * 25)
49 mt.KeyTap(vt.KeyRCtrl, vt.KeyL)
50 mt.SetSize(vt.Coord{X: 10, Y: 10})
51 mt.Drain()
52 time.Sleep(time.Millisecond * 25)
53 mt.KeyTap(vt.KeyLCtrl, vt.KeyQ)
54 mt.Drain()
55 wg.Wait()
56
57 if count < 10 { // CI runs *slow*
58 t.Errorf("Too few boxes: %d", count)
59 }
60 if drawTime < time.Microsecond {
61 // on windows this can happen because our tick counter is too coarse,
62 // and our mock screen is basically limited only by CPU.
63 t.Logf("Draw time very short: %s", drawTime)
64 }
65
66 // It should not take 10 milliseconds to draw a box,
67 // as we generally see values sub millisecond here.
68 if drawTime > 10*time.Millisecond*time.Duration(count) {
69 t.Errorf("Draw time too long: %s", drawTime)
70 }
71 })
72 }
73 }