| 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.chrome; | 5 library test.runner.browser.chrome; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
| 11 | 11 |
| 12 import '../../util/io.dart'; | 12 import '../../util/io.dart'; |
| 13 import 'browser.dart'; |
| 13 | 14 |
| 14 // TODO(nweiz): move this into its own package? | 15 // TODO(nweiz): move this into its own package? |
| 15 // TODO(nweiz): support other browsers. | 16 // TODO(nweiz): support other browsers. |
| 16 /// A class for running an instance of Chrome. | 17 /// A class for running an instance of Chrome. |
| 17 /// | 18 /// |
| 18 /// Most of the communication with the browser is expected to happen via HTTP, | 19 /// Most of the communication with the browser is expected to happen via HTTP, |
| 19 /// so this exposes a bare-bones API. The browser starts as soon as the class is | 20 /// so this exposes a bare-bones API. The browser starts as soon as the class is |
| 20 /// constructed, and is killed when [close] is called. | 21 /// constructed, and is killed when [close] is called. |
| 21 /// | 22 /// |
| 22 /// Any errors starting or running the process are reported through [onExit]. | 23 /// Any errors starting or running the process are reported through [onExit]. |
| 23 class Chrome { | 24 class Chrome implements Browser { |
| 24 /// The underlying process. | 25 /// The underlying process. |
| 25 Process _process; | 26 Process _process; |
| 26 | 27 |
| 27 /// The temporary directory used as the browser's user data dir. | 28 /// The temporary directory used as the browser's user data dir. |
| 28 /// | 29 /// |
| 29 /// A new data dir is created for each run to ensure that they're | 30 /// A new data dir is created for each run to ensure that they're |
| 30 /// well-isolated. | 31 /// well-isolated. |
| 31 String _dir; | 32 String _dir; |
| 32 | 33 |
| 33 /// A future that completes when the browser exits. | |
| 34 /// | |
| 35 /// If there's a problem starting or running the browser, this will complete | |
| 36 /// with an error. | |
| 37 Future get onExit => _onExitCompleter.future; | 34 Future get onExit => _onExitCompleter.future; |
| 38 final _onExitCompleter = new Completer(); | 35 final _onExitCompleter = new Completer(); |
| 39 | 36 |
| 40 /// A future that completes when the browser process has started. | 37 /// A future that completes when the browser process has started. |
| 41 /// | 38 /// |
| 42 /// This is used to ensure that [close] works regardless of when it's called. | 39 /// This is used to ensure that [close] works regardless of when it's called. |
| 43 Future get _onProcessStarted => _onProcessStartedCompleter.future; | 40 Future get _onProcessStarted => _onProcessStartedCompleter.future; |
| 44 final _onProcessStartedCompleter = new Completer(); | 41 final _onProcessStartedCompleter = new Completer(); |
| 45 | 42 |
| 46 /// Starts a new instance of Chrome open to the given [url], which may be a | 43 /// Starts a new instance of Chrome open to the given [url], which may be a |
| (...skipping 26 matching lines...) Expand all Loading... |
| 73 // TODO(nweiz): the browser's standard output is almost always useless | 70 // TODO(nweiz): the browser's standard output is almost always useless |
| 74 // noise, but we should allow the user to opt in to seeing it. | 71 // noise, but we should allow the user to opt in to seeing it. |
| 75 return _process.exitCode; | 72 return _process.exitCode; |
| 76 }); | 73 }); |
| 77 }).then((exitCode) { | 74 }).then((exitCode) { |
| 78 if (exitCode != 0) throw "Chrome failed with exit code $exitCode."; | 75 if (exitCode != 0) throw "Chrome failed with exit code $exitCode."; |
| 79 }).then(_onExitCompleter.complete) | 76 }).then(_onExitCompleter.complete) |
| 80 .catchError(_onExitCompleter.completeError); | 77 .catchError(_onExitCompleter.completeError); |
| 81 } | 78 } |
| 82 | 79 |
| 83 /// Kills the browser process. | |
| 84 /// | |
| 85 /// Returns the same [Future] as [onExit], except that it won't emit | |
| 86 /// exceptions. | |
| 87 Future close() { | 80 Future close() { |
| 88 _onProcessStarted.then((_) => _process.kill()); | 81 _onProcessStarted.then((_) => _process.kill()); |
| 89 | 82 |
| 90 // Swallow exceptions. The user should explicitly use [onExit] for these. | 83 // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 91 return onExit.catchError((_) {}); | 84 return onExit.catchError((_) {}); |
| 92 } | 85 } |
| 93 | 86 |
| 94 /// Return the default executable for the current operating system. | 87 /// Return the default executable for the current operating system. |
| 95 String _defaultExecutable() { | 88 String _defaultExecutable() { |
| 96 if (Platform.isMacOS) { | 89 if (Platform.isMacOS) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 112 | 105 |
| 113 var path = p.join(prefix, suffix); | 106 var path = p.join(prefix, suffix); |
| 114 if (new File(p.join(prefix, suffix)).existsSync()) return path; | 107 if (new File(p.join(prefix, suffix)).existsSync()) return path; |
| 115 } | 108 } |
| 116 | 109 |
| 117 // Fall back on looking it up on the path. This probably won't work, but at | 110 // Fall back on looking it up on the path. This probably won't work, but at |
| 118 // least it will fail with a useful error message. | 111 // least it will fail with a useful error message. |
| 119 return "chrome.exe"; | 112 return "chrome.exe"; |
| 120 } | 113 } |
| 121 } | 114 } |
| OLD | NEW |