Debian Codegraph @

demos/beep/beep_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 TestBeep(t *testing.T) {
27
28 mt := vt.NewMockTerm()
29 scr, err := tcell.NewTerminfoScreenFromTty(mt)
30 if err != nil {
31 t.Fatalf("failed to create screen: %v", err)
32 }
33 tcell.ShimScreen(scr)
34 var wg sync.WaitGroup
35 wg.Add(1)
36
37 go func() {
38 defer wg.Done()
39 main()
40 }()
41
42 // give enough time for the screen to draw
43 // this is needed because main is running asynchronously.
44 time.Sleep(time.Millisecond * 50)
45
46 mt.Drain()
47
48 // simulate two presses of B
49 mt.KeyTap(vt.KeyB)
50 time.Sleep(time.Millisecond * 100)
51 mt.KeyTap(vt.KeyB)
52
53 // control L (forces sync)
54 mt.KeyTap(vt.KeyRCtrl, vt.KeyL)
55
56 // sleep at least 3 seconds to get the time driven beep
57 time.Sleep(time.Millisecond * 3500)
58
59 mt.KeyTap(vt.KeyLCtrl, vt.KeyQ)
60
61 wg.Wait()
62
63 if cnt := mt.Bells(); cnt != 3 {
64 t.Errorf("incorrect bell count %d != 3", cnt)
65 }
66 }