-
Notifications
You must be signed in to change notification settings - Fork 2
/
device_test.go
142 lines (122 loc) · 4.06 KB
/
device_test.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package adb
import (
"bytes"
"errors"
"fmt"
"testing"
"github.com/prife/goadb/wire"
"github.com/stretchr/testify/assert"
)
func TestGetAttribute(t *testing.T) {
s := &MockServer{
Status: wire.StatusSuccess,
Messages: []string{"value"},
}
client := (&Adb{s}).Device(DeviceWithSerial("serial"))
v, err := client.getAttribute("attr")
assert.Equal(t, "host-serial:serial:attr", s.Requests[0])
assert.NoError(t, err)
assert.Equal(t, "value", v)
}
func TestGetDeviceInfo(t *testing.T) {
deviceLister := func() ([]*DeviceInfo, error) {
return []*DeviceInfo{
&DeviceInfo{
Serial: "abc",
Product: "Foo",
},
&DeviceInfo{
Serial: "def",
Product: "Bar",
},
}, nil
}
client := newDeviceClientWithDeviceLister("abc", deviceLister)
device, err := client.DeviceInfo()
assert.NoError(t, err)
assert.Equal(t, "Foo", device.Product)
client = newDeviceClientWithDeviceLister("def", deviceLister)
device, err = client.DeviceInfo()
assert.NoError(t, err)
assert.Equal(t, "Bar", device.Product)
client = newDeviceClientWithDeviceLister("serial", deviceLister)
device, err = client.DeviceInfo()
assert.True(t, errors.Is(err, wire.ErrDeviceNotFound))
assert.EqualError(t, errors.Unwrap(err),
"DeviceNotFound: device list doesn't contain serial serial")
assert.Nil(t, device)
}
func newDeviceClientWithDeviceLister(serial string, deviceLister func() ([]*DeviceInfo, error)) *Device {
client := (&Adb{&MockServer{
Status: wire.StatusSuccess,
Messages: []string{serial},
}}).Device(DeviceWithSerial(serial))
client.deviceListFunc = deviceLister
return client
}
func TestRunCommandNoArgs(t *testing.T) {
buf := bytes.NewBuffer([]byte("output"))
s := &MockServer{
Status: wire.StatusSuccess,
// Messages: []string{"output"},
mockConn: mockConn{
Buffer: buf,
},
}
client := (&Adb{s}).Device(AnyDevice())
v, err := client.RunCommand("cmd")
assert.Equal(t, "host:transport-any", s.Requests[0])
assert.Equal(t, "shell:cmd", s.Requests[1])
assert.NoError(t, err)
assert.Equal(t, "output", string(v))
}
func TestPrepareCommandLineNoArgs(t *testing.T) {
result, err := prepareCommandLine("cmd")
assert.NoError(t, err)
assert.Equal(t, "cmd", result)
}
func TestPrepareCommandLineEmptyCommand(t *testing.T) {
_, err := prepareCommandLine("")
assert.True(t, errors.Is(err, wire.ErrAssertion))
assert.Contains(t, err.Error(), "command cannot be empty")
}
func TestPrepareCommandLineBlankCommand(t *testing.T) {
_, err := prepareCommandLine(" ")
assert.True(t, errors.Is(err, wire.ErrAssertion))
assert.Contains(t, err.Error(), "command cannot be empty")
}
func TestPrepareCommandLineCleanArgs(t *testing.T) {
result, err := prepareCommandLine("cmd", "arg1", "arg2")
assert.NoError(t, err)
assert.Equal(t, "cmd arg1 arg2", result)
}
func TestPrepareCommandLineArgWithWhitespaceQuotes(t *testing.T) {
result, err := prepareCommandLine("cmd", "arg with spaces")
assert.NoError(t, err)
assert.Equal(t, "cmd \"arg with spaces\"", result)
}
func TestPrepareCommandLineArgWithDoubleQuoteFails(t *testing.T) {
_, err := prepareCommandLine("cmd", "quoted\"arg")
assert.True(t, errors.Is(err, wire.ErrParse))
assert.Contains(t, err.Error(), "arg at index 0 contains an invalid double quote: quoted\"arg")
}
func Test_featuresStrToMap(t *testing.T) {
str := "shell_v2,cmd,stat_v2,ls_v2,fixed_push_mkdir,apex,abb,fixed_push_symlink_timestamp,abb_exec,remount_shell,track_app,sendrecv_v2,sendrecv_v2_brotli,sendrecv_v2_lz4,sendrecv_v2_zstd,sendrecv_v2_dry_run_send,openscreen_mdns,delayed_ack"
features := featuresStrToMap(str)
assert.Equal(t, len(features), 18)
// for k, _ := range features {
// fmt.Println(k)
// }
}
func TestDevice_State(t *testing.T) {
assert.NotNil(t, adbclient)
d := adbclient.Device(DeviceWithSerial("not-existed"))
state, err := d.State()
assert.Equal(t, state, StateInvalid)
assert.ErrorIs(t, err, wire.ErrDeviceNotFound)
d2 := adbclient.Device(AnyDevice())
state, err = d2.State()
fmt.Printf("state:%v, err=%v", state, err)
assert.Equal(t, state, StateInvalid)
assert.Contains(t, err.Error(), "no devices/emulators found")
}