File: usb_driver_test.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (103 lines) | stat: -rw-r--r-- 1,848 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package bootcommand

import (
	"context"
	"testing"
	"time"

	"golang.org/x/mobile/event/key"
)

func TestUSBDriver(t *testing.T) {
	tc := []struct {
		command string
		code    key.Code
		shift   bool
	}{
		{
			"<leftShift>",
			key.CodeLeftShift,
			false,
		},
		{
			"<leftShiftOff>",
			key.CodeLeftShift,
			false,
		},
		{
			"<leftShiftOn>",
			key.CodeLeftShift,
			true,
		},
		{
			"<leftsuper>",
			key.CodeLeftGUI,
			false,
		},
		{
			"<spacebar>",
			key.CodeSpacebar,
			false,
		},
		{
			"<return>",
			key.CodeReturnEnter,
			false,
		},
		{
			"a",
			key.CodeA,
			false,
		},
		{
			"A",
			key.CodeA,
			true,
		},
		{
			"_",
			key.CodeHyphenMinus,
			true,
		},
	}
	for _, tt := range tc {
		t.Run(tt.command, func(t *testing.T) {
			var code key.Code
			var shift bool
			sendCodes := func(c key.Code, d bool) error {
				code = c
				shift = d
				return nil
			}
			d := NewUSBDriver(sendCodes, time.Duration(0))
			seq, err := GenerateExpressionSequence(tt.command)
			if err != nil {
				t.Fatalf("bad: not expected error: %s", err.Error())
			}
			err = seq.Do(context.Background(), d)
			if err != nil {
				t.Fatalf("bad: not expected error: %s", err.Error())
			}
			if code != tt.code {
				t.Fatalf("bad: wrong scan code: \n expected: %s \n actual: %s", tt.code, code)
			}
			if shift != tt.shift {
				t.Fatalf("bad: wrong shift: \n expected: %t \n actual: %t", tt.shift, shift)
			}
		})
	}
}

func TestUSBDriver_KeyIntervalNotGiven(t *testing.T) {
	d := NewUSBDriver(nil, time.Duration(0))
	if d.interval != time.Duration(100)*time.Millisecond {
		t.Fatal("not expected key interval")
	}
}

func TestUSBDriver_KeyIntervalGiven(t *testing.T) {
	d := NewUSBDriver(nil, time.Duration(5000)*time.Millisecond)
	if d.interval != time.Duration(5000)*time.Millisecond {
		t.Fatal("not expected key interval")
	}
}