| 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.firefox; | 5 library test.runner.browser.firefox; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | |
| 9 import 'dart:io'; | 8 import 'dart:io'; |
| 10 | 9 |
| 11 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
| 12 import 'package:stack_trace/stack_trace.dart'; | |
| 13 | 11 |
| 14 import '../../util/io.dart'; | 12 import '../../util/io.dart'; |
| 15 import '../../utils.dart'; | |
| 16 import '../application_exception.dart'; | |
| 17 import 'browser.dart'; | 13 import 'browser.dart'; |
| 18 | 14 |
| 19 final _preferences = ''' | 15 final _preferences = ''' |
| 20 user_pref("browser.shell.checkDefaultBrowser", false); | 16 user_pref("browser.shell.checkDefaultBrowser", false); |
| 21 user_pref("dom.disable_open_during_load", false); | 17 user_pref("dom.disable_open_during_load", false); |
| 22 user_pref("dom.max_script_run_time", 0); | 18 user_pref("dom.max_script_run_time", 0); |
| 23 '''; | 19 '''; |
| 24 | 20 |
| 25 /// A class for running an instance of Firefox. | 21 /// A class for running an instance of Firefox. |
| 26 /// | 22 /// |
| 27 /// 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, |
| 28 /// 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 |
| 29 /// constructed, and is killed when [close] is called. | 25 /// constructed, and is killed when [close] is called. |
| 30 /// | 26 /// |
| 31 /// Any errors starting or running the process are reported through [onExit]. | 27 /// Any errors starting or running the process are reported through [onExit]. |
| 32 class Firefox implements Browser { | 28 class Firefox extends Browser { |
| 33 /// The underlying process. | 29 final name = "Firefox"; |
| 34 Process _process; | |
| 35 | 30 |
| 36 Future get onExit => _onExitCompleter.future; | 31 Firefox(url, {String executable}) |
| 37 final _onExitCompleter = new Completer(); | 32 : super(() => _startBrowser(url, executable)); |
| 38 | |
| 39 /// A future that completes when the browser process has started. | |
| 40 /// | |
| 41 /// This is used to ensure that [close] works regardless of when it's called. | |
| 42 Future get _onProcessStarted => _onProcessStartedCompleter.future; | |
| 43 final _onProcessStartedCompleter = new Completer(); | |
| 44 | 33 |
| 45 /// Starts a new instance of Firefox open to the given [url], which may be a | 34 /// Starts a new instance of Firefox open to the given [url], which may be a |
| 46 /// [Uri] or a [String]. | 35 /// [Uri] or a [String]. |
| 47 /// | 36 /// |
| 48 /// If [executable] is passed, it's used as the Firefox executable. Otherwise | 37 /// If [executable] is passed, it's used as the Firefox executable. |
| 49 /// the default executable name for the current OS will be used. | 38 /// Otherwise the default executable name for the current OS will be used. |
| 50 Firefox(url, {String executable}) { | 39 static Future<Process> _startBrowser(url, [String executable]) async { |
| 51 if (executable == null) executable = _defaultExecutable(); | 40 if (executable == null) executable = _defaultExecutable(); |
| 52 | 41 |
| 53 // Don't return a Future here because there's no need for the caller to wait | 42 var dir = createTempDir(); |
| 54 // for the process to actually start. They should just wait for the HTTP | 43 new File(p.join(dir, 'prefs.js')).writeAsStringSync(_preferences); |
| 55 // request instead. | |
| 56 invoke(() async { | |
| 57 try { | |
| 58 var exitCode = await withTempDir((dir) async { | |
| 59 new File(p.join(dir, 'prefs.js')).writeAsStringSync(_preferences); | |
| 60 | 44 |
| 61 var process = await Process.start(executable, [ | 45 var process = await Process.start(executable, [ |
| 62 "--profile", | 46 "--profile", "$dir", |
| 63 "$dir", | 47 url.toString(), |
| 64 url.toString(), | 48 "--no-remote" |
| 65 "--no-remote" | 49 ], environment: { |
| 66 ], environment: { | 50 "MOZ_CRASHREPORTER_DISABLE": "1" |
| 67 "MOZ_CRASHREPORTER_DISABLE": "1" | 51 }); |
| 68 }); | |
| 69 | 52 |
| 70 _process = process; | 53 process.exitCode |
| 71 _onProcessStartedCompleter.complete(); | 54 .then((_) => new Directory(dir).deleteSync(recursive: true)); |
| 72 | 55 |
| 73 // TODO(nweiz): the browser's standard output is almost always useless | 56 return process; |
| 74 // noise, but we should allow the user to opt in to seeing it. | |
| 75 return await _process.exitCode; | |
| 76 }); | |
| 77 | |
| 78 if (exitCode != 0) { | |
| 79 var error = await UTF8.decodeStream(_process.stderr); | |
| 80 throw new ApplicationException( | |
| 81 "Firefox failed with exit code $exitCode:\n$error"); | |
| 82 } | |
| 83 | |
| 84 _onExitCompleter.complete(); | |
| 85 } catch (error, stackTrace) { | |
| 86 if (stackTrace == null) stackTrace = new Trace.current(); | |
| 87 _onExitCompleter.completeError( | |
| 88 new ApplicationException( | |
| 89 "Failed to start Firefox: ${getErrorMessage(error)}."), | |
| 90 stackTrace); | |
| 91 } | |
| 92 }); | |
| 93 } | |
| 94 | |
| 95 Future close() { | |
| 96 _onProcessStarted.then((_) => _process.kill()); | |
| 97 | |
| 98 // Swallow exceptions. The user should explicitly use [onExit] for these. | |
| 99 return onExit.catchError((_) {}); | |
| 100 } | 57 } |
| 101 | 58 |
| 102 /// Return the default executable for the current operating system. | 59 /// Return the default executable for the current operating system. |
| 103 String _defaultExecutable() { | 60 static String _defaultExecutable() { |
| 104 if (Platform.isMacOS) { | 61 if (Platform.isMacOS) { |
| 105 return '/Applications/Firefox.app/Contents/MacOS/firefox-bin'; | 62 return '/Applications/Firefox.app/Contents/MacOS/firefox-bin'; |
| 106 } | 63 } |
| 107 if (!Platform.isWindows) return 'firefox'; | 64 if (!Platform.isWindows) return 'firefox'; |
| 108 | 65 |
| 109 // Firefox could be installed in several places on Windows. The only way to | 66 // Firefox could be installed in several places on Windows. The only way to |
| 110 // find it is to check. | 67 // find it is to check. |
| 111 var prefixes = [ | 68 var prefixes = [ |
| 112 Platform.environment['PROGRAMFILES'], | 69 Platform.environment['PROGRAMFILES'], |
| 113 Platform.environment['PROGRAMFILES(X86)'] | 70 Platform.environment['PROGRAMFILES(X86)'] |
| 114 ]; | 71 ]; |
| 115 var suffix = r'Mozilla Firefox\firefox.exe'; | 72 var suffix = r'Mozilla Firefox\firefox.exe'; |
| 116 | 73 |
| 117 for (var prefix in prefixes) { | 74 for (var prefix in prefixes) { |
| 118 if (prefix == null) continue; | 75 if (prefix == null) continue; |
| 119 | 76 |
| 120 var path = p.join(prefix, suffix); | 77 var path = p.join(prefix, suffix); |
| 121 if (new File(p.join(prefix, suffix)).existsSync()) return path; | 78 if (new File(p.join(prefix, suffix)).existsSync()) return path; |
| 122 } | 79 } |
| 123 | 80 |
| 124 // Fall back on looking it up on the path. This probably won't work, but at | 81 // Fall back on looking it up on the path. This probably won't work, but at |
| 125 // least it will fail with a useful error message. | 82 // least it will fail with a useful error message. |
| 126 return "firefox.exe"; | 83 return "firefox.exe"; |
| 127 } | 84 } |
| 128 } | 85 } |
| OLD | NEW |