flutter_platform.dart 21.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8
import 'dart:async';

9
import 'package:meta/meta.dart';
10
import 'package:package_config/package_config.dart';
11
import 'package:stream_channel/stream_channel.dart';
12
import 'package:test_core/src/platform.dart'; // ignore: implementation_imports
13

14
import '../base/common.dart';
15
import '../base/file_system.dart';
16
import '../base/io.dart';
17
import '../compile.dart';
18
import '../convert.dart';
19
import '../dart/language_version.dart';
20
import '../device.dart';
21
import '../globals.dart' as globals;
22
import '../project.dart';
23
import '../test/test_wrapper.dart';
24 25 26

import 'flutter_tester_device.dart';
import 'font_config_manager.dart';
27
import 'test_compiler.dart';
28
import 'test_config.dart';
29
import 'test_device.dart';
30
import 'watcher.dart';
31

32 33
/// The address at which our WebSocket server resides and at which the sky_shell
/// processes will host the Observatory server.
34
final Map<InternetAddressType, InternetAddress> _kHosts = <InternetAddressType, InternetAddress>{
35 36
  InternetAddressType.IPv4: InternetAddress.loopbackIPv4,
  InternetAddressType.IPv6: InternetAddress.loopbackIPv6,
37
};
38

39 40
typedef PlatformPluginRegistration = void Function(FlutterPlatform platform);

41 42
/// Configure the `test` package to work with Flutter.
///
43
/// On systems where each [FlutterPlatform] is only used to run one test suite
44
/// (that is, one Dart file with a `*_test.dart` file name and a single `void
45
/// main()`), you can set an observatory port explicitly.
46
FlutterPlatform installHook({
47
  TestWrapper testWrapper = const TestWrapper(),
48
  @required String shellPath,
49
  @required DebuggingOptions debuggingOptions,
50
  TestWatcher watcher,
51 52
  bool enableObservatory = false,
  bool machine = false,
53
  String precompiledDillPath,
54
  Map<String, String> precompiledDillFiles,
55
  bool updateGoldens = false,
56
  bool buildTestAssets = false,
57
  InternetAddressType serverType = InternetAddressType.IPv4,
58
  Uri projectRootDirectory,
59
  FlutterProject flutterProject,
60
  String icudtlPath,
61
  PlatformPluginRegistration platformPluginRegistration,
62
}) {
63
  assert(testWrapper != null);
64
  assert(enableObservatory || (!debuggingOptions.startPaused && debuggingOptions.hostVmServicePort == null));
65 66 67

  // registerPlatformPlugin can be injected for testing since it's not very mock-friendly.
  platformPluginRegistration ??= (FlutterPlatform platform) {
68
    testWrapper.registerPlatformPlugin(
69
      <Runtime>[Runtime.vm],
70
      () {
71
        return platform;
72
      },
73 74 75 76
    );
  };
  final FlutterPlatform platform = FlutterPlatform(
    shellPath: shellPath,
77
    debuggingOptions: debuggingOptions,
78 79 80 81 82 83 84 85 86 87 88
    watcher: watcher,
    machine: machine,
    enableObservatory: enableObservatory,
    host: _kHosts[serverType],
    precompiledDillPath: precompiledDillPath,
    precompiledDillFiles: precompiledDillFiles,
    updateGoldens: updateGoldens,
    buildTestAssets: buildTestAssets,
    projectRootDirectory: projectRootDirectory,
    flutterProject: flutterProject,
    icudtlPath: icudtlPath,
89
  );
90 91
  platformPluginRegistration(platform);
  return platform;
92 93
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/// Generates the bootstrap entry point script that will be used to launch an
/// individual test file.
///
/// The [testUrl] argument specifies the path to the test file that is being
/// launched.
///
/// The [host] argument specifies the address at which the test harness is
/// running.
///
/// If [testConfigFile] is specified, it must follow the conventions of test
/// configuration files as outlined in the [flutter_test] library. By default,
/// the test file will be launched directly.
///
/// The [updateGoldens] argument will set the [autoUpdateGoldens] global
/// variable in the [flutter_test] package before invoking the test.
109 110
// NOTE: this API is used by the fuchsia source tree, do not add new
// required or position parameters.
111 112 113 114
String generateTestBootstrap({
  @required Uri testUrl,
  @required InternetAddress host,
  File testConfigFile,
115
  bool updateGoldens = false,
116
  String languageVersionHeader = '',
117
  bool nullSafety = false,
118
  bool flutterTestDep = true,
119 120 121 122 123
}) {
  assert(testUrl != null);
  assert(host != null);
  assert(updateGoldens != null);

124
  final String websocketUrl = host.type == InternetAddressType.IPv4
125 126 127 128
      ? 'ws://${host.address}'
      : 'ws://[${host.address}]';
  final String encodedWebsocketUrl = Uri.encodeComponent(websocketUrl);

129
  final StringBuffer buffer = StringBuffer();
130
  buffer.write('''
131
$languageVersionHeader
132
import 'dart:async';
133 134
import 'dart:convert';  // ignore: dart_convert_import
import 'dart:io';  // ignore: dart_io_import
135
import 'dart:isolate';
136 137 138
''');
  if (flutterTestDep) {
    buffer.write('''
139
import 'package:flutter_test/flutter_test.dart';
140 141 142
''');
  }
  buffer.write('''
143
import 'package:test_api/src/remote_listener.dart';
144
import 'package:stream_channel/stream_channel.dart';
145
import 'package:stack_trace/stack_trace.dart';
146 147

import '$testUrl' as test;
148
''');
149 150
  if (testConfigFile != null) {
    buffer.write('''
151
import '${Uri.file(testConfigFile.path)}' as test_config;
152
''');
153 154 155
  }
  buffer.write('''

156
/// Returns a serialized test suite.
157 158
StreamChannel<dynamic> serializeSuite(Function getMain()) {
  return RemoteListener.start(getMain);
159 160 161 162 163 164 165 166 167 168 169 170
}

/// Capture any top-level errors (mostly lazy syntax errors, since other are
/// caught below) and report them to the parent isolate.
void catchIsolateErrors() {
  final ReceivePort errorPort = ReceivePort();
  // Treat errors non-fatal because otherwise they'll be double-printed.
  Isolate.current.setErrorsFatal(false);
  Isolate.current.addErrorListener(errorPort.sendPort);
  errorPort.listen((dynamic message) {
    // Masquerade as an IsolateSpawnException because that's what this would
    // be if the error had been detected statically.
171 172 173 174
    final IsolateSpawnException error = IsolateSpawnException(
        message[0] as String);
    final Trace stackTrace = message[1] == null ?
        Trace(const <Frame>[]) : Trace.parse(message[1] as String);
175 176 177 178
    Zone.current.handleUncaughtError(error, stackTrace);
  });
}

179
void main() {
180
  String serverPort = Platform.environment['SERVER_PORT'] ?? '';
181
  String server = Uri.decodeComponent('$encodedWebsocketUrl:\$serverPort');
182
  StreamChannel<dynamic> testChannel = serializeSuite(() {
183
    catchIsolateErrors();
184
''');
185 186
  if (flutterTestDep) {
    buffer.write('''
187 188
    goldenFileComparator = LocalFileComparator(Uri.parse('$testUrl'));
    autoUpdateGoldenFiles = $updateGoldens;
189 190
''');
  }
191 192
  if (testConfigFile != null) {
    buffer.write('''
193
    return () => test_config.testExecutable(test.main);
194 195 196 197 198 199 200 201 202
''');
  } else {
    buffer.write('''
    return test.main;
''');
  }
  buffer.write('''
  });
  WebSocket.connect(server).then((WebSocket socket) {
203 204 205 206 207
    socket.map((dynamic message) {
      // We're only communicating with string encoded JSON.
      return json.decode(message as String);
    }).pipe(testChannel.sink);
    socket.addStream(testChannel.stream.map(json.encode));
208 209
  });
}
210
''');
211 212 213
  return buffer.toString();
}

214
typedef Finalizer = Future<void> Function();
215

216 217 218
/// The flutter test platform used to integrate with package:test.
class FlutterPlatform extends PlatformPlugin {
  FlutterPlatform({
219
    @required this.shellPath,
220
    @required this.debuggingOptions,
221 222 223 224 225
    this.watcher,
    this.enableObservatory,
    this.machine,
    this.host,
    this.precompiledDillPath,
226
    this.precompiledDillFiles,
227
    this.updateGoldens,
228
    this.buildTestAssets,
229
    this.projectRootDirectory,
230
    this.flutterProject,
231
    this.icudtlPath,
232 233
  }) : assert(shellPath != null);

234
  final String shellPath;
235
  final DebuggingOptions debuggingOptions;
236 237
  final TestWatcher watcher;
  final bool enableObservatory;
238
  final bool machine;
239
  final InternetAddress host;
240
  final String precompiledDillPath;
241
  final Map<String, String> precompiledDillFiles;
242
  final bool updateGoldens;
243
  final bool buildTestAssets;
244
  final Uri projectRootDirectory;
245
  final FlutterProject flutterProject;
246
  final String icudtlPath;
247

248
  final FontConfigManager _fontConfigManager = FontConfigManager();
249 250 251

  /// The test compiler produces dill files for each test main.
  ///
252
  /// To speed up compilation, each compile is initialized from an existing
253 254
  /// dill file from previous runs, if possible.
  TestCompiler compiler;
255

256 257 258 259 260 261 262
  // Each time loadChannel() is called, we spin up a local WebSocket server,
  // then spin up the engine in a subprocess. We pass the engine a Dart file
  // that connects to our WebSocket server, then we proxy JSON messages from
  // the test harness to the engine and back again. If at any time the engine
  // crashes, we inject an error into that stream. When the process closes,
  // we clean everything up.

263 264
  int _testCount = 0;

265 266 267 268 269 270 271 272 273
  @override
  Future<RunnerSuite> load(
    String path,
    SuitePlatform platform,
    SuiteConfiguration suiteConfig,
    Object message,
  ) async {
    // loadChannel may throw an exception. That's fine; it will cause the
    // LoadSuite to emit an error, which will be presented to the user.
274 275
    // Except for the Declarer error, which is a specific test incompatibility
    // error we need to catch.
276 277 278 279
    final StreamChannel<dynamic> channel = loadChannel(path, platform);
    final RunnerSuiteController controller = deserializeSuite(path, platform,
      suiteConfig, const PluginEnvironment(), channel, message);
    return controller.suite;
280 281
  }

282
  @override
283
  StreamChannel<dynamic> loadChannel(String path, SuitePlatform platform) {
284 285
    if (_testCount > 0) {
      // Fail if there will be a port conflict.
286
      if (debuggingOptions.hostVmServicePort != null) {
287
        throwToolExit('installHook() was called with an observatory port or debugger mode enabled, but then more than one test suite was run.');
288
      }
289
      // Fail if we're passing in a precompiled entry-point.
290
      if (precompiledDillPath != null) {
291
        throwToolExit('installHook() was called with a precompiled test entry-point, but then more than one test suite was run.');
292
      }
293
    }
294

295
    final int ourTestCount = _testCount;
296
    _testCount += 1;
297 298 299 300
    final StreamController<dynamic> localController = StreamController<dynamic>();
    final StreamController<dynamic> remoteController = StreamController<dynamic>();
    final Completer<_AsyncError> testCompleteCompleter = Completer<_AsyncError>();
    final _FlutterPlatformStreamSinkWrapper<dynamic> remoteSink = _FlutterPlatformStreamSinkWrapper<dynamic>(
301 302 303
      remoteController.sink,
      testCompleteCompleter.future,
    );
304
    final StreamChannel<dynamic> localChannel = StreamChannel<dynamic>.withGuarantees(
305 306 307
      remoteController.stream,
      localController.sink,
    );
308
    final StreamChannel<dynamic> remoteChannel = StreamChannel<dynamic>.withGuarantees(
309 310 311
      localController.stream,
      remoteSink,
    );
312
    testCompleteCompleter.complete(_startTest(path, localChannel, ourTestCount));
313
    return remoteChannel;
314
  }
315

316 317 318 319 320 321 322 323 324
  Future<String> _compileExpressionService(
    String isolateId,
    String expression,
    List<String> definitions,
    List<String> typeDefinitions,
    String libraryUri,
    String klass,
    bool isStatic,
  ) async {
325 326 327 328 329 330
    if (compiler == null || compiler.compiler == null) {
      throw 'Compiler is not set up properly to compile $expression';
    }
    final CompilerOutput compilerOutput =
      await compiler.compiler.compileExpression(expression, definitions,
        typeDefinitions, libraryUri, klass, isStatic);
331 332
    if (compilerOutput != null && compilerOutput.expressionData != null) {
      return base64.encode(compilerOutput.expressionData);
333 334 335 336
    }
    throw 'Failed to compile $expression';
  }

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
  TestDevice _createTestDevice(int ourTestCount) {
    return FlutterTesterTestDevice(
      id: ourTestCount,
      platform: globals.platform,
      fileSystem: globals.fs,
      processManager: globals.processManager,
      logger: globals.logger,
      shellPath: shellPath,
      enableObservatory: enableObservatory,
      machine: machine,
      debuggingOptions: debuggingOptions,
      host: host,
      buildTestAssets: buildTestAssets,
      flutterProject: flutterProject,
      icudtlPath: icudtlPath,
      compileExpression: _compileExpressionService,
      fontConfigManager: _fontConfigManager
    );
  }
356

357
  Future<_AsyncError> _startTest(
358
    String testPath,
359
    StreamChannel<dynamic> testHarnessChannel,
360 361
    int ourTestCount,
  ) async {
362
    globals.printTrace('test $ourTestCount: starting test $testPath');
363

364
    _AsyncError outOfBandError; // error that we couldn't send to the harness that we need to send via our future
365

366
    final List<Finalizer> finalizers = <Finalizer>[]; // Will be run in reverse order.
367 368
    bool controllerSinkClosed = false;
    try {
369
      // Callback can't throw since it's just setting a variable.
370
      unawaited(testHarnessChannel.sink.done.whenComplete(() {
371
        controllerSinkClosed = true;
372
      }));
373

374
      // If a kernel file is given, then use that to launch the test.
375 376 377 378 379 380 381 382
      // If mapping is provided, look kernel file from mapping.
      // If all fails, create a "listener" dart that invokes actual test.
      String mainDart;
      if (precompiledDillPath != null) {
        mainDart = precompiledDillPath;
      } else if (precompiledDillFiles != null) {
        mainDart = precompiledDillFiles[testPath];
      }
383
      mainDart ??= _createListenerDart(finalizers, ourTestCount, testPath);
384

385
      if (precompiledDillPath == null && precompiledDillFiles == null) {
386
        // Lazily instantiate compiler so it is built only if it is actually used.
387
        compiler ??= TestCompiler(debuggingOptions.buildInfo, flutterProject);
388
        mainDart = await compiler.compile(globals.fs.file(mainDart).uri);
389 390

        if (mainDart == null) {
391
          testHarnessChannel.sink.addError('Compilation failed for testPath=$testPath');
392
          return null;
393
        }
394
      }
395

396 397 398 399 400
      globals.printTrace('test $ourTestCount: starting test device');

      final TestDevice testDevice = _createTestDevice(ourTestCount);
      final Future<StreamChannel<String>> remoteChannelFuture = testDevice.start(
        compiledEntrypointPath: mainDart,
401 402
      );
      finalizers.add(() async {
403 404
        globals.printTrace('test $ourTestCount: ensuring test device is terminated.');
        await testDevice.kill();
405
      });
406

407 408 409 410 411 412 413 414 415 416
      // At this point, these things can happen:
      // A. The test device could crash, in which case [testDevice.finished]
      // will complete.
      // B. The test device could connect to us, in which case
      // [remoteChannelFuture] will complete.
      globals.printTrace('test $ourTestCount: awaiting connection to test device');
      await Future.any<void>(<Future<void>>[
        testDevice.finished,
        () async {
          final Uri processObservatoryUri = await testDevice.observatoryUri;
417 418
          if (processObservatoryUri != null) {
            globals.printTrace('test $ourTestCount: Observatory uri is available at $processObservatoryUri');
419 420
          } else {
            globals.printTrace('test $ourTestCount: Observatory uri is not available');
421
          }
422
          watcher?.handleStartedDevice(processObservatoryUri);
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
          final StreamChannel<String> remoteChannel = await remoteChannelFuture;
          globals.printTrace('test $ourTestCount: connected to test device, now awaiting test result');

          await _pipeHarnessToRemote(
            id: ourTestCount,
            harnessChannel: testHarnessChannel,
            remoteChannel: remoteChannel,
          );

          globals.printTrace('test $ourTestCount: finished');
          await watcher?.handleFinishedTest(testDevice);
        }()
      ]);
    } on Exception catch (error, stackTrace) {
      Object reportedError = error;
      StackTrace reportedStackTrace = stackTrace;
      if (error is TestDeviceException) {
        reportedError = error.message;
        reportedStackTrace = error.stackTrace;
443
      }
444

445
      globals.printTrace('test $ourTestCount: error caught during test; ${controllerSinkClosed ? "reporting to console" : "sending to test framework"}');
446
      if (!controllerSinkClosed) {
447
        testHarnessChannel.sink.addError(reportedError, reportedStackTrace);
448
      } else {
449 450
        globals.printError('unhandled error during test:\n$testPath\n$reportedError\n$reportedStackTrace');
        outOfBandError ??= _AsyncError(reportedError, reportedStackTrace);
451 452
      }
    } finally {
453
      globals.printTrace('test $ourTestCount: cleaning up...');
454
      // Finalizers are treated like a stack; run them in reverse order.
455
      for (final Finalizer finalizer in finalizers.reversed) {
456 457
        try {
          await finalizer();
458
        } on Exception catch (error, stack) {
459
          globals.printTrace('test $ourTestCount: error while cleaning up; ${controllerSinkClosed ? "reporting to console" : "sending to test framework"}');
460
          if (!controllerSinkClosed) {
461
            testHarnessChannel.sink.addError(error, stack);
462
          } else {
463
            globals.printError('unhandled error during finalization of test:\n$testPath\n$error\n$stack');
464
            outOfBandError ??= _AsyncError(error, stack);
465 466 467
          }
        }
      }
468
      if (!controllerSinkClosed) {
469
        // Waiting below with await.
470
        unawaited(testHarnessChannel.sink.close());
471
        globals.printTrace('test $ourTestCount: waiting for controller sink to close');
472
        await testHarnessChannel.sink.done;
473 474 475
      }
    }
    assert(controllerSinkClosed);
476
    if (outOfBandError != null) {
477
      globals.printTrace('test $ourTestCount: finished with out-of-band failure');
478
    } else {
479
      globals.printTrace('test $ourTestCount: finished');
480
    }
481
    return outOfBandError;
482 483
  }

484
  String _createListenerDart(
485
    List<Finalizer> finalizers,
486 487 488
    int ourTestCount,
    String testPath,
  ) {
489
    // Prepare a temporary directory to store the Dart file that will talk to us.
490
    final Directory tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_test_listener.');
491
    finalizers.add(() async {
492
      globals.printTrace('test $ourTestCount: deleting temporary directory');
493
      tempDir.deleteSync(recursive: true);
494 495 496
    });

    // Prepare the Dart file that will talk to us and start the test.
497
    final File listenerFile = globals.fs.file('${tempDir.path}/listener.dart');
498 499
    listenerFile.createSync();
    listenerFile.writeAsStringSync(_generateTestMain(
500
      testUrl: globals.fs.path.toUri(globals.fs.path.absolute(testPath)),
501 502 503 504
    ));
    return listenerFile.path;
  }

505
  String _generateTestMain({
506
    Uri testUrl,
507
  }) {
508
    assert(testUrl.scheme == 'file');
509
    final File file = globals.fs.file(testUrl);
510 511
    final PackageConfig packageConfig = debuggingOptions.buildInfo.packageConfig;

512 513
    final LanguageVersion languageVersion = determineLanguageVersion(
      file,
514
      packageConfig[flutterProject?.manifest?.appName],
515
    );
516 517
    return generateTestBootstrap(
      testUrl: testUrl,
518
      testConfigFile: findTestConfigFile(globals.fs.file(testUrl)),
519 520
      host: host,
      updateGoldens: updateGoldens,
521
      flutterTestDep: packageConfig['flutter_test'] != null,
522
      languageVersionHeader: '// @dart=${languageVersion.major}.${languageVersion.minor}'
523
    );
524 525
  }

526 527 528
  @override
  Future<dynamic> close() async {
    if (compiler != null) {
529
      await compiler.dispose();
530 531
      compiler = null;
    }
532
    await _fontConfigManager.dispose();
533
  }
534
}
535

536 537 538 539 540 541 542 543 544 545 546 547 548
// The [_shellProcessClosed] future can't have errors thrown on it because it
// crosses zones (it's fed in a zone created by the test package, but listened
// to by a parent zone, the same zone that calls [close] below).
//
// This is because Dart won't let errors that were fed into a Future in one zone
// propagate to listeners in another zone. (Specifically, the zone in which the
// future was completed with the error, and the zone in which the listener was
// registered, are what matters.)
//
// Because of this, the [_shellProcessClosed] future takes an [_AsyncError]
// object as a result. If it's null, it's as if it had completed correctly; if
// it's non-null, it contains the error and stack trace of the actual error, as
// if it had completed with that error.
549 550
class _FlutterPlatformStreamSinkWrapper<S> implements StreamSink<S> {
  _FlutterPlatformStreamSinkWrapper(this._parent, this._shellProcessClosed);
551

552
  final StreamSink<S> _parent;
553
  final Future<_AsyncError> _shellProcessClosed;
554 555

  @override
556
  Future<void> get done => _done.future;
557
  final Completer<void> _done = Completer<void>();
558 559 560

  @override
  Future<dynamic> close() {
561
    Future.wait<dynamic>(<Future<dynamic>>[
562 563
      _parent.close(),
      _shellProcessClosed,
564
    ]).then<void>(
565 566 567
      (List<dynamic> futureResults) {
        assert(futureResults.length == 2);
        assert(futureResults.first == null);
568 569 570
        final dynamic lastResult = futureResults.last;
        if (lastResult is _AsyncError) {
          _done.completeError(lastResult.error, lastResult.stack);
571
        } else {
572
          assert(lastResult == null);
573 574
          _done.complete();
        }
575
      },
576
      onError: _done.completeError,
577 578 579 580 581 582 583
    );
    return done;
  }

  @override
  void add(S event) => _parent.add(event);
  @override
584
  void addError(dynamic errorEvent, [ StackTrace stackTrace ]) => _parent.addError(errorEvent, stackTrace);
585 586 587
  @override
  Future<dynamic> addStream(Stream<S> stream) => _parent.addStream(stream);
}
588 589 590 591 592 593

@immutable
class _AsyncError {
  const _AsyncError(this.error, this.stack);
  final dynamic error;
  final StackTrace stack;
594
}
595

596
/// Bridges the package:test harness and the remote device.
597
///
598 599 600 601 602 603
/// The returned future completes when either side is closed, which also
/// indicates when the tests have finished.
Future<void> _pipeHarnessToRemote({
  @required int id,
  @required StreamChannel<dynamic> harnessChannel,
  @required StreamChannel<String> remoteChannel,
604
}) async {
605
  globals.printTrace('test $id: Waiting for test harness or tests to finish');
606 607

  await Future.any<void>(<Future<void>>[
608 609 610 611 612 613 614 615 616 617 618 619
    harnessChannel.stream
      .map<String>(json.encode)
      .pipe(remoteChannel.sink)
      .then<void>((void value) {
        globals.printTrace('test $id: Test process is no longer needed by test harness');
      }),
    remoteChannel.stream
      .map<dynamic>(json.decode)
      .pipe(harnessChannel.sink)
      .then<void>((void value) {
        globals.printTrace('test $id: Test harness is no longer needed by test process');
      }),
620 621
  ]);
}