-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathmultitouch-helper.test.ts
More file actions
257 lines (240 loc) · 7.68 KB
/
multitouch-helper.test.ts
File metadata and controls
257 lines (240 loc) · 7.68 KB
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
import assert from 'node:assert/strict';
import crypto from 'node:crypto';
import { promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { beforeEach, test } from 'vitest';
import { ANDROID_EMULATOR } from '../../../__tests__/test-utils/index.ts';
import {
ensureAndroidMultiTouchHelper,
parseAndroidMultiTouchHelperOutput,
pinchAndroid,
resetAndroidMultiTouchHelperInstallCache,
rotateGestureAndroid,
runAndroidMultiTouchHelperGesture,
transformGestureAndroid,
} from '../multitouch-helper.ts';
import {
withAndroidAdbProvider,
type AndroidAdbExecutor,
type AndroidAdbProvider,
} from '../adb-executor.ts';
const manifest = {
name: 'android-multitouch-helper' as const,
version: '0.15.0',
assetName: 'helper.apk',
sha256: 'a'.repeat(64),
packageName: 'com.callstack.agentdevice.multitouchhelper',
versionCode: 15000,
instrumentationRunner: 'com.callstack.agentdevice.multitouchhelper/.MultiTouchInstrumentation',
statusProtocol: 'android-multitouch-helper-v1' as const,
};
beforeEach(() => {
resetAndroidMultiTouchHelperInstallCache();
});
test('parseAndroidMultiTouchHelperOutput returns final instrumentation gesture metadata', () => {
const parsed = parseAndroidMultiTouchHelperOutput(
[
resultRecord({
ok: 'true',
kind: 'pinch',
helperApiVersion: '1',
injectedEvents: '24',
elapsedMs: '315',
}),
'INSTRUMENTATION_CODE: 0',
].join('\n'),
);
assert.deepEqual(parsed, {
kind: 'pinch',
helperApiVersion: '1',
injectedEvents: 24,
elapsedMs: 315,
});
});
test('runAndroidMultiTouchHelperGesture encodes protocol payload for instrumentation', async () => {
let capturedArgs: string[] | undefined;
let capturedOptions: Parameters<AndroidAdbExecutor>[1];
const result = await runAndroidMultiTouchHelperGesture({
adb: async (args, options) => {
capturedArgs = args;
capturedOptions = options;
return {
exitCode: 0,
stdout: [resultRecord({ ok: 'true', kind: 'rotate' }), 'INSTRUMENTATION_CODE: 0'].join(
'\n',
),
stderr: '',
};
},
request: { kind: 'rotate', x: 100, y: 200, degrees: 145, radius: 120, durationMs: 250 },
packageName: manifest.packageName,
instrumentationRunner: manifest.instrumentationRunner,
});
assert.equal(result.kind, 'rotate');
assert.ok(capturedArgs);
assert.deepEqual(capturedArgs.slice(0, 7), [
'shell',
'am',
'instrument',
'-w',
'-e',
'payloadBase64',
capturedArgs[6],
]);
assert.deepEqual(JSON.parse(Buffer.from(capturedArgs[6]!, 'base64').toString('utf8')), {
protocol: 'android-multitouch-helper-v1',
kind: 'rotate',
x: 100,
y: 200,
degrees: 145,
radius: 120,
durationMs: 250,
});
assert.equal(capturedArgs.at(-1), manifest.instrumentationRunner);
assert.equal(capturedOptions?.timeoutMs, 45_000);
});
test('parseAndroidMultiTouchHelperOutput distinguishes missing final results', () => {
assert.throws(() => parseAndroidMultiTouchHelperOutput('INSTRUMENTATION_CODE: 0'), {
code: 'ANDROID_MULTITOUCH_HELPER_NO_FINAL_RESULT',
message: 'Android multi-touch helper did not return a final result',
});
});
test('runAndroidMultiTouchHelperGesture preserves helper failure messages', async () => {
await assert.rejects(
() =>
runAndroidMultiTouchHelperGesture({
adb: async () => ({
exitCode: 1,
stdout: [
resultRecord({
ok: 'false',
errorType: 'java.lang.IllegalStateException',
message: 'injectInputEvent returned false',
}),
'INSTRUMENTATION_CODE: 1',
].join('\n'),
stderr: '',
}),
request: { kind: 'pinch', x: 100, y: 200, scale: 1.5, radius: 120, durationMs: 250 },
packageName: manifest.packageName,
instrumentationRunner: manifest.instrumentationRunner,
}),
{
code: 'COMMAND_FAILED',
message: 'injectInputEvent returned false',
},
);
});
test('pinchAndroid, rotateGestureAndroid, and transformGestureAndroid prefer provider-native touch injection', async () => {
const calls: unknown[] = [];
await withAndroidAdbProvider(
{
exec: async () => {
throw new Error('adb should not run when native touch is available');
},
touch: async (request) => {
calls.push(request);
return { backendDetail: 'native' };
},
},
{ serial: ANDROID_EMULATOR.id },
async () => {
const pinch = await pinchAndroid(ANDROID_EMULATOR, { scale: 2, x: 100, y: 200 });
const rotate = await rotateGestureAndroid(ANDROID_EMULATOR, {
degrees: -215,
x: 100,
y: 200,
});
const transform = await transformGestureAndroid(ANDROID_EMULATOR, {
x: 100,
y: 200,
dx: 30,
dy: -20,
scale: 1.5,
degrees: 35,
});
assert.equal(pinch.backend, 'provider-native-touch');
assert.equal(rotate.backend, 'provider-native-touch');
assert.equal(transform.backend, 'provider-native-touch');
},
);
assert.deepEqual(calls, [
{ kind: 'pinch', x: 100, y: 200, scale: 2, durationMs: undefined },
{ kind: 'rotate', x: 100, y: 200, degrees: -215, durationMs: undefined },
{
kind: 'transform',
x: 100,
y: 200,
dx: 30,
dy: -20,
scale: 1.5,
degrees: 35,
durationMs: undefined,
},
]);
});
test('rotateGestureAndroid rejects zero velocity before provider dispatch', async () => {
await withAndroidAdbProvider(
{
exec: async () => {
throw new Error('adb should not run for invalid input');
},
touch: async () => {
throw new Error('native touch should not run for invalid input');
},
},
{ serial: ANDROID_EMULATOR.id },
async () => {
await assert.rejects(
() => rotateGestureAndroid(ANDROID_EMULATOR, { degrees: 90, velocity: 0 }),
{ code: 'INVALID_ARGS' },
);
},
);
});
test('ensureAndroidMultiTouchHelper installs with semantic provider install options', async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'multitouch-helper-install-'));
const apkPath = path.join(tmpDir, 'helper.apk');
await fs.writeFile(apkPath, 'helper-apk');
const installCalls: Array<{
apkPath: string;
replace?: boolean;
allowTestPackages?: boolean;
}> = [];
const adb: AndroidAdbExecutor = async (args) => {
if (args.includes('--show-versioncode')) {
return { exitCode: 1, stdout: '', stderr: 'not found' };
}
throw new Error(`unexpected adb call: ${args.join(' ')}`);
};
const adbProvider: AndroidAdbProvider = {
exec: adb,
install: async (path, options) => {
installCalls.push({
apkPath: path,
replace: options?.replace,
allowTestPackages: options?.allowTestPackages,
});
return { exitCode: 0, stdout: '', stderr: '' };
},
};
const result = await ensureAndroidMultiTouchHelper({
adb,
adbProvider,
artifact: { apkPath, manifest: { ...manifest, sha256: sha256Text('helper-apk') } },
deviceKey: 'android:emulator-5554',
});
assert.equal(result.installed, true);
assert.equal(result.reason, 'missing');
assert.deepEqual(installCalls, [{ apkPath, replace: true, allowTestPackages: true }]);
});
function resultRecord(values: Record<string, string>): string {
return [
'INSTRUMENTATION_RESULT: agentDeviceProtocol=android-multitouch-helper-v1',
...Object.entries(values).map(([key, value]) => `INSTRUMENTATION_RESULT: ${key}=${value}`),
].join('\n');
}
function sha256Text(text: string): string {
return crypto.createHash('sha256').update(text).digest('hex');
}