From 332ac93e8d6fbd6043213ac59850b4ae0b3b235d Mon Sep 17 00:00:00 2001 From: Maurice McCabe Date: Wed, 17 Jul 2019 09:15:45 -0700 Subject: [PATCH] Improved management of setting locale and starting/stopping simulator --- lib/src/run.dart | 92 +++++++++++++++++++++++--------------- test/screenshots_test.dart | 56 +++++++---------------- 2 files changed, 72 insertions(+), 76 deletions(-) diff --git a/lib/src/run.dart b/lib/src/run.dart index 517d0847..45252c94 100644 --- a/lib/src/run.dart +++ b/lib/src/run.dart @@ -49,9 +49,8 @@ Future run( final stagingDir = configInfo['staging']; await Directory(stagingDir + '/$kTestScreenshotsDir').create(recursive: true); await resources.unpackScripts(stagingDir); - final archiveDir = configInfo['archive']; - Archive archive = Archive(stagingDir, archiveDir); - if (runMode==RunMode.archive) { + Archive archive = Archive(configInfo['archive']); + if (runMode == RunMode.archive) { print('Archiving screenshots to ${archive.archiveDirPrefix}...'); } else { await fastlane.clearFastlaneDirs(configInfo, screens, runMode); @@ -69,7 +68,7 @@ Future run( print(' $recordingDir/ios/fastlane/screenshots'); print(' $recordingDir/android/fastlane/metadata/android'); } else { - if (runMode==RunMode.archive) { + if (runMode == RunMode.archive) { print(' ${archive.archiveDirPrefix}'); } else { print(' ios/fastlane/screenshots'); @@ -147,10 +146,11 @@ Future runTestsOnAll(DaemonClient daemonClient, List devices, List emulators, // check if current device is pending a locale change if (locales[0] == utils.getIosSimulatorLocale(deviceId)) { print('Starting $configDeviceName...'); - startSimulator(deviceId); + await startSimulator(daemonClient, deviceId); } else { pendingIosLocaleChangeAtStart = true; - print('Not starting $configDeviceName due to pending locale change'); +// print( +// 'Postponing \'$configDeviceName\' startup due to pending locale change'); } } } @@ -161,23 +161,35 @@ Future runTestsOnAll(DaemonClient daemonClient, List devices, List emulators, final deviceType = getDeviceType(configInfo, configDeviceName); // if device is real ios or android, cannot change locale if (device != null && !device['emulator']) { - final defaultLocale = 'en-US'; // todo: need actual local + final defaultLocale = 'en-US'; // todo: need actual locale print('Warning: the locale of a real device cannot be changed.'); await runProcessTests(config, screens, configDeviceName, defaultLocale, deviceType, testPaths, deviceId, imageProcessor, runMode, archive); } else { - // Check for a running android device or emulator + // Function to check for a running android device or emulator bool isRunningAndroidDeviceOrEmulator(Map device, Map emulator) { return (device != null && device['platform'] != 'ios') || (device == null && emulator != null); } - // save original locale for reverting later if necessary + // save original android locale for reverting later if necessary String origAndroidLocale; if (isRunningAndroidDeviceOrEmulator(device, emulator)) { origAndroidLocale = utils.getAndroidDeviceLocale(deviceId); } + // Function to check for a running ios device or simulator. + bool isRunningIosDeviceOrSimulator(Map device, Map emulator) { + return (device != null && device['platform'] == 'ios') || + (device == null && simulator != null); + } + + // save original ios locale for reverting later if necessary + String origIosLocale; + if (isRunningIosDeviceOrSimulator(device, emulator)) { + origIosLocale = utils.getIosSimulatorLocale(deviceId); + } + for (final locale in locales) { // set locale if android device or emulator if (isRunningAndroidDeviceOrEmulator(device, emulator)) { @@ -185,26 +197,29 @@ Future runTestsOnAll(DaemonClient daemonClient, List devices, List emulators, } // set locale if ios simulator if ((device != null && - device['platform'] == 'ios' && - device['emulator'])) { - // an already running simulator - await setSimulatorLocale( + device['platform'] == 'ios' && + device['emulator']) || + (device == null && + simulator != null && + !pendingIosLocaleChangeAtStart)) { + // an already running simulator or a started simulator + final localeChanged = await setSimulatorLocale( deviceId, configDeviceName, locale, stagingDir, daemonClient); - } else { - if (device == null && simulator != null) { - if (pendingIosLocaleChangeAtStart) { - // a non-running simulator - await setSimulatorLocale( - deviceId, configDeviceName, locale, stagingDir, daemonClient, - running: false); - pendingIosLocaleChangeAtStart = false; - } else { - // a running simulator - await setSimulatorLocale( - deviceId, configDeviceName, locale, stagingDir, daemonClient); - } + if (localeChanged) { + // restart simulator + print('Restarting \'$configDeviceName\' due to locale change...'); + shutdownSimulator(deviceId); + await startSimulator(daemonClient, deviceId); } } + if (pendingIosLocaleChangeAtStart) { + // a non-running simulator + await setSimulatorLocale( + deviceId, configDeviceName, locale, stagingDir, daemonClient); + print('Starting $configDeviceName...'); + await startSimulator(daemonClient, deviceId); + pendingIosLocaleChangeAtStart = false; + } // Change orientation if required final deviceOrientation = configInfo['devices'] @@ -250,7 +265,8 @@ Future runTestsOnAll(DaemonClient daemonClient, List devices, List emulators, await shutdownAndroidEmulator(daemonClient, deviceId); } if (simulator != null) { - // todo: revert locale + await setSimulatorLocale(deviceId, configDeviceName, origIosLocale, + stagingDir, daemonClient); shutdownSimulator(deviceId); } } @@ -285,8 +301,9 @@ void shutdownSimulator(String deviceId) { cmd('xcrun', ['simctl', 'shutdown', deviceId]); } -void startSimulator(String deviceId) { +Future startSimulator(DaemonClient daemonClient, String deviceId) async { cmd('xcrun', ['simctl', 'boot', deviceId]); + await waitForEmulatorToStart(daemonClient, deviceId); } /// Start android emulator and return device id. @@ -327,22 +344,23 @@ Map _findDevice(List devices, List emulators, String deviceName) { return device; } -/// Set the locale for a running simulator. -Future setSimulatorLocale(String deviceId, String deviceName, String testLocale, - String stagingDir, DaemonClient daemonClient, - {bool running = true}) async { +/// Set the simulator locale. +/// (Startup managed elsewhere) +/// Returns true of locale changed. +Future setSimulatorLocale(String deviceId, String deviceName, + String testLocale, String stagingDir, DaemonClient daemonClient) async { // a running simulator final deviceLocale = utils.getIosSimulatorLocale(deviceId); -// print('simulator locale=$deviceLocale'); + print('simulator locale=$deviceLocale'); + bool localeChanged = false; if (testLocale != deviceLocale) { - if (running) shutdownSimulator(deviceId); +// if (running) shutdownSimulator(deviceId); print( 'Changing locale from $deviceLocale to $testLocale on \'$deviceName\'...'); await _changeSimulatorLocale(stagingDir, deviceId, testLocale); - print('Starting $deviceName...'); - startSimulator(deviceId); - await waitForEmulatorToStart(daemonClient, deviceName); + localeChanged = true; } + return localeChanged; } /// Set the locale of a running emulator. diff --git a/test/screenshots_test.dart b/test/screenshots_test.dart index bc4e382a..ff0adaa9 100644 --- a/test/screenshots_test.dart +++ b/test/screenshots_test.dart @@ -291,8 +291,7 @@ void main() { final daemonClient = DaemonClient(); daemonClient.verbose = true; await daemonClient.start; - run.startSimulator(deviceId); - await waitForEmulatorToStart(daemonClient, simulatorName); + await run.startSimulator(daemonClient, deviceId); run.shutdownSimulator(deviceId); await daemonClient.stop; }); @@ -315,26 +314,12 @@ void main() { ProcessStartMode.detached); }); - // reproduce https://github.com/flutter/flutter/issues/27785 - // on android (hangs during test) - // tested on android emulator in default locale (en-US) and it worked - // tested on android emulator in automatically changed to locale fr-CA and it hangs - // tested on android emulator booted in locale fr-CA and it hangs -// [trace] FlutterDriver: Isolate found with number: 939713595 -// [trace] FlutterDriver: Isolate is paused at start. -// [trace] FlutterDriver: Attempting to resume isolate -// [trace] FlutterDriver: Waiting for service extension -// [info ] FlutterDriver: Connected to Flutter application. -// 00:04 +0: end-to-end test tap on the floating action button; verify counter -// [warning] FlutterDriver: waitFor message is taking a long time to complete... -// hangs test('change locale on android and test', () async { final emulatorId = 'Nexus_6P_API_28'; final deviceName = 'any device name'; final stagingDir = '/tmp/tmp'; final origLocale = 'en-US'; - final newLocale = 'en-US'; // succeeds -// final newLocale = 'fr-CA'; // fails + final newLocale = 'fr-CA'; final testAppDir = 'example'; final testAppSrcPath = 'test_driver/main.dart'; @@ -343,6 +328,7 @@ void main() { final daemonClient = DaemonClient(); await daemonClient.start; + // start emulator final deviceId = await daemonClient.launchEmulator(emulatorId); @@ -352,12 +338,12 @@ void main() { // run test await utils.streamCmd('flutter', ['drive', testAppSrcPath], testAppDir); - // stop emulator + // restore orig locale await run.setEmulatorLocale(deviceId, origLocale, deviceName); + + // stop emulator expect(await run.shutdownAndroidEmulator(daemonClient, deviceId), deviceId); - }, - timeout: - Timeout(Duration(seconds: 90))); // increase time to get stacktrace + }, timeout: Timeout(Duration(seconds: 90))); test('get android device locale', () async { final emulatorId = 'Nexus_6P_API_28'; @@ -374,17 +360,11 @@ void main() { expect(deviceLocale, locale); }); - // reproduce https://github.com/flutter/flutter/issues/27785 - // on ios - // tested on ios device in default locale (en-US) and it worked - // tested on ios device in manually changed to locale fr-CA and it hangs - // tested on ios simulator in default locale (en-US) and it worked - // tested on ios simulator in automatically changed to locale fr-CA and it hangs test('change locale on iOS and test', () async { final simulatorName = 'iPhone X'; final stagingDir = '/tmp/tmp'; - final locale = 'en-US'; // default locale (works) -// final locale = 'fr-CA'; // fails + final origLocale = 'en-US'; + final locale = 'fr-CA'; final testAppDir = 'example'; final testAppSrcPath = 'test_driver/main.dart'; @@ -399,13 +379,10 @@ void main() { utils.getHighestIosSimulator(utils.getIosSimulators(), simulatorName); final deviceId = simulatorInfo['udid']; await run.setSimulatorLocale( - deviceId, simulatorName, locale, stagingDir, daemonClient, - running: false); + deviceId, simulatorName, locale, stagingDir, daemonClient); // start simulator -// final daemonClient = DaemonClient(); -// await daemonClient.start; - run.startSimulator(deviceId); + await run.startSimulator(daemonClient, deviceId); // run test await utils.streamCmd( @@ -413,9 +390,11 @@ void main() { // stop simulator run.shutdownSimulator(deviceId); - }, - // increase time to get stacktrace - timeout: Timeout(Duration(minutes: 2))); + + // restore orig locale + await run.setSimulatorLocale( + deviceId, simulatorName, origLocale, stagingDir, daemonClient); + }, timeout: Timeout(Duration(seconds: 90))); test('get ios simulator locale', () async { final udId = '03D4FC12-3927-4C8B-A226-17DE34AE9C18'; @@ -702,11 +681,10 @@ devices: final simulatorInfo = utils.getHighestIosSimulator(utils.getIosSimulators(), simulatorName); final deviceId = simulatorInfo['udid']; - run.startSimulator(deviceId); final daemonClient = DaemonClient(); daemonClient.verbose = true; await daemonClient.start; - await waitForEmulatorToStart(daemonClient, simulatorName); + await run.startSimulator(daemonClient, deviceId); await Future.delayed(Duration(milliseconds: 5000)); // finish booting orient.changeDeviceOrientation( DeviceType.ios, orient.Orientation.LandscapeRight,