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