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.chrome; |
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 // TODO(nweiz): move this into its own package? | 19 // TODO(nweiz): move this into its own package? |
16 // TODO(nweiz): support other browsers. | 20 // TODO(nweiz): support other browsers. |
17 /// A class for running an instance of Chrome. | 21 /// A class for running an instance of Chrome. |
18 /// | 22 /// |
19 /// 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, |
20 /// 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 |
21 /// constructed, and is killed when [close] is called. | 25 /// constructed, and is killed when [close] is called. |
22 /// | 26 /// |
23 /// Any errors starting or running the process are reported through [onExit]. | 27 /// Any errors starting or running the process are reported through [onExit]. |
24 class Chrome implements Browser { | 28 class Chrome implements Browser { |
25 /// The underlying process. | 29 /// The underlying process. |
26 Process _process; | 30 Process _process; |
27 | 31 |
28 /// The temporary directory used as the browser's user data dir. | |
29 /// | |
30 /// A new data dir is created for each run to ensure that they're | |
31 /// well-isolated. | |
32 String _dir; | |
33 | |
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 Chrome 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 Chrome 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 Chrome(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; | |
56 return Process.start(executable, [ | 53 return Process.start(executable, [ |
57 "--user-data-dir=$_dir", | 54 "--user-data-dir=$dir", |
58 url.toString(), | 55 url.toString(), |
59 "--disable-extensions", | 56 "--disable-extensions", |
60 "--disable-popup-blocking", | 57 "--disable-popup-blocking", |
61 "--bwsi", | 58 "--bwsi", |
62 "--no-first-run", | 59 "--no-first-run", |
63 "--no-default-browser-check", | 60 "--no-default-browser-check", |
64 "--disable-default-apps", | 61 "--disable-default-apps", |
65 "--disable-translate" | 62 "--disable-translate" |
66 ]).then((process) { | 63 ]).then((process) { |
67 _process = process; | 64 _process = process; |
68 _onProcessStartedCompleter.complete(); | 65 _onProcessStartedCompleter.complete(); |
69 | 66 |
70 // TODO(nweiz): the browser's standard output is almost always useless | 67 // 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. | 68 // noise, but we should allow the user to opt in to seeing it. |
72 return _process.exitCode; | 69 return _process.exitCode; |
73 }); | 70 }); |
74 }).then((exitCode) { | 71 }).then((exitCode) { |
75 if (exitCode != 0) throw "Chrome failed with exit code $exitCode."; | 72 if (exitCode == 0) return null; |
76 }).then(_onExitCompleter.complete) | 73 |
77 .catchError(_onExitCompleter.completeError); | 74 return UTF8.decodeStream(_process.stderr).then((error) { |
| 75 throw new ApplicationException( |
| 76 "Chrome failed with exit code $exitCode:\n$error"); |
| 77 }); |
| 78 }).then(_onExitCompleter.complete).catchError((error, stackTrace) { |
| 79 if (stackTrace == null) stackTrace = new Trace.current(); |
| 80 _onExitCompleter.completeError( |
| 81 new ApplicationException( |
| 82 "Failed to start Chrome: ${getErrorMessage(error)}."), |
| 83 stackTrace); |
| 84 }); |
78 } | 85 } |
79 | 86 |
80 Future close() { | 87 Future close() { |
81 _onProcessStarted.then((_) => _process.kill()); | 88 _onProcessStarted.then((_) => _process.kill()); |
82 | 89 |
83 // Swallow exceptions. The user should explicitly use [onExit] for these. | 90 // Swallow exceptions. The user should explicitly use [onExit] for these. |
84 return onExit.catchError((_) {}); | 91 return onExit.catchError((_) {}); |
85 } | 92 } |
86 | 93 |
87 /// Return the default executable for the current operating system. | 94 /// Return the default executable for the current operating system. |
(...skipping 17 matching lines...) Expand all Loading... |
105 | 112 |
106 var path = p.join(prefix, suffix); | 113 var path = p.join(prefix, suffix); |
107 if (new File(p.join(prefix, suffix)).existsSync()) return path; | 114 if (new File(p.join(prefix, suffix)).existsSync()) return path; |
108 } | 115 } |
109 | 116 |
110 // Fall back on looking it up on the path. This probably won't work, but at | 117 // Fall back on looking it up on the path. This probably won't work, but at |
111 // least it will fail with a useful error message. | 118 // least it will fail with a useful error message. |
112 return "chrome.exe"; | 119 return "chrome.exe"; |
113 } | 120 } |
114 } | 121 } |
OLD | NEW |