| 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'; |
| 8 import 'dart:io'; | 9 import 'dart:io'; |
| 9 | 10 |
| 10 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
| 12 import 'package:stack_trace/stack_trace.dart'; |
| 11 | 13 |
| 12 import '../../util/io.dart'; | 14 import '../../util/io.dart'; |
| 15 import '../../utils.dart'; |
| 16 import '../application_exception.dart'; |
| 13 import 'browser.dart'; | 17 import 'browser.dart'; |
| 14 | 18 |
| 15 final _preferences = ''' | 19 final _preferences = ''' |
| 16 user_pref("browser.shell.checkDefaultBrowser", false); | 20 user_pref("browser.shell.checkDefaultBrowser", false); |
| 17 user_pref("dom.disable_open_during_load", false); | 21 user_pref("dom.disable_open_during_load", false); |
| 18 user_pref("dom.max_script_run_time", 0); | 22 user_pref("dom.max_script_run_time", 0); |
| 19 '''; | 23 '''; |
| 20 | 24 |
| 21 /// A class for running an instance of Firefox. | 25 /// A class for running an instance of Firefox. |
| 22 /// | 26 /// |
| 23 /// Most of the communication with the browser is expected to happen via HTTP, | 27 /// Most of the communication with the browser is expected to happen via HTTP, |
| 24 /// so this exposes a bare-bones API. The browser starts as soon as the class is | 28 /// so this exposes a bare-bones API. The browser starts as soon as the class is |
| 25 /// constructed, and is killed when [close] is called. | 29 /// constructed, and is killed when [close] is called. |
| 26 /// | 30 /// |
| 27 /// Any errors starting or running the process are reported through [onExit]. | 31 /// Any errors starting or running the process are reported through [onExit]. |
| 28 class Firefox implements Browser { | 32 class Firefox implements Browser { |
| 29 /// The underlying process. | 33 /// The underlying process. |
| 30 Process _process; | 34 Process _process; |
| 31 | 35 |
| 32 /// The temporary directory used as the browser's user data dir. | |
| 33 /// | |
| 34 /// A new data dir is created for each run to ensure that they're | |
| 35 /// well-isolated. | |
| 36 String _dir; | |
| 37 | |
| 38 Future get onExit => _onExitCompleter.future; | 36 Future get onExit => _onExitCompleter.future; |
| 39 final _onExitCompleter = new Completer(); | 37 final _onExitCompleter = new Completer(); |
| 40 | 38 |
| 41 /// A future that completes when the browser process has started. | 39 /// A future that completes when the browser process has started. |
| 42 /// | 40 /// |
| 43 /// This is used to ensure that [close] works regardless of when it's called. | 41 /// This is used to ensure that [close] works regardless of when it's called. |
| 44 Future get _onProcessStarted => _onProcessStartedCompleter.future; | 42 Future get _onProcessStarted => _onProcessStartedCompleter.future; |
| 45 final _onProcessStartedCompleter = new Completer(); | 43 final _onProcessStartedCompleter = new Completer(); |
| 46 | 44 |
| 47 /// Starts a new instance of Firefox open to the given [url], which may be a | 45 /// Starts a new instance of Firefox open to the given [url], which may be a |
| 48 /// [Uri] or a [String]. | 46 /// [Uri] or a [String]. |
| 49 /// | 47 /// |
| 50 /// If [executable] is passed, it's used as the Firefox executable. Otherwise | 48 /// If [executable] is passed, it's used as the Firefox executable. Otherwise |
| 51 /// the default executable name for the current OS will be used. | 49 /// the default executable name for the current OS will be used. |
| 52 Firefox(url, {String executable}) { | 50 Firefox(url, {String executable}) { |
| 53 if (executable == null) executable = _defaultExecutable(); | 51 if (executable == null) executable = _defaultExecutable(); |
| 54 | 52 |
| 55 // Don't return a Future here because there's no need for the caller to wait | 53 // Don't return a Future here because there's no need for the caller to wait |
| 56 // for the process to actually start. They should just wait for the HTTP | 54 // for the process to actually start. They should just wait for the HTTP |
| 57 // request instead. | 55 // request instead. |
| 58 withTempDir((dir) { | 56 withTempDir((dir) { |
| 59 _dir = dir; | |
| 60 | |
| 61 new File(p.join(dir, 'prefs.js')).writeAsStringSync(_preferences); | 57 new File(p.join(dir, 'prefs.js')).writeAsStringSync(_preferences); |
| 62 | 58 |
| 63 return Process.start(executable, [ | 59 return Process.start(executable, [ |
| 64 "--profile", | 60 "--profile", |
| 65 "$_dir", | 61 "$dir", |
| 66 url.toString(), | 62 url.toString(), |
| 67 "--no-remote" | 63 "--no-remote" |
| 68 ], environment: { | 64 ], environment: { |
| 69 "MOZ_CRASHREPORTER_DISABLE": "1" | 65 "MOZ_CRASHREPORTER_DISABLE": "1" |
| 70 }).then((process) { | 66 }).then((process) { |
| 71 _process = process; | 67 _process = process; |
| 72 _onProcessStartedCompleter.complete(); | 68 _onProcessStartedCompleter.complete(); |
| 73 | 69 |
| 74 // TODO(nweiz): the browser's standard output is almost always useless | 70 // TODO(nweiz): the browser's standard output is almost always useless |
| 75 // 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. |
| 76 return _process.exitCode; | 72 return _process.exitCode; |
| 77 }); | 73 }); |
| 78 }).then((exitCode) { | 74 }).then((exitCode) { |
| 79 if (exitCode != 0) throw "Firefox failed with exit code $exitCode."; | 75 if (exitCode == 0) return null; |
| 80 }).then(_onExitCompleter.complete) | 76 |
| 81 .catchError(_onExitCompleter.completeError); | 77 return UTF8.decodeStream(_process.stderr).then((error) { |
| 78 throw new ApplicationException( |
| 79 "Firefox failed with exit code $exitCode:\n$error"); |
| 80 }); |
| 81 }).then(_onExitCompleter.complete).catchError((error, stackTrace) { |
| 82 if (stackTrace == null) stackTrace = new Trace.current(); |
| 83 _onExitCompleter.completeError( |
| 84 new ApplicationException( |
| 85 "Failed to start Firefox: ${getErrorMessage(error)}."), |
| 86 stackTrace); |
| 87 }); |
| 82 } | 88 } |
| 83 | 89 |
| 84 Future close() { | 90 Future close() { |
| 85 _onProcessStarted.then((_) => _process.kill()); | 91 _onProcessStarted.then((_) => _process.kill()); |
| 86 | 92 |
| 87 // Swallow exceptions. The user should explicitly use [onExit] for these. | 93 // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 88 return onExit.catchError((_) {}); | 94 return onExit.catchError((_) {}); |
| 89 } | 95 } |
| 90 | 96 |
| 91 /// Return the default executable for the current operating system. | 97 /// Return the default executable for the current operating system. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 108 | 114 |
| 109 var path = p.join(prefix, suffix); | 115 var path = p.join(prefix, suffix); |
| 110 if (new File(p.join(prefix, suffix)).existsSync()) return path; | 116 if (new File(p.join(prefix, suffix)).existsSync()) return path; |
| 111 } | 117 } |
| 112 | 118 |
| 113 // Fall back on looking it up on the path. This probably won't work, but at | 119 // Fall back on looking it up on the path. This probably won't work, but at |
| 114 // least it will fail with a useful error message. | 120 // least it will fail with a useful error message. |
| 115 return "firefox.exe"; | 121 return "firefox.exe"; |
| 116 } | 122 } |
| 117 } | 123 } |
| OLD | NEW |