/
adb-test.ts
321 lines (292 loc) · 9.56 KB
/
adb-test.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { CommandError } from '../../../../utils/errors';
import {
Device,
getAdbNameForDeviceIdAsync,
getAttachedDevicesAsync,
getDeviceABIsAsync,
getPropertyDataForDeviceAsync,
getServer,
isBootAnimationCompleteAsync,
isDeviceBootedAsync,
isPackageInstalledAsync,
launchActivityAsync,
openAppIdAsync,
sanitizeAdbDeviceName,
openUrlAsync,
} from '../adb';
jest.mock('../ADBServer', () => ({
ADBServer: jest.fn(() => ({
runAsync: jest.fn(async () => ''),
getFileOutputAsync: jest.fn(async () => ''),
})),
}));
const asDevice = (device: Partial<Device>): Device => device as Device;
const device = asDevice({ name: 'Pixel 5', pid: '123' });
describe(openUrlAsync, () => {
it(`escapes & in the url`, async () => {
await openUrlAsync(device, { url: 'acme://foo?bar=1&baz=2' });
expect(getServer().runAsync).toBeCalledWith([
'-s',
'123',
'shell',
'am',
'start',
'-a',
'android.intent.action.VIEW',
'-d',
// Ensure this is escaped
'acme://foo?bar=1\\&baz=2',
]);
});
});
describe(launchActivityAsync, () => {
it(`asserts that the launch activity does not exist`, async () => {
jest
.mocked(getServer().runAsync)
.mockResolvedValueOnce('Error: Activity class dev.bacon.app/.MainActivity does not exist.');
await expect(
launchActivityAsync(device, {
launchActivity: 'dev.bacon.app/.MainActivity',
})
).rejects.toThrow(CommandError);
});
it(`runs`, async () => {
jest.mocked(getServer().runAsync).mockResolvedValueOnce('...');
await launchActivityAsync(device, {
launchActivity: 'dev.bacon.app/.MainActivity',
});
expect(getServer().runAsync).toBeCalledWith([
'-s',
'123',
'shell',
'am',
'start',
'-f',
'0x20000000',
'-n',
'dev.bacon.app/.MainActivity',
]);
});
});
describe(isPackageInstalledAsync, () => {
it(`returns true when a package is installed`, async () => {
jest
.mocked(getServer().runAsync)
.mockResolvedValueOnce(
[
'package:com.google.android.networkstack.tethering',
'package:com.android.cts.priv.ctsshim',
'package:com.google.android.youtube',
].join('\n')
);
expect(await isPackageInstalledAsync(device, 'com.google.android.youtube')).toBe(true);
expect(getServer().runAsync).toBeCalledWith([
'-s',
'123',
'shell',
'pm',
'list',
'packages',
'--user',
'0',
'com.google.android.youtube',
]);
});
it(`returns false when a package is not isntalled`, async () => {
jest.mocked(getServer().runAsync).mockResolvedValueOnce('');
expect(await isPackageInstalledAsync(device, 'com.google.android.youtube')).toBe(false);
});
});
describe(openAppIdAsync, () => {
it(`asserts that the app does not exist`, async () => {
jest
.mocked(getServer().runAsync)
.mockResolvedValueOnce('Error: Activity not started, unable to resolve Intent');
await expect(
openAppIdAsync(device, {
applicationId: 'dev.bacon.app',
})
).rejects.toThrow(CommandError);
});
});
describe(getAdbNameForDeviceIdAsync, () => {
it(`returns a device name`, async () => {
jest.mocked(getServer().runAsync).mockResolvedValueOnce(['Pixel_4_XL_API_30', 'OK'].join('\n'));
expect(await getAdbNameForDeviceIdAsync(asDevice({ pid: 'emulator-5554' }))).toBe(
'Pixel_4_XL_API_30'
);
});
it(`asserts when a device is not found`, async () => {
jest
.mocked(getServer().runAsync)
.mockResolvedValueOnce('error: could not connect to TCP port 55534: Connection refused');
await expect(
getAdbNameForDeviceIdAsync(asDevice({ pid: 'emulator-5554' }))
).rejects.toThrowError(CommandError);
});
});
describe(isDeviceBootedAsync, () => {
it(`returns a device when booted`, async () => {
jest
.mocked(getServer().runAsync)
.mockResolvedValueOnce(
[
'List of devices attached',
'emulator-5554 device product:sdk_gphone_x86_arm model:sdk_gphone_x86_arm device:generic_x86_arm transport_id:1',
'',
].join('\n')
)
.mockResolvedValueOnce(
// Return the emulator name
['Pixel_4_XL_API_30', 'OK'].join('\n')
);
expect(await isDeviceBootedAsync(asDevice({ name: 'Pixel_4_XL_API_30' }))).toStrictEqual({
isAuthorized: true,
isBooted: true,
name: 'Pixel_4_XL_API_30',
pid: 'emulator-5554',
type: 'emulator',
});
});
it(`returns null when the device is not booted`, async () => {
jest.mocked(getServer().runAsync).mockResolvedValueOnce('');
expect(await isDeviceBootedAsync(device)).toBe(null);
});
});
describe(getAttachedDevicesAsync, () => {
it(`gets devices`, async () => {
jest
.mocked(getServer().runAsync)
.mockResolvedValueOnce(
[
'List of devices attached',
// unauthorized
'FA8251A00719 unauthorized usb:338690048X transport_id:5',
// authorized
'FA8251A00720 device usb:338690048X product:walleye model:Pixel_2 device:walleye transport_id:4',
// Emulator
'emulator-5554 device product:sdk_gphone_x86_arm model:sdk_gphone_x86_arm device:generic_x86_arm transport_id:1',
'',
].join('\n')
)
.mockResolvedValueOnce(
// Return the emulator name
['Pixel_4_XL_API_30', 'OK'].join('\n')
);
const devices = await getAttachedDevicesAsync();
expect(devices).toEqual([
{
isAuthorized: false,
isBooted: true,
name: 'Device FA8251A00719',
pid: 'FA8251A00719',
type: 'device',
},
{
isAuthorized: true,
isBooted: true,
name: 'Pixel_2',
pid: 'FA8251A00720',
type: 'device',
},
{
isAuthorized: true,
isBooted: true,
name: 'Pixel_4_XL_API_30',
pid: 'emulator-5554',
type: 'emulator',
},
]);
});
it(`gets devices when ADB_TRACE is set`, async () => {
jest
.mocked(getServer().runAsync)
.mockResolvedValueOnce(
[
'List of devices attached',
'adb D 03-06 15:25:53 63677 4018815 adb_client.cpp:393] adb_query: host:devices-l',
'adb D 03-06 15:25:53 63677 4018815 adb_client.cpp:351] adb_connect: service: host:devices-l',
'adb D 03-06 15:25:53 63677 4018815 adb_client.cpp:160] _adb_connect: host:devices-l',
'adb D 03-06 15:25:53 63677 4018815 adb_client.cpp:194] _adb_connect: return fd 3',
'adb D 03-06 15:25:53 63677 4018815 adb_client.cpp:369] adb_connect: return fd 3',
// Emulator
'emulator-5554 offline transport_id:1',
'',
].join('\n')
)
.mockResolvedValueOnce(
// Return the emulator name
['Pixel_4_XL_API_30', 'OK'].join('\n')
);
const devices = await getAttachedDevicesAsync();
expect(devices).toEqual([
{
isAuthorized: true,
isBooted: true,
name: 'Pixel_4_XL_API_30',
pid: 'emulator-5554',
type: 'emulator',
},
]);
});
});
describe(isBootAnimationCompleteAsync, () => {
it(`returns true if the boot animation is complete for a device`, async () => {
jest
.mocked(getServer().getFileOutputAsync)
.mockResolvedValueOnce(['[init.svc.bootanim]: [stopped]'].join('\n'));
await expect(isBootAnimationCompleteAsync()).resolves.toBe(true);
});
it(`returns false if the boot animation is not complete`, async () => {
jest
.mocked(getServer().getFileOutputAsync)
.mockResolvedValueOnce(['[init.svc.bootanim]: [running]'].join('\n'));
await expect(isBootAnimationCompleteAsync()).resolves.toBe(false);
});
it(`returns false if the properties cannot be read`, async () => {
jest.mocked(getServer().getFileOutputAsync).mockImplementationOnce(() => {
throw new Error('File not found');
});
await expect(isBootAnimationCompleteAsync()).resolves.toBe(false);
});
});
describe(getPropertyDataForDeviceAsync, () => {
it(`returns parsed property data`, async () => {
jest.mocked(getServer().getFileOutputAsync).mockResolvedValueOnce(
[
'[wifi.direct.interface]: [p2p-dev-wlan0]',
'[init.svc.bootanim]: [stopped]',
'[wifi.interface]: [wlan0]',
// Should be stripped
'[invalid]: foobar',
].join('\n')
);
await expect(getPropertyDataForDeviceAsync(asDevice({ pid: '123' }))).resolves.toStrictEqual({
'init.svc.bootanim': 'stopped',
'wifi.direct.interface': 'p2p-dev-wlan0',
'wifi.interface': 'wlan0',
});
});
});
describe(getDeviceABIsAsync, () => {
it(`returns a list of device ABIs`, async () => {
jest
.mocked(getServer().getFileOutputAsync)
.mockResolvedValueOnce(['x86,armeabi-v7a,armeabi', ''].join('\n'));
await expect(isBootAnimationCompleteAsync()).resolves.toBe(false);
});
});
describe(sanitizeAdbDeviceName, () => {
it(`returns the avd device name from single line`, () => {
expect(sanitizeAdbDeviceName('Pixel_3_API_28')).toBe('Pixel_3_API_28');
});
it(`returns the avd device name from multi line with LF`, () => {
expect(sanitizeAdbDeviceName(`Pixel_4_API_29\nOK`)).toBe('Pixel_4_API_29');
});
it(`returns the avd device name from multi line with CR LF`, () => {
expect(sanitizeAdbDeviceName(`Pixel_5_API_30\r\nOK`)).toBe('Pixel_5_API_30');
});
it(`returns the avd device name from multi line with CR`, () => {
expect(sanitizeAdbDeviceName(`Pixel_6_API_31\rOK`)).toBe('Pixel_6_API_31');
});
});