browser_tabnotificationbox_switch_tabs.js (4860B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 function assertNotificationBoxHidden(reason, browser) { 7 let notificationBox = gBrowser.readNotificationBox(browser); 8 9 if (!notificationBox) { 10 ok(!notificationBox, `Notification box has not been created ${reason}`); 11 return; 12 } 13 14 let name = notificationBox._stack.getAttribute("name"); 15 ok(name, `Notification box has a name ${reason}`); 16 17 let { selectedViewName } = notificationBox._stack.parentElement; 18 Assert.notEqual( 19 selectedViewName, 20 name, 21 `Box is not shown ${reason} ${selectedViewName} != ${name}` 22 ); 23 } 24 25 function assertNotificationBoxShown(reason, browser) { 26 let notificationBox = gBrowser.readNotificationBox(browser); 27 ok(notificationBox, `Notification box has been created ${reason}`); 28 29 let name = notificationBox._stack.getAttribute("name"); 30 ok(name, `Notification box has a name ${reason}`); 31 32 let { selectedViewName } = notificationBox._stack.parentElement; 33 is(selectedViewName, name, `Box is shown ${reason}`); 34 } 35 36 async function createNotification({ browser, label, value, priority }) { 37 let notificationBox = gBrowser.getNotificationBox(browser); 38 let notification = await notificationBox.appendNotification(value, { 39 label, 40 priority: notificationBox[priority], 41 }); 42 return notification; 43 } 44 45 add_task(async function testNotificationInBackgroundTab() { 46 let firstTab = gBrowser.selectedTab; 47 48 // Navigating to a page should not create the notification box 49 await BrowserTestUtils.withNewTab("https://example.com", async browser => { 50 let notificationBox = gBrowser.readNotificationBox(browser); 51 ok(!notificationBox, "The notification box has not been created"); 52 53 gBrowser.selectedTab = firstTab; 54 assertNotificationBoxHidden("initial first tab"); 55 56 await createNotification({ 57 browser, 58 label: "My notification body", 59 value: "test-notification", 60 priority: "PRIORITY_INFO_LOW", 61 }); 62 63 gBrowser.selectedTab = gBrowser.getTabForBrowser(browser); 64 assertNotificationBoxShown("notification created"); 65 }); 66 }); 67 68 add_task(async function testNotificationInActiveTab() { 69 // Open about:blank so the notification box isn't created on tab open. 70 await BrowserTestUtils.withNewTab("about:blank", async browser => { 71 ok(!gBrowser.readNotificationBox(browser), "No notifications for new tab"); 72 73 await createNotification({ 74 browser, 75 label: "Notification!", 76 value: "test-notification", 77 priority: "PRIORITY_INFO_LOW", 78 }); 79 assertNotificationBoxShown("after appendNotification"); 80 }); 81 }); 82 83 add_task(async function testNotificationMultipleTabs() { 84 let tabOne = gBrowser.selectedTab; 85 let tabTwo = await BrowserTestUtils.openNewForegroundTab({ 86 gBrowser, 87 url: "about:blank", 88 }); 89 let tabThree = await BrowserTestUtils.openNewForegroundTab({ 90 gBrowser, 91 url: "https://example.com", 92 }); 93 let browserOne = tabOne.linkedBrowser; 94 let browserTwo = tabTwo.linkedBrowser; 95 let browserThree = tabThree.linkedBrowser; 96 97 is(gBrowser.selectedBrowser, browserThree, "example.com selected"); 98 99 let notificationBoxOne = gBrowser.readNotificationBox(browserOne); 100 let notificationBoxTwo = gBrowser.readNotificationBox(browserTwo); 101 let notificationBoxThree = gBrowser.readNotificationBox(browserThree); 102 103 ok(!notificationBoxOne, "no initial tab box"); 104 ok(!notificationBoxTwo, "no about:blank box"); 105 ok(!notificationBoxThree, "no example.com box"); 106 107 // Verify the correct box is shown after creating tabs. 108 assertNotificationBoxHidden("after open", browserOne); 109 assertNotificationBoxHidden("after open", browserTwo); 110 assertNotificationBoxHidden("after open", browserThree); 111 112 await createNotification({ 113 browser: browserTwo, 114 label: "Test blank", 115 value: "blank", 116 priority: "PRIORITY_INFO_LOW", 117 }); 118 notificationBoxTwo = gBrowser.readNotificationBox(browserTwo); 119 ok(notificationBoxTwo, "Notification box was created"); 120 121 // Verify the selected browser's notification box is still hidden. 122 assertNotificationBoxHidden("hidden create", browserTwo); 123 assertNotificationBoxHidden("other create", browserThree); 124 125 await createNotification({ 126 browser: browserThree, 127 label: "Test active tab", 128 value: "active", 129 priority: "PRIORITY_CRITICAL_LOW", 130 }); 131 // Verify the selected browser's notification box is still shown. 132 assertNotificationBoxHidden("active create", browserTwo); 133 assertNotificationBoxShown("active create", browserThree); 134 135 gBrowser.selectedTab = tabTwo; 136 137 // Verify the notification box for the tab that has one gets shown. 138 assertNotificationBoxShown("tab switch", browserTwo); 139 assertNotificationBoxHidden("tab switch", browserThree); 140 141 BrowserTestUtils.removeTab(tabTwo); 142 BrowserTestUtils.removeTab(tabThree); 143 });