Add emulator.getEmulators and emulator.launch to daemon (#17466)
Related to: #14822 Dart-Code/Dart-Code#490 #13379
This commit is contained in:
@@ -17,6 +17,7 @@ import '../base/utils.dart';
|
||||
import '../build_info.dart';
|
||||
import '../cache.dart';
|
||||
import '../device.dart';
|
||||
import '../emulator.dart';
|
||||
import '../globals.dart';
|
||||
import '../ios/devices.dart';
|
||||
import '../ios/simulators.dart';
|
||||
@@ -88,6 +89,7 @@ class Daemon {
|
||||
_registerDomain(daemonDomain = new DaemonDomain(this));
|
||||
_registerDomain(appDomain = new AppDomain(this));
|
||||
_registerDomain(deviceDomain = new DeviceDomain(this));
|
||||
_registerDomain(emulatorDomain = new EmulatorDomain(this));
|
||||
|
||||
// Start listening.
|
||||
_commandSubscription = commandStream.listen(
|
||||
@@ -102,6 +104,7 @@ class Daemon {
|
||||
DaemonDomain daemonDomain;
|
||||
AppDomain appDomain;
|
||||
DeviceDomain deviceDomain;
|
||||
EmulatorDomain emulatorDomain;
|
||||
StreamSubscription<Map<String, dynamic>> _commandSubscription;
|
||||
|
||||
final DispatchCommand sendCommand;
|
||||
@@ -729,6 +732,13 @@ Future<Map<String, dynamic>> _deviceToMap(Device device) async {
|
||||
};
|
||||
}
|
||||
|
||||
Map<String, dynamic> _emulatorToMap(Emulator emulator) {
|
||||
return <String, dynamic>{
|
||||
'id': emulator.id,
|
||||
'name': emulator.name,
|
||||
};
|
||||
}
|
||||
|
||||
Map<String, dynamic> _operationResultToMap(OperationResult result) {
|
||||
final Map<String, dynamic> map = <String, dynamic>{
|
||||
'code': result.code,
|
||||
@@ -824,6 +834,34 @@ class AppInstance {
|
||||
}
|
||||
}
|
||||
|
||||
/// This domain responds to methods like [getEmulators] and [launch].
|
||||
class EmulatorDomain extends Domain {
|
||||
EmulatorManager emulators = new EmulatorManager();
|
||||
|
||||
EmulatorDomain(Daemon daemon) : super(daemon, 'emulator') {
|
||||
registerHandler('getEmulators', getEmulators);
|
||||
registerHandler('launch', launch);
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> getEmulators([Map<String, dynamic> args]) async {
|
||||
final List<Emulator> list = await emulators.getAllAvailableEmulators();
|
||||
return list.map(_emulatorToMap).toList();
|
||||
}
|
||||
|
||||
Future<Null> launch(Map<String, dynamic> args) async {
|
||||
final String emulatorId = _getStringArg(args, 'emulatorId', required: true);
|
||||
final List<Emulator> matches =
|
||||
await emulators.getEmulatorsMatching(emulatorId);
|
||||
if (matches.isEmpty) {
|
||||
throw "emulator '$emulatorId' not found";
|
||||
} else if (matches.length > 1) {
|
||||
throw "multiple emulators match '$emulatorId'";
|
||||
} else {
|
||||
await matches.first.launch();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A [Logger] which sends log messages to a listening daemon client.
|
||||
///
|
||||
/// This class can either:
|
||||
|
||||
@@ -263,6 +263,43 @@ void main() {
|
||||
AndroidWorkflow: () => new MockAndroidWorkflow(),
|
||||
IOSWorkflow: () => new MockIOSWorkflow(),
|
||||
});
|
||||
|
||||
testUsingContext('emulator.launch without an emulatorId should report an error', () async {
|
||||
final DaemonCommand command = new DaemonCommand();
|
||||
applyMocksToCommand(command);
|
||||
|
||||
final StreamController<Map<String, dynamic>> commands = new StreamController<Map<String, dynamic>>();
|
||||
final StreamController<Map<String, dynamic>> responses = new StreamController<Map<String, dynamic>>();
|
||||
daemon = new Daemon(
|
||||
commands.stream,
|
||||
responses.add,
|
||||
daemonCommand: command,
|
||||
notifyingLogger: notifyingLogger
|
||||
);
|
||||
|
||||
commands.add(<String, dynamic>{ 'id': 0, 'method': 'emulator.launch' });
|
||||
final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent);
|
||||
expect(response['id'], 0);
|
||||
expect(response['error'], contains('emulatorId is required'));
|
||||
responses.close();
|
||||
commands.close();
|
||||
});
|
||||
|
||||
testUsingContext('emulator.getEmulators should respond with list', () async {
|
||||
final StreamController<Map<String, dynamic>> commands = new StreamController<Map<String, dynamic>>();
|
||||
final StreamController<Map<String, dynamic>> responses = new StreamController<Map<String, dynamic>>();
|
||||
daemon = new Daemon(
|
||||
commands.stream,
|
||||
responses.add,
|
||||
notifyingLogger: notifyingLogger
|
||||
);
|
||||
commands.add(<String, dynamic>{'id': 0, 'method': 'emulator.getEmulators'});
|
||||
final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent);
|
||||
expect(response['id'], 0);
|
||||
expect(response['result'], isList);
|
||||
responses.close();
|
||||
commands.close();
|
||||
});
|
||||
});
|
||||
|
||||
group('daemon serialization', () {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'dart:io';
|
||||
|
||||
Process daemon;
|
||||
|
||||
@@ -15,6 +15,8 @@ Process daemon;
|
||||
// start: start an app
|
||||
// stop: stop a running app
|
||||
// devices: list devices
|
||||
// emulators: list emulators
|
||||
// launch: launch an emulator
|
||||
|
||||
Future<Null> main() async {
|
||||
daemon = await Process.start('dart', <String>['bin/flutter_tools.dart', 'daemon']);
|
||||
@@ -62,6 +64,15 @@ Future<Null> main() async {
|
||||
}
|
||||
} else if (line == 'devices') {
|
||||
_send(<String, dynamic>{'method': 'device.getDevices'});
|
||||
} else if (line == 'emulators') {
|
||||
_send(<String, dynamic>{'method': 'emulator.getEmulators'});
|
||||
} else if (words.first == 'emulator-launch') {
|
||||
_send(<String, dynamic>{
|
||||
'method': 'emulator.launch',
|
||||
'params': <String, dynamic>{
|
||||
'emulatorId': words[1]
|
||||
}
|
||||
});
|
||||
} else if (line == 'enable') {
|
||||
_send(<String, dynamic>{'method': 'device.enable'});
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user