| 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.dartium; | 5 library test.runner.browser.dartium; |
| 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 /// A class for running an instance of Dartium. | 19 /// A class for running an instance of Dartium. |
| 16 /// | 20 /// |
| 17 /// Most of the communication with the browser is expected to happen via HTTP, | 21 /// Most of the communication with the browser is expected to happen via HTTP, |
| 18 /// so this exposes a bare-bones API. The browser starts as soon as the class is | 22 /// so this exposes a bare-bones API. The browser starts as soon as the class is |
| 19 /// constructed, and is killed when [close] is called. | 23 /// constructed, and is killed when [close] is called. |
| 20 /// | 24 /// |
| 21 /// Any errors starting or running the process are reported through [onExit]. | 25 /// Any errors starting or running the process are reported through [onExit]. |
| 22 class Dartium implements Browser { | 26 class Dartium implements Browser { |
| 23 /// The underlying process. | 27 /// The underlying process. |
| 24 Process _process; | 28 Process _process; |
| 25 | 29 |
| 26 /// The temporary directory used as the browser's user data dir. | |
| 27 /// | |
| 28 /// A new data dir is created for each run to ensure that they're | |
| 29 /// well-isolated. | |
| 30 String _dir; | |
| 31 | |
| 32 Future get onExit => _onExitCompleter.future; | 30 Future get onExit => _onExitCompleter.future; |
| 33 final _onExitCompleter = new Completer(); | 31 final _onExitCompleter = new Completer(); |
| 34 | 32 |
| 35 /// A future that completes when the browser process has started. | 33 /// A future that completes when the browser process has started. |
| 36 /// | 34 /// |
| 37 /// This is used to ensure that [close] works regardless of when it's called. | 35 /// This is used to ensure that [close] works regardless of when it's called. |
| 38 Future get _onProcessStarted => _onProcessStartedCompleter.future; | 36 Future get _onProcessStarted => _onProcessStartedCompleter.future; |
| 39 final _onProcessStartedCompleter = new Completer(); | 37 final _onProcessStartedCompleter = new Completer(); |
| 40 | 38 |
| 41 /// Starts a new instance of Dartium open to the given [url], which may be a | 39 /// Starts a new instance of Dartium open to the given [url], which may be a |
| 42 /// [Uri] or a [String]. | 40 /// [Uri] or a [String]. |
| 43 /// | 41 /// |
| 44 /// If [executable] is passed, it's used as the Dartium executable. Otherwise | 42 /// If [executable] is passed, it's used as the Dartium executable. Otherwise |
| 45 /// the default executable name for the current OS will be used. | 43 /// the default executable name for the current OS will be used. |
| 46 Dartium(url, {String executable}) { | 44 Dartium(url, {String executable}) { |
| 47 if (executable == null) executable = _defaultExecutable(); | 45 if (executable == null) executable = _defaultExecutable(); |
| 48 | 46 |
| 49 // Don't return a Future here because there's no need for the caller to wait | 47 // Don't return a Future here because there's no need for the caller to wait |
| 50 // for the process to actually start. They should just wait for the HTTP | 48 // for the process to actually start. They should just wait for the HTTP |
| 51 // request instead. | 49 // request instead. |
| 52 withTempDir((dir) { | 50 withTempDir((dir) { |
| 53 _dir = dir; | |
| 54 return Process.start(executable, [ | 51 return Process.start(executable, [ |
| 55 "--user-data-dir=$_dir", | 52 "--user-data-dir=$dir", |
| 56 url.toString(), | 53 url.toString(), |
| 57 "--disable-extensions", | 54 "--disable-extensions", |
| 58 "--disable-popup-blocking", | 55 "--disable-popup-blocking", |
| 59 "--bwsi", | 56 "--bwsi", |
| 60 "--no-first-run", | 57 "--no-first-run", |
| 61 "--no-default-browser-check", | 58 "--no-default-browser-check", |
| 62 "--disable-default-apps", | 59 "--disable-default-apps", |
| 63 "--disable-translate" | 60 "--disable-translate" |
| 64 ], environment: {"DART_FLAGS": "--checked"}).then((process) { | 61 ], environment: {"DART_FLAGS": "--checked"}).then((process) { |
| 65 _process = process; | 62 _process = process; |
| 66 _onProcessStartedCompleter.complete(); | 63 _onProcessStartedCompleter.complete(); |
| 67 | 64 |
| 68 // TODO(nweiz): the browser's standard output is almost always useless | 65 // 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. | 66 // noise, but we should allow the user to opt in to seeing it. |
| 70 return _process.exitCode; | 67 return _process.exitCode; |
| 71 }); | 68 }); |
| 72 }).then((exitCode) { | 69 }).then((exitCode) { |
| 73 if (exitCode != 0) throw "Dartium failed with exit code $exitCode."; | 70 if (exitCode == 0) return null; |
| 74 }).then(_onExitCompleter.complete) | 71 |
| 75 .catchError(_onExitCompleter.completeError); | 72 return UTF8.decodeStream(_process.stderr).then((error) { |
| 73 throw new ApplicationException( |
| 74 "Dartium failed with exit code $exitCode:\n$error"); |
| 75 }); |
| 76 }).then(_onExitCompleter.complete).catchError((error, stackTrace) { |
| 77 if (stackTrace == null) stackTrace = new Trace.current(); |
| 78 _onExitCompleter.completeError( |
| 79 new ApplicationException( |
| 80 "Failed to start Dartium: ${getErrorMessage(error)}."), |
| 81 stackTrace); |
| 82 }); |
| 76 } | 83 } |
| 77 | 84 |
| 78 Future close() { | 85 Future close() { |
| 79 _onProcessStarted.then((_) => _process.kill()); | 86 _onProcessStarted.then((_) => _process.kill()); |
| 80 | 87 |
| 81 // Swallow exceptions. The user should explicitly use [onExit] for these. | 88 // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 82 return onExit.catchError((_) {}); | 89 return onExit.catchError((_) {}); |
| 83 } | 90 } |
| 84 | 91 |
| 85 /// Return the default executable for the current operating system. | 92 /// Return the default executable for the current operating system. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 110 return new File(dartium).existsSync() ? dartium : null; | 117 return new File(dartium).existsSync() ? dartium : null; |
| 111 } | 118 } |
| 112 | 119 |
| 113 assert(Platform.isLinux); | 120 assert(Platform.isLinux); |
| 114 if (!new File(p.join(dir, "DartEditor")).existsSync()) return null; | 121 if (!new File(p.join(dir, "DartEditor")).existsSync()) return null; |
| 115 | 122 |
| 116 var dartium = p.join(dir, "chromium", "chrome"); | 123 var dartium = p.join(dir, "chromium", "chrome"); |
| 117 return new File(dartium).existsSync() ? dartium : null; | 124 return new File(dartium).existsSync() ? dartium : null; |
| 118 } | 125 } |
| 119 } | 126 } |
| OLD | NEW |