add a regression test for daemon device notification (#9618)

* add a regression test for daemon device notification

* revert event send simplification
This commit is contained in:
Devon Carew 2017-04-27 09:30:47 -07:00 committed by GitHub
parent d2c6b0a637
commit 9ac2e44ee7
3 changed files with 71 additions and 18 deletions

View file

@ -521,28 +521,26 @@ class DeviceDomain extends Domain {
registerHandler('forward', forward);
registerHandler('unforward', unforward);
PollingDeviceDiscovery deviceDiscovery = new AndroidDevices();
if (deviceDiscovery.supportsPlatform)
_discoverers.add(deviceDiscovery);
deviceDiscovery = new IOSDevices();
if (deviceDiscovery.supportsPlatform)
_discoverers.add(deviceDiscovery);
deviceDiscovery = new IOSSimulators();
if (deviceDiscovery.supportsPlatform)
_discoverers.add(deviceDiscovery);
for (PollingDeviceDiscovery discoverer in _discoverers) {
discoverer.onAdded.listen(_onDeviceEvent('device.added'));
discoverer.onRemoved.listen(_onDeviceEvent('device.removed'));
}
addDeviceDiscoverer(new AndroidDevices());
addDeviceDiscoverer(new IOSDevices());
addDeviceDiscoverer(new IOSSimulators());
}
Future<Null> _deviceEvents = new Future<Null>.value();
void addDeviceDiscoverer(PollingDeviceDiscovery discoverer) {
if (!discoverer.supportsPlatform)
return;
_discoverers.add(discoverer);
discoverer.onAdded.listen(_onDeviceEvent('device.added'));
discoverer.onRemoved.listen(_onDeviceEvent('device.removed'));
}
Future<Null> _serializeDeviceEvents = new Future<Null>.value();
_DeviceEventHandler _onDeviceEvent(String eventName) {
return (Device device) {
_deviceEvents = _deviceEvents.then((_) async {
_serializeDeviceEvents = _serializeDeviceEvents.then((_) async {
sendEvent(eventName, await _deviceToMap(device));
});
};
@ -673,6 +671,7 @@ dynamic _toJsonable(dynamic obj) {
return obj;
if (obj is OperationResult)
return obj;
assert(false, 'obj not jsonable');
return '$obj';
}

View file

@ -231,6 +231,31 @@ void main() {
responses.close();
commands.close();
});
_testUsingContext('device.notify', () {
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
);
final MockPollingDeviceDiscovery discoverer = new MockPollingDeviceDiscovery();
daemon.deviceDomain.addDeviceDiscoverer(discoverer);
discoverer.addDevice(new MockAndroidDevice());
return responses.stream.first.then((Map<String, dynamic> response) {
expect(response['event'], 'device.added');
expect(response['params'], isMap);
final Map<String, dynamic> params = response['params'];
expect(params['platform'], isNotEmpty); // the mock device has a platform of 'android-arm'
responses.close();
commands.close();
});
});
});
}

View file

@ -29,6 +29,35 @@ class MockApplicationPackageStore extends ApplicationPackageStore {
);
}
class MockPollingDeviceDiscovery extends PollingDeviceDiscovery {
List<Device> _devices = <Device>[];
StreamController<Device> _onAddedController = new StreamController<Device>.broadcast();
StreamController<Device> _onRemovedController = new StreamController<Device>.broadcast();
MockPollingDeviceDiscovery() : super('mock');
@override
List<Device> pollingGetDevices() => _devices;
@override
bool get supportsPlatform => true;
void addDevice(MockAndroidDevice device) {
_devices.add(device);
_onAddedController.add(device);
}
@override
List<Device> get devices => _devices;
@override
Stream<Device> get onAdded => _onAddedController.stream;
@override
Stream<Device> get onRemoved => _onRemovedController.stream;
}
class MockAndroidDevice extends Mock implements AndroidDevice {
@override
Future<TargetPlatform> get targetPlatform async => TargetPlatform.android_arm;