test_driver.dart 27.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// 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';
9
import 'package:flutter_tools/src/base/common.dart';
10 11
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
12
import 'package:flutter_tools/src/base/utils.dart';
13
import 'package:flutter_tools/src/globals.dart' as globals;
14
import 'package:meta/meta.dart';
15
import 'package:process/process.dart';
16 17
import 'package:vm_service/vm_service.dart';
import 'package:vm_service/vm_service_io.dart';
18 19 20

import '../src/common.dart';

21 22 23 24 25 26 27 28 29 30
// Set this to true for debugging to get verbose logs written to stdout.
// The logs include the following:
//   <=stdout= data that the flutter tool running in --verbose mode wrote to stdout.
//   <=stderr= data that the flutter tool running in --verbose mode wrote to stderr.
//   =stdin=> data that the test sent to the flutter tool over stdin.
//   =vm=> data that was sent over the VM service channel to the app running on the test device.
//   <=vm= data that was sent from the app on the test device over the VM service channel.
//   Messages regarding what the test is doing.
// If this is false, then only critical errors and logs when things appear to be
// taking a long time are printed to the console.
31
const bool _printDebugOutputToStdOut = false;
32 33 34 35

final DateTime startTime = DateTime.now();

const Duration defaultTimeout = Duration(seconds: 5);
36 37
const Duration appStartTimeout = Duration(seconds: 120);
const Duration quitTimeout = Duration(seconds: 10);
38

39
abstract class FlutterTestDriver {
40 41 42 43
  FlutterTestDriver(
    this._projectFolder, {
    String logPrefix,
  }) : _logPrefix = logPrefix != null ? '$logPrefix: ' : '';
44

45
  final Directory _projectFolder;
46
  final String _logPrefix;
47 48
  Process _process;
  int _processPid;
49 50 51 52
  final StreamController<String> _stdout = StreamController<String>.broadcast();
  final StreamController<String> _stderr = StreamController<String>.broadcast();
  final StreamController<String> _allMessages = StreamController<String>.broadcast();
  final StringBuffer _errorBuffer = StringBuffer();
53
  String _lastResponse;
54
  Uri _vmServiceWsUri;
55
  bool _hasExited = false;
56

57
  VmService _vmService;
58
  String get lastErrorInfo => _errorBuffer.toString();
59
  Stream<String> get stdout => _stdout.stream;
60
  int get vmServicePort => _vmServiceWsUri.port;
61
  bool get hasExited => _hasExited;
62

63 64 65 66 67 68 69 70 71 72 73 74
  String lastTime = '';
  void _debugPrint(String message, { String topic = '' }) {
    const int maxLength = 2500;
    final String truncatedMessage = message.length > maxLength ? message.substring(0, maxLength) + '...' : message;
    final String line = '${topic.padRight(10)} $truncatedMessage';
    _allMessages.add(line);
    final int timeInSeconds = DateTime.now().difference(startTime).inSeconds;
    String time = timeInSeconds.toString().padLeft(5) + 's ';
    if (time == lastTime) {
      time = ' ' * time.length;
    } else {
      lastTime = time;
75
    }
76
    if (_printDebugOutputToStdOut) {
77
      print('$time$_logPrefix$line');
78
    }
79
  }
80

81
  Future<void> _setupProcess(
82
    List<String> arguments, {
83
    String script,
84 85 86
    bool withDebugger = false,
    File pidFile,
  }) async {
87
    final String flutterBin = globals.fs.path.join(getFlutterRoot(), 'bin', 'flutter');
88
    if (withDebugger) {
89
      arguments.add('--start-paused');
90 91
    }
    if (_printDebugOutputToStdOut) {
92
      arguments.add('--verbose');
93
    }
94
    if (pidFile != null) {
95
      arguments.addAll(<String>['--pid-file', pidFile.path]);
96
    }
97
    if (script != null) {
98
      arguments.add(script);
99
    }
100
    _debugPrint('Spawning flutter $arguments in ${_projectFolder.path}');
101

102
    const ProcessManager _processManager = LocalProcessManager();
103 104 105 106 107
    _process = await _processManager.start(
      <String>[flutterBin]
        .followedBy(arguments)
        .toList(),
      workingDirectory: _projectFolder.path,
108 109
      // The web environment variable has the same effect as `flutter config --enable-web`.
      environment: <String, String>{'FLUTTER_TEST': 'true', 'FLUTTER_WEB': 'true'},
110
    );
111

112 113
    // This class doesn't use the result of the future. It's made available
    // via a getter for external uses.
114
    unawaited(_process.exitCode.then((int code) {
115 116
      _debugPrint('Process exited ($code)');
      _hasExited = true;
117
    }));
118 119
    transformToLines(_process.stdout).listen(_stdout.add);
    transformToLines(_process.stderr).listen(_stderr.add);
120 121 122 123 124

    // Capture stderr to a buffer so we can show it all if any requests fail.
    _stderr.stream.listen(_errorBuffer.writeln);

    // This is just debug printing to aid running/debugging tests locally.
125 126
    _stdout.stream.listen((String message) => _debugPrint(message, topic: '<=stdout='));
    _stderr.stream.listen((String message) => _debugPrint(message, topic: '<=stderr='));
127 128
  }

129 130
  Future<void> get done => _process.exitCode;

131
  Future<void> connectToVmService({ bool pauseOnExceptions = false }) async {
132 133 134
    _vmService = await vmServiceConnectUri('$_vmServiceWsUri');
    _vmService.onSend.listen((String s) => _debugPrint(s, topic: '=vm=>'));
    _vmService.onReceive.listen((String s) => _debugPrint(s, topic: '<=vm='));
135 136

    final Completer<void> isolateStarted = Completer<void>();
137
    _vmService.onIsolateEvent.listen((Event event) {
138 139 140
      if (event.kind == EventKind.kIsolateStart) {
        isolateStarted.complete();
      } else if (event.kind == EventKind.kIsolateExit && event.isolate.id == _flutterIsolateId) {
141 142 143
        // Hot restarts cause all the isolates to exit, so we need to refresh
        // our idea of what the Flutter isolate ID is.
        _flutterIsolateId = null;
144
      }
145 146 147 148 149 150 151
    });

    await Future.wait(<Future<Success>>[
      _vmService.streamListen('Isolate'),
      _vmService.streamListen('Debug'),
    ]);

152 153 154 155
    if ((await _vmService.getVM()).isolates.isEmpty) {
      await isolateStarted.future;
    }

156 157 158 159 160 161 162
    await waitForPause();
    if (pauseOnExceptions) {
      await _vmService.setExceptionPauseMode(
        await _getFlutterIsolateId(),
        ExceptionPauseMode.kUnhandled,
      );
    }
163 164
  }

165 166
  Future<int> quit() => _killGracefully();

167
  Future<int> _killGracefully() async {
168
    if (_processPid == null) {
169
      return -1;
170
    }
171 172 173 174 175 176
    // If we try to kill the process while it's paused, we'll end up terminating
    // it forcefully and it won't terminate child processes, so we need to ensure
    // it's running before terminating.
    await resume().timeout(defaultTimeout)
        .catchError((Object e) => _debugPrint('Ignoring failure to resume during shutdown'));

177
    _debugPrint('Sending SIGTERM to $_processPid..');
178
    ProcessSignal.SIGTERM.send(_processPid);
179
    return _process.exitCode.timeout(quitTimeout, onTimeout: _killForcefully);
180 181 182
  }

  Future<int> _killForcefully() {
183
    _debugPrint('Sending SIGKILL to $_processPid..');
184
    ProcessSignal.SIGKILL.send(_processPid);
185
    return _process.exitCode;
186 187
  }

188 189
  String _flutterIsolateId;
  Future<String> _getFlutterIsolateId() async {
190 191
    // Currently these tests only have a single isolate. If this
    // ceases to be the case, this code will need changing.
192 193 194 195 196 197 198 199
    if (_flutterIsolateId == null) {
      final VM vm = await _vmService.getVM();
      _flutterIsolateId = vm.isolates.first.id;
    }
    return _flutterIsolateId;
  }

  Future<Isolate> _getFlutterIsolate() async {
200
    final Isolate isolate = await _vmService.getIsolate(await _getFlutterIsolateId());
201
    return isolate;
202 203
  }

204 205 206 207 208 209 210 211 212 213 214 215 216 217
  /// Add a breakpoint and wait for it to trip the program execution.
  ///
  /// Only call this when you are absolutely sure that the program under test
  /// will hit the breakpoint _in the future_.
  ///
  /// In particular, do not call this if the program is currently racing to pass
  /// the line of code you are breaking on. Pretend that calling this will take
  /// an hour before setting the breakpoint. Would the code still eventually hit
  /// the breakpoint and stop?
  Future<void> breakAt(Uri uri, int line) async {
    await addBreakpoint(uri, line);
    await waitForPause();
  }

218
  Future<void> addBreakpoint(Uri uri, int line) async {
219
    _debugPrint('Sending breakpoint for: $uri:$line');
220
    await _vmService.addBreakpointWithScriptUri(
221 222 223 224
      await _getFlutterIsolateId(),
      uri.toString(),
      line,
    );
225 226
  }

227 228
  // This method isn't racy. If the isolate is already paused,
  // it will immediately return.
229
  Future<Isolate> waitForPause() async {
230 231 232 233 234 235 236 237 238 239 240 241
    return _timeoutWithMessages<Isolate>(
      () async {
        final String flutterIsolate = await _getFlutterIsolateId();
        final Completer<Event> pauseEvent = Completer<Event>();

        // Start listening for pause events.
        final StreamSubscription<Event> pauseSubscription = _vmService.onDebugEvent
          .where((Event event) {
            return event.isolate.id == flutterIsolate
                && event.kind.startsWith('Pause');
          })
          .listen((Event event) {
242
            if (!pauseEvent.isCompleted) {
243
              pauseEvent.complete(event);
244
            }
245 246 247 248 249
          });

        // But also check if the isolate was already paused (only after we've set
        // up the subscription) to avoid races. If it was paused, we don't need to wait
        // for the event.
250
        final Isolate isolate = await _vmService.getIsolate(flutterIsolate);
251 252 253 254 255 256
        if (isolate.pauseEvent.kind.startsWith('Pause')) {
          _debugPrint('Isolate was already paused (${isolate.pauseEvent.kind}).');
        } else {
          _debugPrint('Isolate is not already paused, waiting for event to arrive...');
          await pauseEvent.future;
        }
257

258 259
        // Cancel the subscription on either of the above.
        await pauseSubscription.cancel();
260

261 262 263 264
        return _getFlutterIsolate();
      },
      task: 'Waiting for isolate to pause',
    );
265
  }
266

267 268
  Future<Isolate> resume({ bool waitForNextPause = false }) => _resume(null, waitForNextPause);
  Future<Isolate> stepOver({ bool waitForNextPause = true }) => _resume(StepOption.kOver, waitForNextPause);
269
  Future<Isolate> stepOverAsync({ bool waitForNextPause = true }) => _resume(StepOption.kOverAsyncSuspension, waitForNextPause);
270 271
  Future<Isolate> stepInto({ bool waitForNextPause = true }) => _resume(StepOption.kInto, waitForNextPause);
  Future<Isolate> stepOut({ bool waitForNextPause = true }) => _resume(StepOption.kOut, waitForNextPause);
272

273 274 275 276 277
  Future<bool> isAtAsyncSuspension() async {
    final Isolate isolate = await _getFlutterIsolate();
    return isolate.pauseEvent.atAsyncSuspension == true;
  }

278
  Future<Isolate> stepOverOrOverAsyncSuspension({ bool waitForNextPause = true }) async {
279
    if (await isAtAsyncSuspension()) {
280
      return await stepOverAsync(waitForNextPause: waitForNextPause);
281
    }
282
    return await stepOver(waitForNextPause: waitForNextPause);
283
  }
284

285 286 287 288 289 290 291
  Future<Isolate> _resume(String step, bool waitForNextPause) async {
    assert(waitForNextPause != null);
    await _timeoutWithMessages<dynamic>(
      () async => _vmService.resume(await _getFlutterIsolateId(), step: step),
      task: 'Resuming isolate (step=$step)',
    );
    return waitForNextPause ? waitForPause() : null;
292 293
  }

294 295 296
  Future<ObjRef> evaluateInFrame(String expression) async {
    return _timeoutWithMessages<ObjRef>(
      () async => await _vmService.evaluateInFrame(await _getFlutterIsolateId(), 0, expression) as ObjRef,
297 298
      task: 'Evaluating expression ($expression)',
    );
299 300
  }

301 302
  Future<InstanceRef> evaluate(String targetId, String expression) async {
    return _timeoutWithMessages<InstanceRef>(
303
      () async => await _vmService.evaluate(await _getFlutterIsolateId(), targetId, expression) as InstanceRef,
304 305
      task: 'Evaluating expression ($expression for $targetId)',
    );
306 307 308 309
  }

  Future<Frame> getTopStackFrame() async {
    final String flutterIsolateId = await _getFlutterIsolateId();
310
    final Stack stack = await _vmService.getStack(flutterIsolateId);
311
    if (stack.frames.isEmpty) {
312
      throw Exception('Stack is empty');
313
    }
314 315 316
    return stack.frames.first;
  }

317 318 319
  Future<SourcePosition> getSourceLocation() async {
    final String flutterIsolateId = await _getFlutterIsolateId();
    final Frame frame = await getTopStackFrame();
320
    final Script script = await _vmService.getObject(flutterIsolateId, frame.location.script.id) as Script;
321 322 323 324
    return _lookupTokenPos(script.tokenPosTable, frame.location.tokenPos);
  }

  SourcePosition _lookupTokenPos(List<List<int>> table, int tokenPos) {
325
    for (final List<int> row in table) {
326 327 328 329 330 331 332 333 334 335 336
      final int lineNumber = row[0];
      int index = 1;

      for (index = 1; index < row.length - 1; index += 2) {
        if (row[index] == tokenPos) {
          return SourcePosition(lineNumber, row[index + 1]);
        }
      }
    }

    return null;
337 338
  }

339 340 341
  Future<Map<String, dynamic>> _waitFor({
    String event,
    int id,
342
    Duration timeout = defaultTimeout,
343 344
    bool ignoreAppStopEvent = false,
  }) async {
345 346 347 348
    assert(timeout != null);
    assert(event != null || id != null);
    assert(event == null || id == null);
    final String interestingOccurrence = event != null ? '$event event' : 'response to request $id';
349
    final Completer<Map<String, dynamic>> response = Completer<Map<String, dynamic>>();
350 351
    StreamSubscription<String> subscription;
    subscription = _stdout.stream.listen((String line) async {
352
      final Map<String, dynamic> json = parseFlutterResponse(line);
353
      _lastResponse = line;
354
      if (json == null) {
355
        return;
356
      }
357
      if ((event != null && json['event'] == event) ||
358
          (id != null && json['id'] == id)) {
359 360
        await subscription.cancel();
        _debugPrint('OK ($interestingOccurrence)');
361
        response.complete(json);
362
      } else if (!ignoreAppStopEvent && json['event'] == 'app.stop') {
363
        await subscription.cancel();
364
        final StringBuffer error = StringBuffer();
365
        error.write('Received app.stop event while waiting for $interestingOccurrence\n\n');
366 367 368 369 370 371 372
        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());
373 374
      }
    });
375

376 377 378 379 380
    return _timeoutWithMessages(
      () => response.future,
      timeout: timeout,
      task: 'Expecting $interestingOccurrence',
    ).whenComplete(subscription.cancel);
381 382
  }

383 384
  Future<T> _timeoutWithMessages<T>(
    Future<T> Function() callback, {
385 386 387 388 389 390 391 392
    @required String task,
    Duration timeout = defaultTimeout,
  }) {
    assert(task != null);
    assert(timeout != null);

    if (_printDebugOutputToStdOut) {
      _debugPrint('$task...');
393 394
      final Timer longWarning = Timer(timeout, () => _debugPrint('$task is taking longer than usual...'));
      return callback().whenComplete(longWarning.cancel);
395 396 397 398 399 400
    }

    // We're not showing all output to the screen, so let's capture the output
    // that we would have printed if we were, and output it if we take longer
    // than the timeout or if we get an error.
    final StringBuffer messages = StringBuffer('$task\n');
401
    final DateTime start = DateTime.now();
402 403
    bool timeoutExpired = false;
    void logMessage(String logLine) {
404
      final int ms = DateTime.now().difference(start).inMilliseconds;
405 406
      final String formattedLine = '[+ ${ms.toString().padLeft(5)}] $logLine';
      messages.writeln(formattedLine);
407
    }
408
    final StreamSubscription<String> subscription = _allMessages.stream.listen(logMessage);
409

410
    final Timer longWarning = Timer(timeout, () {
411
      _debugPrint(messages.toString());
412
      timeoutExpired = true;
413
      _debugPrint('$task is taking longer than usual...');
414
    });
415
    final Future<T> future = callback().whenComplete(longWarning.cancel);
416 417 418 419

    return future.catchError((dynamic error) {
      if (!timeoutExpired) {
        timeoutExpired = true;
420
        _debugPrint(messages.toString());
421 422 423
      }
      throw error;
    }).whenComplete(() => subscription.cancel());
424
  }
425 426 427
}

class FlutterRunTestDriver extends FlutterTestDriver {
428 429 430 431
  FlutterRunTestDriver(
    Directory projectFolder, {
    String logPrefix,
  }) : super(projectFolder, logPrefix: logPrefix);
432 433 434

  String _currentRunningAppId;

435
  Future<void> run({
436
    bool withDebugger = false,
437
    bool startPaused = false,
438
    bool pauseOnExceptions = false,
439
    bool chrome = false,
440
    File pidFile,
441
    String script,
442
  }) async {
443 444
    await _setupProcess(
      <String>[
445
        'run',
446 447
        if (!chrome)
          '--disable-service-auth-codes',
448 449
        '--machine',
        '-d',
450
        if (chrome)
451
          ...<String>['chrome', '--web-run-headless', '--web-enable-expression-evaluation']
452 453
        else
          'flutter-tester',
454 455 456 457 458
      ],
      withDebugger: withDebugger,
      startPaused: startPaused,
      pauseOnExceptions: pauseOnExceptions,
      pidFile: pidFile,
459
      script: script,
460
    );
461 462 463 464 465
  }

  Future<void> attach(
    int port, {
    bool withDebugger = false,
466
    bool startPaused = false,
467 468 469
    bool pauseOnExceptions = false,
    File pidFile,
  }) async {
470 471
    await _setupProcess(
      <String>[
472 473 474 475 476 477
        'attach',
        '--machine',
        '-d',
        'flutter-tester',
        '--debug-port',
        '$port',
478 479 480 481 482 483
      ],
      withDebugger: withDebugger,
      startPaused: startPaused,
      pauseOnExceptions: pauseOnExceptions,
      pidFile: pidFile,
    );
484 485 486 487 488
  }

  @override
  Future<void> _setupProcess(
    List<String> args, {
489
    String script,
490
    bool withDebugger = false,
491
    bool startPaused = false,
492 493 494
    bool pauseOnExceptions = false,
    File pidFile,
  }) async {
495
    assert(!startPaused || withDebugger);
496 497
    await super._setupProcess(
      args,
498
      script: script,
499 500 501 502
      withDebugger: withDebugger,
      pidFile: pidFile,
    );

503 504 505 506 507 508 509 510
    final Completer<void> prematureExitGuard = Completer<void>();

    // If the process exits before all of the `await`s below are done, then it
    // exited prematurely. This causes the currently suspended `await` to
    // deadlock until the test times out. Instead, this causes the test to fail
    // fast.
    unawaited(_process.exitCode.then((_) {
      if (!prematureExitGuard.isCompleted) {
511
        prematureExitGuard.completeError('Process exited prematurely: ${args.join(' ')}: $_errorBuffer');
512 513
      }
    }));
514

515 516 517 518 519 520
    unawaited(() async {
      try {
        // Stash the PID so that we can terminate the VM more reliably than using
        // _process.kill() (`flutter` is a shell script so _process itself is a
        // shell, not the flutter tool's Dart process).
        final Map<String, dynamic> connected = await _waitFor(event: 'daemon.connected');
521
        _processPid = connected['params']['pid'] as int;
522 523 524 525 526 527 528

        // Set this up now, but we don't wait it yet. We want to make sure we don't
        // miss it while waiting for debugPort below.
        final Future<Map<String, dynamic>> started = _waitFor(event: 'app.started', timeout: appStartTimeout);

        if (withDebugger) {
          final Map<String, dynamic> debugPort = await _waitFor(event: 'app.debugPort', timeout: appStartTimeout);
529
          final String wsUriString = debugPort['params']['wsUri'] as String;
530 531 532 533 534 535
          _vmServiceWsUri = Uri.parse(wsUriString);
          await connectToVmService(pauseOnExceptions: pauseOnExceptions);
          if (!startPaused) {
            await resume(waitForNextPause: false);
          }
        }
536

537 538
        // Now await the started event; if it had already happened the future will
        // have already completed.
539
        _currentRunningAppId = (await started)['params']['appId'] as String;
540
        prematureExitGuard.complete();
541
      } on Exception catch (error, stackTrace) {
542
        prematureExitGuard.completeError(error, stackTrace);
543
      }
544
    }());
545

546
    return prematureExitGuard.future;
547 548
  }

549
  Future<void> hotRestart({ bool pause = false }) => _restart(fullRestart: true, pause: pause);
550 551
  Future<void> hotReload() => _restart(fullRestart: false);

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
  Future<void> scheduleFrame() async {
    if (_currentRunningAppId == null) {
      throw Exception('App has not started yet');
    }
    await _sendRequest(
      'app.callServiceExtension',
      <String, dynamic>{'appId': _currentRunningAppId, 'methodName': 'ext.ui.window.scheduleFrame'},
    );
  }

  Future<void> reloadMethod({ String libraryId, String classId }) async {
    if (_currentRunningAppId == null) {
      throw Exception('App has not started yet');
    }
    final dynamic reloadMethodResponse = await _sendRequest(
      'app.reloadMethod',
      <String, dynamic>{'appId': _currentRunningAppId, 'class': classId, 'library': libraryId},
    );
    if (reloadMethodResponse == null || reloadMethodResponse['code'] != 0) {
      _throwErrorResponse('reloadMethodResponse request failed');
    }
  }

575
  Future<void> _restart({ bool fullRestart = false, bool pause = false }) async {
576
    if (_currentRunningAppId == null) {
577
      throw Exception('App has not started yet');
578
    }
579

580 581 582
    _debugPrint('Performing ${ pause ? "paused " : "" }${ fullRestart ? "hot restart" : "hot reload" }...');
    final dynamic hotReloadResponse = await _sendRequest(
      'app.restart',
583
      <String, dynamic>{'appId': _currentRunningAppId, 'fullRestart': fullRestart, 'pause': pause},
584
    );
585
    _debugPrint('${fullRestart ? "Hot restart" : "Hot reload"} complete.');
586

587
    if (hotReloadResponse == null || hotReloadResponse['code'] != 0) {
588
      _throwErrorResponse('Hot ${fullRestart ? 'restart' : 'reload'} request failed');
589
    }
590 591 592
  }

  Future<int> detach() async {
593 594 595
    if (_process == null) {
      return 0;
    }
596
    if (_vmService != null) {
597
      _debugPrint('Closing VM service...');
598 599 600
      _vmService.dispose();
    }
    if (_currentRunningAppId != null) {
601
      _debugPrint('Detaching from app...');
602
      await Future.any<void>(<Future<void>>[
603
        _process.exitCode,
604 605 606 607 608 609 610 611 612 613
        _sendRequest(
          'app.detach',
          <String, dynamic>{'appId': _currentRunningAppId},
        ),
      ]).timeout(
        quitTimeout,
        onTimeout: () { _debugPrint('app.detach did not return within $quitTimeout'); },
      );
      _currentRunningAppId = null;
    }
614 615
    _debugPrint('Waiting for process to end...');
    return _process.exitCode.timeout(quitTimeout, onTimeout: _killGracefully);
616 617 618 619
  }

  Future<int> stop() async {
    if (_vmService != null) {
620
      _debugPrint('Closing VM service...');
621 622 623
      _vmService.dispose();
    }
    if (_currentRunningAppId != null) {
624
      _debugPrint('Stopping application...');
625
      await Future.any<void>(<Future<void>>[
626
        _process.exitCode,
627 628 629 630 631 632 633 634 635 636
        _sendRequest(
          'app.stop',
          <String, dynamic>{'appId': _currentRunningAppId},
        ),
      ]).timeout(
        quitTimeout,
        onTimeout: () { _debugPrint('app.stop did not return within $quitTimeout'); },
      );
      _currentRunningAppId = null;
    }
637 638 639
    if (_process != null) {
      _debugPrint('Waiting for process to end...');
      return _process.exitCode.timeout(quitTimeout, onTimeout: _killGracefully);
640 641 642 643
    }
    return 0;
  }

644 645 646
  int id = 1;
  Future<dynamic> _sendRequest(String method, dynamic params) async {
    final int requestId = id++;
647
    final Map<String, dynamic> request = <String, dynamic>{
648 649
      'id': requestId,
      'method': method,
650
      'params': params,
651
    };
652
    final String jsonEncoded = json.encode(<Map<String, dynamic>>[request]);
653
    _debugPrint(jsonEncoded, topic: '=stdin=>');
654

655
    // Set up the response future before we send the request to avoid any
656
    // races. If the method we're calling is app.stop then we tell _waitFor not
657 658 659 660 661
    // to throw if it sees an app.stop event before the response to this request.
    final Future<Map<String, dynamic>> responseFuture = _waitFor(
      id: requestId,
      ignoreAppStopEvent: method == 'app.stop',
    );
662
    _process.stdin.writeln(jsonEncoded);
663
    final Map<String, dynamic> response = await responseFuture;
664

665
    if (response['error'] != null || response['result'] == null) {
666
      _throwErrorResponse('Unexpected error response');
667
    }
668

669
    return response['result'];
670 671
  }

672 673
  void _throwErrorResponse(String message) {
    throw '$message\n\n$_lastResponse\n\n${_errorBuffer.toString()}'.trim();
674
  }
675 676
}

677
class FlutterTestTestDriver extends FlutterTestDriver {
678 679
  FlutterTestTestDriver(Directory _projectFolder, {String logPrefix})
    : super(_projectFolder, logPrefix: logPrefix);
680 681 682 683 684

  Future<void> test({
    String testFile = 'test/test.dart',
    bool withDebugger = false,
    bool pauseOnExceptions = false,
685
    bool coverage = false,
686 687 688 689
    File pidFile,
    Future<void> Function() beforeStart,
  }) async {
    await _setupProcess(<String>[
690 691 692
      'test',
      '--disable-service-auth-codes',
      '--machine',
693 694
      if (coverage)
        '--coverage',
695
    ], script: testFile, withDebugger: withDebugger, pauseOnExceptions: pauseOnExceptions, pidFile: pidFile, beforeStart: beforeStart);
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
  }

  @override
  Future<void> _setupProcess(
    List<String> args, {
    String script,
    bool withDebugger = false,
    bool pauseOnExceptions = false,
    File pidFile,
    Future<void> Function() beforeStart,
  }) async {
    await super._setupProcess(
      args,
      script: script,
      withDebugger: withDebugger,
      pidFile: pidFile,
    );

    // Stash the PID so that we can terminate the VM more reliably than using
    // _proc.kill() (because _proc is a shell, because `flutter` is a shell
    // script).
    final Map<String, dynamic> version = await _waitForJson();
718
    _processPid = version['pid'] as int;
719 720 721

    if (withDebugger) {
      final Map<String, dynamic> startedProcess = await _waitFor(event: 'test.startedProcess', timeout: appStartTimeout);
722
      final String vmServiceHttpString = startedProcess['params']['observatoryUri'] as String;
723 724 725 726 727 728
      _vmServiceWsUri = Uri.parse(vmServiceHttpString).replace(scheme: 'ws', path: '/ws');
      await connectToVmService(pauseOnExceptions: pauseOnExceptions);
      // Allow us to run code before we start, eg. to set up breakpoints.
      if (beforeStart != null) {
        await beforeStart();
      }
729
      await resume(waitForNextPause: false);
730 731 732 733
    }
  }

  Future<Map<String, dynamic>> _waitForJson({
734
    Duration timeout = defaultTimeout,
735
  }) async {
736
    assert(timeout != null);
737
    return _timeoutWithMessages<Map<String, dynamic>>(
738 739
      () => _stdout.stream.map<Map<String, dynamic>>(_parseJsonResponse)
          .firstWhere((Map<String, dynamic> output) => output != null),
740
      timeout: timeout,
741
      task: 'Waiting for JSON',
742 743 744 745 746
    );
  }

  Map<String, dynamic> _parseJsonResponse(String line) {
    try {
747
      return castStringKeyedMap(json.decode(line));
748
    } on Exception {
749 750 751 752
      // Not valid JSON, so likely some other output.
      return null;
    }
  }
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

  Future<void> waitForCompletion() async {
    final Completer<bool> done = Completer<bool>();
    // Waiting for `{"success":true,"type":"done",...}` line indicating
    // end of test run.
    final StreamSubscription<String> subscription = _stdout.stream.listen(
        (String line) async {
          final Map<String, dynamic> json = _parseJsonResponse(line);
          if (json != null && json['type'] != null && json['success'] != null) {
            done.complete(json['type'] == 'done' && json['success'] == true);
          }
        });

    await resume();

    final Future<dynamic> timeoutFuture =
        Future<dynamic>.delayed(defaultTimeout);
    await Future.any<dynamic>(<Future<dynamic>>[done.future, timeoutFuture]);
    await subscription.cancel();
    if (!done.isCompleted) {
      await quit();
    }
  }
776 777
}

778
Stream<String> transformToLines(Stream<List<int>> byteStream) {
779
  return byteStream.transform<String>(utf8.decoder).transform<String>(const LineSplitter());
780
}
781

782 783 784
Map<String, dynamic> parseFlutterResponse(String line) {
  if (line.startsWith('[') && line.endsWith(']')) {
    try {
785
      final Map<String, dynamic> response = castStringKeyedMap(json.decode(line)[0]);
786
      return response;
787
    } on Exception {
788 789 790 791 792 793 794
      // Not valid JSON, so likely some other output that was surrounded by [brackets]
      return null;
    }
  }
  return null;
}

795 796 797 798 799 800
class SourcePosition {
  SourcePosition(this.line, this.column);

  final int line;
  final int column;
}