-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathRuntimeDevice.js
440 lines (362 loc) · 11.9 KB
/
RuntimeDevice.js
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
const _ = require('lodash');
const DetoxRuntimeError = require('../../errors/DetoxRuntimeError');
const debug = require('../../utils/debug'); // debug utils, leave here even if unused
const { traceCall } = require('../../utils/trace');
const wrapWithStackTraceCutter = require('../../utils/wrapWithStackTraceCutter');
const LaunchArgsEditor = require('./utils/LaunchArgsEditor');
class RuntimeDevice {
constructor({
appsConfig,
behaviorConfig,
deviceConfig,
eventEmitter,
sessionConfig,
runtimeErrorComposer
}, deviceDriver) {
wrapWithStackTraceCutter(this, [
'captureViewHierarchy',
'clearKeychain',
'disableSynchronization',
'enableSynchronization',
'installApp',
'launchApp',
'matchFace',
'matchFinger',
'openURL',
'pressBack',
'relaunchApp',
'reloadReactNative',
'resetContentAndSettings',
'resetStatusBar',
'reverseTcpPort',
'selectApp',
'sendToHome',
'sendUserActivity',
'sendUserNotification',
'setBiometricEnrollment',
'setLocation',
'setOrientation',
'setStatusBar',
'setURLBlacklist',
'shake',
'takeScreenshot',
'terminateApp',
'uninstallApp',
'unmatchFace',
'unmatchFinger',
'unreverseTcpPort',
'resetAppState'
]);
this._appsConfig = appsConfig;
this._behaviorConfig = behaviorConfig;
this._deviceConfig = deviceConfig;
this._sessionConfig = sessionConfig;
this._emitter = eventEmitter;
this._errorComposer = runtimeErrorComposer;
/** @type {Detox.DetoxAppConfig | null} */
this._currentApp = null;
this._currentAppLaunchArgs = new LaunchArgsEditor();
this._processes = {};
this.deviceDriver = deviceDriver;
this.deviceDriver.validateDeviceConfig(deviceConfig);
this.debug = debug;
}
get id() {
return this.deviceDriver.getExternalId();
}
get name() {
return this.deviceDriver.getDeviceName();
}
get type() {
return this._deviceConfig.type;
}
get appLaunchArgs() {
return this._currentAppLaunchArgs;
}
async _prepare() {
const appAliases = Object.keys(this._appsConfig);
if (appAliases.length === 1) {
await this.selectApp(appAliases[0]);
}
}
async selectApp(name) {
if (name === undefined) {
throw this._errorComposer.cantSelectEmptyApp();
}
if (this._currentApp) {
await this.terminateApp();
}
if (name === null) { // Internal use to unselect the app
this._currentApp = null;
return;
}
const appConfig = this._appsConfig[name];
if (!appConfig) {
throw this._errorComposer.cantFindApp(name);
}
this._currentApp = appConfig;
this._currentAppLaunchArgs.reset();
this._currentAppLaunchArgs.modify(this._currentApp.launchArgs);
await this._inferBundleIdFromBinary();
}
async launchApp(params = {}, bundleId = this._bundleId) {
return traceCall('launchApp', () => this._doLaunchApp(params, bundleId));
}
/**
* @deprecated
*/
async relaunchApp(params = {}, bundleId) {
if (params.newInstance === undefined) {
params['newInstance'] = true;
}
await this.launchApp(params, bundleId);
}
async takeScreenshot(name) {
if (!name) {
throw new DetoxRuntimeError('Cannot take a screenshot with an empty name.');
}
return this.deviceDriver.takeScreenshot(name);
}
async captureViewHierarchy(name = 'capture') {
return this.deviceDriver.captureViewHierarchy(name);
}
async sendToHome() {
await this.deviceDriver.sendToHome();
await this.deviceDriver.waitForBackground();
}
async setBiometricEnrollment(toggle) {
const yesOrNo = toggle ? 'YES' : 'NO';
await this.deviceDriver.setBiometricEnrollment(yesOrNo);
}
async matchFace() {
await this.deviceDriver.matchFace();
await this.deviceDriver.waitForActive();
}
async unmatchFace() {
await this.deviceDriver.unmatchFace();
await this.deviceDriver.waitForActive();
}
async matchFinger() {
await this.deviceDriver.matchFinger();
await this.deviceDriver.waitForActive();
}
async unmatchFinger() {
await this.deviceDriver.unmatchFinger();
await this.deviceDriver.waitForActive();
}
async shake() {
await this.deviceDriver.shake();
}
async terminateApp(bundleId) {
const _bundleId = bundleId || this._bundleId;
await this.deviceDriver.terminate(_bundleId);
this._processes[_bundleId] = undefined;
}
async resetAppState() {
if (this._behaviorConfig.optimizeAppInstall) {
await this.deviceDriver.optimizedInstallApp(this._currentApp.bundleId, this._currentApp.binaryPath, this._currentApp.testBinaryPath);
} else {
await this.uninstallApp();
await this.installApp();
}
}
async installApp(binaryPath, testBinaryPath) {
await traceCall('appInstall', async () => {
/** @type {*} */
const currentApp = binaryPath ? { binaryPath, testBinaryPath } : this._getCurrentApp();
await this.deviceDriver.installApp(currentApp.binaryPath, currentApp.testBinaryPath);
if (!_.isEmpty(currentApp.permissions)) {
await this.deviceDriver.setPermissions(currentApp.bundleId, currentApp.permissions);
}
});
}
async uninstallApp(bundleId) {
const _bundleId = bundleId || this._bundleId;
await traceCall('appUninstall', () =>
this.deviceDriver.uninstallApp(_bundleId));
}
async installUtilBinaries() {
const paths = this._deviceConfig.utilBinaryPaths;
if (paths) {
await traceCall('installUtilBinaries', () =>
this.deviceDriver.installUtilBinaries(paths));
}
}
async reloadReactNative() {
await traceCall('reloadRN', () =>
this.deviceDriver.reloadReactNative());
}
async openURL(params) {
if (typeof params !== 'object' || !params.url) {
throw new DetoxRuntimeError(`openURL must be called with JSON params, and a value for 'url' key must be provided. example: await device.openURL({url: "url", sourceApp[optional]: "sourceAppBundleID"}`);
}
await this.deviceDriver.deliverPayload(params);
}
async setOrientation(orientation) {
await this.deviceDriver.setOrientation(orientation);
}
async setLocation(lat, lon) {
lat = String(lat);
lon = String(lon);
await this.deviceDriver.setLocation(lat, lon);
}
async reverseTcpPort(port) {
await this.deviceDriver.reverseTcpPort(port);
}
async unreverseTcpPort(port) {
await this.deviceDriver.unreverseTcpPort(port);
}
async clearKeychain() {
await this.deviceDriver.clearKeychain();
}
async sendUserActivity(params) {
await this._sendPayload('detoxUserActivityDataURL', params);
}
async sendUserNotification(params) {
await this._sendPayload('detoxUserNotificationDataURL', params);
}
async setURLBlacklist(urlList) {
await this.deviceDriver.setURLBlacklist(urlList);
}
async enableSynchronization() {
await this.deviceDriver.enableSynchronization();
}
async disableSynchronization() {
await this.deviceDriver.disableSynchronization();
}
async resetContentAndSettings() {
await this.deviceDriver.resetContentAndSettings();
}
getPlatform() {
return this.deviceDriver.getPlatform();
}
async _cleanup() {
const bundleId = this._currentApp && this._currentApp.bundleId;
await this.deviceDriver.cleanup(bundleId);
}
async pressBack() {
await this.deviceDriver.pressBack();
}
getUiDevice() {
return this.deviceDriver.getUiDevice();
}
async setStatusBar(params) {
await this.deviceDriver.setStatusBar(params);
}
async resetStatusBar() {
await this.deviceDriver.resetStatusBar();
}
/**
* @internal
*/
async _typeText(text) {
await this.deviceDriver.typeText(text);
}
get _bundleId() {
return this._getCurrentApp().bundleId;
}
_getCurrentApp() {
if (!this._currentApp) {
throw this._errorComposer.appNotSelected();
}
return this._currentApp;
}
async _doLaunchApp(params, bundleId) {
const payloadParams = ['url', 'userNotification', 'userActivity'];
const hasPayload = this._assertHasSingleParam(payloadParams, params);
const newInstance = params.newInstance !== undefined
? params.newInstance
: this._processes[bundleId] == null;
if (params.delete) {
await this.terminateApp(bundleId);
await this.resetAppState();
} else if (newInstance) {
await this.terminateApp(bundleId);
}
const baseLaunchArgs = {
...this._currentAppLaunchArgs.get(),
...params.launchArgs
};
if (params.url) {
baseLaunchArgs['detoxURLOverride'] = params.url;
if (params.sourceApp) {
baseLaunchArgs['detoxSourceAppOverride'] = params.sourceApp;
}
} else if (params.userNotification) {
this._createPayloadFileAndUpdatesParamsObject('userNotification', 'detoxUserNotificationDataURL', params, baseLaunchArgs);
} else if (params.userActivity) {
this._createPayloadFileAndUpdatesParamsObject('userActivity', 'detoxUserActivityDataURL', params, baseLaunchArgs);
}
if (params.permissions) {
await this.deviceDriver.setPermissions(bundleId, params.permissions);
}
if (params.disableTouchIndicators) {
baseLaunchArgs['detoxDisableTouchIndicators'] = true;
}
if (this._isAppRunning(bundleId) && hasPayload) {
await this.deviceDriver.deliverPayload({ ...params, delayPayload: true });
}
if (this._behaviorConfig.launchApp === 'manual') {
this._processes[bundleId] = await this.deviceDriver.waitForAppLaunch(bundleId, this._prepareLaunchArgs(baseLaunchArgs), params.languageAndLocale);
} else {
this._processes[bundleId] = await this.deviceDriver.launchApp(bundleId, this._prepareLaunchArgs(baseLaunchArgs), params.languageAndLocale);
await this.deviceDriver.waitUntilReady();
await this.deviceDriver.waitForActive();
}
await this._emitter.emit('appReady', {
deviceId: this.deviceDriver.getExternalId(),
bundleId,
pid: this._processes[bundleId]
});
if (params.detoxUserNotificationDataURL) {
await this.deviceDriver.cleanupRandomDirectory(params.detoxUserNotificationDataURL);
}
if (params.detoxUserActivityDataURL) {
await this.deviceDriver.cleanupRandomDirectory(params.detoxUserActivityDataURL);
}
}
async _sendPayload(key, params) {
const payloadFilePath = this.deviceDriver.createPayloadFile(params);
const payload = {
[key]: payloadFilePath
};
await this.deviceDriver.deliverPayload(payload);
this.deviceDriver.cleanupRandomDirectory(payloadFilePath);
}
_createPayloadFileAndUpdatesParamsObject(key, launchKey, params, baseLaunchArgs) {
const payloadFilePath = this.deviceDriver.createPayloadFile(params[key]);
baseLaunchArgs[launchKey] = payloadFilePath;
//`params` will be used later for `predeliverPayload`, so remove the actual notification and add the file URL
delete params[key];
params[launchKey] = payloadFilePath;
}
_isAppRunning(bundleId = this._bundleId) {
return this._processes[bundleId] != null;
}
_assertHasSingleParam(singleParams, params) {
let paramsCounter = 0;
singleParams.forEach((item) => {
if (params[item]) {
paramsCounter += 1;
}
});
if (paramsCounter > 1) {
throw new DetoxRuntimeError(`Call to 'launchApp(${JSON.stringify(params)})' must contain only one of ${JSON.stringify(singleParams)}.`);
}
return (paramsCounter === 1);
}
_prepareLaunchArgs(additionalLaunchArgs) {
return {
detoxServer: this._sessionConfig.server,
detoxSessionId: this._sessionConfig.sessionId,
...additionalLaunchArgs
};
}
async _inferBundleIdFromBinary() {
const { binaryPath, bundleId } = this._currentApp;
if (!bundleId) {
this._currentApp.bundleId = await this.deviceDriver.getBundleIdFromBinary(binaryPath);
}
}
}
module.exports = RuntimeDevice;