Bug 1000114 - Extend test coverage for experiments in addon manager UI. r=unfocused, a=sylvestre
authorGeorg Fritzsche <georg.fritzsche@googlemail.com>
Thu, 08 May 2014 19:43:04 +0200
changeset 199224 dcb7e6f08e77ce65536d55f3514b00f2f5c26a69
parent 199223 50bfc782f73aa63059542483ac0ee7031bb3f52d
child 199225 6670f513893933a21adbc64c6887483e85f207c0
push id3624
push userasasaki@mozilla.com
push dateMon, 09 Jun 2014 21:49:01 +0000
treeherdermozilla-beta@b1a5da15899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersunfocused, sylvestre
bugs1000114
milestone31.0a2
Bug 1000114 - Extend test coverage for experiments in addon manager UI. r=unfocused, a=sylvestre
toolkit/mozapps/extensions/test/browser/browser_experiments.js
--- a/toolkit/mozapps/extensions/test/browser/browser_experiments.js
+++ b/toolkit/mozapps/extensions/test/browser/browser_experiments.js
@@ -6,50 +6,101 @@ let {AddonTestUtils} = Components.utils.
 let {HttpServer} = Components.utils.import("resource://testing-common/httpd.js", {});
 
 let gManagerWindow;
 let gCategoryUtilities;
 let gExperiments;
 let gHttpServer;
 
 let gSavedManifestURI;
+let gIsEnUsLocale;
+
+const SEC_IN_ONE_DAY = 24 * 60 * 60;
+const MS_IN_ONE_DAY  = SEC_IN_ONE_DAY * 1000;
 
 function getExperimentAddons() {
   let deferred = Promise.defer();
   AddonManager.getAddonsByTypes(["experiment"], (addons) => {
     deferred.resolve(addons);
   });
   return deferred.promise;
 }
 
+function getInstallItem() {
+  let doc = gManagerWindow.document;
+  let view = doc.getElementById("view-port").selectedPanel;
+  let list = doc.getElementById("addon-list");
+
+  let node = list.firstChild;
+  while (node) {
+    if (node.getAttribute("status") == "installing") {
+      return node;
+    }
+    node = node.nextSibling;
+  }
+
+  return null;
+}
+
+function patchPolicy(policy, data) {
+  for (let key of Object.keys(data)) {
+    Object.defineProperty(policy, key, {
+      value: data[key],
+      writable: true,
+    });
+  }
+}
+
+function defineNow(policy, time) {
+  patchPolicy(policy, { now: () => new Date(time) });
+}
+
+function openDetailsView(aId) {
+  let item = get_addon_element(gManagerWindow, aId);
+  Assert.ok(item, "Should have got add-on element.");
+  is_element_visible(item, "Add-on element should be visible.");
+
+  EventUtils.synthesizeMouseAtCenter(item, { clickCount: 1 }, gManagerWindow);
+  EventUtils.synthesizeMouseAtCenter(item, { clickCount: 2 }, gManagerWindow);
+
+  let deferred = Promise.defer();
+  wait_for_view_load(gManagerWindow, deferred.resolve);
+  return deferred.promise;
+}
+
 add_task(function* initializeState() {
   gManagerWindow = yield open_manager();
   gCategoryUtilities = new CategoryUtilities(gManagerWindow);
 
   registerCleanupFunction(() => {
     Services.prefs.clearUserPref("experiments.enabled");
     if (gHttpServer) {
       gHttpServer.stop(() => {});
       Services.prefs.clearUserPref("experiments.manifest.cert.checkAttributes");
       if (gSavedManifestURI !== undefined) {
         Services.prefs.setCharPref("experments.manifest.uri", gSavedManifestURI);
       }
     }
     if (gExperiments) {
-      gExperiments._policy.ignoreHashes = false;
+      let tmp = {};
+      Cu.import("resource:///modules/experiments/Experiments.jsm", tmp);
+      gExperiments._policy = new tmp.Experiments.Policy();
     }
   });
 
+  let chrome = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry);
+  gIsEnUsLocale = chrome.getSelectedLocale("global") == "en-US";
+
   // The Experiments Manager will interfere with us by preventing installs
   // of experiments it doesn't know about. We remove it from the equation
   // because here we are only concerned with core Addon Manager operation,
   // not the superset Experiments Manager has imposed.
   if ("@mozilla.org/browser/experiments-service;1" in Components.classes) {
     let tmp = {};
-    Components.utils.import("resource:///modules/experiments/Experiments.jsm", tmp);
+    Cu.import("resource:///modules/experiments/Experiments.jsm", tmp);
     // There is a race condition between XPCOM service initialization and
     // this test running. We have to initialize the instance first, then
     // uninitialize it to prevent this.
     gExperiments = tmp.Experiments.instance();
     yield gExperiments._mainTask;
     yield gExperiments.uninit();
   }
 });
@@ -152,16 +203,17 @@ add_task(function* testOpenPreferences()
 
   yield deferred.promise;
 });
 
 add_task(function* testButtonPresence() {
   yield gCategoryUtilities.openType("experiment");
   let item = get_addon_element(gManagerWindow, "test-experiment1@experiments.mozilla.org");
   Assert.ok(item, "Got add-on element.");
+  item.parentNode.ensureElementIsVisible(item);
 
   let el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "remove-btn");
   // Corresponds to the uninstall permission.
   is_element_visible(el, "Remove button is visible.");
   // Corresponds to lack of disable permission.
   el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "disable-btn");
   is_element_hidden(el, "Disable button not visible.");
   // Corresponds to lack of enable permission.
@@ -284,29 +336,255 @@ add_task(function testDeactivateExperime
   Assert.equal(addons[0].id, "experiment-1", "Add-on ID matches expected.");
 
   // Verify the UI looks sane.
 
   Assert.ok(gCategoryUtilities.isTypeVisible("experiment"), "Experiment tab visible.");
   let item = get_addon_element(gManagerWindow, "experiment-1");
   Assert.ok(item, "Got add-on element.");
   Assert.ok(!item.active, "Element should not be active.");
+  item.parentNode.ensureElementIsVisible(item);
 
   // User control buttons should not be present because previous experiments
   // should have no permissions.
   let el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "remove-btn");
   is_element_hidden(el, "Remove button is not visible.");
   el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "disable-btn");
   is_element_hidden(el, "Disable button is not visible.");
   el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "enable-btn");
   is_element_hidden(el, "Enable button is not visible.");
   el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "preferences-btn");
   is_element_hidden(el, "Preferences button is not visible.");
 });
 
+add_task(function testActivateRealExperiments() {
+  if (!gExperiments) {
+    info("Skipping experiments test because that feature isn't available.");
+    return;
+  }
+
+  yield gExperiments._updateExperiments({
+    "version": 1,
+    "experiments": [
+      {
+        id: "experiment-2",
+        xpiURL: TESTROOT + "addons/browser_experiment1.xpi",
+        xpiHash: "IRRELEVANT",
+        startTime: Date.now() / 1000 - 3600,
+        endTime: Date.now() / 1000 + 3600,
+        maxActiveSeconds: 600,
+        appName: [Services.appinfo.name],
+        channel: [gExperiments._policy.updatechannel()],
+      },
+    ],
+  });
+  yield gExperiments._run();
+
+  // Check the active experiment.
+
+  let item = get_addon_element(gManagerWindow, "test-experiment1@experiments.mozilla.org");
+  Assert.ok(item, "Got add-on element.");
+  item.parentNode.ensureElementIsVisible(item);
+
+  let el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "experiment-state");
+  is_element_visible(el, "Experiment state label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "Active");
+  }
+
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "experiment-time");
+  is_element_visible(el, "Experiment time label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "Less than a day remaining");
+  }
+
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "error-container");
+  is_element_hidden(el, "error-container should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "warning-container");
+  is_element_hidden(el, "warning-container should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "pending-container");
+  is_element_hidden(el, "pending-container should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "version");
+  is_element_hidden(el, "version should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "class", "disabled-postfix");
+  is_element_hidden(el, "disabled-postfix should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "class", "update-postfix");
+  is_element_hidden(el, "update-postfix should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "class", "experiment-bullet");
+  is_element_visible(el, "experiment-bullet should be visible.");
+
+  // Check the previous experiment.
+
+  item = get_addon_element(gManagerWindow, "experiment-1");
+  Assert.ok(item, "Got add-on element.");
+  item.parentNode.ensureElementIsVisible(item);
+
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "experiment-state");
+  is_element_visible(el, "Experiment state label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "Complete");
+  }
+
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "experiment-time");
+  is_element_visible(el, "Experiment time label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "Less than a day ago");
+  }
+
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "error-container");
+  is_element_hidden(el, "error-container should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "warning-container");
+  is_element_hidden(el, "warning-container should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "pending-container");
+  is_element_hidden(el, "pending-container should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "version");
+  is_element_hidden(el, "version should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "class", "disabled-postfix");
+  is_element_hidden(el, "disabled-postfix should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "class", "update-postfix");
+  is_element_hidden(el, "update-postfix should be hidden.");
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "class", "experiment-bullet");
+  is_element_visible(el, "experiment-bullet should be visible.");
+
+  // Install an "older" experiment.
+
+  yield gExperiments.disableExperiment("experiment-2");
+
+  let now = Date.now();
+  let fakeNow = now - 5 * MS_IN_ONE_DAY;
+  defineNow(gExperiments._policy, fakeNow);
+
+  yield gExperiments._updateExperiments({
+    "version": 1,
+    "experiments": [
+      {
+        id: "experiment-3",
+        xpiURL: TESTROOT + "addons/browser_experiment1.xpi",
+        xpiHash: "IRRELEVANT",
+        startTime: fakeNow / 1000 - SEC_IN_ONE_DAY,
+        endTime: now / 1000 + 10 * SEC_IN_ONE_DAY,
+        maxActiveSeconds: 100 * SEC_IN_ONE_DAY,
+        appName: [Services.appinfo.name],
+        channel: [gExperiments._policy.updatechannel()],
+      },
+    ],
+  });
+  yield gExperiments._run();
+
+  // Check the active experiment.
+
+  item = get_addon_element(gManagerWindow, "test-experiment1@experiments.mozilla.org");
+  Assert.ok(item, "Got add-on element.");
+  item.parentNode.ensureElementIsVisible(item);
+
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "experiment-state");
+  is_element_visible(el, "Experiment state label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "Active");
+  }
+
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "experiment-time");
+  is_element_visible(el, "Experiment time label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "10 days remaining");
+  }
+
+  // Disable it and check it's previous experiment entry.
+
+  yield gExperiments.disableExperiment("experiment-3");
+
+  item = get_addon_element(gManagerWindow, "experiment-3");
+  Assert.ok(item, "Got add-on element.");
+  item.parentNode.ensureElementIsVisible(item);
+
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "experiment-state");
+  is_element_visible(el, "Experiment state label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "Complete");
+  }
+
+  el = item.ownerDocument.getAnonymousElementByAttribute(item, "anonid", "experiment-time");
+  is_element_visible(el, "Experiment time label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "5 days ago");
+  }
+});
+
+add_task(function testDetailView() {
+  if (!gExperiments) {
+    info("Skipping experiments test because that feature isn't available.");
+    return;
+  }
+
+  defineNow(gExperiments._policy, Date.now());
+  yield gExperiments._updateExperiments({
+    "version": 1,
+    "experiments": [
+      {
+        id: "experiment-4",
+        xpiURL: TESTROOT + "addons/browser_experiment1.xpi",
+        xpiHash: "IRRELEVANT",
+        startTime: Date.now() / 1000 - 3600,
+        endTime: Date.now() / 1000 + 3600,
+        maxActiveSeconds: 600,
+        appName: [Services.appinfo.name],
+        channel: [gExperiments._policy.updatechannel()],
+      },
+    ],
+  });
+  yield gExperiments._run();
+
+  // Check active experiment.
+
+  yield openDetailsView("test-experiment1@experiments.mozilla.org");
+
+  let el = gManagerWindow.document.getElementById("detail-experiment-state");
+  is_element_visible(el, "Experiment state label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "Active");
+  }
+
+  el = gManagerWindow.document.getElementById("detail-experiment-time");
+  is_element_visible(el, "Experiment time label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "Less than a day remaining");
+  }
+
+  el = gManagerWindow.document.getElementById("detail-version");
+  is_element_hidden(el, "detail-version should be hidden.");
+  el = gManagerWindow.document.getElementById("detail-creator");
+  is_element_hidden(el, "detail-creator should be hidden.");
+  el = gManagerWindow.document.getElementById("detail-experiment-bullet");
+  is_element_visible(el, "experiment-bullet should be visible.");
+
+  // Check previous experiment.
+
+  yield gCategoryUtilities.openType("experiment");
+  yield openDetailsView("experiment-3");
+
+  let el = gManagerWindow.document.getElementById("detail-experiment-state");
+  is_element_visible(el, "Experiment state label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "Complete");
+  }
+
+  el = gManagerWindow.document.getElementById("detail-experiment-time");
+  is_element_visible(el, "Experiment time label should be visible.");
+  if (gIsEnUsLocale) {
+    Assert.equal(el.value, "5 days ago");
+  }
+
+  el = gManagerWindow.document.getElementById("detail-version");
+  is_element_hidden(el, "detail-version should be hidden.");
+  el = gManagerWindow.document.getElementById("detail-creator");
+  is_element_hidden(el, "detail-creator should be hidden.");
+  el = gManagerWindow.document.getElementById("detail-experiment-bullet");
+  is_element_visible(el, "experiment-bullet should be visible.");
+});
+
 add_task(function* testCleanup() {
   if (gExperiments) {
     Services.prefs.clearUserPref("experiments.enabled");
     Services.prefs.clearUserPref("experiments.manifest.cert.checkAttributes");
     Services.prefs.setCharPref("experiments.manifest.uri", gSavedManifestURI);
 
     // We perform the uninit/init cycle to purge any leftover state.
     yield OS.File.remove(gExperiments._cacheFilePath);