test_bug61098.html (10987B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=61098 5 --> 6 <head> 7 <title>Test for Bug 61098</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <script src="/tests/SimpleTest/EventUtils.js"></script> 10 <script src="/tests/SimpleTest/MockObjects.js"></script> 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 12 </head> 13 <body onload="runtests();"> 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=61098">Mozilla Bug 61098</a> 15 <p id="display"> 16 </p> 17 <div id="content" style="display: none"> 18 </div> 19 <pre id="test"> 20 </pre> 21 <script class="testbody" type="text/javascript"> 22 /** Test for Bug 61098 */ 23 24 SimpleTest.waitForExplicitFinish(); 25 26 var mockPromptServiceRegisterer; 27 28 var promptState; 29 30 function registerMockPromptService() 31 { 32 var { XPCOMUtils } = SpecialPowers.ChromeUtils.importESModule( 33 "resource://gre/modules/XPCOMUtils.sys.mjs" 34 ); 35 var Ci = SpecialPowers.Ci; 36 37 function MockPrompt(aDOMWindow) { 38 this.domWindow = SpecialPowers.unwrap(aDOMWindow); 39 } 40 41 MockPrompt.prototype = { 42 QueryInterface(iid) { 43 if (iid.equals(Ci.nsIPrompt)) { 44 return this; 45 } 46 throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE; 47 }, 48 49 domWindow : null, 50 51 _toggleModalState() 52 { 53 // The real prompt service puts the window into a modal state 54 // immediately before showing a modal prompt, and leaves the modal state 55 // when the prompt is dismissed by the user. This mock prompt doesn't 56 // show anything to the user, so we only need to enter and immediately 57 // leave the modal state -- this is done to trigger the necessary 58 // accounting for triggering the "stop showing more prompts" code for 59 // abusive pages. 60 var winUtils = SpecialPowers.getDOMWindowUtils(this.domWindow); 61 winUtils.enterModalState(); 62 winUtils.leaveModalState(); 63 }, 64 65 alert(aDialogTitle, aText) 66 { 67 this._toggleModalState(); 68 promptState = {method: "alert", 69 parent: SpecialPowers.unwrap(this.domWindow), 70 title: aDialogTitle, 71 msg: aText 72 }; 73 }, 74 75 alertCheck(aDialogTitle, aText, aCheckMsg, aCheckState) 76 { 77 this._toggleModalState(); 78 promptState = {method: "alertCheck", 79 parent: SpecialPowers.unwrap(this.domWindow), 80 title: aDialogTitle, 81 msg: aText, 82 checkMsg: aCheckMsg, 83 checkState: aCheckState 84 }; 85 86 SpecialPowers.wrap(aCheckState).value = true; 87 }, 88 89 confirm(aDialogTitle, aText) 90 { 91 this._toggleModalState(); 92 promptState = {method: "confirm", 93 parent: SpecialPowers.unwrap(this.domWindow), 94 title: aDialogTitle, 95 msg: aText 96 }; 97 98 return true; 99 }, 100 101 confirmCheck(aDialogTitle, aText, aCheckMsg, aCheckState) 102 { 103 this._toggleModalState(); 104 promptState = {method: "confirmCheck", 105 parent: SpecialPowers.unwrap(this.domWindow), 106 title: aDialogTitle, 107 msg: aText, 108 checkMsg: aCheckMsg, 109 checkState: aCheckState 110 }; 111 112 SpecialPowers.wrap(aCheckState).value = true; 113 114 return true; 115 }, 116 117 confirmEx(aDialogTitle, aText, aButtonFlags, 118 aButton0Title, aButton1Title, aButton2Title, 119 aCheckMsg, aCheckState) 120 { 121 this._toggleModalState(); 122 promptState = {method: "confirmCheck", 123 parent: SpecialPowers.unwrap(this.domWindow), 124 title: aDialogTitle, 125 msg: aText, 126 checkMsg: aCheckMsg, 127 checkState: aCheckState 128 }; 129 130 if (aCheckMsg != null) 131 SpecialPowers.wrap(aCheckState).value = true; 132 133 return 0; 134 }, 135 136 prompt(aDialogTitle, aText, aValue, aCheckMsg, 137 aCheckState) 138 { 139 this._toggleModalState(); 140 promptState = {method: "prompt", 141 parent: SpecialPowers.unwrap(this.domWindow), 142 title: aDialogTitle, 143 msg: aText, 144 checkMsg: aCheckMsg, 145 checkState: aCheckState 146 }; 147 148 if (aCheckMsg != null) 149 SpecialPowers.wrap(aCheckState).value = true; 150 151 return true; 152 }, 153 }; 154 155 156 // Override the prompt service with our own so that we can test 157 // modal dialogs 158 159 function MockPromptService() 160 { 161 } 162 163 MockPromptService.prototype = { 164 QueryInterface(iid) { 165 if (iid.equals(Ci.nsIPromptFactory) || iid.equals(Ci.nsIPromptService)) { 166 return this; 167 } 168 throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE; 169 }, 170 171 getPrompt(aDOMWindow, aIID) 172 { 173 return SpecialPowers.wrapCallbackObject(new MockPrompt(aDOMWindow)); 174 }, 175 176 alert(aParent, aDialogTitle, aText) 177 { 178 var prompt = new MockPrompt(aParent); 179 return prompt.alert(aDialogTitle, aText); 180 }, 181 182 alertCheck(aParent, aDialogTitle, aText, aCheckMsg, aCheckState) 183 { 184 var prompt = new MockPrompt(aParent); 185 return prompt.alertCheck(aDialogTitle, aText, aCheckMsg, aCheckState); 186 }, 187 188 confirm(aParent, aDialogTitle, aText) 189 { 190 var prompt = new MockPrompt(aParent); 191 return prompt.confirm(aDialogTitle, aText); 192 }, 193 194 confirmCheck(aParent, aDialogTitle, aText, aCheckMsg, 195 aCheckState) 196 { 197 var prompt = new MockPrompt(aParent); 198 return prompt.confirmCheck(aDialogTitle, aText, aCheckMsg, aCheckState); 199 }, 200 201 confirmEx(aParent, aDialogTitle, aText, aButtonFlags, 202 aButton0Title, aButton1Title, aButton2Title, 203 aCheckMsg, aCheckState) 204 { 205 var prompt = new MockPrompt(aParent); 206 return prompt.confirmEx(aDialogTitle, aText, aButtonFlags, 207 aButton0Title, aButton1Title, aButton2Title, 208 aCheckMsg, aCheckState); 209 }, 210 211 prompt(aParent, aDialogTitle, aText, aValue, aCheckMsg, 212 aCheckState) 213 { 214 var prompt = new MockPrompt(aParent); 215 return prompt.prompt(aDialogTitle, aText, aValue, aCheckMsg, aCheckState); 216 }, 217 218 }; 219 220 mockPromptServiceRegisterer = 221 new MockObjectRegisterer("@mozilla.org/prompter;1", 222 MockPromptService); 223 mockPromptServiceRegisterer.register(); 224 }; 225 226 var expectedState; 227 228 function runtests() 229 { 230 SpecialPowers.pushPrefEnv({'set': [["dom.successive_dialog_time_limit", 3]]}, 231 runtestsInner); 232 } 233 234 async function runtestsInner() 235 { 236 registerMockPromptService(); 237 238 // Test that alert() works normally and then gets blocked on the 239 // second call. 240 w = window.open(); 241 w.alert("alert message 1"); 242 is (promptState.method, "alert", "Wrong prompt method called"); 243 is (promptState.parent, w, "Wrong alert parent"); 244 is (promptState.msg, "alert message 1", "Wrong alert message"); 245 promptState = void(0); 246 247 w.alert("alert message 2"); 248 is (promptState.method, "alertCheck", "Wrong prompt method called"); 249 is (promptState.parent, w, "Wrong alert parent"); 250 is (promptState.msg, "alert message 2", "Wrong alert message"); 251 promptState = void(0); 252 253 try { 254 w.alert("alert message 3"); 255 } catch(e) { 256 is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 257 } 258 259 is (promptState, void(0), "Wrong prompt state after blocked alert()"); 260 261 w.close(); 262 263 // Then check that alert() gets blocked with a new window, because 264 // dialogs are disabled/enabled on basis of browsing context groups. 265 w = window.open(); 266 267 try { 268 w.alert("alert message 4"); 269 } catch(e) { 270 is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 271 } 272 273 is (promptState, void(0), "Wrong prompt state after blocked alert()"); 274 275 // Reset the state and enable the dialogs again. 276 SpecialPowers.DOMWindowUtils.enableDialogs(); 277 SpecialPowers.DOMWindowUtils.resetDialogAbuseState(); 278 279 // Test that confirm() works normally and then gets blocked on the 280 // second call. 281 w.confirm("confirm message 1"); 282 is (promptState.method, "confirm", "Wrong prompt method called"); 283 is (promptState.parent, w, "Wrong confirm parent"); 284 is (promptState.msg, "confirm message 1", "Wrong confirm message"); 285 promptState = void(0); 286 287 w.confirm("confirm message 2"); 288 is (promptState.method, "confirmCheck", "Wrong prompt method called"); 289 is (promptState.parent, w, "Wrong confirm parent"); 290 is (promptState.msg, "confirm message 2", "Wrong confirm message"); 291 promptState = void(0); 292 293 try { 294 w.confirm("confirm message 3"); 295 } catch(e) { 296 is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 297 } 298 299 is (promptState, void(0), "Wrong prompt state after blocked confirm()"); 300 301 w.close(); 302 303 // Then check that confirm() gets blocked with a new window, because 304 // dialogs are disabled/enabled on basis of browsing context groups. 305 w = window.open(); 306 307 try { 308 w.confirm("confirm message 4"); 309 } catch(e) { 310 is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 311 } 312 313 is (promptState, void(0), "Wrong prompt state after blocked prompt()"); 314 315 // Reset the state and enable the dialogs again. 316 SpecialPowers.DOMWindowUtils.enableDialogs(); 317 SpecialPowers.DOMWindowUtils.resetDialogAbuseState(); 318 319 // Test that prompt() works normally and then gets blocked on the 320 // second call. 321 w.prompt("prompt message 1"); 322 is (promptState.method, "prompt", "Wrong prompt method called"); 323 is (promptState.parent, w, "Wrong prompt parent"); 324 is (promptState.msg, "prompt message 1", "Wrong prompt message"); 325 is (promptState.checkMsg, null, "Wrong dialog value"); 326 promptState = void(0); 327 328 w.prompt("prompt message 2"); 329 is (promptState.method, "prompt", "Wrong prompt method called"); 330 is (promptState.parent, w, "Wrong prompt parent"); 331 is (promptState.msg, "prompt message 2", "Wrong prompt message"); 332 is (promptState.checkMsg, "Prevent this page from creating additional dialogs", "Wrong dialog value"); 333 promptState = void(0); 334 335 try { 336 w.prompt("prompt message 3"); 337 } catch(e) { 338 is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 339 } 340 341 is (promptState, void(0), "Wrong prompt state after blocked prompt()"); 342 343 w.close(); 344 345 // Then check that prompt() gets blocked with a new window, because 346 // dialogs are disabled/enabled on basis of browsing context groups. 347 w = window.open(); 348 349 try { 350 w.prompt("prompt message 4"); 351 } catch(e) { 352 is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 353 } 354 355 is (promptState, void(0), "Wrong prompt state after blocked prompt()"); 356 357 w.close(); 358 359 // Reset the state and enable the dialogs again. 360 SpecialPowers.DOMWindowUtils.enableDialogs(); 361 SpecialPowers.DOMWindowUtils.resetDialogAbuseState(); 362 363 mockPromptServiceRegisterer.unregister(); 364 365 SimpleTest.finish(); 366 } 367 368 </script> 369 </body> 370 </html>