|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:pedantic/pedantic.dart'; |
| 10 | +import 'package:stack_trace/stack_trace.dart'; |
| 11 | +import 'package:typed_data/typed_buffers.dart'; |
| 12 | + |
| 13 | +import 'package:test_api/src/utils.dart'; // ignore: implementation_imports |
| 14 | + |
| 15 | +/// An interface for running browser instances. |
| 16 | +/// |
| 17 | +/// This is intentionally coarse-grained: browsers are controlled primary from |
| 18 | +/// inside a single tab. Thus this interface only provides support for closing |
| 19 | +/// the browser and seeing if it closes itself. |
| 20 | +/// |
| 21 | +/// Any errors starting or running the browser process are reported through |
| 22 | +/// [onExit]. |
| 23 | +abstract class Browser { |
| 24 | + String get name; |
| 25 | + |
| 26 | + /// The Observatory URL for this browser. |
| 27 | + /// |
| 28 | + /// This will return `null` for browsers that aren't running the Dart VM, or |
| 29 | + /// if the Observatory URL can't be found. |
| 30 | + Future<Uri> get observatoryUrl => null; |
| 31 | + |
| 32 | + /// The remote debugger URL for this browser. |
| 33 | + /// |
| 34 | + /// This will return `null` for browsers that don't support remote debugging, |
| 35 | + /// or if the remote debugging URL can't be found. |
| 36 | + Future<Uri> get remoteDebuggerUrl => null; |
| 37 | + |
| 38 | + /// The underlying process. |
| 39 | + /// |
| 40 | + /// This will fire once the process has started successfully. |
| 41 | + Future<Process> get _process => _processCompleter.future; |
| 42 | + final _processCompleter = Completer<Process>(); |
| 43 | + |
| 44 | + /// Whether [close] has been called. |
| 45 | + var _closed = false; |
| 46 | + |
| 47 | + /// A future that completes when the browser exits. |
| 48 | + /// |
| 49 | + /// If there's a problem starting or running the browser, this will complete |
| 50 | + /// with an error. |
| 51 | + Future get onExit => _onExitCompleter.future; |
| 52 | + final _onExitCompleter = Completer(); |
| 53 | + |
| 54 | + /// Standard IO streams for the underlying browser process. |
| 55 | + final _ioSubscriptions = <StreamSubscription>[]; |
| 56 | + |
| 57 | + /// Creates a new browser. |
| 58 | + /// |
| 59 | + /// This is intended to be called by subclasses. They pass in [startBrowser], |
| 60 | + /// which asynchronously returns the browser process. Any errors in |
| 61 | + /// [startBrowser] (even those raised asynchronously after it returns) are |
| 62 | + /// piped to [onExit] and will cause the browser to be killed. |
| 63 | + Browser(Future<Process> startBrowser()) { |
| 64 | + // Don't return a Future here because there's no need for the caller to wait |
| 65 | + // for the process to actually start. They should just wait for the HTTP |
| 66 | + // request instead. |
| 67 | + runZoned(() async { |
| 68 | + var process = await startBrowser(); |
| 69 | + _processCompleter.complete(process); |
| 70 | + |
| 71 | + var output = Uint8Buffer(); |
| 72 | + drainOutput(Stream<List<int>> stream) { |
| 73 | + try { |
| 74 | + _ioSubscriptions |
| 75 | + .add(stream.listen(output.addAll, cancelOnError: true)); |
| 76 | + } on StateError catch (_) {} |
| 77 | + } |
| 78 | + |
| 79 | + // If we don't drain the stdout and stderr the process can hang. |
| 80 | + drainOutput(process.stdout); |
| 81 | + drainOutput(process.stderr); |
| 82 | + |
| 83 | + var exitCode = await process.exitCode; |
| 84 | + |
| 85 | + // This hack dodges an otherwise intractable race condition. When the user |
| 86 | + // presses Control-C, the signal is sent to the browser and the test |
| 87 | + // runner at the same time. It's possible for the browser to exit before |
| 88 | + // the [Browser.close] is called, which would trigger the error below. |
| 89 | + // |
| 90 | + // A negative exit code signals that the process exited due to a signal. |
| 91 | + // However, it's possible that this signal didn't come from the user's |
| 92 | + // Control-C, in which case we do want to throw the error. The only way to |
| 93 | + // resolve the ambiguity is to wait a brief amount of time and see if this |
| 94 | + // browser is actually closed. |
| 95 | + if (!_closed && exitCode < 0) { |
| 96 | + await Future.delayed(Duration(milliseconds: 200)); |
| 97 | + } |
| 98 | + |
| 99 | + if (!_closed && exitCode != 0) { |
| 100 | + var outputString = utf8.decode(output); |
| 101 | + var message = '$name failed with exit code $exitCode.'; |
| 102 | + if (outputString.isNotEmpty) { |
| 103 | + message += '\nStandard output:\n$outputString'; |
| 104 | + } |
| 105 | + |
| 106 | + throw Exception(message); |
| 107 | + } |
| 108 | + |
| 109 | + _onExitCompleter.complete(); |
| 110 | + }, onError: (error, StackTrace stackTrace) { |
| 111 | + // Ignore any errors after the browser has been closed. |
| 112 | + if (_closed) return; |
| 113 | + |
| 114 | + // Make sure the process dies even if the error wasn't fatal. |
| 115 | + _process.then((process) => process.kill()); |
| 116 | + |
| 117 | + if (stackTrace == null) stackTrace = Trace.current(); |
| 118 | + if (_onExitCompleter.isCompleted) return; |
| 119 | + _onExitCompleter.completeError( |
| 120 | + Exception('Failed to run $name: ${getErrorMessage(error)}.'), |
| 121 | + stackTrace); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + /// Kills the browser process. |
| 126 | + /// |
| 127 | + /// Returns the same [Future] as [onExit], except that it won't emit |
| 128 | + /// exceptions. |
| 129 | + Future close() async { |
| 130 | + _closed = true; |
| 131 | + |
| 132 | + // If we don't manually close the stream the test runner can hang. |
| 133 | + // For example this happens with Chrome Headless. |
| 134 | + // See SDK issue: https://github.com/dart-lang/sdk/issues/31264 |
| 135 | + for (var stream in _ioSubscriptions) { |
| 136 | + unawaited(stream.cancel()); |
| 137 | + } |
| 138 | + |
| 139 | + (await _process).kill(); |
| 140 | + |
| 141 | + // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 142 | + return onExit.catchError((_) {}); |
| 143 | + } |
| 144 | +} |
0 commit comments