Skip to content

Commit

Permalink
Add cold boot option to emulator launch command (flutter#82647)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamdor committed May 19, 2021
1 parent 647712e commit 2d28350
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ Hidenori Matsubayashi <Hidenori.Matsubayashi@sony.com>
Perqin Xie <perqinxie@gmail.com>
Seongyun Kim <helloworld@cau.ac.kr>
Ludwik Trammer <ludwik@gmail.com>
Marian Triebe <m.triebe@live.de>
13 changes: 9 additions & 4 deletions packages/flutter_tools/lib/src/android/android_emulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,15 @@ class AndroidEmulator extends Emulator {
String _prop(String name) => _properties != null ? _properties[name] : null;

@override
Future<void> launch({@visibleForTesting Duration startupDuration}) async {
final Process process = await _processUtils.start(
<String>[_androidSdk.emulatorPath, '-avd', id],
);
Future<void> launch({@visibleForTesting Duration startupDuration, bool coldBoot = false}) async {
final List<String> command = <String>[
_androidSdk.emulatorPath,
'-avd',
id,
if (coldBoot)
'-no-snapshot-load'
];
final Process process = await _processUtils.start(command);

// Record output from the emulator process.
final List<String> stdoutList = <String>[];
Expand Down
10 changes: 7 additions & 3 deletions packages/flutter_tools/lib/src/commands/emulators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class EmulatorsCommand extends FlutterCommand {
EmulatorsCommand() {
argParser.addOption('launch',
help: 'The full or partial ID of the emulator to launch.');
argParser.addFlag('cold',
help: 'Used with the "--launch" flag to cold boot the emulator instance (Android only).',
negatable: false);
argParser.addFlag('create',
help: 'Creates a new Android emulator based on a Pixel device.',
negatable: false);
Expand Down Expand Up @@ -43,7 +46,8 @@ class EmulatorsCommand extends FlutterCommand {
}

if (argResults.wasParsed('launch')) {
await _launchEmulator(stringArg('launch'));
final bool coldBoot = argResults.wasParsed('cold');
await _launchEmulator(stringArg('launch'), coldBoot: coldBoot);
} else if (argResults.wasParsed('create')) {
await _createEmulator(name: stringArg('name'));
} else {
Expand All @@ -57,7 +61,7 @@ class EmulatorsCommand extends FlutterCommand {
return FlutterCommandResult.success();
}

Future<void> _launchEmulator(String id) async {
Future<void> _launchEmulator(String id, {bool coldBoot}) async {
final List<Emulator> emulators =
await emulatorManager.getEmulatorsMatching(id);

Expand All @@ -69,7 +73,7 @@ class EmulatorsCommand extends FlutterCommand {
"More than one emulator matches '$id':",
);
} else {
await emulators.first.launch();
await emulators.first.launch(coldBoot: coldBoot);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/emulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ abstract class Emulator {
&& other.id == id;
}

Future<void> launch();
Future<void> launch({bool coldBoot});

@override
String toString() => name;
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/ios/ios_emulators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class IOSEmulator extends Emulator {
PlatformType get platformType => PlatformType.ios;

@override
Future<void> launch() async {
Future<void> launch({bool coldBoot = false}) async {
Future<bool> launchSimulator(List<String> additionalArgs) async {
final List<String> args = <String>[
'open',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ void main() {
await emulator.launch(startupDuration: Duration.zero);
});

testWithoutContext('succeeds with coldboot launch', () async {
final List<String> kEmulatorLauchColdBootCommand = <String>[
...kEmulatorLaunchCommand,
'-no-snapshot-load'
];
final AndroidEmulator emulator = AndroidEmulator(emulatorID,
processManager: FakeProcessManager.list(<FakeCommand>[
FakeCommand(command: kEmulatorLauchColdBootCommand),
]),
androidSdk: mockSdk,
logger: BufferLogger.test(),
);

await emulator.launch(startupDuration: Duration.zero, coldBoot: true);
});

testWithoutContext('prints error on failure', () async {
final BufferLogger logger = BufferLogger.test();
final AndroidEmulator emulator = AndroidEmulator(emulatorID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class FakeEmulator extends Emulator {
PlatformType get platformType => PlatformType.android;

@override
Future<void> launch() {
Future<void> launch({bool coldBoot = false}) {
throw UnimplementedError('Not implemented in Mock');
}
}
Expand Down

0 comments on commit 2d28350

Please sign in to comment.