Debian Codegraph @

demos/clipboard/clipboard_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 // TestDemo tests the clipboard demo.
27 func TestDemo(t *testing.T) {
28
29 mt := vt.NewMockTerm()
30 scr, err := tcell.NewTerminfoScreenFromTty(mt, tcell.OptColors(8), tcell.OptTerm("ansi"))
31 if err != nil {
32 t.Fatalf("failed to create screen: %v", err)
33 }
34 tcell.ShimScreen(scr)
35 var wg sync.WaitGroup
36 wg.Add(1)
37
38 go func() {
39 defer wg.Done()
40 main()
41 }()
42
43 // give enough time for the screen to draw
44 // this is needed because main is running asynchronously.
45 time.Sleep(time.Millisecond * 50)
46
47 mt.KeyTap(vt.Key1)
48 mt.Drain()
49 time.Sleep(time.Millisecond * 20)
50
51 mt.KeyTap(vt.Key2)
52 mt.Drain()
53 time.Sleep(time.Millisecond * 20)
54
55 expect := "Enjoy your new clipboard content!"
56 if result := string(mt.Backend().GetClipboard()); result != expect {
57 t.Errorf("clipboard content did not match! %q != %q", result, expect)
58 }
59
60 // a long string
61 mt.Backend().SetClipboard([]byte("The quick brown fox jumps over the lazy dog."))
62 mt.KeyTap(vt.Key2)
63 mt.Drain()
64 time.Sleep(time.Millisecond * 20)
65
66 // stick some invalid utf-8
67 mt.Backend().SetClipboard([]byte{0xff})
68 mt.KeyTap(vt.Key2)
69 mt.Drain()
70 time.Sleep(time.Millisecond * 20)
71
72 // now nil
73 mt.Backend().SetClipboard(nil)
74 mt.KeyTap(vt.Key2)
75 mt.Drain()
76 time.Sleep(time.Millisecond * 20)
77
78 mt.KeyTap(vt.KeyLCtrl, vt.KeyQ)
79 mt.Backend().GetSize()
80 wg.Wait()
81 }