| 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 unittest.runner.browser.chrome; | 5 library unittest.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; |
| 11 |
| 10 import '../../util/io.dart'; | 12 import '../../util/io.dart'; |
| 11 | 13 |
| 12 // TODO(nweiz): move this into its own package? | 14 // TODO(nweiz): move this into its own package? |
| 13 // TODO(nweiz): support other browsers. | 15 // TODO(nweiz): support other browsers. |
| 14 /// A class for running an instance of Chrome. | 16 /// A class for running an instance of Chrome. |
| 15 /// | 17 /// |
| 16 /// Most of the communication with the browser is expected to happen via HTTP, | 18 /// Most of the communication with the browser is expected to happen via HTTP, |
| 17 /// so this exposes a bare-bones API. The browser starts as soon as the class is | 19 /// so this exposes a bare-bones API. The browser starts as soon as the class is |
| 18 /// constructed, and is killed when [close] is called. | 20 /// constructed, and is killed when [close] is called. |
| 19 /// | 21 /// |
| (...skipping 18 matching lines...) Expand all Loading... |
| 38 /// A future that completes when the browser process has started. | 40 /// A future that completes when the browser process has started. |
| 39 /// | 41 /// |
| 40 /// This is used to ensure that [close] works regardless of when it's called. | 42 /// This is used to ensure that [close] works regardless of when it's called. |
| 41 Future get _onProcessStarted => _onProcessStartedCompleter.future; | 43 Future get _onProcessStarted => _onProcessStartedCompleter.future; |
| 42 final _onProcessStartedCompleter = new Completer(); | 44 final _onProcessStartedCompleter = new Completer(); |
| 43 | 45 |
| 44 /// Starts a new instance of Chrome open to the given [url], which may be a | 46 /// Starts a new instance of Chrome open to the given [url], which may be a |
| 45 /// [Uri] or a [String]. | 47 /// [Uri] or a [String]. |
| 46 /// | 48 /// |
| 47 /// If [executable] is passed, it's used as the Chrome executable. Otherwise | 49 /// If [executable] is passed, it's used as the Chrome executable. Otherwise |
| 48 /// `"google-chrome"` will be looked up on the system PATH. | 50 /// the default executable name for the current OS will be used. |
| 49 Chrome(url, {String executable}) { | 51 Chrome(url, {String executable}) { |
| 50 if (executable == null) executable = "google-chrome"; | 52 if (executable == null) executable = _defaultExecutable(); |
| 51 | 53 |
| 52 // Don't return a Future here because there's no need for the caller to wait | 54 // Don't return a Future here because there's no need for the caller to wait |
| 53 // for the process to actually start. They should just wait for the HTTP | 55 // for the process to actually start. They should just wait for the HTTP |
| 54 // request instead. | 56 // request instead. |
| 55 withTempDir((dir) { | 57 withTempDir((dir) { |
| 56 _dir = dir; | 58 _dir = dir; |
| 57 return Process.start(executable, [ | 59 return Process.start(executable, [ |
| 58 "--user-data-dir=$_dir", | 60 "--user-data-dir=$_dir", |
| 59 url.toString(), | 61 url.toString(), |
| 60 "--disable-extensions", | 62 "--disable-extensions", |
| 61 "--disable-popup-blocking", | 63 "--disable-popup-blocking", |
| 62 "--bwsi", | 64 "--bwsi", |
| 63 "--no-first-run" | 65 "--no-first-run", |
| 66 "--no-default-browser-check", |
| 67 "--disable-default-apps", |
| 68 "--disable-translate" |
| 64 ]).then((process) { | 69 ]).then((process) { |
| 65 _process = process; | 70 _process = process; |
| 66 _onProcessStartedCompleter.complete(); | 71 _onProcessStartedCompleter.complete(); |
| 67 | 72 |
| 68 // TODO(nweiz): the browser's standard output is almost always useless | 73 // TODO(nweiz): the browser's standard output is almost always useless |
| 69 // noise, but we should allow the user to opt in to seeing it. | 74 // noise, but we should allow the user to opt in to seeing it. |
| 70 return _process.exitCode; | 75 return _process.exitCode; |
| 71 }); | 76 }); |
| 72 }).then((exitCode) { | 77 }).then((exitCode) { |
| 73 if (exitCode != 0) throw "Chrome failed with exit code $exitCode."; | 78 if (exitCode != 0) throw "Chrome failed with exit code $exitCode."; |
| 74 }).then(_onExitCompleter.complete) | 79 }).then(_onExitCompleter.complete) |
| 75 .catchError(_onExitCompleter.completeError); | 80 .catchError(_onExitCompleter.completeError); |
| 76 } | 81 } |
| 77 | 82 |
| 78 /// Kills the browser process. | 83 /// Kills the browser process. |
| 79 /// | 84 /// |
| 80 /// Returns the same [Future] as [onExit], except that it won't emit | 85 /// Returns the same [Future] as [onExit], except that it won't emit |
| 81 /// exceptions. | 86 /// exceptions. |
| 82 Future close() { | 87 Future close() { |
| 83 _onProcessStarted.then((_) => _process.kill()); | 88 _onProcessStarted.then((_) => _process.kill()); |
| 84 | 89 |
| 85 // Swallow exceptions. The user should explicitly use [onExit] for these. | 90 // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 86 return onExit.catchError((_) {}); | 91 return onExit.catchError((_) {}); |
| 87 } | 92 } |
| 93 |
| 94 /// Return the default executable for the current operating system. |
| 95 String _defaultExecutable() { |
| 96 if (Platform.isMacOS) { |
| 97 return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; |
| 98 } |
| 99 if (!Platform.isWindows) return 'google-chrome'; |
| 100 |
| 101 // Chrome could be installed in several places on Windows. The only way to |
| 102 // find it is to check. |
| 103 var prefixes = [ |
| 104 Platform.environment['LOCALAPPDATA'], |
| 105 Platform.environment['PROGRAMFILES'], |
| 106 Platform.environment['PROGRAMFILES(X86)'] |
| 107 ]; |
| 108 var suffix = r'Google\Chrome\Application\chrome.exe'; |
| 109 |
| 110 for (var prefix in prefixes) { |
| 111 if (prefix == null) continue; |
| 112 |
| 113 var path = p.join(prefix, suffix); |
| 114 if (new File(p.join(prefix, suffix)).existsSync()) return path; |
| 115 } |
| 116 |
| 117 // 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. |
| 119 return "chrome.exe"; |
| 120 } |
| 88 } | 121 } |
| OLD | NEW |