From 506ceebdd613e5f0d6d0bb942871f8e4f039aa86 Mon Sep 17 00:00:00 2001 From: "lliabraa@chromium.org" Date: Wed, 9 Jul 2014 22:12:14 +0000 Subject: [PATCH] In iossim, use default home directory if no -u override is given. With Xcode 6, the home directory of the iOS Simulator and iossim processes must match so that the iOS Simulator can find the SimDevice object created by iossim. Since the existing behavior of iossim is to create a new, random, home directory on each run (if no -u value is specified on the command line), the home directories will be mismatched if iossim is launched multiple times without killing the iOS Simulator in between runs. The fix here is to only override the home directory in iossim if -u is specified. Since this will often reuse the default SimDeviceSet (at ~/Library/Developer/CoreSimulator/Devices), functionality is also changed to reuse an existing SimDevice if possible (though iossim will create a SimDevice if necessary). BUG=None TBR=rohitrao Review URL: https://codereview.chromium.org/382563002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282139 0039d316-1c4b-4281-b951-d872f2087c98 --- testing/iossim/iossim.mm | 69 ++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/testing/iossim/iossim.mm b/testing/iossim/iossim.mm index 1593a5a15447..b5cbd51f5ff0 100644 --- a/testing/iossim/iossim.mm +++ b/testing/iossim/iossim.mm @@ -609,16 +609,34 @@ DTiPhoneSimulatorSessionConfig* BuildSessionConfig( NSString* identifier = systemRoot.runtime.identifier; id simRuntime = [simRuntimeClass supportedRuntimesByIdentifier][identifier]; + // Attempt to use an existing device, but create one if a suitable match can't + // be found. For example, if the simulator is running with a non-default home + // directory (e.g. via iossim's -u command line arg) then there won't be any + // devices so one will have to be created. Class simDeviceSetClass = FindClassByName(@"SimDeviceSet"); - NSError* error = nil; - id simDevice = - [[simDeviceSetClass defaultSet] createDeviceWithType:simDeviceType - runtime:simRuntime - name:@"iossim" - error:&error]; - if (error) { - LogError(@"Failed to create device: %@", error); - exit(kExitInitializationFailure); + id deviceSet = + [simDeviceSetClass setForSetPath:[simDeviceSetClass defaultSetPath]]; + id simDevice = nil; + for (id device in [deviceSet availableDevices]) { + if ([device runtime] == simRuntime && + [device deviceType] == simDeviceType) { + simDevice = device; + break; + } + } + if (!simDevice) { + NSError* error = nil; + // n.b. only the device name is necessary because the iOS Simulator menu + // already splits devices by runtime version. + NSString* name = [NSString stringWithFormat:@"iossim - %@ ", deviceName]; + simDevice = [deviceSet createDeviceWithType:simDeviceType + runtime:simRuntime + name:name + error:&error]; + if (error) { + LogError(@"Failed to create device: %@", error); + exit(kExitInitializationFailure); + } } sessionConfig.device = simDevice; #endif @@ -866,14 +884,21 @@ int main(int argc, char* const argv[]) { // Determine the deviceFamily based on the deviceName NSNumber* deviceFamily = nil; +// TODO(lliabraa): Once all builders are on Xcode 6 this ifdef can be removed +// (crbug.com/385030). +#if defined(IOSSIM_USE_XCODE_6) + Class simDeviceTypeClass = FindClassByName(@"SimDeviceType"); + if ([simDeviceTypeClass supportedDeviceTypesByName][deviceName] == nil) { + LogError(@"Invalid device name: %@.", deviceName); + PrintSupportedDevices(); + exit(kExitInvalidArguments); + } +#else if (!deviceName || CaseInsensitivePrefixSearch(deviceName, @"iPhone")) { deviceFamily = [NSNumber numberWithInt:kIPhoneFamily]; } else if (CaseInsensitivePrefixSearch(deviceName, @"iPad")) { deviceFamily = [NSNumber numberWithInt:kIPadFamily]; } -// TODO(lliabraa): Once all builders are on Xcode 6 this ifdef can be removed -// (crbug.com/385030). -#if !defined(IOSSIM_USE_XCODE_6) else { LogError(@"Invalid device name: %@. Must begin with 'iPhone' or 'iPad'", deviceName); @@ -881,24 +906,16 @@ int main(int argc, char* const argv[]) { } #endif // !defined(IOSSIM_USE_XCODE_6) - // Set up the user home directory for the simulator - if (!simHomePath) { - NSString* dirNameTemplate = - [NSString stringWithFormat:@"iossim-%@-%@-XXXXXX", appName, deviceName]; - simHomePath = CreateTempDirectory(dirNameTemplate); - if (!simHomePath) { - LogError(@"Unable to create unique directory for template %@", - dirNameTemplate); + // Set up the user home directory for the simulator only if a non-default + // value was specified. + if (simHomePath) { + if (!InitializeSimulatorUserHome(simHomePath)) { + LogError(@"Unable to initialize home directory for simulator: %@", + simHomePath); exit(kExitInitializationFailure); } } - if (!InitializeSimulatorUserHome(simHomePath)) { - LogError(@"Unable to initialize home directory for simulator: %@", - simHomePath); - exit(kExitInitializationFailure); - } - // Create the config and simulator session. DTiPhoneSimulatorSessionConfig* config = BuildSessionConfig(appSpec, systemRoot, -- 2.11.4.GIT