Skip to content

Commit

Permalink
Improved management of setting locale and starting/stopping simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcc007 committed Jul 17, 2019
1 parent 8f73cc7 commit 332ac93
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 76 deletions.
92 changes: 55 additions & 37 deletions lib/src/run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ Future<void> 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);
Expand All @@ -69,7 +68,7 @@ Future<void> 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');
Expand Down Expand Up @@ -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');
}
}
}
Expand All @@ -161,50 +161,65 @@ 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)) {
await setEmulatorLocale(deviceId, locale, configDeviceName);
}
// 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']
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -285,8 +301,9 @@ void shutdownSimulator(String deviceId) {
cmd('xcrun', ['simctl', 'shutdown', deviceId]);
}

void startSimulator(String deviceId) {
Future<void> startSimulator(DaemonClient daemonClient, String deviceId) async {
cmd('xcrun', ['simctl', 'boot', deviceId]);
await waitForEmulatorToStart(daemonClient, deviceId);
}

/// Start android emulator and return device id.
Expand Down Expand Up @@ -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<bool> 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.
Expand Down
56 changes: 17 additions & 39 deletions test/screenshots_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand All @@ -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';

Expand All @@ -343,6 +328,7 @@ void main() {

final daemonClient = DaemonClient();
await daemonClient.start;

// start emulator
final deviceId = await daemonClient.launchEmulator(emulatorId);

Expand All @@ -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';
Expand All @@ -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';

Expand All @@ -399,23 +379,22 @@ 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(
'flutter', ['-d', deviceId, 'drive', testAppSrcPath], testAppDir);

// 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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 332ac93

Please sign in to comment.