Unverified Commit 605debfc authored by Zachary Anderson's avatar Zachary Anderson Committed by GitHub

[flutter_tools] Make AndroidConsole check for next line (#52353)

parent ac6ea52b
......@@ -42,21 +42,29 @@ class AndroidConsole {
}
Future<String> getAvdName() async {
if (_queue == null) {
return null;
}
_write('avd name\n');
return _readResponse();
}
void destroy() {
if (_socket != null) {
_socket.destroy();
_socket = null;
_queue = null;
}
_socket?.destroy();
_socket = null;
_queue = null;
}
Future<String> _readResponse() async {
if (_queue == null) {
return null;
}
final StringBuffer output = StringBuffer();
while (true) {
if (!await _queue.hasNext) {
destroy();
return null;
}
final String text = await _queue.next;
final String trimmedText = text.trim();
if (trimmedText == 'OK') {
......@@ -72,6 +80,6 @@ class AndroidConsole {
}
void _write(String text) {
_socket.add(ascii.encode(text));
_socket?.add(ascii.encode(text));
}
}
......@@ -523,6 +523,8 @@ flutter:
const String dummyEmulatorId = 'dummyEmulatorId';
final Future<Socket> Function(String host, int port) unresponsiveSocket =
(String host, int port) async => MockUnresponsiveAndroidConsoleSocket();
final Future<Socket> Function(String host, int port) disconnectingSocket =
(String host, int port) async => MockDisconnectingAndroidConsoleSocket();
final Future<Socket> Function(String host, int port) workingSocket =
(String host, int port) async => MockWorkingAndroidConsoleSocket(dummyEmulatorId);
String hardware;
......@@ -597,6 +599,14 @@ flutter:
AndroidConsoleSocketFactory: () => unresponsiveSocket,
ProcessManager: () => mockProcessManager,
});
testUsingContext('returns null on early disconnect', () async {
final AndroidDevice device = AndroidDevice('emulator-5555');
expect(await device.emulatorId, isNull);
}, overrides: <Type, Generator>{
AndroidConsoleSocketFactory: () => disconnectingSocket,
ProcessManager: () => mockProcessManager,
});
});
group('portForwarder', () {
......@@ -980,6 +990,26 @@ class MockUnresponsiveAndroidConsoleSocket extends Mock implements Socket {
void add(List<int> data) {}
}
/// An Android console socket that drops all input and returns no output.
class MockDisconnectingAndroidConsoleSocket extends Mock implements Socket {
MockDisconnectingAndroidConsoleSocket() {
_controller.add('Android Console: Welcome!\n');
// Include OK in the same packet here. In the response to "avd name"
// it's sent alone to ensure both are handled.
_controller.add('Android Console: Some intro text\nOK\n');
}
final StreamController<String> _controller = StreamController<String>();
@override
Stream<E> asyncMap<E>(FutureOr<E> convert(Uint8List event)) => _controller.stream as Stream<E>;
@override
void add(List<int> data) {
_controller.close();
}
}
class AndroidPackageTest extends ApplicationPackage {
AndroidPackageTest() : super(id: 'app-id');
......
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