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