OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library test.runner.browser.safari; | |
6 | |
7 import 'dart:async'; | |
kevmoo
2015/04/15 22:54:29
need dart:convert, too
| |
8 import 'dart:io'; | |
9 | |
10 import 'package:path/path.dart' as p; | |
11 | |
12 import '../../util/io.dart'; | |
13 import 'browser.dart'; | |
14 | |
15 /// A class for running an instance of Safari. | |
16 /// | |
17 /// Any errors starting or running the process are reported through [onExit]. | |
18 class Safari implements Browser { | |
19 /// The underlying process. | |
20 Process _process; | |
21 | |
22 Future get onExit => _onExitCompleter.future; | |
23 final _onExitCompleter = new Completer(); | |
24 | |
25 /// A future that completes when the browser process has started. | |
26 /// | |
27 /// This is used to ensure that [close] works regardless of when it's called. | |
28 Future get _onProcessStarted => _onProcessStartedCompleter.future; | |
29 final _onProcessStartedCompleter = new Completer(); | |
30 | |
31 /// Starts a new instance of Safari open to the given [url], which may be a | |
32 /// [Uri] or a [String]. | |
33 /// | |
34 /// If [executable] is passed, it's used as the Safari executable. Otherwise | |
35 /// the default executable name for the current OS will be used. | |
36 Safari(url, {String executable}) { | |
37 if (executable == null) { | |
38 executable = '/Applications/Safari.app/Contents/MacOS/Safari'; | |
39 } | |
40 | |
41 // Don't return a Future here because there's no need for the caller to wait | |
42 // for the process to actually start. They should just wait for the HTTP | |
43 // request instead. | |
44 withTempDir((dir) { | |
45 // Safari will only open files (not general URLs) via the command-line | |
46 // API, so we create a dummy file to redirect it to the page we actually | |
47 // want it to load. | |
48 var redirect = p.join(dir, 'redirect.html'); | |
49 new File(redirect).writeAsStringSync( | |
50 "<script>location = " + | |
51 HTML_ESCAPE.convert(JSON.encode(url.toString())) + "</script>"); | |
52 | |
53 return Process.start(executable, [redirect]).then((process) { | |
54 _process = process; | |
55 _onProcessStartedCompleter.complete(); | |
56 | |
57 // TODO(nweiz): the browser's standard output is almost always useless | |
58 // noise, but we should allow the user to opt in to seeing it. | |
59 return _process.exitCode; | |
60 }); | |
61 }).then((exitCode) { | |
62 if (exitCode != 0) throw "Safari failed with exit code $exitCode."; | |
63 }).then(_onExitCompleter.complete) | |
64 .catchError(_onExitCompleter.completeError); | |
65 } | |
66 | |
67 Future close() { | |
68 _onProcessStarted.then((_) => _process.kill()); | |
69 | |
70 // Swallow exceptions. The user should explicitly use [onExit] for these. | |
71 return onExit.catchError((_) {}); | |
72 } | |
73 } | |
OLD | NEW |