Bug 1347503 - Handle "swapping" event for permission notifications. r=johannh draft
authorNihanth Subramanya <nhnt11@gmail.com>
Sun, 19 Mar 2017 02:22:39 +0530
changeset 503357 9caf4877f0a9632bea4f000bf731f16716e85796
parent 503306 7513b3f42058e9bcf9950d4acf4647d4ad2240f0
child 550405 c31662f787b1bcccc75e79fbebaf4bcc73848b45
push id50554
push usernhnt11@gmail.com
push dateThu, 23 Mar 2017 04:45:49 +0000
reviewersjohannh
bugs1347503
milestone55.0a1
Bug 1347503 - Handle "swapping" event for permission notifications. r=johannh MozReview-Commit-ID: ig8XaAe8D2
browser/modules/PermissionUI.jsm
browser/modules/test/browser/browser_PermissionUI.js
--- a/browser/modules/PermissionUI.jsm
+++ b/browser/modules/PermissionUI.jsm
@@ -346,16 +346,20 @@ this.PermissionPromptPrototype = {
     let options = this.popupOptions;
 
     if (!options.hasOwnProperty("displayURI") || options.displayURI) {
       options.displayURI = this.principal.URI;
     }
     // Permission prompts are always persistent; the close button is controlled by a pref.
     options.persistent = true;
     options.hideClose = !Services.prefs.getBoolPref("privacy.permissionPrompts.showCloseButton");
+    // When the docshell of the browser is aboout to be swapped to another one,
+    // the "swapping" event is called. Returning true causes the notification
+    // to be moved to the new browser.
+    options.eventCallback = topic => topic == "swapping";
 
     this.onBeforeShow();
     chromeWin.PopupNotifications.show(this.browser,
                                       this.notificationID,
                                       this.message,
                                       this.anchorID,
                                       mainAction,
                                       secondaryActions,
--- a/browser/modules/test/browser/browser_PermissionUI.js
+++ b/browser/modules/test/browser/browser_PermissionUI.js
@@ -392,8 +392,77 @@ add_task(function* test_no_request() {
     TestPrompt.prompt();
     yield shownPromise;
 
     // Next test allowing the permission request.
     yield clickMainAction();
     Assert.ok(allowed, "The mainAction callback should have fired");
   });
 });
+
+/**
+ * Tests that when the tab is moved to a different window, the notification
+ * is transferred to the new window.
+ */
+add_task(function* test_window_swap() {
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: "http://example.com",
+  }, function*(browser) {
+    const kTestNotificationID = "test-notification";
+    const kTestMessage = "Test message";
+
+    let mainAction = {
+      label: "Test action",
+      accessKey: "T",
+    };
+    let secondaryAction = {
+      label: "Secondary",
+      accessKey: "S",
+    };
+
+    let mockRequest = makeMockPermissionRequest(browser);
+
+    let TestPrompt = {
+      __proto__: PermissionUI.PermissionPromptForRequestPrototype,
+      request: mockRequest,
+      notificationID: kTestNotificationID,
+      message: kTestMessage,
+      promptActions: [mainAction, secondaryAction],
+    };
+
+    let shownPromise =
+      BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
+    TestPrompt.prompt();
+    yield shownPromise;
+
+    let newWindowOpened = BrowserTestUtils.waitForNewWindow();
+    gBrowser.replaceTabWithWindow(gBrowser.selectedTab);
+    let newWindow = yield newWindowOpened;
+    shownPromise =
+      BrowserTestUtils.waitForEvent(newWindow.PopupNotifications.panel, "popupshown");
+    TestPrompt.prompt();
+    yield shownPromise;
+
+    let notification =
+      newWindow.PopupNotifications.getNotification(kTestNotificationID,
+                                                   newWindow.gBrowser.selectedBrowser);
+    Assert.ok(notification, "Should have gotten the notification");
+
+    Assert.equal(notification.message, kTestMessage,
+                 "Should be showing the right message");
+    Assert.equal(notification.mainAction.label, mainAction.label,
+                 "The main action should have the right label");
+    Assert.equal(notification.mainAction.accessKey, mainAction.accessKey,
+                 "The main action should have the right access key");
+    Assert.equal(notification.secondaryActions.length, 1,
+                 "There should only be 1 secondary action");
+    Assert.equal(notification.secondaryActions[0].label, secondaryAction.label,
+                 "The secondary action should have the right label");
+    Assert.equal(notification.secondaryActions[0].accessKey,
+                 secondaryAction.accessKey,
+                 "The secondary action should have the right access key");
+    Assert.ok(notification.options.displayURI.equals(mockRequest.principal.URI),
+              "Should be showing the URI of the requesting page");
+
+    yield BrowserTestUtils.closeWindow(newWindow);
+  });
+});