Unverified Commit 632e21b4 authored by Devon Carew's avatar Devon Carew Committed by GitHub

fix the daemon device.getDevices call (#25443)

parent a039b2ca
......@@ -612,12 +612,18 @@ class DeviceDomain extends Domain {
final List<PollingDeviceDiscovery> _discoverers = <PollingDeviceDiscovery>[];
Future<List<Device>> getDevices([Map<String, dynamic> args]) async {
final List<Device> devices = <Device>[];
/// Return a list of the current devices, with each device represented as a map
/// of properties (id, name, platform, ...).
Future<List<Map<String, dynamic>>> getDevices([Map<String, dynamic> args]) async {
final List<Map<String, dynamic>> devicesInfo = <Map<String, dynamic>>[];
for (PollingDeviceDiscovery discoverer in _discoverers) {
devices.addAll(await discoverer.devices);
for (Device device in await discoverer.devices) {
devicesInfo.add(await _deviceToMap(device));
}
}
return devices;
return devicesInfo;
}
/// Enable device events.
......
......@@ -217,6 +217,27 @@ void main() {
await commands.close();
});
testUsingContext('device.getDevices reports avaiable devices', () async {
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
daemon = Daemon(
commands.stream,
responses.add,
notifyingLogger: notifyingLogger,
);
final MockPollingDeviceDiscovery discoverer = MockPollingDeviceDiscovery();
daemon.deviceDomain.addDeviceDiscoverer(discoverer);
discoverer.addDevice(MockAndroidDevice());
commands.add(<String, dynamic>{'id': 0, 'method': 'device.getDevices'});
final Map<String, dynamic> response = await responses.stream.firstWhere(_notEvent);
expect(response['id'], 0);
final dynamic result = response['result'];
expect(result, isList);
expect(result, isNotEmpty);
await responses.close();
await commands.close();
});
testUsingContext('should send device.added event when device is discovered', () async {
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'package:file/file.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:process/process.dart';
import '../src/common.dart';
import 'test_data/basic_project.dart';
import 'test_driver.dart';
import 'test_utils.dart';
void main() {
group('daemon_mode', () {
Directory tempDir;
final BasicProject _project = BasicProject();
Process process;
setUp(() async {
tempDir = createResolvedTempDirectorySync();
await _project.setUpIn(tempDir);
});
tearDown(() async {
tryToDelete(tempDir);
process?.kill();
});
test('device.getDevices', () async {
final String flutterBin =
fs.path.join(getFlutterRoot(), 'bin', 'flutter');
const ProcessManager processManager = LocalProcessManager();
process = await processManager.start(
<String>[flutterBin, '--show-test-device', 'daemon'],
workingDirectory: tempDir.path);
final StreamController<String> stdout =
StreamController<String>.broadcast();
transformToLines(process.stdout)
.listen((String line) => stdout.add(line));
final Stream<Map<String, dynamic>> stream =
stdout.stream.where((String line) {
final Map<String, dynamic> response = parseFlutterResponse(line);
// ignore 'Starting device daemon...'
if (response == null) {
return false;
}
// TODO(devoncarew): Remove this after #25440 lands.
if (response['event'] == 'daemon.showMessage') {
return false;
}
return true;
}).map(parseFlutterResponse);
Map<String, dynamic> response = await stream.first;
expect(response['event'], 'daemon.connected');
// start listening for devices
process.stdin.writeln('[${jsonEncode(<String, dynamic>{
'id': 1,
'method': 'device.enable',
})}]');
response = await stream.first;
expect(response['id'], 1);
expect(response['error'], isNull);
// [{"event":"device.added","params":{"id":"flutter-tester","name":
// "Flutter test device","platform":"flutter-tester","emulator":false}}]
response = await stream.first;
expect(response['event'], 'device.added');
// get the list of all devices
process.stdin.writeln('[${jsonEncode(<String, dynamic>{
'id': 2,
'method': 'device.getDevices',
})}]');
response = await stream.first;
expect(response['id'], 2);
expect(response['error'], isNull);
final dynamic result = response['result'];
expect(result, isList);
expect(result, isNotEmpty);
});
}, timeout: const Timeout.factor(2));
}
......@@ -32,7 +32,7 @@ void main() {
test('reports an error if an invalid device is supplied', () async {
// This test forces flutter to check for all possible devices to catch issues
// like https://github.com/flutter/flutter/issues/21418 which were skipped
// over because other integration tesst run using flutter-tester which short-cuts
// over because other integration tests run using flutter-tester which short-cuts
// some of the checks for devices.
final String flutterBin = fs.path.join(getFlutterRoot(), 'bin', 'flutter');
......
......@@ -82,8 +82,8 @@ abstract class FlutterTestDriver {
_debugPrint('Process exited ($code)');
_hasExited = true;
});
_transformToLines(_proc.stdout).listen((String line) => _stdout.add(line));
_transformToLines(_proc.stderr).listen((String line) => _stderr.add(line));
transformToLines(_proc.stdout).listen((String line) => _stdout.add(line));
transformToLines(_proc.stderr).listen((String line) => _stderr.add(line));
// Capture stderr to a buffer so we can show it all if any requests fail.
_stderr.stream.listen(_errorBuffer.writeln);
......@@ -236,7 +236,8 @@ abstract class FlutterTestDriver {
final Completer<Map<String, dynamic>> response = Completer<Map<String, dynamic>>();
StreamSubscription<String> sub;
sub = _stdout.stream.listen((String line) async {
final dynamic json = _parseFlutterResponse(line);
final dynamic json = parseFlutterResponse(line);
_lastResponse = line;
if (json == null) {
return;
} else if (
......@@ -285,20 +286,6 @@ abstract class FlutterTestDriver {
throw '$error\nReceived:\n${messages.toString()}';
}).whenComplete(() => sub.cancel());
}
Map<String, dynamic> _parseFlutterResponse(String line) {
if (line.startsWith('[') && line.endsWith(']')) {
try {
final Map<String, dynamic> resp = json.decode(line)[0];
_lastResponse = line;
return resp;
} catch (e) {
// Not valid JSON, so likely some other output that was surrounded by [brackets]
return null;
}
}
return null;
}
}
class FlutterRunTestDriver extends FlutterTestDriver {
......@@ -509,10 +496,23 @@ class FlutterRunTestDriver extends FlutterTestDriver {
}
}
Stream<String> _transformToLines(Stream<List<int>> byteStream) {
Stream<String> transformToLines(Stream<List<int>> byteStream) {
return byteStream.transform<String>(utf8.decoder).transform<String>(const LineSplitter());
}
Map<String, dynamic> parseFlutterResponse(String line) {
if (line.startsWith('[') && line.endsWith(']')) {
try {
final Map<String, dynamic> resp = json.decode(line)[0];
return resp;
} catch (e) {
// Not valid JSON, so likely some other output that was surrounded by [brackets]
return null;
}
}
return null;
}
class SourcePosition {
SourcePosition(this.line, this.column);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment