browser_popupNotification_4.js (8692B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 function test() { 6 waitForExplicitFinish(); 7 8 ok(PopupNotifications, "PopupNotifications object exists"); 9 ok(PopupNotifications.panel, "PopupNotifications panel exists"); 10 11 setup(); 12 } 13 14 var tests = [ 15 // Popup Notifications main actions should catch exceptions from callbacks 16 { 17 id: "Test#1", 18 run() { 19 this.testNotif = new ErrorNotification(this.id); 20 showNotification(this.testNotif); 21 }, 22 onShown(popup) { 23 checkPopup(popup, this.testNotif); 24 triggerMainCommand(popup); 25 }, 26 onHidden() { 27 ok(this.testNotif.mainActionClicked, "main action has been triggered"); 28 }, 29 }, 30 // Popup Notifications secondary actions should catch exceptions from callbacks 31 { 32 id: "Test#2", 33 run() { 34 this.testNotif = new ErrorNotification(this.id); 35 showNotification(this.testNotif); 36 }, 37 onShown(popup) { 38 checkPopup(popup, this.testNotif); 39 triggerSecondaryCommand(popup, 0); 40 }, 41 onHidden() { 42 ok( 43 this.testNotif.secondaryActionClicked, 44 "secondary action has been triggered" 45 ); 46 }, 47 }, 48 // Existing popup notification shouldn't disappear when adding a dismissed notification 49 { 50 id: "Test#3", 51 run() { 52 this.notifyObj1 = new BasicNotification(this.id); 53 this.notifyObj1.id += "_1"; 54 this.notifyObj1.anchorID = "default-notification-icon"; 55 this.notification1 = showNotification(this.notifyObj1); 56 }, 57 onShown(popup) { 58 // Now show a dismissed notification, and check that it doesn't clobber 59 // the showing one. 60 this.notifyObj2 = new BasicNotification(this.id); 61 this.notifyObj2.id += "_2"; 62 this.notifyObj2.anchorID = "geo-notification-icon"; 63 this.notifyObj2.options.dismissed = true; 64 this.notification2 = showNotification(this.notifyObj2); 65 66 checkPopup(popup, this.notifyObj1); 67 68 // check that both anchor icons are showing 69 is( 70 document 71 .getElementById("default-notification-icon") 72 .getAttribute("showing"), 73 "true", 74 "notification1 anchor should be visible" 75 ); 76 is( 77 document 78 .getElementById("geo-notification-icon") 79 .getAttribute("showing"), 80 "true", 81 "notification2 anchor should be visible" 82 ); 83 84 dismissNotification(popup); 85 }, 86 onHidden() { 87 this.notification1.remove(); 88 this.notification2.remove(); 89 }, 90 }, 91 // Showing should be able to modify the popup data 92 { 93 id: "Test#4", 94 run() { 95 this.notifyObj = new BasicNotification(this.id); 96 let normalCallback = this.notifyObj.options.eventCallback; 97 this.notifyObj.options.eventCallback = function (eventName) { 98 if (eventName == "showing") { 99 this.mainAction.label = "Alternate Label"; 100 } 101 normalCallback.call(this, eventName); 102 }; 103 showNotification(this.notifyObj); 104 }, 105 onShown(popup) { 106 // checkPopup checks for the matching label. Note that this assumes that 107 // this.notifyObj.mainAction is the same as notification.mainAction, 108 // which could be a problem if we ever decided to deep-copy. 109 checkPopup(popup, this.notifyObj); 110 triggerMainCommand(popup); 111 }, 112 onHidden() {}, 113 }, 114 // Moving a tab to a new window should remove non-swappable notifications. 115 { 116 id: "Test#5", 117 async run() { 118 await BrowserTestUtils.openNewForegroundTab( 119 gBrowser, 120 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 121 "http://example.com/" 122 ); 123 124 let notifyObj = new BasicNotification(this.id); 125 126 let shown = waitForNotificationPanel(); 127 showNotification(notifyObj); 128 await shown; 129 130 let promiseWin = BrowserTestUtils.waitForNewWindow(); 131 gBrowser.replaceTabWithWindow(gBrowser.selectedTab); 132 let win = await promiseWin; 133 134 let anchor = win.document.getElementById("default-notification-icon"); 135 win.PopupNotifications._reshowNotifications(anchor); 136 ok( 137 !win.PopupNotifications.panel.children.length, 138 "no notification displayed in new window" 139 ); 140 ok( 141 notifyObj.swappingCallbackTriggered, 142 "the swapping callback was triggered" 143 ); 144 ok( 145 notifyObj.removedCallbackTriggered, 146 "the removed callback was triggered" 147 ); 148 149 await BrowserTestUtils.closeWindow(win); 150 await waitForWindowReadyForPopupNotifications(window); 151 152 goNext(); 153 }, 154 }, 155 // Moving a tab to a new window should preserve swappable notifications. 156 { 157 id: "Test#6", 158 async run() { 159 await BrowserTestUtils.openNewForegroundTab( 160 gBrowser, 161 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 162 "http://example.com/" 163 ); 164 let notifyObj = new BasicNotification(this.id); 165 let originalCallback = notifyObj.options.eventCallback; 166 notifyObj.options.eventCallback = function (eventName) { 167 originalCallback(eventName); 168 return eventName == "swapping"; 169 }; 170 171 let shown = waitForNotificationPanel(); 172 let notification = showNotification(notifyObj); 173 await shown; 174 175 let promiseWin = BrowserTestUtils.waitForNewWindow(); 176 gBrowser.replaceTabWithWindow(gBrowser.selectedTab); 177 let win = await promiseWin; 178 await waitForWindowReadyForPopupNotifications(win); 179 180 await new Promise(resolve => { 181 let callback = notification.options.eventCallback; 182 notification.options.eventCallback = function (eventName) { 183 callback(eventName); 184 if (eventName == "shown") { 185 resolve(); 186 } 187 }; 188 info("Showing the notification again"); 189 notification.reshow(); 190 }); 191 192 checkPopup(win.PopupNotifications.panel, notifyObj); 193 ok( 194 notifyObj.swappingCallbackTriggered, 195 "the swapping callback was triggered" 196 ); 197 198 await BrowserTestUtils.closeWindow(win); 199 await waitForWindowReadyForPopupNotifications(window); 200 201 goNext(); 202 }, 203 }, 204 // the main action callback can keep the notification. 205 { 206 id: "Test#8", 207 run() { 208 this.notifyObj = new BasicNotification(this.id); 209 this.notifyObj.mainAction.dismiss = true; 210 this.notification = showNotification(this.notifyObj); 211 }, 212 onShown(popup) { 213 checkPopup(popup, this.notifyObj); 214 triggerMainCommand(popup); 215 }, 216 onHidden() { 217 ok( 218 this.notifyObj.dismissalCallbackTriggered, 219 "dismissal callback was triggered" 220 ); 221 ok( 222 !this.notifyObj.removedCallbackTriggered, 223 "removed callback wasn't triggered" 224 ); 225 this.notification.remove(); 226 }, 227 }, 228 // a secondary action callback can keep the notification. 229 { 230 id: "Test#9", 231 run() { 232 this.notifyObj = new BasicNotification(this.id); 233 this.notifyObj.secondaryActions[0].dismiss = true; 234 this.notification = showNotification(this.notifyObj); 235 }, 236 onShown(popup) { 237 checkPopup(popup, this.notifyObj); 238 triggerSecondaryCommand(popup, 0); 239 }, 240 onHidden() { 241 ok( 242 this.notifyObj.dismissalCallbackTriggered, 243 "dismissal callback was triggered" 244 ); 245 ok( 246 !this.notifyObj.removedCallbackTriggered, 247 "removed callback wasn't triggered" 248 ); 249 this.notification.remove(); 250 }, 251 }, 252 // returning true in the showing callback should dismiss the notification. 253 { 254 id: "Test#10", 255 run() { 256 let notifyObj = new BasicNotification(this.id); 257 let originalCallback = notifyObj.options.eventCallback; 258 notifyObj.options.eventCallback = function (eventName) { 259 originalCallback(eventName); 260 return eventName == "showing"; 261 }; 262 263 let notification = showNotification(notifyObj); 264 ok( 265 notifyObj.showingCallbackTriggered, 266 "the showing callback was triggered" 267 ); 268 ok( 269 !notifyObj.shownCallbackTriggered, 270 "the shown callback wasn't triggered" 271 ); 272 notification.remove(); 273 goNext(); 274 }, 275 }, 276 // the main action button should apply non-default(no highlight) style. 277 { 278 id: "Test#11", 279 run() { 280 this.notifyObj = new BasicNotification(this.id); 281 this.notifyObj.secondaryActions = undefined; 282 this.notification = showNotification(this.notifyObj); 283 }, 284 onShown(popup) { 285 checkPopup(popup, this.notifyObj); 286 dismissNotification(popup); 287 }, 288 onHidden() {}, 289 }, 290 ];