fune/browser/base/content/test/alerts/browser_notification_tab_switching.js
Matthew Noorenberghe d1625cdffe Bug 1208295 - Open content preferences after receiving notifications-open-settings. r=jaws
--HG--
rename : browser/base/content/test/general/browser_notification_tab_switching.js => browser/base/content/test/alerts/browser_notification_tab_switching.js
rename : browser/base/content/test/general/file_dom_notifications.html => browser/base/content/test/alerts/file_dom_notifications.html
extra : commitid : L8SBHW3urul
extra : rebase_source : 2b308872a836a6df9634e991409c008f3e72040c
2015-09-25 13:18:29 -07:00

95 lines
3.1 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
"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();
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();
});
}
function onAlertShowing() {
info("Notification alert showing");
notification.removeEventListener("show", onAlertShowing);
let alertWindow = Services.wm.getMostRecentWindow("alert:alert");
if (!alertWindow) {
todo(false, "Notifications don't use XUL windows on all platforms.");
notification.close();
newWindowOpenedFromTab.close();
finish();
return;
}
gBrowser.tabContainer.addEventListener("TabSelect", onTabSelect);
EventUtils.synthesizeMouseAtCenter(alertWindow.document.getElementById("alertTitleLabel"), {}, alertWindow);
info("Clicked on notification");
alertWindow.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);
}
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();
}