From aadaad9b0ac85ad9858424fc89ed44d62e30fcce Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Tue, 22 Dec 2020 17:14:55 +0100 Subject: [PATCH] Fixed use of FBApplicationLaunchMode for FBDevice Previously, the app would be restarted for FBApplicationLaunchModeForegroundIfRunning and not fail if already running for FBApplicationLaunchModeFailIfRunning. --- .../Commands/FBDeviceApplicationCommands.m | 22 +++++++++++++++++-- .../Management/FBInstrumentsClient.m | 3 ++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/FBDeviceControl/Commands/FBDeviceApplicationCommands.m b/FBDeviceControl/Commands/FBDeviceApplicationCommands.m index ddbf630a3..585b7c421 100644 --- a/FBDeviceControl/Commands/FBDeviceApplicationCommands.m +++ b/FBDeviceControl/Commands/FBDeviceApplicationCommands.m @@ -237,6 +237,26 @@ - (instancetype)initWithDevice:(FBDevice *)device } - (FBFuture> *)launchApplication:(FBApplicationLaunchConfiguration *)configuration +{ + if (configuration.launchMode == FBApplicationLaunchModeFailIfRunning) { + return [[self processIDWithBundleID:configuration.bundleID] onQueue:self.device.asyncQueue chain:^ (FBFuture* processIdQueryResult) { + if (processIdQueryResult.state == FBFutureStateDone) { + return [[FBDeviceControlError + describeFormat:@"Application %@ already running with pid %@", configuration.bundleID, processIdQueryResult.result] + failFuture]; + } else if (processIdQueryResult.state == FBFutureStateFailed) { + return (FBFuture*)[self launchApplicationIgnoreCurrentState:configuration]; + } else { + return (FBFuture*)processIdQueryResult; + } + }]; + } + return [self launchApplicationIgnoreCurrentState:configuration]; +} + +#pragma mark Private + +- (FBFuture> *)launchApplicationIgnoreCurrentState:(FBApplicationLaunchConfiguration *)configuration { return [[[self remoteInstrumentsClient] @@ -248,8 +268,6 @@ - (instancetype)initWithDevice:(FBDevice *)device }]; } -#pragma mark Private - - (FBFuture *)killApplicationWithProcessIdentifier:(pid_t)processIdentifier { return [[self diff --git a/FBDeviceControl/Management/FBInstrumentsClient.m b/FBDeviceControl/Management/FBInstrumentsClient.m index 142fef458..5fbcec0b8 100644 --- a/FBDeviceControl/Management/FBInstrumentsClient.m +++ b/FBDeviceControl/Management/FBInstrumentsClient.m @@ -140,7 +140,8 @@ - (instancetype)initWithConnection:(FBAMDServiceConnection *)connection channels onQueue:self.queue resolveValue:^ NSNumber * (NSError **error) { NSDictionary *options = @{ @"StartSuspendedKey": @(configuration.waitForDebugger), - @"KillExisting": @(configuration.launchMode != FBApplicationLaunchModeFailIfRunning), + // FBApplicationLaunchModeFailIfRunning needs to be taken care of prior to this call. + @"KillExisting": @(configuration.launchMode == FBApplicationLaunchModeRelaunchIfRunning), }; ResponsePayload response = [self onChannelIdentifier:ProcessControlChannel