browser_popupNotification_2.js (10818B)
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 // Test optional params 16 { 17 id: "Test#1", 18 run() { 19 this.notifyObj = new BasicNotification(this.id); 20 this.notifyObj.secondaryActions = undefined; 21 this.notification = showNotification(this.notifyObj); 22 }, 23 onShown(popup) { 24 checkPopup(popup, this.notifyObj); 25 dismissNotification(popup); 26 }, 27 onHidden() { 28 ok( 29 this.notifyObj.dismissalCallbackTriggered, 30 "dismissal callback triggered" 31 ); 32 this.notification.remove(); 33 ok(this.notifyObj.removedCallbackTriggered, "removed callback triggered"); 34 }, 35 }, 36 // Test that icons appear 37 { 38 id: "Test#2", 39 run() { 40 this.notifyObj = new BasicNotification(this.id); 41 this.notifyObj.id = "geolocation"; 42 this.notifyObj.anchorID = "geo-notification-icon"; 43 this.notification = showNotification(this.notifyObj); 44 }, 45 onShown(popup) { 46 checkPopup(popup, this.notifyObj); 47 isnot( 48 document.getElementById("geo-notification-icon").getBoundingClientRect() 49 .width, 50 0, 51 "geo anchor should be visible" 52 ); 53 dismissNotification(popup); 54 }, 55 onHidden() { 56 let icon = document.getElementById("geo-notification-icon"); 57 isnot( 58 icon.getBoundingClientRect().width, 59 0, 60 "geo anchor should be visible after dismissal" 61 ); 62 this.notification.remove(); 63 is( 64 icon.getBoundingClientRect().width, 65 0, 66 "geo anchor should not be visible after removal" 67 ); 68 }, 69 }, 70 71 // Test that persistence allows the notification to persist across reloads 72 { 73 id: "Test#3", 74 async run() { 75 this.oldSelectedTab = gBrowser.selectedTab; 76 await BrowserTestUtils.openNewForegroundTab( 77 gBrowser, 78 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 79 "http://example.com/" 80 ); 81 this.notifyObj = new BasicNotification(this.id); 82 this.notifyObj.addOptions({ 83 persistence: 2, 84 }); 85 this.notification = showNotification(this.notifyObj); 86 }, 87 async onShown() { 88 this.complete = false; 89 await BrowserTestUtils.loadURIString({ 90 browser: gBrowser.selectedTab.linkedBrowser, 91 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 92 uriString: "http://example.org/", 93 }); 94 await BrowserTestUtils.loadURIString({ 95 browser: gBrowser.selectedTab.linkedBrowser, 96 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 97 uriString: "http://example.com/", 98 }); 99 // Next load will remove the notification 100 this.complete = true; 101 await BrowserTestUtils.loadURIString({ 102 browser: gBrowser.selectedTab.linkedBrowser, 103 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 104 uriString: "http://example.org/", 105 }); 106 }, 107 onHidden() { 108 ok( 109 this.complete, 110 "Should only have hidden the notification after 3 page loads" 111 ); 112 ok(this.notifyObj.removedCallbackTriggered, "removal callback triggered"); 113 gBrowser.removeTab(gBrowser.selectedTab); 114 gBrowser.selectedTab = this.oldSelectedTab; 115 }, 116 }, 117 // Test that a timeout allows the notification to persist across reloads 118 { 119 id: "Test#4", 120 async run() { 121 this.oldSelectedTab = gBrowser.selectedTab; 122 await BrowserTestUtils.openNewForegroundTab( 123 gBrowser, 124 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 125 "http://example.com/" 126 ); 127 this.notifyObj = new BasicNotification(this.id); 128 // Set a timeout of 10 minutes that should never be hit 129 this.notifyObj.addOptions({ 130 timeout: Date.now() + 600000, 131 }); 132 this.notification = showNotification(this.notifyObj); 133 }, 134 async onShown() { 135 this.complete = false; 136 await BrowserTestUtils.loadURIString({ 137 browser: gBrowser.selectedTab.linkedBrowser, 138 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 139 uriString: "http://example.org/", 140 }); 141 await BrowserTestUtils.loadURIString({ 142 browser: gBrowser.selectedTab.linkedBrowser, 143 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 144 uriString: "http://example.com/", 145 }); 146 // Next load will hide the notification 147 this.notification.options.timeout = Date.now() - 1; 148 this.complete = true; 149 await BrowserTestUtils.loadURIString({ 150 browser: gBrowser.selectedTab.linkedBrowser, 151 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 152 uriString: "http://example.org/", 153 }); 154 }, 155 onHidden() { 156 ok( 157 this.complete, 158 "Should only have hidden the notification after the timeout was passed" 159 ); 160 this.notification.remove(); 161 gBrowser.removeTab(gBrowser.selectedTab); 162 gBrowser.selectedTab = this.oldSelectedTab; 163 }, 164 }, 165 // Test that setting persistWhileVisible allows a visible notification to 166 // persist across location changes 167 { 168 id: "Test#5", 169 async run() { 170 this.oldSelectedTab = gBrowser.selectedTab; 171 await BrowserTestUtils.openNewForegroundTab( 172 gBrowser, 173 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 174 "http://example.com/" 175 ); 176 this.notifyObj = new BasicNotification(this.id); 177 this.notifyObj.addOptions({ 178 persistWhileVisible: true, 179 }); 180 this.notification = showNotification(this.notifyObj); 181 }, 182 async onShown(popup) { 183 this.complete = false; 184 185 await BrowserTestUtils.loadURIString({ 186 browser: gBrowser.selectedTab.linkedBrowser, 187 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 188 uriString: "http://example.org/", 189 }); 190 await BrowserTestUtils.loadURIString({ 191 browser: gBrowser.selectedTab.linkedBrowser, 192 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 193 uriString: "http://example.com/", 194 }); 195 // Notification should persist across location changes 196 this.complete = true; 197 dismissNotification(popup); 198 }, 199 onHidden() { 200 ok( 201 this.complete, 202 "Should only have hidden the notification after it was dismissed" 203 ); 204 this.notification.remove(); 205 gBrowser.removeTab(gBrowser.selectedTab); 206 gBrowser.selectedTab = this.oldSelectedTab; 207 }, 208 }, 209 210 // Test that nested icon nodes correctly activate popups 211 { 212 id: "Test#6", 213 run() { 214 // Add a temporary box as the anchor with a button 215 this.box = document.createXULElement("box"); 216 PopupNotifications.iconBox.appendChild(this.box); 217 218 let button = document.createXULElement("button"); 219 button.setAttribute("label", "Please click me!"); 220 this.box.appendChild(button); 221 222 // The notification should open up on the box 223 this.notifyObj = new BasicNotification(this.id); 224 this.notifyObj.anchorID = this.box.id = "nested-box"; 225 this.notifyObj.addOptions({ dismissed: true }); 226 this.notification = showNotification(this.notifyObj); 227 228 // This test places a normal button in the notification area, which has 229 // standard GTK styling and dimensions. Due to the clip-path, this button 230 // gets clipped off, which makes it necessary to synthesize the mouse click 231 // a little bit downward. To be safe, we click at center. 232 EventUtils.synthesizeMouseAtCenter(button, {}); 233 }, 234 onShown(popup) { 235 checkPopup(popup, this.notifyObj); 236 dismissNotification(popup); 237 }, 238 onHidden() { 239 this.notification.remove(); 240 this.box.remove(); 241 }, 242 }, 243 // Test that popupnotifications without popups have anchor icons shown 244 { 245 id: "Test#7", 246 async run() { 247 let notifyObj = new BasicNotification(this.id); 248 notifyObj.anchorID = "geo-notification-icon"; 249 notifyObj.addOptions({ neverShow: true }); 250 let promiseTopic = TestUtils.topicObserved( 251 "PopupNotifications-updateNotShowing" 252 ); 253 showNotification(notifyObj); 254 await promiseTopic; 255 isnot( 256 document.getElementById("geo-notification-icon").getBoundingClientRect() 257 .width, 258 0, 259 "geo anchor should be visible" 260 ); 261 goNext(); 262 }, 263 }, 264 // Test that autoplay media icon is shown 265 { 266 id: "Test#8", 267 async run() { 268 let notifyObj = new BasicNotification(this.id); 269 notifyObj.anchorID = "autoplay-media-notification-icon"; 270 notifyObj.addOptions({ neverShow: true }); 271 let promiseTopic = TestUtils.topicObserved( 272 "PopupNotifications-updateNotShowing" 273 ); 274 showNotification(notifyObj); 275 await promiseTopic; 276 isnot( 277 document 278 .getElementById("autoplay-media-notification-icon") 279 .getBoundingClientRect().width, 280 0, 281 "autoplay media icon should be visible" 282 ); 283 goNext(); 284 }, 285 }, 286 // Test notification close button 287 { 288 id: "Test#9", 289 run() { 290 this.notifyObj = new BasicNotification(this.id); 291 this.notification = showNotification(this.notifyObj); 292 }, 293 onShown(popup) { 294 checkPopup(popup, this.notifyObj); 295 let notification = popup.children[0]; 296 EventUtils.synthesizeMouseAtCenter(notification.closebutton, {}); 297 }, 298 onHidden() { 299 ok( 300 this.notifyObj.dismissalCallbackTriggered, 301 "dismissal callback triggered" 302 ); 303 this.notification.remove(); 304 ok(this.notifyObj.removedCallbackTriggered, "removed callback triggered"); 305 ok( 306 !this.notifyObj.secondaryActionClicked, 307 "secondary action not clicked" 308 ); 309 }, 310 }, 311 // Test notification when chrome is hidden 312 { 313 id: "Test#11", 314 run() { 315 window.locationbar.visible = false; 316 this.notifyObj = new BasicNotification(this.id); 317 this.notification = showNotification(this.notifyObj); 318 }, 319 onShown(popup) { 320 checkPopup(popup, this.notifyObj); 321 is( 322 popup.anchorNode.className, 323 "tabbrowser-tab", 324 "notification anchored to tab" 325 ); 326 dismissNotification(popup); 327 }, 328 onHidden() { 329 ok( 330 this.notifyObj.dismissalCallbackTriggered, 331 "dismissal callback triggered" 332 ); 333 this.notification.remove(); 334 ok(this.notifyObj.removedCallbackTriggered, "removed callback triggered"); 335 window.locationbar.visible = true; 336 }, 337 }, 338 ];