forked from zach-klippenstein/goadb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_test.go
121 lines (102 loc) · 3.29 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
package adb
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zach-klippenstein/goadb/internal/errors"
"github.com/zach-klippenstein/goadb/wire"
)
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, HasErrCode(err, DeviceNotFound))
assert.EqualError(t, err.(*errors.Err).Cause,
"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) {
s := &MockServer{
Status: wire.StatusSuccess,
Messages: []string{"output"},
}
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", 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.Equal(t, errors.AssertionError, code(err))
assert.Equal(t, "command cannot be empty", message(err))
}
func TestPrepareCommandLineBlankCommand(t *testing.T) {
_, err := prepareCommandLine(" ")
assert.Equal(t, errors.AssertionError, code(err))
assert.Equal(t, "command cannot be empty", message(err))
}
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.Equal(t, errors.ParseError, code(err))
assert.Equal(t, "arg at index 0 contains an invalid double quote: quoted\"arg", message(err))
}
func code(err error) errors.ErrCode {
return err.(*errors.Err).Code
}
func message(err error) string {
return err.(*errors.Err).Message
}