From e6f65753852bc93e2cd928d032da1e7f3411f0d8 Mon Sep 17 00:00:00 2001
From: Natalie Weizenbaum <nweiz@google.com>
Date: Thu, 5 Mar 2015 17:38:29 -0800
Subject: [PATCH] Better configure the Chrome launcher.

This uses the right executables on OS X and Windows, and passes flags
to disable even more features.

R=kevmoo@google.com

Review URL: https://codereview.chromium.org//978093003
---
 lib/src/runner/browser/chrome.dart | 39 +++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)

diff --git a/lib/src/runner/browser/chrome.dart b/lib/src/runner/browser/chrome.dart
index 2a198a15..8b155c11 100644
--- a/lib/src/runner/browser/chrome.dart
+++ b/lib/src/runner/browser/chrome.dart
@@ -7,6 +7,8 @@ library unittest.runner.browser.chrome;
 import 'dart:async';
 import 'dart:io';
 
+import 'package:path/path.dart' as p;
+
 import '../../util/io.dart';
 
 // TODO(nweiz): move this into its own package?
@@ -45,9 +47,9 @@ class Chrome {
   /// [Uri] or a [String].
   ///
   /// If [executable] is passed, it's used as the Chrome executable. Otherwise
-  /// `"google-chrome"` will be looked up on the system PATH.
+  /// the default executable name for the current OS will be used.
   Chrome(url, {String executable}) {
-    if (executable == null) executable = "google-chrome";
+    if (executable == null) executable = _defaultExecutable();
 
     // Don't return a Future here because there's no need for the caller to wait
     // for the process to actually start. They should just wait for the HTTP
@@ -60,7 +62,10 @@ class Chrome {
         "--disable-extensions",
         "--disable-popup-blocking",
         "--bwsi",
-        "--no-first-run"
+        "--no-first-run",
+        "--no-default-browser-check",
+        "--disable-default-apps",
+        "--disable-translate"
       ]).then((process) {
         _process = process;
         _onProcessStartedCompleter.complete();
@@ -85,4 +90,32 @@ class Chrome {
     // Swallow exceptions. The user should explicitly use [onExit] for these.
     return onExit.catchError((_) {});
   }
+
+  /// Return the default executable for the current operating system.
+  String _defaultExecutable() {
+    if (Platform.isMacOS) {
+      return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
+    }
+    if (!Platform.isWindows) return 'google-chrome';
+
+    // Chrome could be installed in several places on Windows. The only way to
+    // find it is to check.
+    var prefixes = [
+      Platform.environment['LOCALAPPDATA'],
+      Platform.environment['PROGRAMFILES'],
+      Platform.environment['PROGRAMFILES(X86)']
+    ];
+    var suffix = r'Google\Chrome\Application\chrome.exe';
+
+    for (var prefix in prefixes) {
+      if (prefix == null) continue;
+
+      var path = p.join(prefix, suffix);
+      if (new File(p.join(prefix, suffix)).existsSync()) return path;
+    }
+
+    // Fall back on looking it up on the path. This probably won't work, but at
+    // least it will fail with a useful error message.
+    return "chrome.exe";
+  }
 }
-- 
GitLab