Bug 1100662 - Fix browser_notification_tab_switching.js to work in e10s mode. r=MattN, a=test-only
authorJared Wein <jwein@mozilla.com>
Tue, 24 Nov 2015 11:21:10 -0500
changeset 291700 cfcf9ab4f70505ab3b4058e957f16a92026b035b
parent 291699 a256d40aa813723030043f469ae57b3b9e1c8c29
child 291701 5b1d1495f58eabdd5ed89e2012c045b597402603
push id8782
push usercbook@mozilla.com
push dateTue, 01 Dec 2015 15:43:31 +0000
treeherdermozilla-aurora@d39cb9d10fcf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN, test-only
bugs1100662
milestone44.0a2
Bug 1100662 - Fix browser_notification_tab_switching.js to work in e10s mode. r=MattN, a=test-only
browser/base/content/test/alerts/browser.ini
browser/base/content/test/alerts/browser_notification_tab_switching.js
browser/base/content/test/alerts/file_dom_notifications.html
--- a/browser/base/content/test/alerts/browser.ini
+++ b/browser/base/content/test/alerts/browser.ini
@@ -3,11 +3,10 @@ support-files =
   head.js
   file_dom_notifications.html
 
 [browser_notification_close.js]
 [browser_notification_do_not_disturb.js]
 [browser_notification_open_settings.js]
 [browser_notification_remove_permission.js]
 [browser_notification_permission_migration.js]
-
 [browser_notification_tab_switching.js]
-skip-if = buildapp == 'mulet' || e10s # Bug 1100662 - content access causing uncaught exception - Error: cannot ipc non-cpow object at chrome://mochitests/content/browser/browser/base/content/test/general/browser_notification_tab_switching.js:32 (or in RemoteAddonsChild.jsm)
+skip-if = buildapp == 'mulet'
--- a/browser/base/content/test/alerts/browser_notification_tab_switching.js
+++ b/browser/base/content/test/alerts/browser_notification_tab_switching.js
@@ -4,92 +4,62 @@
 
 "use strict";
 
 var tab;
 var notification;
 var notificationURL = "http://example.org/browser/browser/base/content/test/alerts/file_dom_notifications.html";
 var newWindowOpenedFromTab;
 
-function test () {
-  waitForExplicitFinish();
-
+add_task(function* test_notificationPreventDefaultAndSwitchTabs() {
   let pm = Services.perms;
-  registerCleanupFunction(function() {
-    pm.remove(makeURI(notificationURL), "desktop-notification");
-    gBrowser.removeTab(tab);
-    window.restore();
-  });
-
   pm.add(makeURI(notificationURL), "desktop-notification", pm.ALLOW_ACTION);
 
-  tab = gBrowser.addTab(notificationURL);
-  tab.linkedBrowser.addEventListener("load", onLoad, true);
-}
-
-function onLoad() {
-  isnot(gBrowser.selectedTab, tab, "Notification page loaded as a background tab");
-  tab.linkedBrowser.removeEventListener("load", onLoad, true);
-  let win = tab.linkedBrowser.contentWindow.wrappedJSObject;
-  win.newWindow = win.open("about:blank", "", "height=100,width=100");
-  newWindowOpenedFromTab = win.newWindow;
-  win.newWindow.addEventListener("load", function() {
-    info("new window loaded");
-    win.newWindow.addEventListener("blur", function b() {
-      info("new window got blur");
-      win.newWindow.removeEventListener("blur", b);
-      notification = win.showNotification1();
-      win.newWindow.addEventListener("focus", onNewWindowFocused);
-      notification.addEventListener("show", onAlertShowing);
-    });
-
-    function waitUntilNewWindowHasFocus() {
-      if (!win.newWindow.document.hasFocus()) {
-        setTimeout(waitUntilNewWindowHasFocus, 50);
-      } else {
-        // Focus another window so that new window gets blur event.
-        gBrowser.selectedBrowser.contentWindow.focus();
-      }
-    }
-    win.newWindow.focus();
-    waitUntilNewWindowHasFocus();
-  });
-}
+  let originalTab = gBrowser.selectedTab;
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: notificationURL
+  }, function* dummyTabTask(aBrowser) {
+    // Put new tab in background so it is obvious when it is re-focused.
+    yield BrowserTestUtils.switchTab(gBrowser, originalTab);
+    isnot(gBrowser.selectedBrowser, aBrowser, "Notification page loaded as a background tab");
 
-function onAlertShowing() {
-  info("Notification alert showing");
-  notification.removeEventListener("show", onAlertShowing);
-
-  let alertWindow = Services.wm.getMostRecentWindow("alert:alert");
-  if (!alertWindow) {
-    ok(true, "Notifications don't use XUL windows on all platforms.");
+    // First, show a notification that will be have the tab-switching prevented.
+    let win = aBrowser.contentWindow.wrappedJSObject;
+    let notification = win.showNotification1();
+    yield BrowserTestUtils.waitForEvent(notification, "show");
+    info("Notification alert showing");
+    let alertWindow = Services.wm.getMostRecentWindow("alert:alert");
+    if (!alertWindow) {
+      ok(true, "Notifications don't use XUL windows on all platforms.");
+      notification.close();
+      return;
+    }
+    info("Clicking on notification");
+    let promiseClickEvent = BrowserTestUtils.waitForEvent(notification, "click");
+    EventUtils.synthesizeMouseAtCenter(alertWindow.document.getElementById("alertTitleLabel"),
+                                       {},
+                                       alertWindow);
+    let clickEvent = yield promiseClickEvent;
+    ok(clickEvent.defaultPrevented, "The event handler for the first notification cancels the event");
+    isnot(gBrowser.selectedBrowser, aBrowser, "Notification page still a background tab");
     notification.close();
-    newWindowOpenedFromTab.close();
-    finish();
-    return;
-  }
-  gBrowser.tabContainer.addEventListener("TabSelect", onTabSelect);
-  EventUtils.synthesizeMouseAtCenter(alertWindow.document.getElementById("alertTitleLabel"), {}, alertWindow);
-  info("Clicked on notification");
-  alertWindow.close();
-}
+    yield BrowserTestUtils.waitForEvent(notification, "close");
 
-function onNewWindowFocused(event) {
-  event.target.close();
-  isnot(gBrowser.selectedTab, tab, "Notification page loaded as a background tab");
-  // Using timeout to test that something do *not* happen!
-  setTimeout(openSecondNotification, 50);
-}
+    // Second, show a notification that will cause the tab to get switched.
+    let notification2 = win.showNotification2();
+    yield BrowserTestUtils.waitForEvent(notification2, "show");
+    alertWindow = Services.wm.getMostRecentWindow("alert:alert");
+    let promiseTabSelect = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabSelect");
+    EventUtils.synthesizeMouseAtCenter(alertWindow.document.getElementById("alertTitleLabel"),
+                                       {},
+                                       alertWindow);
+    yield promiseTabSelect;
+    is(gBrowser.selectedBrowser.currentURI.spec, notificationURL,
+       "Clicking on the second notification should select its originating tab");
+    notification2.close();
+    yield BrowserTestUtils.waitForEvent(notification2, "close");
+  });
+});
 
-function openSecondNotification() {
-  isnot(gBrowser.selectedTab, tab, "Notification page loaded as a background tab");
-  let win = tab.linkedBrowser.contentWindow.wrappedJSObject;
-  notification = win.showNotification2();
-  notification.addEventListener("show", onAlertShowing);
-}
-
-function onTabSelect() {
-  gBrowser.tabContainer.removeEventListener("TabSelect", onTabSelect);
-  is(gBrowser.selectedBrowser.contentWindow.location.href, notificationURL,
-     "Notification tab should be selected.");
-
-  finish();
-}
+add_task(function* cleanup() {
+  Services.perms.remove(makeURI(notificationURL), "desktop-notification");
+});
--- a/browser/base/content/test/alerts/file_dom_notifications.html
+++ b/browser/base/content/test/alerts/file_dom_notifications.html
@@ -10,18 +10,16 @@ function showNotification1() {
       lang: undefined,
       body: "Test body",
       tag: "Test tag",
       icon: undefined,
   };
   var n = new Notification("Test title", options);
   n.addEventListener("click", function(event) {
     event.preventDefault();
-    dump("Should focus new window.");
-    newWindow.focus();
   });
   return n;
 }
 
 function showNotification2() {
   var options = {
       dir: undefined,
       lang: undefined,