Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 065512b

Browse files
Add isAppInstalledOnDevices public method
The method return array of boolean Promises. All of them will indicate if app is installed on the specified device identifier. Add unit tests for devicesService and fix some issues.
1 parent 5f5efc2 commit 065512b

File tree

4 files changed

+1009
-25
lines changed

4 files changed

+1009
-25
lines changed

definitions/mobile.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ declare module Mobile {
233233
hasDevices: boolean;
234234
deviceCount: number;
235235
execute(action: (device: Mobile.IDevice) => IFuture<void>, canExecute?: (dev: Mobile.IDevice) => boolean, options?: {allowNoDevices?: boolean}): IFuture<void>;
236-
initialize(data: IDevicesServicesInitializationOptions): IFuture<void>;
236+
initialize(data?: IDevicesServicesInitializationOptions): IFuture<void>;
237237
platform: string;
238238
getDevices(): Mobile.IDeviceInfo[];
239239
getDevicesForPlatform(platform: string): Mobile.IDevice[];
@@ -243,6 +243,9 @@ declare module Mobile {
243243
isiOSDevice(device: Mobile.IDevice): boolean;
244244
isiOSSimulator(device: Mobile.IDevice): boolean;
245245
isOnlyiOSSimultorRunning(): boolean;
246+
isAppInstalledOnDevices(deviceIdentifiers: string[], appIdentifier: string): IFuture<boolean>[];
247+
setLogLevel(logLevel: string, deviceIdentifier?: string): void;
248+
deployOnDevices(deviceIdentifiers: string[], packageFile: string, packageName: string): IFuture<void>[];
246249
}
247250

248251
interface IiTunesValidator {

mobile/mobile-core/devices-service.ts

+47-21
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,22 @@ export class DevicesService implements Mobile.IDevicesService {
6060
}
6161

6262
public isiOSSimulator(device: Mobile.IDevice): boolean {
63-
return this.$mobileHelper.isiOSPlatform(device.deviceInfo.platform) && device.isEmulator;
63+
return !!(this.$mobileHelper.isiOSPlatform(device.deviceInfo.platform) && device.isEmulator);
6464
}
6565

6666
/* tslint:disable:no-unused-variable */
6767
@exported("devicesService")
68-
private setLogLevel(logLevel: string, deviceIdentifier?: string): void {
68+
public setLogLevel(logLevel: string, deviceIdentifier?: string): void {
6969
this.$deviceLogProvider.setLogLevel(logLevel, deviceIdentifier);
7070
}
7171
/* tslint:enable:no-unused-variable */
7272

73+
@exportedPromise("devicesService")
74+
public isAppInstalledOnDevices(deviceIdentifiers: string[], appIdentifier: string): IFuture<boolean>[] {
75+
this.$logger.trace(`Called isInstalledOnDevices for identifiers ${deviceIdentifiers}. AppIdentifier is ${appIdentifier}.`);
76+
return _.map(deviceIdentifiers, deviceIdentifier => this.isApplicationInstalledOnDevice(deviceIdentifier, appIdentifier));
77+
}
78+
7379
public getDeviceInstances(): Mobile.IDevice[] {
7480
return _.values(this._devices);
7581
}
@@ -210,9 +216,7 @@ export class DevicesService implements Mobile.IDevicesService {
210216

211217
let futures = _.map(sortedDevices, (device: Mobile.IDevice) => {
212218
if (!canExecute || canExecute(device)) {
213-
let future = action(device);
214-
Future.settle(future);
215-
return future;
219+
return action(device);
216220
} else {
217221
return Future.fromResult();
218222
}
@@ -235,7 +239,9 @@ export class DevicesService implements Mobile.IDevicesService {
235239
if (this.$hostInfo.isDarwin && this._platform && this.$mobileHelper.isiOSPlatform(this._platform) &&
236240
this.$options.emulator && !this.isOnlyiOSSimultorRunning()) {
237241
this.startEmulator().wait();
238-
canExecute = (dev: Mobile.IDevice): boolean => this.isiOSSimulator(dev); // Executes the command only on iOS simulator
242+
// Executes the command only on iOS simulator
243+
let originalCanExecute = canExecute;
244+
canExecute = (dev: Mobile.IDevice): boolean => this.isiOSSimulator(dev) && (!originalCanExecute || !!(originalCanExecute(dev)));
239245
}
240246
this.executeCore(action, canExecute).wait();
241247
} else {
@@ -278,11 +284,23 @@ export class DevicesService implements Mobile.IDevicesService {
278284
} else if(platform && !deviceOption) {
279285
this._platform = this.getPlatform(platform);
280286
this.startLookingForDevices().wait();
281-
} else if(!platform && !deviceOption) {
287+
} else {
288+
// platform and deviceId are not specified
282289
this.startLookingForDevices().wait();
283290
if (!data.skipInferPlatform) {
284291
let devices = this.getDeviceInstances();
285-
let platforms = _.uniq(_.map(devices, (device) => device.deviceInfo.platform));
292+
let platforms = _(devices)
293+
.map(device => device.deviceInfo.platform)
294+
.filter(pl => {
295+
try {
296+
return this.getPlatform(pl);
297+
} catch(err) {
298+
this.$logger.warn(err.message);
299+
return null;
300+
}
301+
})
302+
.uniq()
303+
.value();
286304

287305
if (platforms.length === 1) {
288306
this._platform = platforms[0];
@@ -296,7 +314,7 @@ export class DevicesService implements Mobile.IDevicesService {
296314
}
297315

298316
if (!this.$hostInfo.isDarwin && this._platform && this.$mobileHelper.isiOSPlatform(this._platform) && this.$options.emulator) {
299-
this.$errors.failWithoutHelp("You are not allowed to use iOS simulator on Windows.");
317+
this.$errors.failWithoutHelp("You can use iOS simulator only on OS X.");
300318
}
301319
this._isInitialized = true;
302320
}).future<void>()();
@@ -321,19 +339,15 @@ export class DevicesService implements Mobile.IDevicesService {
321339

322340
private deployOnDevice(deviceIdentifier: string, packageFile: string, packageName: string): IFuture<void> {
323341
return (() => {
324-
if(_(this._devices).keys().find(d => d === deviceIdentifier)) {
325-
let device = this._devices[deviceIdentifier];
326-
device.applicationManager.reinstallApplication(packageName, packageFile).wait();
327-
this.$logger.info(`Successfully deployed on device with identifier '${device.deviceInfo.identifier}'.`);
328-
if (device.applicationManager.canStartApplication()) {
329-
try {
330-
device.applicationManager.startApplication(packageName).wait();
331-
} catch(err) {
332-
this.$logger.trace("Unable to start application on device. Error is: ", err);
333-
}
342+
let device = this.getDeviceByIdentifier(deviceIdentifier);
343+
device.applicationManager.reinstallApplication(packageName, packageFile).wait();
344+
this.$logger.info(`Successfully deployed on device with identifier '${device.deviceInfo.identifier}'.`);
345+
if (device.applicationManager.canStartApplication()) {
346+
try {
347+
device.applicationManager.startApplication(packageName).wait();
348+
} catch(err) {
349+
this.$logger.trace("Unable to start application on device. Error is: ", err);
334350
}
335-
} else {
336-
throw new Error(`Cannot find device with identifier ${deviceIdentifier}.`);
337351
}
338352
}).future<void>()();
339353
}
@@ -358,11 +372,16 @@ export class DevicesService implements Mobile.IDevicesService {
358372
} else if (this.$mobileHelper.isAndroidPlatform(this._platform)) {
359373
return this.$injector.resolve("androidEmulatorServices");
360374
}
375+
376+
return null;
361377
}
362378

363379
private startEmulator(): IFuture<void> {
364380
return (() => {
365381
let emulatorServices = this.resolveEmulatorServices();
382+
if(!emulatorServices) {
383+
this.$errors.failWithoutHelp("Unable to detect platform for which to start emulator.");
384+
}
366385
emulatorServices.startEmulator().wait();
367386
if (this.$mobileHelper.isAndroidPlatform(this._platform)) {
368387
this.$androidDeviceDiscovery.checkForDevices().wait();
@@ -378,6 +397,13 @@ export class DevicesService implements Mobile.IDevicesService {
378397
}
379398

380399
return this.executeOnAllConnectedDevices(action, canExecute);
400+
}
401+
402+
private isApplicationInstalledOnDevice(deviceIdentifier: string, appIdentifier: string): IFuture<boolean> {
403+
return ((): boolean => {
404+
let device = this.getDeviceByIdentifier(deviceIdentifier);
405+
return device.applicationManager.isApplicationInstalled(appIdentifier).wait();
406+
}).future<boolean>()();
381407
}
382408
}
383409

0 commit comments

Comments
 (0)