This repository was archived by the owner on May 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathplatform.js
More file actions
413 lines (411 loc) · 17.5 KB
/
platform.js
File metadata and controls
413 lines (411 loc) · 17.5 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
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
"use strict";
var Q = require("q");
var testUtil_1 = require("./testUtil");
//////////////////////////////////////////////////////////////////////////////////////////
// PLATFORMS
/**
* Android implementations of IPlatform.
*/
var Android = (function () {
function Android(emulatorManager) {
this.emulatorManager = emulatorManager;
}
/**
* Gets the platform name. (e.g. "android" for the Android platform).
*/
Android.prototype.getName = function () {
return "android";
};
/**
* The command line flag used to determine whether or not this platform should run.
* Runs when the flag is present, doesn't run otherwise.
*/
Android.prototype.getCommandLineFlagName = function () {
return "--android";
};
/**
* Gets the server url used for testing.
*/
Android.prototype.getServerUrl = function () {
if (!this.serverUrl)
this.serverUrl = process.env.ANDROID_SERVER ? process.env.ANDROID_SERVER : Android.DEFAULT_ANDROID_SERVER_URL;
return this.serverUrl;
};
/**
* Gets an IEmulatorManager that is used to control the emulator during the tests.
*/
Android.prototype.getEmulatorManager = function () {
return this.emulatorManager;
};
/**
* Gets the default deployment key.
*/
Android.prototype.getDefaultDeploymentKey = function () {
return "mock-android-deployment-key";
};
Android.DEFAULT_ANDROID_SERVER_URL = "http://10.0.2.2:3001";
return Android;
}());
exports.Android = Android;
/**
* IOS implementation of IPlatform.
*/
var IOS = (function () {
function IOS(emulatorManager) {
this.emulatorManager = emulatorManager;
}
/**
* Gets the platform name. (e.g. "android" for the Android platform).
*/
IOS.prototype.getName = function () {
return "ios";
};
/**
* The command line flag used to determine whether or not this platform should run.
* Runs when the flag is present, doesn't run otherwise.
*/
IOS.prototype.getCommandLineFlagName = function () {
return "--ios";
};
/**
* Gets the server url used for testing.
*/
IOS.prototype.getServerUrl = function () {
if (!this.serverUrl)
this.serverUrl = process.env.IOS_SERVER ? process.env.IOS_SERVER : IOS.DEFAULT_IOS_SERVER_URL;
return this.serverUrl;
};
/**
* Gets an IEmulatorManager that is used to control the emulator during the tests.
*/
IOS.prototype.getEmulatorManager = function () {
return this.emulatorManager;
};
/**
* Gets the default deployment key.
*/
IOS.prototype.getDefaultDeploymentKey = function () {
return "mock-ios-deployment-key";
};
IOS.DEFAULT_IOS_SERVER_URL = "http://127.0.0.1:3000";
return IOS;
}());
exports.IOS = IOS;
//////////////////////////////////////////////////////////////////////////////////////////
// EMULATOR MANAGERS
// bootEmulatorInternal constants
var emulatorMaxReadyAttempts = 50;
var emulatorReadyCheckDelayMs = 5 * 1000;
/**
* Helper function for EmulatorManager implementations to use to boot an emulator with a given platformName and check, start, and kill methods.
*/
function bootEmulatorInternal(platformName, restartEmulators, targetEmulator, checkEmulator, startEmulator, killEmulator) {
var deferred = Q.defer();
console.log("Setting up " + platformName + " emulator.");
function onEmulatorReady() {
console.log(platformName + " emulator is ready!");
deferred.resolve(undefined);
return deferred.promise;
}
// Called to check if the emulator for the platform is initialized.
function checkEmulatorReady() {
var checkDeferred = Q.defer();
console.log("Checking if " + platformName + " emulator is ready yet...");
// Dummy command that succeeds if emulator is ready and fails otherwise.
checkEmulator(targetEmulator)
.then(function () {
checkDeferred.resolve(undefined);
}, function (error) {
console.info(error);
console.log(platformName + " emulator is not ready yet!");
checkDeferred.reject(error);
});
return checkDeferred.promise;
}
var emulatorReadyAttempts = 0;
// Loops checks to see if the emulator is ready and eventually fails after surpassing emulatorMaxReadyAttempts.
function checkEmulatorReadyLooper() {
var looperDeferred = Q.defer();
emulatorReadyAttempts++;
if (emulatorReadyAttempts > emulatorMaxReadyAttempts) {
console.log(platformName + " emulator is not ready after " + emulatorMaxReadyAttempts + " attempts, abort.");
deferred.reject(platformName + " emulator failed to boot.");
looperDeferred.resolve(undefined);
}
setTimeout(function () {
checkEmulatorReady()
.then(function () {
looperDeferred.resolve(undefined);
onEmulatorReady();
}, function () {
return checkEmulatorReadyLooper().then(function () { looperDeferred.resolve(undefined); }, function () { looperDeferred.reject(undefined); });
});
}, emulatorReadyCheckDelayMs);
return looperDeferred.promise;
}
// Starts and loops the emulator.
function startEmulatorAndLoop() {
console.log("Booting " + platformName + " emulator named " + targetEmulator + ".");
startEmulator(targetEmulator).catch(function (error) { console.log(error); deferred.reject(error); });
return checkEmulatorReadyLooper();
}
var promise;
if (restartEmulators) {
console.log("Killing " + platformName + " emulator.");
promise = killEmulator().catch(function () { return null; }).then(startEmulatorAndLoop);
}
else {
promise = checkEmulatorReady().then(onEmulatorReady, startEmulatorAndLoop);
}
return deferred.promise;
}
var AndroidEmulatorManager = (function () {
function AndroidEmulatorManager() {
}
/**
* Returns the target emulator, which is specified through the command line.
*/
AndroidEmulatorManager.prototype.getTargetEmulator = function () {
let _this = this;
if (this.targetEmulator)
return Q(this.targetEmulator);
else {
const deferred = Q.defer();
const targetAndroidEmulator = process.env.ANDROID_EMU;
if (!targetAndroidEmulator) {
// If no Android simulator is specified, get the most recent Android simulator to run tests on.
testUtil_1.TestUtil.getProcessOutput("emulator -list-avds", { noLogCommand: true, noLogStdOut: true, noLogStdErr: true })
.then((Devices) => {
const listOfDevices = Devices.trim().split("\n");
deferred.resolve(listOfDevices[listOfDevices.length - 1]);
}, (error) => {
deferred.reject(error);
});
}
else {
// Use the simulator specified on the command line.
deferred.resolve(targetAndroidEmulator);
}
return deferred.promise
.then((targetEmulator) => {
_this.targetEmulator = targetEmulator;
console.log("Using Android simulator named " + _this.targetEmulator);
return _this.targetEmulator;
});
}
};
/**
* Boots the target emulator.
*/
AndroidEmulatorManager.prototype.bootEmulator = function (restartEmulators) {
function checkAndroidEmulator(androidEmulatorName) {
// A command that does nothing but only succeeds if the emulator is running.
// List all of the packages on the device.
return testUtil_1.TestUtil.getProcessOutput("adb shell pm list packages", { noLogCommand: true, noLogStdOut: true, noLogStdErr: true }).then(function () { return null; });
}
function startAndroidEmulator(androidEmulatorName) {
const androidEmulatorCommand = `emulator @${androidEmulatorName}`;
let osSpecificCommand = "";
if (process.platform === "darwin") {
osSpecificCommand = `${androidEmulatorCommand} &`;
} else {
osSpecificCommand = `START /B ${androidEmulatorCommand}`;
}
return testUtil_1.TestUtil.getProcessOutput(osSpecificCommand, { noLogStdErr: true, timeout: 5000 });
}
function killAndroidEmulator() {
return testUtil_1.TestUtil.getProcessOutput("adb emu kill").then(function () { return null; });
}
return this.getTargetEmulator()
.then(function (targetEmulator) {
return bootEmulatorInternal("Android", restartEmulators, targetEmulator, checkAndroidEmulator, startAndroidEmulator, killAndroidEmulator);
});
};
/**
* Launches an already installed application by app id.
*/
AndroidEmulatorManager.prototype.launchInstalledApplication = function (appId) {
return testUtil_1.TestUtil.getProcessOutput("adb shell monkey -p " + appId + " -c android.intent.category.LAUNCHER 1").then(function () { return null; });
};
/**
* Ends a running application given its app id.
*/
AndroidEmulatorManager.prototype.endRunningApplication = function (appId) {
return testUtil_1.TestUtil.getProcessOutput("adb shell am force-stop " + appId).then(function () { return Q.delay(10000); });
};
/**
* Restarts an already installed application by app id.
*/
AndroidEmulatorManager.prototype.restartApplication = function (appId) {
var _this = this;
return this.endRunningApplication(appId)
.then(function () {
// Wait for a 1 second before restarting.
return Q.delay(1000);
})
.then(function () {
return _this.launchInstalledApplication(appId);
});
};
/**
* Navigates away from the current app, waits for a delay (defaults to 1 second), then navigates to the specified app.
*/
AndroidEmulatorManager.prototype.resumeApplication = function (appId, delayBeforeResumingMs) {
var _this = this;
if (delayBeforeResumingMs === void 0) { delayBeforeResumingMs = 1000; }
// Open a default Android app (for example, settings).
return this.launchInstalledApplication("com.android.settings")
.then(function () {
console.log("Waiting for " + delayBeforeResumingMs + "ms before resuming the test application.");
return Q.delay(delayBeforeResumingMs);
})
.then(function () {
// Reopen the app.
return _this.launchInstalledApplication(appId);
});
};
/**
* Prepares the emulator for a test.
*/
AndroidEmulatorManager.prototype.prepareEmulatorForTest = function (appId) {
return this.endRunningApplication(appId)
.then(function () {
return commandWithCheckAppExistence("adb shell pm clear", appId);
});
};
/**
* Uninstalls the app from the emulator.
*/
AndroidEmulatorManager.prototype.uninstallApplication = function (appId) {
return commandWithCheckAppExistence("adb uninstall", appId);
};
return AndroidEmulatorManager;
}());
exports.AndroidEmulatorManager = AndroidEmulatorManager;
var IOSEmulatorManager = (function () {
function IOSEmulatorManager() {
}
/**
* Returns the target emulator, which is specified through the command line.
*/
IOSEmulatorManager.prototype.getTargetEmulator = function () {
let _this = this;
if (this.targetEmulator)
return Q(this.targetEmulator);
else {
let deferred = Q.defer();
let targetIOSEmulator = process.env.IOS_EMU;
if (!targetIOSEmulator) {
// If no iOS simulator is specified, get the most recent iOS simulator to run tests on.
testUtil_1.TestUtil.getProcessOutput("xcrun simctl list", { noLogCommand: true, noLogStdOut: true, noLogStdErr: true })
.then((listOfDevicesWithDevicePairs) => {
let listOfDevices = listOfDevicesWithDevicePairs.slice(listOfDevicesWithDevicePairs.indexOf("-- iOS"), listOfDevicesWithDevicePairs.indexOf("-- tvOS"));
let phoneDevice = /iPhone\ \S*\ ?.*?\(([0-9A-Z-]*)\)/g;
let match = phoneDevice.exec(listOfDevices);
deferred.resolve(match[1]);
}, (error) => {
deferred.reject(error);
});
}
else {
// Use the simulator specified on the command line.
deferred.resolve(targetIOSEmulator);
}
return deferred.promise
.then((targetEmulator) => {
_this.targetEmulator = targetEmulator;
console.log("Using iOS simulator named " + _this.targetEmulator);
return _this.targetEmulator;
});
}
};
/**
* Boots the target emulator.
*/
IOSEmulatorManager.prototype.bootEmulator = function (restartEmulators) {
function checkIOSEmulator(iOSEmulatorId) {
// A command that does nothing but only succeeds if the emulator is running.
return testUtil_1.TestUtil.getProcessOutput("xcrun simctl getenv booted SIMULATOR_UDID", { noLogCommand: true, noLogStdOut: true, noLogStdErr: true }).then(function (simUdid) {
return simUdid.trim() == iOSEmulatorId.trim() ? true : Promise.reject(new Error('Waiting for device to boot'));
});
}
function startIOSEmulator(iOSEmulatorId) {
return testUtil_1.TestUtil.getProcessOutput("xcrun simctl boot " + iOSEmulatorId, { noLogStdErr: true })
.catch(function (error) { return undefined; /* Always fails because we do not specify a template, which is not necessary to just start the emulator */ }).then(function () { return null; });
}
function killIOSEmulator() {
return testUtil_1.TestUtil.getProcessOutput("xcrun simctl shutdown all").then(function () { return null; });
}
return this.getTargetEmulator()
.then(function (targetEmulator) {
return bootEmulatorInternal("iOS", restartEmulators, targetEmulator, checkIOSEmulator, startIOSEmulator, killIOSEmulator);
});
};
/**
* Launches an already installed application by app id.
*/
IOSEmulatorManager.prototype.launchInstalledApplication = function (appId) {
return testUtil_1.TestUtil.getProcessOutput("xcrun simctl launch booted " + appId, undefined).then(function () { return null; });
};
/**
* Ends a running application given its app id.
*/
IOSEmulatorManager.prototype.endRunningApplication = function (appId) {
return testUtil_1.TestUtil.getProcessOutput("xcrun simctl terminate booted " + appId, undefined).then(function () { return null; })
};
/**
* Restarts an already installed application by app id.
*/
IOSEmulatorManager.prototype.restartApplication = function (appId) {
var _this = this;
return this.endRunningApplication(appId)
.then(function () {
// Wait for a second before restarting.
return Q.delay(1000);
})
.then(function () { return _this.launchInstalledApplication(appId); });
};
/**
* Navigates away from the current app, waits for a delay (defaults to 1 second), then navigates to the specified app.
*/
IOSEmulatorManager.prototype.resumeApplication = function (appId, delayBeforeResumingMs) {
var _this = this;
if (delayBeforeResumingMs === void 0) { delayBeforeResumingMs = 1000; }
// Open a default iOS app (for example, settings).
return this.launchInstalledApplication("com.apple.Preferences")
.then(function () {
console.log("Waiting for " + delayBeforeResumingMs + "ms before resuming the test application.");
return Q.delay(delayBeforeResumingMs);
})
.then(function () {
// Reopen the app.
return _this.launchInstalledApplication(appId);
});
};
/**
* Prepares the emulator for a test.
*/
IOSEmulatorManager.prototype.prepareEmulatorForTest = function (appId) {
return this.endRunningApplication(appId);
};
/**
* Uninstalls the app from the emulator.
*/
IOSEmulatorManager.prototype.uninstallApplication = function (appId) {
return testUtil_1.TestUtil.getProcessOutput("xcrun simctl uninstall booted " + appId).then(function () { return null; });
};
return IOSEmulatorManager;
}());
exports.IOSEmulatorManager = IOSEmulatorManager;
function commandWithCheckAppExistence(command, appId) {
return testUtil_1.TestUtil.getProcessOutput("adb shell pm list packages", { noLogCommand: true, noLogStdOut: true, noLogStdErr: true })
.then((output) => {
return output.includes(appId);
}).then((isAppExist) => {
if (isAppExist) {
return testUtil_1.TestUtil.getProcessOutput(`${command} ${appId}`).then(function () { return null; });
}
console.log(`Command "${command}" is skipped because the application has not yet been installed`)
return null;
});
}