Bug 1049001 - Refactor mozscreenshots Preferences configuration to fix a typo and reduce duplication. r=me
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Tue, 13 Sep 2016 14:27:26 -0700
changeset 313651 bf23ff06c7cdcc66eb1a4d4a4db4637a3d7bd94f
parent 313650 f5d043ce6d36a3c461cbd829d4a4a38394b7c436
child 313652 46f83778b4bb441770406754db09ada5911600fd
push id20526
push usermozilla@noorenberghe.ca
push dateWed, 14 Sep 2016 00:03:14 +0000
treeherderfx-team@46f83778b4bb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersme
bugs1049001
milestone51.0a1
Bug 1049001 - Refactor mozscreenshots Preferences configuration to fix a typo and reduce duplication. r=me MozReview-Commit-ID: 6dv83WERL78
browser/tools/mozscreenshots/mozscreenshots/extension/configurations/Preferences.jsm
--- a/browser/tools/mozscreenshots/mozscreenshots/extension/configurations/Preferences.jsm
+++ b/browser/tools/mozscreenshots/mozscreenshots/extension/configurations/Preferences.jsm
@@ -5,81 +5,95 @@
 "use strict";
 
 this.EXPORTED_SYMBOLS = ["Preferences"];
 
 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Task.jsm");
-Cu.import("resource://gre/modules/Timer.jsm");
 Cu.import("resource://testing-common/TestUtils.jsm");
 Cu.import("resource://testing-common/ContentTask.jsm");
 
 this.Preferences = {
 
   init(libDir) {
-    Services.prefs.setBoolPref("browser.preferences.inContent", true);
-
     let panes = [
       ["paneGeneral", null],
       ["paneSearch", null],
       ["paneContent", null],
       ["paneApplications", null],
       ["panePrivacy", null],
+      ["panePrivacy", null, DNTDialog],
       ["paneSecurity", null],
       ["paneSync", null],
       ["paneAdvanced", "generalTab"],
       ["paneAdvanced", "dataChoicesTab"],
       ["paneAdvanced", "networkTab"],
       ["paneAdvanced", "updateTab"],
       ["paneAdvanced", "encryptionTab"],
     ];
-    for (let [primary, advanced] of panes) {
+    for (let [primary, advanced, customFn] of panes) {
       let configName = primary.replace(/^pane/, "prefs") + (advanced ? "-" + advanced : "");
+      if (customFn) {
+        configName += "-" + customFn.name;
+      }
       this.configurations[configName] = {};
-      this.configurations[configName].applyConfig = prefHelper.bind(null, primary, advanced);
+      this.configurations[configName].applyConfig = prefHelper.bind(null, primary, advanced, customFn);
     }
   },
 
-  configurations: {
-    "panePrivacy-DNTDialog": {
-      applyConfig: Task.async(function*() {
-        let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
-        yield prefHelper("panePrivacy", null);
-
-        yield ContentTask.spawn(browserWindow.gBrowser.selectedBrowser, null, function* () {
-          content.document.getElementById("doNotTrackSettings").click();
-        });
-      }),
-    },
-  },
+  configurations: {},
 };
 
-let prefHelper = Task.async(function*(primary, advanced) {
+let prefHelper = Task.async(function*(primary, advanced = null, customFn = null) {
   let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
-  let selectedBrowser = browserWindow.gBrowser;
+  let selectedBrowser = browserWindow.gBrowser.selectedBrowser;
+
+  // close any dialog that might still be open
+  yield ContentTask.spawn(selectedBrowser, null, function*() {
+    if (!content.window.gSubDialog) {
+      return;
+    }
+    content.window.gSubDialog.close();
+  });
+
   let readyPromise = null;
   if (selectedBrowser.currentURI.specIgnoringRef == "about:preferences") {
-    readyPromise = new Promise((resolve) => {
-      browserWindow.addEventListener("MozAfterPaint", function paneSwitch() {
-        browserWindow.removeEventListener("MozAfterPaint", paneSwitch);
-        resolve();
-      });
-    });
-
+    if (selectedBrowser.currentURI.spec == "about:preferences#" + primary.replace(/^pane/, "")) {
+      // We're already on the correct pane.
+      readyPromise = Promise.resolve();
+    } else {
+      readyPromise = paintPromise(browserWindow);
+    }
   } else {
     readyPromise = TestUtils.topicObserved("advanced-pane-loaded");
   }
 
   if (primary == "paneAdvanced") {
     browserWindow.openAdvancedPreferences(advanced);
   } else {
     browserWindow.openPreferences(primary);
   }
 
   yield readyPromise;
 
-  // close any dialog that might still be open
-  yield ContentTask.spawn(selectedBrowser.selectedBrowser, null, function*() {
-    content.window.gSubDialog.close();
+  if (customFn) {
+    let customPaintPromise = paintPromise(browserWindow);
+    yield* customFn(selectedBrowser);
+    yield customPaintPromise;
+  }
+});
+
+function paintPromise(browserWindow) {
+  return new Promise((resolve) => {
+    browserWindow.addEventListener("MozAfterPaint", function onPaint() {
+      browserWindow.removeEventListener("MozAfterPaint", onPaint);
+      resolve();
+    });
   });
-});
+}
+
+function* DNTDialog(aBrowser) {
+  yield ContentTask.spawn(aBrowser, null, function* () {
+    content.document.getElementById("doNotTrackSettings").click();
+  });
+}