| 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.internet_explorer; | 5 library test.runner.browser.internet_explorer; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | |
| 9 import 'dart:io'; | 8 import 'dart:io'; |
| 10 | 9 |
| 11 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
| 12 import 'package:stack_trace/stack_trace.dart'; | |
| 13 | 11 |
| 14 import '../../utils.dart'; | |
| 15 import '../application_exception.dart'; | |
| 16 import 'browser.dart'; | 12 import 'browser.dart'; |
| 17 | 13 |
| 18 /// A class for running an instance of Internet Explorer. | 14 /// A class for running an instance of Internet Explorer. |
| 19 /// | 15 /// |
| 20 /// Any errors starting or running the process are reported through [onExit]. | 16 /// Any errors starting or running the process are reported through [onExit]. |
| 21 class InternetExplorer implements Browser { | 17 class InternetExplorer extends Browser { |
| 22 /// The underlying process. | 18 final name = "Internet Explorer"; |
| 23 Process _process; | |
| 24 | 19 |
| 25 Future get onExit => _onExitCompleter.future; | 20 InternetExplorer(url, {String executable}) |
| 26 final _onExitCompleter = new Completer(); | 21 : super(() => _startBrowser(url, executable)); |
| 27 | |
| 28 /// A future that completes when the browser process has started. | |
| 29 /// | |
| 30 /// This is used to ensure that [close] works regardless of when it's called. | |
| 31 Future get _onProcessStarted => _onProcessStartedCompleter.future; | |
| 32 final _onProcessStartedCompleter = new Completer(); | |
| 33 | 22 |
| 34 /// Starts a new instance of Internet Explorer open to the given [url], which | 23 /// Starts a new instance of Internet Explorer open to the given [url], which |
| 35 /// may be a [Uri] or a [String]. | 24 /// may be a [Uri] or a [String]. |
| 36 /// | 25 /// |
| 37 /// If [executable] is passed, it's used as the Internet Explorer executable. | 26 /// If [executable] is passed, it's used as the Internet Explorer executable. |
| 38 /// Otherwise the default executable name will be used. | 27 /// Otherwise the default executable name for the current OS will be used. |
| 39 InternetExplorer(url, {String executable}) { | 28 static Future<Process> _startBrowser(url, [String executable]) { |
| 40 if (executable == null) executable = _defaultExecutable(); | 29 if (executable == null) executable = _defaultExecutable(); |
| 41 | 30 return Process.start(executable, ['-extoff', url.toString()]); |
| 42 // Don't return a Future here because there's no need for the caller to wait | |
| 43 // for the process to actually start. They should just wait for the HTTP | |
| 44 // request instead. | |
| 45 invoke(() async { | |
| 46 try { | |
| 47 var process = await Process.start( | |
| 48 executable, ['-extoff', url.toString()]); | |
| 49 | |
| 50 _process = process; | |
| 51 _onProcessStartedCompleter.complete(); | |
| 52 | |
| 53 // TODO(nweiz): the browser's standard output is almost always useless | |
| 54 // noise, but we should allow the user to opt in to seeing it. | |
| 55 var exitCode = await _process.exitCode; | |
| 56 if (exitCode != 0) { | |
| 57 var error = await UTF8.decodeStream(_process.stderr); | |
| 58 throw new ApplicationException( | |
| 59 "Internet Explorer failed with exit code $exitCode:\n$error"); | |
| 60 } | |
| 61 | |
| 62 _onExitCompleter.complete(); | |
| 63 } catch (error, stackTrace) { | |
| 64 if (stackTrace == null) stackTrace = new Trace.current(); | |
| 65 _onExitCompleter.completeError( | |
| 66 new ApplicationException( | |
| 67 "Failed to start Internet Explorer: " | |
| 68 "${getErrorMessage(error)}."), | |
| 69 stackTrace); | |
| 70 } | |
| 71 }); | |
| 72 } | |
| 73 | |
| 74 Future close() { | |
| 75 _onProcessStarted.then((_) => _process.kill()); | |
| 76 | |
| 77 // Swallow exceptions. The user should explicitly use [onExit] for these. | |
| 78 return onExit.catchError((_) {}); | |
| 79 } | 31 } |
| 80 | 32 |
| 81 /// Return the default executable for the current operating system. | 33 /// Return the default executable for the current operating system. |
| 82 String _defaultExecutable() { | 34 static String _defaultExecutable() { |
| 83 // Internet Explorer could be installed in several places on Windows. The | 35 // Internet Explorer could be installed in several places on Windows. The |
| 84 // only way to find it is to check. | 36 // only way to find it is to check. |
| 85 var prefixes = [ | 37 var prefixes = [ |
| 86 Platform.environment['PROGRAMW6432'], | 38 Platform.environment['PROGRAMW6432'], |
| 87 Platform.environment['PROGRAMFILES'], | 39 Platform.environment['PROGRAMFILES'], |
| 88 Platform.environment['PROGRAMFILES(X86)'] | 40 Platform.environment['PROGRAMFILES(X86)'] |
| 89 ]; | 41 ]; |
| 90 var suffix = r'Internet Explorer\iexplore.exe'; | 42 var suffix = r'Internet Explorer\iexplore.exe'; |
| 91 | 43 |
| 92 for (var prefix in prefixes) { | 44 for (var prefix in prefixes) { |
| 93 if (prefix == null) continue; | 45 if (prefix == null) continue; |
| 94 | 46 |
| 95 var path = p.join(prefix, suffix); | 47 var path = p.join(prefix, suffix); |
| 96 if (new File(p.join(prefix, suffix)).existsSync()) return path; | 48 if (new File(p.join(prefix, suffix)).existsSync()) return path; |
| 97 } | 49 } |
| 98 | 50 |
| 99 // Fall back on looking it up on the path. This probably won't work, but at | 51 // Fall back on looking it up on the path. This probably won't work, but at |
| 100 // least it will fail with a useful error message. | 52 // least it will fail with a useful error message. |
| 101 return "iexplore.exe"; | 53 return "iexplore.exe"; |
| 102 } | 54 } |
| 103 } | 55 } |
| OLD | NEW |