Chromium Code Reviews| 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'; | |
| 8 import 'dart:io'; | |
|
kevmoo
2015/04/15 23:03:22
still need dart:convert
| |
| 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 = " + JSON.encode(url.toString()) + "</script>"); | |
| 51 | |
| 52 return Process.start(executable, [redirect]).then((process) { | |
| 53 _process = process; | |
| 54 _onProcessStartedCompleter.complete(); | |
| 55 | |
| 56 // TODO(nweiz): the browser's standard output is almost always useless | |
| 57 // noise, but we should allow the user to opt in to seeing it. | |
| 58 return _process.exitCode; | |
| 59 }); | |
| 60 }).then((exitCode) { | |
| 61 if (exitCode != 0) throw "Safari failed with exit code $exitCode."; | |
| 62 }).then(_onExitCompleter.complete) | |
| 63 .catchError(_onExitCompleter.completeError); | |
| 64 } | |
| 65 | |
| 66 Future close() { | |
| 67 _onProcessStarted.then((_) => _process.kill()); | |
| 68 | |
| 69 // Swallow exceptions. The user should explicitly use [onExit] for these. | |
| 70 return onExit.catchError((_) {}); | |
| 71 } | |
| 72 } | |
| OLD | NEW |