flutter_test_driver.dart 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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';
12 13
import 'package:source_span/source_span.dart';
import 'package:stream_channel/stream_channel.dart';
14
import 'package:vm_service_client/vm_service_client.dart';
15
import 'package:web_socket_channel/io.dart';
16 17 18 19 20

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

// Set this to true for debugging to get JSON written to stdout.
const bool _printJsonAndStderr = false;
21 22 23 24 25 26 27 28 29
const Duration defaultTimeout = const Duration(seconds: 20);

String debugPrint(String msg) {
  const int maxLength = 200;
  if (_printJsonAndStderr) {
    print(msg.length > maxLength ? msg.substring(0, maxLength) + '...' : msg);
  }
  return msg;
}
30 31 32 33 34 35 36

class FlutterTestDriver {
  Directory _projectFolder;
  Process _proc;
  final StreamController<String> _stdout = new StreamController<String>.broadcast();
  final StreamController<String> _stderr = new StreamController<String>.broadcast();
  final StringBuffer _errorBuffer = new StringBuffer();
37
  String _lastResponse;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  String _currentRunningAppId;

  FlutterTestDriver(this._projectFolder);

  VMServiceClient vmService;
  String get lastErrorInfo => _errorBuffer.toString();

  // TODO(dantup): Is there a better way than spawning a proc? This breaks debugging..
  // However, there's a lot of logic inside RunCommand that wouldn't be good
  // to duplicate here.
  Future<void> run({bool withDebugger = false}) async {
    _proc = await _runFlutter(_projectFolder);
    _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);

    // This is just debug printing to aid running/debugging tests locally.
57 58
    _stdout.stream.listen(debugPrint);
    _stderr.stream.listen(debugPrint);
59 60 61 62 63 64 65

    // 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');

    if (withDebugger) {
      final Future<Map<String, dynamic>> debugPort = _waitFor(event: 'app.debugPort');
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      final String wsUriString = (await debugPort)['params']['wsUri'];
      final Uri uri = Uri.parse(wsUriString);
      // Proxy the stream/sink for the VM Client so we can debugPrint it.
      final StreamChannel<String> channel = new IOWebSocketChannel.connect(uri)
          .cast<String>()
          .changeStream((Stream<String> stream) => stream.map(debugPrint))
          .changeSink((StreamSink<String> sink) =>
              new StreamController<String>()
                ..stream.listen((String s) => sink.add(debugPrint(s))));
      vmService = new VMServiceClient(channel);

      // Because we start paused, resume so the app is in a "running" state as
      // expected by tests. Tests will reload/restart as required if they need
      // to hit breakpoints, etc. 
      await waitForPause();
      await resume(wait: false);
82 83 84 85 86 87 88
    }

    // Now await the started event; if it had already happened the future will
    // have already completed.
    _currentRunningAppId = (await started)['params']['appId'];
  }

89 90 91 92
  Future<void> hotRestart({bool pause = false}) => _restart(fullRestart: true, pause: pause);
  Future<void> hotReload() => _restart(fullRestart: false);

  Future<void> _restart({bool fullRestart = false, bool pause = false}) async {
93 94 95 96 97
    if (_currentRunningAppId == null)
      throw new Exception('App has not started yet');

    final dynamic hotReloadResp = await _sendRequest(
        'app.restart',
98
        <String, dynamic>{'appId': _currentRunningAppId, 'fullRestart': fullRestart, 'pause': pause}
99 100 101
    );

    if (hotReloadResp == null || hotReloadResp['code'] != 0)
102
      _throwErrorResponse('Hot ${fullRestart ? 'restart' : 'reload'} request failed');
103 104 105
  }

  Future<int> stop() async {
106 107 108
    if (vmService != null) {
      await vmService.close();
    }
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    if (_currentRunningAppId != null) {
      await _sendRequest(
          'app.stop',
          <String, dynamic>{'appId': _currentRunningAppId}
      );
    }
    _currentRunningAppId = null;
    return _proc.exitCode;
  }

  Future<Process> _runFlutter(Directory projectDir) async {
    final String flutterBin = fs.path.join(getFlutterRoot(), 'bin', 'flutter');
    final List<String> command = <String>[
        flutterBin,
        'run',
        '--machine',
        '-d',
        'flutter-tester',
127
        '--start-paused',
128
    ];
129 130
    debugPrint('Spawning $command in ${projectDir.path}');
    
131 132 133 134 135 136 137 138 139 140 141
    const ProcessManager _processManager = const LocalProcessManager();
    return _processManager.start(
        command,
        workingDirectory: projectDir.path,
        environment: <String, String>{'FLUTTER_TEST': 'true'}
    );
  }

  Future<void> addBreakpoint(String path, int line) async {
    final VM vm = await vmService.getVM();
    final VMIsolate isolate = await vm.isolates.first.load();
142
    debugPrint('Sending breakpoint for $path:$line');
143 144 145
    await isolate.addBreakpoint(path, line);
  }

146
  Future<VMIsolate> waitForPause() async {
147 148
    final VM vm = await vmService.getVM();
    final VMIsolate isolate = await vm.isolates.first.load();
149 150 151
    debugPrint('Waiting for isolate to pause');
    await isolate.waitUntilPaused()
        .timeout(defaultTimeout, onTimeout: () => throw 'Isolate did not pause');
152 153 154
    return isolate.load();
  }

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  Future<VMIsolate> resume({ bool wait = true }) => _resume(wait: wait);
  Future<VMIsolate> stepOver({ bool wait = true }) => _resume(step: VMStep.over, wait: wait);
  Future<VMIsolate> stepInto({ bool wait = true }) => _resume(step: VMStep.into, wait: wait);
  Future<VMIsolate> stepOut({ bool wait = true }) => _resume(step: VMStep.out, wait: wait);

  Future<VMIsolate> _resume({VMStep step, bool wait = true}) async {
    final VM vm = await vmService.getVM();
    final VMIsolate isolate = await vm.isolates.first.load();
    debugPrint('Sending resume ($step)');
    await isolate.resume(step: step)
        .timeout(defaultTimeout, onTimeout: () => throw 'Isolate did not respond to resume ($step)');
    return wait ? waitForPause() : null;
  }

  Future<VMIsolate> breakAt(String path, int line, { bool restart = false }) async {
    if (restart) {
      // For a hot restart, we need to send the breakpoints after the restart
      // so we need to pause during the restart to avoid races.
      await hotRestart(pause: true);
      await addBreakpoint(path, line);
      return resume();
    } else {
      await addBreakpoint(path, line);
      await hotReload();
      return waitForPause();
    }
181 182 183
  }

  Future<VMInstanceRef> evaluateExpression(String expression) async {
184 185 186 187 188 189
    final VMFrame topFrame = await getTopStackFrame();
    return topFrame.evaluate(expression)
        .timeout(defaultTimeout, onTimeout: () => throw 'Timed out evaluating expression ($expression)');
  }

  Future<VMFrame> getTopStackFrame() async {
190 191 192 193
    final VM vm = await vmService.getVM();
    final VMIsolate isolate = await vm.isolates.first.load();
    final VMStack stack = await isolate.getStack();
    if (stack.frames.isEmpty) {
194
      throw new Exception('Stack is empty');
195
    }
196 197 198 199 200 201 202
    return stack.frames.first;
  }

  Future<FileLocation> getSourceLocation() async {
    final VMFrame frame = await getTopStackFrame();
    final VMScript script = await frame.location.script.load();
    return script.sourceLocation(frame.location.token);
203 204 205 206
  }

  Future<Map<String, dynamic>> _waitFor({String event, int id}) async {
    // Capture output to a buffer so if we don't get the repsonse we want we can show
207
    // the output that did arrive in the timeout error.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    final StringBuffer messages = new StringBuffer();
    _stdout.stream.listen(messages.writeln);
    _stderr.stream.listen(messages.writeln);

    final Completer<Map<String, dynamic>> response = new Completer<Map<String, dynamic>>();
    final StreamSubscription<String> sub = _stdout.stream.listen((String line) {
      final dynamic json = _parseFlutterResponse(line);
      if (json == null) {
        return;
      } else if (
          (event != null && json['event'] == event)
          || (id != null && json['id'] == id)) {
        response.complete(json);
      }
    });
    
224
    return response.future.timeout(defaultTimeout, onTimeout: () {
225
          if (event != null)
226
            throw 'Did not receive expected $event event.\nDid get:\n${messages.toString()}';
227
          else if (id != null)
228 229
            throw 'Did not receive response to request "$id".\nDid get:\n${messages.toString()}';
        }).whenComplete(() => sub.cancel());
230 231 232 233 234
  }

  Map<String, dynamic> _parseFlutterResponse(String line) {
    if (line.startsWith('[') && line.endsWith(']')) {
      try {
235 236 237
        final Map<String, dynamic> resp = json.decode(line)[0];
        _lastResponse = line;
        return resp;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
      } catch (e) {
        // Not valid JSON, so likely some other output that was surrounded by [brackets]
        return null;
      }
    }
    return null;
  }

  int id = 1;
  Future<dynamic> _sendRequest(String method, dynamic params) async {
    final int requestId = id++;
    final Map<String, dynamic> req = <String, dynamic>{
      'id': requestId,
      'method': method,
      'params': params
    };
    final String jsonEncoded = json.encode(<Map<String, dynamic>>[req]);
255 256
    debugPrint(jsonEncoded);

257 258 259 260 261 262 263
    // Set up the response future before we send the request to avoid any
    // races.
    final Future<Map<String, dynamic>> responseFuture = _waitFor(id: requestId);
    _proc.stdin.writeln(jsonEncoded);
    final Map<String, dynamic> resp = await responseFuture;

    if (resp['error'] != null || resp['result'] == null)
264
      _throwErrorResponse('Unexpected error response');
265 266 267 268

    return resp['result'];
  }

269 270 271
  void _throwErrorResponse(String msg) {
    throw '$msg\n\n$_lastResponse\n\n${_errorBuffer.toString()}'.trim();
  }
272 273 274 275 276
}

Stream<String> _transformToLines(Stream<List<int>> byteStream) {
  return byteStream.transform(utf8.decoder).transform(const LineSplitter());
}