diff --git a/packages/flutter_tools/test/integration/test_driver.dart b/packages/flutter_tools/test/integration/test_driver.dart index ee9a76d62c..078d3fa45c 100644 --- a/packages/flutter_tools/test/integration/test_driver.dart +++ b/packages/flutter_tools/test/integration/test_driver.dart @@ -76,13 +76,15 @@ class FlutterTestDriver { Future _setupProcess(List args, {bool withDebugger = false, bool pauseOnExceptions = false}) async { final String flutterBin = fs.path.join(getFlutterRoot(), 'bin', 'flutter'); - _debugPrint('Spawning flutter $args in ${_projectFolder.path}'); + final List flutterArgs = withDebugger + ? args.followedBy(['--start-paused']).toList() + : args; + _debugPrint('Spawning flutter $flutterArgs in ${_projectFolder.path}'); const ProcessManager _processManager = LocalProcessManager(); _proc = await _processManager.start( [flutterBin] - .followedBy(args) - .followedBy(withDebugger ? ['--start-paused'] : []) + .followedBy(flutterArgs) .toList(), workingDirectory: _projectFolder.path, environment: {'FLUTTER_TEST': 'true'}); @@ -263,16 +265,35 @@ class FlutterTestDriver { return script.sourceLocation(frame.location.token); } - Future> _waitFor({String event, int id, Duration timeout}) async { + Future> _waitFor({ + String event, + int id, + Duration timeout, + bool ignoreAppStopEvent = false, + }) async { final Completer> response = new Completer>(); - final StreamSubscription sub = _stdout.stream.listen((String line) { + StreamSubscription sub; + sub = _stdout.stream.listen((String line) async { final dynamic json = _parseFlutterResponse(line); if (json == null) { return; } else if ( (event != null && json['event'] == event) || (id != null && json['id'] == id)) { + await sub.cancel(); response.complete(json); + } else if (!ignoreAppStopEvent && json['event'] == 'app.stop') { + await sub.cancel(); + final StringBuffer error = new StringBuffer(); + error.write('Received app.stop event while waiting for '); + error.write('${event != null ? '$event event' : 'response to request $id.'}.\n\n'); + if (json['params'] != null && json['params']['error'] != null) { + error.write('${json['params']['error']}\n\n'); + } + if (json['params'] != null && json['params']['trace'] != null) { + error.write('${json['params']['trace']}\n\n'); + } + response.completeError(error.toString()); } }); @@ -327,8 +348,12 @@ class FlutterTestDriver { _debugPrint(jsonEncoded); // Set up the response future before we send the request to avoid any - // races. - final Future> responseFuture = _waitFor(id: requestId); + // races. If the method we're calling is app.stop then we tell waitFor not + // to throw if it sees an app.stop event before the response to this request. + final Future> responseFuture = _waitFor( + id: requestId, + ignoreAppStopEvent: method == 'app.stop', + ); _proc.stdin.writeln(jsonEncoded); final Map response = await responseFuture;