OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library test.runner.browser.browser; | 5 library test.runner.browser.browser; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import 'package:stack_trace/stack_trace.dart'; |
| 11 |
| 12 import '../../utils.dart'; |
| 13 import '../application_exception.dart'; |
| 14 |
| 15 typedef Future<Process> StartBrowserFn(); |
8 | 16 |
9 /// An interface for running browser instances. | 17 /// An interface for running browser instances. |
10 /// | 18 /// |
11 /// This is intentionally coarse-grained: browsers are controlled primary from | 19 /// This is intentionally coarse-grained: browsers are controlled primary from |
12 /// inside a single tab. Thus this interface only provides support for closing | 20 /// inside a single tab. Thus this interface only provides support for closing |
13 /// the browser and seeing if it closes itself. | 21 /// the browser and seeing if it closes itself. |
14 /// | 22 /// |
15 /// Any errors starting or running the browser process are reported through | 23 /// Any errors starting or running the browser process are reported through |
16 /// [onExit]. | 24 /// [onExit]. |
17 abstract class Browser { | 25 abstract class Browser { |
| 26 String get name; |
| 27 |
| 28 /// The underlying process. |
| 29 /// |
| 30 /// This will fire once the process has started successfully. |
| 31 Future<Process> get _process => _processCompleter.future; |
| 32 final _processCompleter = new Completer<Process>(); |
| 33 |
| 34 /// Whether [close] has been called. |
| 35 var _closed = false; |
| 36 |
18 /// A future that completes when the browser exits. | 37 /// A future that completes when the browser exits. |
19 /// | 38 /// |
20 /// If there's a problem starting or running the browser, this will complete | 39 /// If there's a problem starting or running the browser, this will complete |
21 /// with an error. | 40 /// with an error. |
22 Future get onExit; | 41 Future get onExit => _onExitCompleter.future; |
| 42 final _onExitCompleter = new Completer(); |
| 43 |
| 44 /// Creates a new browser. |
| 45 /// |
| 46 /// This is intended to be called by subclasses. They pass in [startBrowser], |
| 47 /// which asynchronously returns the browser process. Any errors in |
| 48 /// [startBrowser] (even those raised asynchronously after it returns) are |
| 49 /// piped to [onExit] and will cause the browser to be killed. |
| 50 Browser(Future<Process> startBrowser()) { |
| 51 // Don't return a Future here because there's no need for the caller to wait |
| 52 // for the process to actually start. They should just wait for the HTTP |
| 53 // request instead. |
| 54 runZoned(() async { |
| 55 var process = await startBrowser(); |
| 56 _processCompleter.complete(process); |
| 57 |
| 58 var exitCode = await process.exitCode; |
| 59 |
| 60 if (!_closed && exitCode != 0) { |
| 61 throw new ApplicationException( |
| 62 "$name failed with exit code $exitCode."); |
| 63 } |
| 64 |
| 65 _onExitCompleter.complete(); |
| 66 }, onError: (error, stackTrace) { |
| 67 // Ignore any errors after the browser has been closed. |
| 68 if (_closed) return; |
| 69 |
| 70 // Make sure the process dies even if the error wasn't fatal. |
| 71 _process.then((process) => process.kill()); |
| 72 |
| 73 if (stackTrace == null) stackTrace = new Trace.current(); |
| 74 _onExitCompleter.completeError( |
| 75 new ApplicationException( |
| 76 "Failed to run $name: ${getErrorMessage(error)}."), |
| 77 stackTrace); |
| 78 }); |
| 79 } |
23 | 80 |
24 /// Kills the browser process. | 81 /// Kills the browser process. |
25 /// | 82 /// |
26 /// Returns the same [Future] as [onExit], except that it won't emit | 83 /// Returns the same [Future] as [onExit], except that it won't emit |
27 /// exceptions. | 84 /// exceptions. |
28 Future close(); | 85 Future close() { |
| 86 _closed = true; |
| 87 _process.then((process) => process.kill()); |
| 88 |
| 89 // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 90 return onExit.catchError((_) {}); |
| 91 } |
29 } | 92 } |
OLD | NEW |