Debian Codegraph @

demos/cursors/cursors_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 "sync"
19 "testing"
20 "time"
21
22 "github.com/gdamore/tcell/v3"
23 "github.com/gdamore/tcell/v3/vt"
24 )
25
26 func verifyCursor(t *testing.T, term vt.MockTerm, expected vt.CursorStyle) {
27 t.Helper()
28 if actual := term.Backend().GetCursor(); actual != expected {
29 t.Errorf("wrong cursor style %x != %x", actual, expected)
30 }
31 }
32
33 // TestDemo tests the cursors demo.
34 func TestDemo(t *testing.T) {
35
36 mt := vt.NewMockTerm()
37 scr, err := tcell.NewTerminfoScreenFromTty(mt, tcell.OptColors(8), tcell.OptTerm("ansi"))
38 if err != nil {
39 t.Fatalf("failed to create screen: %v", err)
40 }
41 tcell.ShimScreen(scr)
42 var wg sync.WaitGroup
43 wg.Add(1)
44
45 go func() {
46 defer wg.Done()
47 main()
48 }()
49
50 // give enough time for the screen to draw
51 // this is needed because main is running asynchronously.
52 time.Sleep(time.Millisecond * 50)
53
54 mt.KeyTap(vt.Key1)
55 time.Sleep(time.Millisecond * 20)
56 verifyCursor(t, mt, vt.BlinkingBlock)
57
58 mt.KeyTap(vt.Key2)
59 time.Sleep(time.Millisecond * 20)
60 verifyCursor(t, mt, vt.SteadyBlock)
61
62 mt.KeyTap(vt.Key3)
63 time.Sleep(time.Millisecond * 20)
64 verifyCursor(t, mt, vt.BlinkingUnderline)
65
66 mt.KeyTap(vt.Key4)
67 time.Sleep(time.Millisecond * 20)
68 verifyCursor(t, mt, vt.SteadyUnderline)
69
70 mt.KeyTap(vt.Key5)
71 time.Sleep(time.Millisecond * 20)
72 verifyCursor(t, mt, vt.BlinkingBar)
73
74 mt.KeyTap(vt.Key6)
75 time.Sleep(time.Millisecond * 20)
76 verifyCursor(t, mt, vt.SteadyBar)
77
78 mt.KeyTap(vt.Key0)
79 time.Sleep(time.Millisecond * 20)
80 verifyCursor(t, mt, vt.BlinkingBlock)
81
82 mt.KeyTap(vt.KeyRCtrl, vt.KeyL)
83 mt.Drain()
84 mt.KeyTap(vt.KeyRCtrl, vt.KeyQ)
85 mt.Backend().GetSize()
86 wg.Wait()
87 }