Bug 1168229 - Fix intermittent orange caused by dead CPOWs. r=markh, a=test-only
authorShane Caraveo <scaraveo@mozilla.com>
Thu, 12 May 2016 15:36:11 -0700
changeset 332925 6aa616713eaec74c48ec67b425490ed36881d49d
parent 332924 f25acdea677c75578e63899934f5f148df02fe9a
child 332926 babe237377ec8bb607ef632812ca1257be8956d0
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmarkh, test-only
bugs1168229
milestone48.0a2
Bug 1168229 - Fix intermittent orange caused by dead CPOWs. r=markh, a=test-only
browser/base/content/test/social/browser_aboutHome_activation.js
browser/base/content/test/social/social_activate.html
browser/base/content/test/social/social_postActivation.html
--- a/browser/base/content/test/social/browser_aboutHome_activation.js
+++ b/browser/base/content/test/social/browser_aboutHome_activation.js
@@ -1,16 +1,14 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
 var SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService;
 
-XPCOMUtils.defineLazyModuleGetter(this, "Promise",
-  "resource://gre/modules/Promise.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Task",
   "resource://gre/modules/Task.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "AboutHomeUtils",
   "resource:///modules/AboutHome.jsm");
 
 var snippet =
 '     <script>' +
 '       var manifest = {' +
@@ -55,96 +53,72 @@ var snippet2 =
 '     <img src="chrome://branding/content/icon32.png"></img>' +
 '     </div>';
 
 var gTests = [
 
 {
   desc: "Test activation with enable panel",
   snippet: snippet,
-  run: function (aSnippetsMap)
-  {
-    let deferred = Promise.defer();
-    let doc = gBrowser.selectedBrowser.contentDocument;
-
-    let snippetsElt = doc.getElementById("snippets");
-    ok(snippetsElt, "Found snippets element");
-    ok(!!doc.getElementById("activationSnippet"),
-       "The snippet is present.");
-
-    activateProvider(gBrowser.selectedTab, true, function() {
-      ok(SocialSidebar.provider, "provider activated");
-      checkSocialUI();
-      is(gBrowser.contentDocument.location.href, SocialSidebar.provider.manifest.postActivationURL);
-      gBrowser.removeTab(gBrowser.selectedTab);
-      SocialService.uninstallProvider(SocialSidebar.provider.origin, function () {
-        info("provider uninstalled");
-        deferred.resolve(true);
-      });
-    });
-    return deferred.promise;
-  }
+  panel: true
 },
 
 {
   desc: "Test activation bypassing enable panel",
   snippet: snippet2,
-  run: function (aSnippetsMap)
-  {
-    let deferred = Promise.defer();
-    let doc = gBrowser.selectedBrowser.contentDocument;
-
-    let snippetsElt = doc.getElementById("snippets");
-    ok(snippetsElt, "Found snippets element");
-    ok(!!doc.getElementById("activationSnippet"),
-       "The snippet is present.");
-
-    activateProvider(gBrowser.selectedTab, false, function() {
-      ok(SocialSidebar.provider, "provider activated");
-      checkSocialUI();
-      is(gBrowser.contentDocument.location.href, SocialSidebar.provider.manifest.postActivationURL);
-      gBrowser.removeTab(gBrowser.selectedTab);
-      SocialService.uninstallProvider(SocialSidebar.provider.origin, function () {
-        info("provider uninstalled");
-        deferred.resolve(true);
-      });
-    });
-    return deferred.promise;
-  }
+  panel: false
 }
 ];
 
 function test()
 {
   waitForExplicitFinish();
   requestLongerTimeout(2);
   ignoreAllUncaughtExceptions();
+  PopupNotifications.panel.setAttribute("animate", "false");
+  registerCleanupFunction(function () {
+    PopupNotifications.panel.removeAttribute("animate");
+  });
 
   Task.spawn(function () {
     for (let test of gTests) {
       info(test.desc);
 
       // Create a tab to run the test.
       let tab = gBrowser.selectedTab = gBrowser.addTab("about:blank");
 
       // Add an event handler to modify the snippets map once it's ready.
       let snippetsPromise = promiseSetupSnippetsMap(tab, test.snippet);
 
-      // Start loading about:home and wait for it to complete.
+      // Start loading about:home and wait for it to complete, snippets should be loaded
       yield promiseTabLoadEvent(tab, "about:home", "AboutHomeLoadSnippetsCompleted");
 
-      // This promise should already be resolved since the page is done,
-      // but we still want to get the snippets map out of it.
-      let snippetsMap = yield snippetsPromise;
+      yield snippetsPromise;
+
+      // ensure our activation snippet is indeed available
+      yield ContentTask.spawn(tab.linkedBrowser, {}, function*(arg) {
+        ok(!!content.document.getElementById("snippets"), "Found snippets element");
+        ok(!!content.document.getElementById("activationSnippet"), "The snippet is present.");
+      });
 
-      info("Running test");
-      let testPromise = test.run(snippetsMap);
-      yield testPromise;
-      info("Cleanup");
-      gBrowser.removeCurrentTab();
+      yield new Promise(resolve => {
+        activateProvider(tab, test.panel).then(() => {
+          ok(SocialSidebar.provider, "provider activated");
+          checkSocialUI();
+          is(gBrowser.currentURI.spec, SocialSidebar.provider.manifest.postActivationURL, "postActivationURL was loaded");
+          SocialService.uninstallProvider(SocialSidebar.provider.origin, function () {
+            info("provider uninstalled");
+            resolve();
+          });
+        });
+      });
+
+      // activation opened a post-activation info tab, close it.
+      yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
+      yield BrowserTestUtils.removeTab(tab);
     }
   }).then(finish, ex => {
     ok(false, "Unexpected Exception: " + ex);
     finish();
   });
 }
 
 /**
@@ -155,30 +129,30 @@ function test()
  * @param aUrl
  *        The url to load.
  * @param aEvent
  *        The load event type to wait for.  Defaults to "load".
  * @return {Promise} resolved when the event is handled.
  */
 function promiseTabLoadEvent(aTab, aURL, aEventType="load")
 {
-  let deferred = Promise.defer();
-  info("Wait tab event: " + aEventType);
-  aTab.linkedBrowser.addEventListener(aEventType, function load(event) {
-    if (event.originalTarget != aTab.linkedBrowser.contentDocument ||
-        event.target.location.href == "about:blank") {
-      info("skipping spurious load event");
-      return;
-    }
-    aTab.linkedBrowser.removeEventListener(aEventType, load, true);
-    info("Tab event received: " + aEventType);
-    deferred.resolve();
-  }, true, true);
-  aTab.linkedBrowser.loadURI(aURL);
-  return deferred.promise;
+  return new Promise(resolve => {
+    info("Wait tab event: " + aEventType);
+    aTab.linkedBrowser.addEventListener(aEventType, function load(event) {
+      if (event.originalTarget != aTab.linkedBrowser.contentDocument ||
+          event.target.location.href == "about:blank") {
+        info("skipping spurious load event");
+        return;
+      }
+      aTab.linkedBrowser.removeEventListener(aEventType, load, true);
+      info("Tab event received: " + aEventType);
+      resolve();
+    }, true, true);
+    aTab.linkedBrowser.loadURI(aURL);
+  });
 }
 
 /**
  * Cleans up snippets and ensures that by default we don't try to check for
  * remote snippets since that may cause network bustage or slowness.
  *
  * @param aTab
  *        The tab containing about:home.
@@ -189,90 +163,72 @@ function promiseTabLoadEvent(aTab, aURL,
 function promiseSetupSnippetsMap(aTab, aSnippet)
 {
   info("Waiting for snippets map");
 
   return ContentTask.spawn(aTab.linkedBrowser,
                     {snippetsVersion: AboutHomeUtils.snippetsVersion,
                      snippet: aSnippet},
                     function*(arg) {
-
-    let obj = {};
-    Cu.import("resource://gre/modules/Promise.jsm", obj);
-    let deferred = obj.Promise.defer();
+    return new Promise(resolve => {
+      addEventListener("AboutHomeLoadSnippets", function load(event) {
+        removeEventListener("AboutHomeLoadSnippets", load, true);
 
-    addEventListener("AboutHomeLoadSnippets", function load(event) {
-      removeEventListener("AboutHomeLoadSnippets", load, true);
-
-      let cw = content.window.wrappedJSObject;
+        let cw = content.window.wrappedJSObject;
 
-      // The snippets should already be ready by this point. Here we're
-      // just obtaining a reference to the snippets map.
-      cw.ensureSnippetsMapThen(function (aSnippetsMap) {
-        aSnippetsMap = Cu.waiveXrays(aSnippetsMap);
-        console.log("Got snippets map: " +
-             "{ last-update: " + aSnippetsMap.get("snippets-last-update") +
-             ", cached-version: " + aSnippetsMap.get("snippets-cached-version") +
-             " }");
-        // Don't try to update.
-        aSnippetsMap.set("snippets-last-update", Date.now());
-        aSnippetsMap.set("snippets-cached-version", arg.snippetsVersion);
-        // Clear snippets.
-        aSnippetsMap.delete("snippets");
-        aSnippetsMap.set("snippets", arg.snippet);
-        deferred.resolve();
-      });
-    }, true, true);
-
-    return deferred.promise;
-
+        // The snippets should already be ready by this point. Here we're
+        // just obtaining a reference to the snippets map.
+        cw.ensureSnippetsMapThen(function (aSnippetsMap) {
+          aSnippetsMap = Cu.waiveXrays(aSnippetsMap);
+          console.log("Got snippets map: " +
+               "{ last-update: " + aSnippetsMap.get("snippets-last-update") +
+               ", cached-version: " + aSnippetsMap.get("snippets-cached-version") +
+               " }");
+          // Don't try to update.
+          aSnippetsMap.set("snippets-last-update", Date.now());
+          aSnippetsMap.set("snippets-cached-version", arg.snippetsVersion);
+          // Clear snippets.
+          aSnippetsMap.delete("snippets");
+          aSnippetsMap.set("snippets", arg.snippet);
+          resolve();
+        });
+      }, true, true);
+    });
   });
 }
 
 
-function sendActivationEvent(tab, callback) {
+function sendActivationEvent(tab) {
   // hack Social.lastEventReceived so we don't hit the "too many events" check.
   Social.lastEventReceived = 0;
   let doc = tab.linkedBrowser.contentDocument;
   // if our test has a frame, use it
   if (doc.defaultView.frames[0])
     doc = doc.defaultView.frames[0].document;
   let button = doc.getElementById("activationSnippet");
   BrowserTestUtils.synthesizeMouseAtCenter(button, {}, tab.linkedBrowser);
-  executeSoon(callback);
 }
 
 function activateProvider(tab, expectPanel, aCallback) {
-  if (expectPanel) {
-    let panel = document.getElementById("servicesInstall-notification");
-    PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() {
-      PopupNotifications.panel.removeEventListener("popupshown", onpopupshown);
-      panel.button.click();
-    });
-  }
-  sendActivationEvent(tab, function() {
-    waitForProviderLoad(function() {
+  return new Promise(resolve => {
+    if (expectPanel) {
+      ensureEventFired(PopupNotifications.panel, "popupshown").then(() => {
+        let panel = document.getElementById("servicesInstall-notification");
+        panel.button.click();
+      });
+    }
+    waitForProviderLoad().then(() => {
       ok(SocialSidebar.provider, "new provider is active");
       ok(SocialSidebar.opened, "sidebar is open");
       checkSocialUI();
-      executeSoon(aCallback);
+      resolve();
     });
+    sendActivationEvent(tab);
   });
 }
 
 function waitForProviderLoad(cb) {
-  Services.obs.addObserver(function providerSet(subject, topic, data) {
-    Services.obs.removeObserver(providerSet, "social:provider-enabled");
-    info("social:provider-enabled observer was notified");
-    waitForCondition(function() {
-      let provider = SocialSidebar.provider;
-      let postActivation = provider && gBrowser.contentDocument.location.href == provider.origin + "/browser/browser/base/content/test/social/social_postActivation.html";
-
-      return provider &&
-             postActivation &&
-             SocialSidebar.browser.docShellIsActive;
-    }, function() {
-      // executeSoon to let the browser UI observers run first
-      executeSoon(cb);
-    },
-    "waitForProviderLoad: provider sidebar was not set, "+SocialSidebar.provider+", "+SocialSidebar.browser.docShellIsActive);
-  }, "social:provider-enabled", false);
+  return Promise.all([
+    promiseObserverNotified("social:provider-enabled"),
+    ensureFrameLoaded(gBrowser, "https://example.com/browser/browser/base/content/test/social/social_postActivation.html"),
+    ensureFrameLoaded(SocialSidebar.browser)
+  ]);
 }
--- a/browser/base/content/test/social/social_activate.html
+++ b/browser/base/content/test/social/social_activate.html
@@ -1,11 +1,12 @@
 <html>
 <head>
-	<title>Activation test</title>
+    <meta charset="utf-8">
+    <title>Activation test</title>
 </head>
 <script>
 // icons from http://findicons.com/icon/158311/firefox?id=356182 by ipapun
 var data = {
   // currently required
   "name": "Demo Social Service",
   "iconURL": "chrome://branding/content/icon16.png",
   "icon32URL": "chrome://branding/content/favicon32.png",
--- a/browser/base/content/test/social/social_postActivation.html
+++ b/browser/base/content/test/social/social_postActivation.html
@@ -1,11 +1,12 @@
 <html>
 <head>
-	<title>Post-Activation test</title>
+    <meta charset="utf-8">
+    <title>Post-Activation test</title>
 </head>
 
 <body>
 
 Post Activation landing page
 
 </body>
 </html>