Bug 1428795 - Move recommended prefs to component. r?automatedtester draft
authorAndreas Tolfsen <ato@sny.no>
Mon, 26 Feb 2018 20:31:19 +0000
changeset 761788 1015df6e4bf8e41da352c46948840905bfe6fa5c
parent 761787 e33efdb3e1517d521deb949de3fcd6d9946ea440
child 761789 40b3706385a8f538e5811fb2d661c770e66c1930
push id101000
push userbmo:ato@sny.no
push dateThu, 01 Mar 2018 14:20:13 +0000
reviewersautomatedtester
bugs1428795
milestone60.0a1
Bug 1428795 - Move recommended prefs to component. r?automatedtester Setting the recommended preferences is not a task specific to the TCP listener. It makes more sense to set these in the Marionette XPCOM component where we manage the Marionette lifetime. MozReview-Commit-ID: G2RuLhKnX9X
testing/marionette/components/marionette.js
testing/marionette/server.js
--- a/testing/marionette/components/marionette.js
+++ b/testing/marionette/components/marionette.js
@@ -15,20 +15,21 @@ ChromeUtils.defineModuleGetter(this, "Pr
     "resource://gre/modules/Preferences.jsm");
 XPCOMUtils.defineLazyGetter(this, "log", () => {
   let log = Log.repository.getLogger("Marionette");
   log.addAppender(new Log.DumpAppender());
   return log;
 });
 
 const PREF_ENABLED = "marionette.enabled";
-const PREF_PORT = "marionette.port";
+const PREF_LOG_LEVEL_FALLBACK = "marionette.logging";
+const PREF_LOG_LEVEL = "marionette.log.level";
 const PREF_PORT_FALLBACK = "marionette.defaultPrefs.port";
-const PREF_LOG_LEVEL = "marionette.log.level";
-const PREF_LOG_LEVEL_FALLBACK = "marionette.logging";
+const PREF_PORT = "marionette.port";
+const PREF_RECOMMENDED = "marionette.prefs.recommended";
 
 const DEFAULT_LOG_LEVEL = "info";
 const NOTIFY_RUNNING = "remote-active";
 
 // Complements -marionette flag for starting the Marionette server.
 // We also set this if Marionette is running in order to start the server
 // again after a Firefox restart.
 const ENV_ENABLED = "MOZ_MARIONETTE";
@@ -41,16 +42,251 @@ const ENV_ENABLED = "MOZ_MARIONETTE";
 // a different profile in order to test things like Firefox refresh.
 // The environment variable itself, if present, is interpreted as a
 // JSON structure, with the keys mapping to preference names in the
 // "marionette." branch, and the values to the values of those prefs. So
 // something like {"port": 4444} would result in the marionette.port
 // pref being set to 4444.
 const ENV_PRESERVE_PREFS = "MOZ_MARIONETTE_PREF_STATE_ACROSS_RESTARTS";
 
+// Marionette sets preferences recommended for automation when it starts,
+// unless marionette.prefs.recommended has been set to false.
+// Where noted, some prefs should also be set in the profile passed to
+// Marionette to prevent them from affecting startup, since some of these
+// are checked before Marionette initialises.
+const RECOMMENDED_PREFS = new Map([
+
+  // Disable automatic downloading of new releases.
+  //
+  // This should also be set in the profile prior to starting Firefox,
+  // as it is picked up at runtime.
+  ["app.update.auto", false],
+
+  // Disable automatically upgrading Firefox.
+  //
+  // This should also be set in the profile prior to starting Firefox,
+  // as it is picked up at runtime.
+  ["app.update.enabled", false],
+
+  // Increase the APZ content response timeout in tests to 1 minute.
+  // This is to accommodate the fact that test environments tends to be
+  // slower than production environments (with the b2g emulator being
+  // the slowest of them all), resulting in the production timeout value
+  // sometimes being exceeded and causing false-positive test failures.
+  //
+  // (bug 1176798, bug 1177018, bug 1210465)
+  ["apz.content_response_timeout", 60000],
+
+  // Indicate that the download panel has been shown once so that
+  // whichever download test runs first doesn't show the popup
+  // inconsistently.
+  ["browser.download.panel.shown", true],
+
+  // Do not show the EULA notification.
+  //
+  // This should also be set in the profile prior to starting Firefox,
+  // as it is picked up at runtime.
+  ["browser.EULA.override", true],
+
+  // Turn off about:newtab and make use of about:blank instead for new
+  // opened tabs.
+  //
+  // This should also be set in the profile prior to starting Firefox,
+  // as it is picked up at runtime.
+  ["browser.newtabpage.enabled", false],
+
+  // Assume the about:newtab page's intro panels have been shown to not
+  // depend on which test runs first and happens to open about:newtab
+  ["browser.newtabpage.introShown", true],
+
+  // Never start the browser in offline mode
+  //
+  // This should also be set in the profile prior to starting Firefox,
+  // as it is picked up at runtime.
+  ["browser.offline", false],
+
+  // Background thumbnails in particular cause grief, and disabling
+  // thumbnails in general cannot hurt
+  ["browser.pagethumbnails.capturing_disabled", true],
+
+  // Disable safebrowsing components.
+  //
+  // These should also be set in the profile prior to starting Firefox,
+  // as it is picked up at runtime.
+  ["browser.safebrowsing.blockedURIs.enabled", false],
+  ["browser.safebrowsing.downloads.enabled", false],
+  ["browser.safebrowsing.passwords.enabled", false],
+  ["browser.safebrowsing.malware.enabled", false],
+  ["browser.safebrowsing.phishing.enabled", false],
+
+  // Disable updates to search engines.
+  //
+  // Should be set in profile.
+  ["browser.search.update", false],
+
+  // Do not restore the last open set of tabs if the browser has crashed
+  ["browser.sessionstore.resume_from_crash", false],
+
+  // Don't check for the default web browser during startup.
+  //
+  // These should also be set in the profile prior to starting Firefox,
+  // as it is picked up at runtime.
+  ["browser.shell.checkDefaultBrowser", false],
+
+  // Start with a blank page (about:blank)
+  ["browser.startup.page", 0],
+
+  // Do not redirect user when a milstone upgrade of Firefox is detected
+  ["browser.startup.homepage_override.mstone", "ignore"],
+
+  // Disable browser animations
+  ["toolkit.cosmeticAnimations.enabled", false],
+
+  // Do not allow background tabs to be zombified, otherwise for tests
+  // that open additional tabs, the test harness tab itself might get
+  // unloaded
+  ["browser.tabs.disableBackgroundZombification", false],
+
+  // Do not warn when closing all other open tabs
+  ["browser.tabs.warnOnCloseOtherTabs", false],
+
+  // Do not warn when multiple tabs will be opened
+  ["browser.tabs.warnOnOpen", false],
+
+  // Disable first run splash page on Windows 10
+  ["browser.usedOnWindows10.introURL", ""],
+
+  // Disable the UI tour.
+  //
+  // Should be set in profile.
+  ["browser.uitour.enabled", false],
+
+  // Turn off search suggestions in the location bar so as not to trigger
+  // network connections.
+  ["browser.urlbar.suggest.searches", false],
+
+  // Turn off the location bar search suggestions opt-in.  It interferes with
+  // tests that don't expect it to be there.
+  ["browser.urlbar.userMadeSearchSuggestionsChoice", true],
+
+  // Do not show datareporting policy notifications which can
+  // interfere with tests
+  [
+    "datareporting.healthreport.documentServerURI",
+    "http://%(server)s/dummy/healthreport/",
+  ],
+  ["datareporting.healthreport.logging.consoleEnabled", false],
+  ["datareporting.healthreport.service.enabled", false],
+  ["datareporting.healthreport.service.firstRun", false],
+  ["datareporting.healthreport.uploadEnabled", false],
+  ["datareporting.policy.dataSubmissionEnabled", false],
+  ["datareporting.policy.dataSubmissionPolicyAccepted", false],
+  ["datareporting.policy.dataSubmissionPolicyBypassNotification", true],
+
+  // Disable popup-blocker
+  ["dom.disable_open_during_load", false],
+
+  // Enabling the support for File object creation in the content process
+  ["dom.file.createInChild", true],
+
+  // Disable the ProcessHangMonitor
+  ["dom.ipc.reportProcessHangs", false],
+
+  // Disable slow script dialogues
+  ["dom.max_chrome_script_run_time", 0],
+  ["dom.max_script_run_time", 0],
+
+  // Only load extensions from the application and user profile
+  // AddonManager.SCOPE_PROFILE + AddonManager.SCOPE_APPLICATION
+  //
+  // Should be set in profile.
+  ["extensions.autoDisableScopes", 0],
+  ["extensions.enabledScopes", 5],
+
+  // Disable metadata caching for installed add-ons by default
+  ["extensions.getAddons.cache.enabled", false],
+
+  // Disable installing any distribution extensions or add-ons.
+  // Should be set in profile.
+  ["extensions.installDistroAddons", false],
+
+  // Make sure Shield doesn't hit the network.
+  ["extensions.shield-recipe-client.api_url", ""],
+
+  ["extensions.showMismatchUI", false],
+
+  // Turn off extension updates so they do not bother tests
+  ["extensions.update.enabled", false],
+  ["extensions.update.notifyUser", false],
+
+  // Make sure opening about:addons will not hit the network
+  [
+    "extensions.webservice.discoverURL",
+    "http://%(server)s/dummy/discoveryURL",
+  ],
+
+  // Allow the application to have focus even it runs in the background
+  ["focusmanager.testmode", true],
+
+  // Disable useragent updates
+  ["general.useragent.updates.enabled", false],
+
+  // Always use network provider for geolocation tests so we bypass the
+  // macOS dialog raised by the corelocation provider
+  ["geo.provider.testing", true],
+
+  // Do not scan Wifi
+  ["geo.wifi.scan", false],
+
+  // No hang monitor
+  ["hangmonitor.timeout", 0],
+
+  // Show chrome errors and warnings in the error console
+  ["javascript.options.showInConsole", true],
+
+  // Do not prompt for temporary redirects
+  ["network.http.prompt-temp-redirect", false],
+
+  // Disable speculative connections so they are not reported as leaking
+  // when they are hanging around
+  ["network.http.speculative-parallel-limit", 0],
+
+  // Do not automatically switch between offline and online
+  ["network.manage-offline-status", false],
+
+  // Make sure SNTP requests do not hit the network
+  ["network.sntp.pools", "%(server)s"],
+
+  // Local documents have access to all other local documents,
+  // including directory listings
+  ["security.fileuri.strict_origin_policy", false],
+
+  // Tests do not wait for the notification button security delay
+  ["security.notification_enable_delay", 0],
+
+  // Ensure blocklist updates do not hit the network
+  ["services.settings.server", "http://%(server)s/dummy/blocklist/"],
+
+  // Do not automatically fill sign-in forms with known usernames and
+  // passwords
+  ["signon.autofillForms", false],
+
+  // Disable password capture, so that tests that include forms are not
+  // influenced by the presence of the persistent doorhanger notification
+  ["signon.rememberSignons", false],
+
+  // Disable first-run welcome page
+  ["startup.homepage_welcome_url", "about:blank"],
+  ["startup.homepage_welcome_url.additional", ""],
+
+  // Prevent starting into safe mode after application crashes
+  ["toolkit.startup.max_resumed_crashes", -1],
+
+]);
+
 const isRemote = Services.appinfo.processType ==
     Services.appinfo.PROCESS_TYPE_CONTENT;
 
 const LogLevel = {
   get(level) {
     let levels = new Map([
       ["fatal", Log.Level.Fatal],
       ["error", Log.Level.Error],
@@ -150,16 +386,17 @@ class MarionetteMainProcess {
 
     // indicates that all pending window checks have been completed
     // and that we are ready to start the Marionette server
     this.finalUIStartup = false;
 
     log.level = prefs.logLevel;
 
     this.enabled = env.exists(ENV_ENABLED);
+    this.alteredPrefs = new Set();
 
     Services.prefs.addObserver(PREF_ENABLED, this);
     Services.ppmm.addMessageListener("Marionette:IsRunning", this);
   }
 
   get running() {
     return this.server && this.server.alive;
   }
@@ -290,16 +527,26 @@ class MarionetteMainProcess {
       // ... and for startup tests
       let startupRecorder = Promise.resolve();
       if ("@mozilla.org/test/startuprecorder;1" in Cc) {
         startupRecorder = Cc["@mozilla.org/test/startuprecorder;1"]
             .getService().wrappedJSObject.done;
       }
       await startupRecorder;
 
+      if (Preferences.get(PREF_RECOMMENDED)) {
+        for (let [k, v] of RECOMMENDED_PREFS) {
+          if (!Preferences.isSet(k)) {
+            log.debug(`Setting recommended pref ${k} to ${v}`);
+            Preferences.set(k, v);
+            this.alteredPrefs.add(k);
+          }
+        }
+      }
+
       try {
         ChromeUtils.import("chrome://marionette/content/server.js");
         let listener = new server.TCPListener(prefs.port);
         listener.start();
         this.server = listener;
       } catch (e) {
         log.fatal("Remote protocol server failed to start", e);
         Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
@@ -308,16 +555,21 @@ class MarionetteMainProcess {
       Services.obs.notifyObservers(this, NOTIFY_RUNNING, true);
       log.info(`Listening on port ${this.server.port}`);
     });
   }
 
   uninit() {
     if (this.running) {
       this.server.stop();
+      for (let k of this.alteredPrefs) {
+        log.debug(`Resetting recommended pref ${k}`);
+        Preferences.reset(k);
+      }
+      this.alteredPrefs.clear();
       Services.obs.notifyObservers(this, NOTIFY_RUNNING);
     }
   }
 
   get QueryInterface() {
     return XPCOMUtils.generateQI([
       Ci.nsICommandLineHandler,
       Ci.nsIMarionette,
--- a/testing/marionette/server.js
+++ b/testing/marionette/server.js
@@ -43,252 +43,16 @@ this.EXPORTED_SYMBOLS = ["server"];
 this.server = {};
 
 const PROTOCOL_VERSION = 3;
 
 const ENV_ENABLED = "MOZ_MARIONETTE";
 
 const PREF_CONTENT_LISTENER = "marionette.contentListener";
 const PREF_PORT = "marionette.port";
-const PREF_RECOMMENDED = "marionette.prefs.recommended";
-
-// Marionette sets preferences recommended for automation when it starts,
-// unless marionette.prefs.recommended has been set to false.
-// Where noted, some prefs should also be set in the profile passed to
-// Marionette to prevent them from affecting startup, since some of these
-// are checked before Marionette initialises.
-const RECOMMENDED_PREFS = new Map([
-
-  // Disable automatic downloading of new releases.
-  //
-  // This should also be set in the profile prior to starting Firefox,
-  // as it is picked up at runtime.
-  ["app.update.auto", false],
-
-  // Disable automatically upgrading Firefox.
-  //
-  // This should also be set in the profile prior to starting Firefox,
-  // as it is picked up at runtime.
-  ["app.update.enabled", false],
-
-  // Increase the APZ content response timeout in tests to 1 minute.
-  // This is to accommodate the fact that test environments tends to be
-  // slower than production environments (with the b2g emulator being
-  // the slowest of them all), resulting in the production timeout value
-  // sometimes being exceeded and causing false-positive test failures.
-  //
-  // (bug 1176798, bug 1177018, bug 1210465)
-  ["apz.content_response_timeout", 60000],
-
-  // Indicate that the download panel has been shown once so that
-  // whichever download test runs first doesn't show the popup
-  // inconsistently.
-  ["browser.download.panel.shown", true],
-
-  // Do not show the EULA notification.
-  //
-  // This should also be set in the profile prior to starting Firefox,
-  // as it is picked up at runtime.
-  ["browser.EULA.override", true],
-
-  // Turn off about:newtab and make use of about:blank instead for new
-  // opened tabs.
-  //
-  // This should also be set in the profile prior to starting Firefox,
-  // as it is picked up at runtime.
-  ["browser.newtabpage.enabled", false],
-
-  // Assume the about:newtab page's intro panels have been shown to not
-  // depend on which test runs first and happens to open about:newtab
-  ["browser.newtabpage.introShown", true],
-
-  // Never start the browser in offline mode
-  //
-  // This should also be set in the profile prior to starting Firefox,
-  // as it is picked up at runtime.
-  ["browser.offline", false],
-
-  // Background thumbnails in particular cause grief, and disabling
-  // thumbnails in general cannot hurt
-  ["browser.pagethumbnails.capturing_disabled", true],
-
-  // Disable safebrowsing components.
-  //
-  // These should also be set in the profile prior to starting Firefox,
-  // as it is picked up at runtime.
-  ["browser.safebrowsing.blockedURIs.enabled", false],
-  ["browser.safebrowsing.downloads.enabled", false],
-  ["browser.safebrowsing.passwords.enabled", false],
-  ["browser.safebrowsing.malware.enabled", false],
-  ["browser.safebrowsing.phishing.enabled", false],
-
-  // Disable updates to search engines.
-  //
-  // Should be set in profile.
-  ["browser.search.update", false],
-
-  // Do not restore the last open set of tabs if the browser has crashed
-  ["browser.sessionstore.resume_from_crash", false],
-
-  // Don't check for the default web browser during startup.
-  //
-  // These should also be set in the profile prior to starting Firefox,
-  // as it is picked up at runtime.
-  ["browser.shell.checkDefaultBrowser", false],
-
-  // Start with a blank page (about:blank)
-  ["browser.startup.page", 0],
-
-  // Do not redirect user when a milstone upgrade of Firefox is detected
-  ["browser.startup.homepage_override.mstone", "ignore"],
-
-  // Disable browser animations
-  ["toolkit.cosmeticAnimations.enabled", false],
-
-  // Do not allow background tabs to be zombified, otherwise for tests
-  // that open additional tabs, the test harness tab itself might get
-  // unloaded
-  ["browser.tabs.disableBackgroundZombification", false],
-
-  // Do not warn when closing all other open tabs
-  ["browser.tabs.warnOnCloseOtherTabs", false],
-
-  // Do not warn when multiple tabs will be opened
-  ["browser.tabs.warnOnOpen", false],
-
-  // Disable first run splash page on Windows 10
-  ["browser.usedOnWindows10.introURL", ""],
-
-  // Disable the UI tour.
-  //
-  // Should be set in profile.
-  ["browser.uitour.enabled", false],
-
-  // Turn off search suggestions in the location bar so as not to trigger
-  // network connections.
-  ["browser.urlbar.suggest.searches", false],
-
-  // Turn off the location bar search suggestions opt-in.  It interferes with
-  // tests that don't expect it to be there.
-  ["browser.urlbar.userMadeSearchSuggestionsChoice", true],
-
-  // Do not show datareporting policy notifications which can
-  // interfere with tests
-  [
-    "datareporting.healthreport.documentServerURI",
-    "http://%(server)s/dummy/healthreport/",
-  ],
-  ["datareporting.healthreport.logging.consoleEnabled", false],
-  ["datareporting.healthreport.service.enabled", false],
-  ["datareporting.healthreport.service.firstRun", false],
-  ["datareporting.healthreport.uploadEnabled", false],
-  ["datareporting.policy.dataSubmissionEnabled", false],
-  ["datareporting.policy.dataSubmissionPolicyAccepted", false],
-  ["datareporting.policy.dataSubmissionPolicyBypassNotification", true],
-
-  // Disable popup-blocker
-  ["dom.disable_open_during_load", false],
-
-  // Enabling the support for File object creation in the content process
-  ["dom.file.createInChild", true],
-
-  // Disable the ProcessHangMonitor
-  ["dom.ipc.reportProcessHangs", false],
-
-  // Disable slow script dialogues
-  ["dom.max_chrome_script_run_time", 0],
-  ["dom.max_script_run_time", 0],
-
-  // Only load extensions from the application and user profile
-  // AddonManager.SCOPE_PROFILE + AddonManager.SCOPE_APPLICATION
-  //
-  // Should be set in profile.
-  ["extensions.autoDisableScopes", 0],
-  ["extensions.enabledScopes", 5],
-
-  // Disable metadata caching for installed add-ons by default
-  ["extensions.getAddons.cache.enabled", false],
-
-  // Disable installing any distribution extensions or add-ons.
-  // Should be set in profile.
-  ["extensions.installDistroAddons", false],
-
-  // Make sure Shield doesn't hit the network.
-  ["extensions.shield-recipe-client.api_url", ""],
-
-  ["extensions.showMismatchUI", false],
-
-  // Turn off extension updates so they do not bother tests
-  ["extensions.update.enabled", false],
-  ["extensions.update.notifyUser", false],
-
-  // Make sure opening about:addons will not hit the network
-  [
-    "extensions.webservice.discoverURL",
-    "http://%(server)s/dummy/discoveryURL",
-  ],
-
-  // Allow the application to have focus even it runs in the background
-  ["focusmanager.testmode", true],
-
-  // Disable useragent updates
-  ["general.useragent.updates.enabled", false],
-
-  // Always use network provider for geolocation tests so we bypass the
-  // macOS dialog raised by the corelocation provider
-  ["geo.provider.testing", true],
-
-  // Do not scan Wifi
-  ["geo.wifi.scan", false],
-
-  // No hang monitor
-  ["hangmonitor.timeout", 0],
-
-  // Show chrome errors and warnings in the error console
-  ["javascript.options.showInConsole", true],
-
-  // Do not prompt for temporary redirects
-  ["network.http.prompt-temp-redirect", false],
-
-  // Disable speculative connections so they are not reported as leaking
-  // when they are hanging around
-  ["network.http.speculative-parallel-limit", 0],
-
-  // Do not automatically switch between offline and online
-  ["network.manage-offline-status", false],
-
-  // Make sure SNTP requests do not hit the network
-  ["network.sntp.pools", "%(server)s"],
-
-  // Local documents have access to all other local documents,
-  // including directory listings
-  ["security.fileuri.strict_origin_policy", false],
-
-  // Tests do not wait for the notification button security delay
-  ["security.notification_enable_delay", 0],
-
-  // Ensure blocklist updates do not hit the network
-  ["services.settings.server", "http://%(server)s/dummy/blocklist/"],
-
-  // Do not automatically fill sign-in forms with known usernames and
-  // passwords
-  ["signon.autofillForms", false],
-
-  // Disable password capture, so that tests that include forms are not
-  // influenced by the presence of the persistent doorhanger notification
-  ["signon.rememberSignons", false],
-
-  // Disable first-run welcome page
-  ["startup.homepage_welcome_url", "about:blank"],
-  ["startup.homepage_welcome_url.additional", ""],
-
-  // Prevent starting into safe mode after application crashes
-  ["toolkit.startup.max_resumed_crashes", -1],
-
-]);
 
 /**
  * Bootstraps Marionette and handles incoming client connections.
  *
  * Starting the Marionette server will open a TCP socket sporting the
  * debugger transport interface on the provided <var>port</var>.
  * For every new connection, a {@link server.TCPConnection} is created.
  */
@@ -298,17 +62,16 @@ server.TCPListener = class {
    *     Port for server to listen to.
    */
   constructor(port) {
     this.port = port;
     this.socket = null;
     this.conns = new Set();
     this.nextConnID = 0;
     this.alive = false;
-    this.alteredPrefs = new Set();
   }
 
   /**
    * Function produces a GeckoDriver.
    *
    * Determines the application to initialise the driver with.
    *
    * @return {GeckoDriver}
@@ -345,47 +108,30 @@ server.TCPListener = class {
    * The marionette.port preference will be populated with the value
    * of {@link #port}.
    */
   start() {
     if (this.alive) {
       return;
     }
 
-    if (Preferences.get(PREF_RECOMMENDED)) {
-      // set recommended prefs if they are not already user-defined
-      for (let [k, v] of RECOMMENDED_PREFS) {
-        if (!Preferences.isSet(k)) {
-          logger.debug(`Setting recommended pref ${k} to ${v}`);
-          Preferences.set(k, v);
-          this.alteredPrefs.add(k);
-        }
-      }
-    }
-
     // Start socket server and listening for connection attempts
     this.acceptConnections = true;
 
     Preferences.set(PREF_PORT, this.port);
     env.set(ENV_ENABLED, "1");
 
     this.alive = true;
   }
 
   stop() {
     if (!this.alive) {
       return;
     }
 
-    for (let k of this.alteredPrefs) {
-      logger.debug(`Resetting recommended pref ${k}`);
-      Preferences.reset(k);
-    }
-    this.alteredPrefs.clear();
-
     // Shutdown server socket, and no longer listen for new connections
     this.acceptConnections = false;
     this.alive = false;
   }
 
   onSocketAccepted(serverSocket, clientSocket) {
     let input = clientSocket.openInputStream(0, 0, 0);
     let output = clientSocket.openOutputStream(0, 0, 0);