| 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.dartium; |
| 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 import 'browser.dart'; |
| 14 | 14 |
| 15 // TODO(nweiz): move this into its own package? | 15 /// A class for running an instance of Dartium. |
| 16 // TODO(nweiz): support other browsers. | |
| 17 /// A class for running an instance of Chrome. | |
| 18 /// | 16 /// |
| 19 /// Most of the communication with the browser is expected to happen via HTTP, | 17 /// Most of the communication with the browser is expected to happen via HTTP, |
| 20 /// so this exposes a bare-bones API. The browser starts as soon as the class is | 18 /// so this exposes a bare-bones API. The browser starts as soon as the class is |
| 21 /// constructed, and is killed when [close] is called. | 19 /// constructed, and is killed when [close] is called. |
| 22 /// | 20 /// |
| 23 /// Any errors starting or running the process are reported through [onExit]. | 21 /// Any errors starting or running the process are reported through [onExit]. |
| 24 class Chrome implements Browser { | 22 class Dartium implements Browser { |
| 25 /// The underlying process. | 23 /// The underlying process. |
| 26 Process _process; | 24 Process _process; |
| 27 | 25 |
| 28 /// The temporary directory used as the browser's user data dir. | 26 /// The temporary directory used as the browser's user data dir. |
| 29 /// | 27 /// |
| 30 /// A new data dir is created for each run to ensure that they're | 28 /// A new data dir is created for each run to ensure that they're |
| 31 /// well-isolated. | 29 /// well-isolated. |
| 32 String _dir; | 30 String _dir; |
| 33 | 31 |
| 34 Future get onExit => _onExitCompleter.future; | 32 Future get onExit => _onExitCompleter.future; |
| 35 final _onExitCompleter = new Completer(); | 33 final _onExitCompleter = new Completer(); |
| 36 | 34 |
| 37 /// A future that completes when the browser process has started. | 35 /// A future that completes when the browser process has started. |
| 38 /// | 36 /// |
| 39 /// This is used to ensure that [close] works regardless of when it's called. | 37 /// This is used to ensure that [close] works regardless of when it's called. |
| 40 Future get _onProcessStarted => _onProcessStartedCompleter.future; | 38 Future get _onProcessStarted => _onProcessStartedCompleter.future; |
| 41 final _onProcessStartedCompleter = new Completer(); | 39 final _onProcessStartedCompleter = new Completer(); |
| 42 | 40 |
| 43 /// Starts a new instance of Chrome open to the given [url], which may be a | 41 /// Starts a new instance of Dartium open to the given [url], which may be a |
| 44 /// [Uri] or a [String]. | 42 /// [Uri] or a [String]. |
| 45 /// | 43 /// |
| 46 /// If [executable] is passed, it's used as the Chrome executable. Otherwise | 44 /// If [executable] is passed, it's used as the Dartium executable. Otherwise |
| 47 /// the default executable name for the current OS will be used. | 45 /// the default executable name for the current OS will be used. |
| 48 Chrome(url, {String executable}) { | 46 Dartium(url, {String executable}) { |
| 49 if (executable == null) executable = _defaultExecutable(); | 47 if (executable == null) executable = _defaultExecutable(); |
| 50 | 48 |
| 51 // Don't return a Future here because there's no need for the caller to wait | 49 // 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 | 50 // for the process to actually start. They should just wait for the HTTP |
| 53 // request instead. | 51 // request instead. |
| 54 withTempDir((dir) { | 52 withTempDir((dir) { |
| 55 _dir = dir; | 53 _dir = dir; |
| 56 return Process.start(executable, [ | 54 return Process.start(executable, [ |
| 57 "--user-data-dir=$_dir", | 55 "--user-data-dir=$_dir", |
| 58 url.toString(), | 56 url.toString(), |
| 59 "--disable-extensions", | 57 "--disable-extensions", |
| 60 "--disable-popup-blocking", | 58 "--disable-popup-blocking", |
| 61 "--bwsi", | 59 "--bwsi", |
| 62 "--no-first-run", | 60 "--no-first-run", |
| 63 "--no-default-browser-check", | 61 "--no-default-browser-check", |
| 64 "--disable-default-apps", | 62 "--disable-default-apps", |
| 65 "--disable-translate" | 63 "--disable-translate" |
| 66 ]).then((process) { | 64 ], environment: {"DART_FLAGS": "--checked"}).then((process) { |
| 67 _process = process; | 65 _process = process; |
| 68 _onProcessStartedCompleter.complete(); | 66 _onProcessStartedCompleter.complete(); |
| 69 | 67 |
| 70 // TODO(nweiz): the browser's standard output is almost always useless | 68 // TODO(nweiz): the browser's standard output is almost always useless |
| 71 // noise, but we should allow the user to opt in to seeing it. | 69 // noise, but we should allow the user to opt in to seeing it. |
| 72 return _process.exitCode; | 70 return _process.exitCode; |
| 73 }); | 71 }); |
| 74 }).then((exitCode) { | 72 }).then((exitCode) { |
| 75 if (exitCode != 0) throw "Chrome failed with exit code $exitCode."; | 73 if (exitCode != 0) throw "Dartium failed with exit code $exitCode."; |
| 76 }).then(_onExitCompleter.complete) | 74 }).then(_onExitCompleter.complete) |
| 77 .catchError(_onExitCompleter.completeError); | 75 .catchError(_onExitCompleter.completeError); |
| 78 } | 76 } |
| 79 | 77 |
| 80 Future close() { | 78 Future close() { |
| 81 _onProcessStarted.then((_) => _process.kill()); | 79 _onProcessStarted.then((_) => _process.kill()); |
| 82 | 80 |
| 83 // Swallow exceptions. The user should explicitly use [onExit] for these. | 81 // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 84 return onExit.catchError((_) {}); | 82 return onExit.catchError((_) {}); |
| 85 } | 83 } |
| 86 | 84 |
| 87 /// Return the default executable for the current operating system. | 85 /// Return the default executable for the current operating system. |
| 88 String _defaultExecutable() { | 86 String _defaultExecutable() { |
| 89 if (Platform.isMacOS) { | 87 var dartium = _executableInEditor(); |
| 90 return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; | 88 if (dartium != null) return dartium; |
| 91 } | 89 return Platform.isWindows ? "dartium.exe" : "dartium"; |
| 92 if (!Platform.isWindows) return 'google-chrome'; | 90 } |
| 93 | 91 |
| 94 // Chrome could be installed in several places on Windows. The only way to | 92 String _executableInEditor() { |
| 95 // find it is to check. | 93 var dir = p.dirname(sdkDir); |
| 96 var prefixes = [ | |
| 97 Platform.environment['LOCALAPPDATA'], | |
| 98 Platform.environment['PROGRAMFILES'], | |
| 99 Platform.environment['PROGRAMFILES(X86)'] | |
| 100 ]; | |
| 101 var suffix = r'Google\Chrome\Application\chrome.exe'; | |
| 102 | 94 |
| 103 for (var prefix in prefixes) { | 95 if (Platform.isWindows) { |
| 104 if (prefix == null) continue; | 96 if (!new File(p.join(dir, "DartEditor.exe")).existsSync()) return null; |
| 105 | 97 |
| 106 var path = p.join(prefix, suffix); | 98 var dartium = p.join(dir, "chromium\\chrome.exe"); |
| 107 if (new File(p.join(prefix, suffix)).existsSync()) return path; | 99 return new File(dartium).existsSync() ? null : dartium; |
| 108 } | 100 } |
| 109 | 101 |
| 110 // Fall back on looking it up on the path. This probably won't work, but at | 102 if (Platform.isMacOS) { |
| 111 // least it will fail with a useful error message. | 103 if (!new File(p.join(dir, "DartEditor.app/Contents/MacOS/DartEditor")) |
| 112 return "chrome.exe"; | 104 .existsSync()) { |
| 105 return null; |
| 106 } |
| 107 |
| 108 var dartium = p.join( |
| 109 dir, "chromium/Chromium.app/Contents/MacOs/Chromium"); |
| 110 return new File(dartium).existsSync() ? null : dartium; |
| 111 } |
| 112 |
| 113 assert(Platform.isLinux); |
| 114 if (!new File(p.join(dir, "DartEditor")).existsSync()) return null; |
| 115 |
| 116 var dartium = p.join(dir, "chromium", "chrome"); |
| 117 return new File(dartium).existsSync() ? null : dartium; |
| 113 } | 118 } |
| 114 } | 119 } |
| OLD | NEW |