1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/bugs/test_bug61098.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,352 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=61098 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug 61098</title> 1.11 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script> 1.13 + <script type="application/javascript" src="/tests/SimpleTest/MockObjects.js"></script> 1.14 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.15 +</head> 1.16 +<body onload="runtests();"> 1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=61098">Mozilla Bug 61098</a> 1.18 +<p id="display"> 1.19 +</p> 1.20 +<div id="content" style="display: none"> 1.21 +</div> 1.22 +<pre id="test"> 1.23 +</pre> 1.24 +<script class="testbody" type="text/javascript"> 1.25 +/** Test for Bug 61098 **/ 1.26 + 1.27 +SimpleTest.expectAssertions(8); 1.28 +SimpleTest.waitForExplicitFinish(); 1.29 + 1.30 +var mockPromptServiceRegisterer, mockPromptFactoryRegisterer; 1.31 + 1.32 +var promptState; 1.33 + 1.34 +function registerMockPromptService() 1.35 +{ 1.36 + netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); 1.37 + var XPCOMUtils = SpecialPowers.Cu.import("resource://gre/modules/XPCOMUtils.jsm").XPCOMUtils; 1.38 + var Ci = SpecialPowers.Ci; 1.39 + 1.40 + function MockPrompt(aDOMWindow) { 1.41 + this.domWindow = aDOMWindow; 1.42 + } 1.43 + 1.44 + MockPrompt.prototype = { 1.45 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIPrompt]), 1.46 + 1.47 + domWindow : null, 1.48 + 1.49 + _toggleModalState: function() 1.50 + { 1.51 + // The real prompt service puts the window into a modal state 1.52 + // immediately before showing a modal prompt, and leaves the modal state 1.53 + // when the prompt is dismissed by the user. This mock prompt doesn't 1.54 + // show anything to the user, so we only need to enter and immediately 1.55 + // leave the modal state -- this is done to trigger the necessary 1.56 + // accounting for triggering the "stop showing more prompts" code for 1.57 + // abusive pages. 1.58 + var winUtils = SpecialPowers.getDOMWindowUtils(this.domWindow); 1.59 + winUtils.enterModalState(); 1.60 + winUtils.leaveModalState(); 1.61 + }, 1.62 + 1.63 + alert: function(aDialogTitle, aText) 1.64 + { 1.65 + this._toggleModalState(); 1.66 + promptState = {method: "alert", 1.67 + parent: this.domWindow, 1.68 + title: aDialogTitle, 1.69 + msg: aText 1.70 + }; 1.71 + }, 1.72 + 1.73 + alertCheck: function(aDialogTitle, aText, aCheckMsg, aCheckState) 1.74 + { 1.75 + this._toggleModalState(); 1.76 + promptState = {method: "alertCheck", 1.77 + parent: this.domWindow, 1.78 + title: aDialogTitle, 1.79 + msg: aText, 1.80 + checkMsg: aCheckMsg, 1.81 + checkState: aCheckState 1.82 + }; 1.83 + 1.84 + SpecialPowers.wrap(aCheckState).value = true; 1.85 + }, 1.86 + 1.87 + confirm: function(aDialogTitle, aText) 1.88 + { 1.89 + this._toggleModalState(); 1.90 + promptState = {method: "confirm", 1.91 + parent: this.domWindow, 1.92 + title: aDialogTitle, 1.93 + msg: aText 1.94 + }; 1.95 + 1.96 + return true; 1.97 + }, 1.98 + 1.99 + confirmCheck: function(aDialogTitle, aText, aCheckMsg, aCheckState) 1.100 + { 1.101 + this._toggleModalState(); 1.102 + promptState = {method: "confirmCheck", 1.103 + parent: this.domWindow, 1.104 + title: aDialogTitle, 1.105 + msg: aText, 1.106 + checkMsg: aCheckMsg, 1.107 + checkState: aCheckState 1.108 + }; 1.109 + 1.110 + SpecialPowers.wrap(aCheckState).value = true; 1.111 + 1.112 + return true; 1.113 + }, 1.114 + 1.115 + confirmEx: function(aDialogTitle, aText, aButtonFlags, 1.116 + aButton0Title, aButton1Title, aButton2Title, 1.117 + aCheckMsg, aCheckState) 1.118 + { 1.119 + this._toggleModalState(); 1.120 + promptState = {method: "confirmCheck", 1.121 + parent: this.domWindow, 1.122 + title: aDialogTitle, 1.123 + msg: aText, 1.124 + checkMsg: aCheckMsg, 1.125 + checkState: aCheckState 1.126 + }; 1.127 + 1.128 + if (aCheckMsg != null) 1.129 + SpecialPowers.wrap(aCheckState).value = true; 1.130 + 1.131 + return 0; 1.132 + }, 1.133 + 1.134 + prompt: function(aDialogTitle, aText, aValue, aCheckMsg, 1.135 + aCheckState) 1.136 + { 1.137 + this._toggleModalState(); 1.138 + promptState = {method: "prompt", 1.139 + parent: this.domWindow, 1.140 + title: aDialogTitle, 1.141 + msg: aText, 1.142 + checkMsg: aCheckMsg, 1.143 + checkState: aCheckState 1.144 + }; 1.145 + 1.146 + if (aCheckMsg != null) 1.147 + SpecialPowers.wrap(aCheckState).value = true; 1.148 + 1.149 + return true; 1.150 + }, 1.151 + }; 1.152 + 1.153 + 1.154 + // Override the prompt service with our own so that we can test 1.155 + // modal dialogs 1.156 + 1.157 + function MockPromptService() 1.158 + { 1.159 + } 1.160 + 1.161 + MockPromptService.prototype = { 1.162 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptFactory, Ci.nsIPromptService]), 1.163 + 1.164 + getPrompt: function(aDOMWindow, aIID) 1.165 + { 1.166 + return new MockPrompt(aDOMWindow); 1.167 + }, 1.168 + 1.169 + alert: function(aParent, aDialogTitle, aText) 1.170 + { 1.171 + var prompt = new MockPrompt(aParent); 1.172 + return prompt.alert(aDialogTitle, aText); 1.173 + }, 1.174 + 1.175 + alertCheck: function(aParent, aDialogTitle, aText, aCheckMsg, aCheckState) 1.176 + { 1.177 + var prompt = new MockPrompt(aParent); 1.178 + return prompt.alertCheck(aDialogTitle, aText, aCheckMsg, aCheckState); 1.179 + }, 1.180 + 1.181 + confirm: function(aParent, aDialogTitle, aText) 1.182 + { 1.183 + var prompt = new MockPrompt(aParent); 1.184 + return prompt.confirm(aDialogTitle, aText); 1.185 + }, 1.186 + 1.187 + confirmCheck: function(aParent, aDialogTitle, aText, aCheckMsg, 1.188 + aCheckState) 1.189 + { 1.190 + var prompt = new MockPrompt(aParent); 1.191 + return prompt.confirmCheck(aDialogTitle, aText, aCheckMsg, aCheckState); 1.192 + }, 1.193 + 1.194 + confirmEx: function(aParent, aDialogTitle, aText, aButtonFlags, 1.195 + aButton0Title, aButton1Title, aButton2Title, 1.196 + aCheckMsg, aCheckState) 1.197 + { 1.198 + var prompt = new MockPrompt(aParent); 1.199 + return prompt.confirmEx(aDialogTitle, aText, aButtonFlags, 1.200 + aButton0Title, aButton1Title, aButton2Title, 1.201 + aCheckMsg, aCheckState); 1.202 + }, 1.203 + 1.204 + prompt: function(aParent, aDialogTitle, aText, aValue, aCheckMsg, 1.205 + aCheckState) 1.206 + { 1.207 + var prompt = new MockPrompt(aParent); 1.208 + return prompt.prompt(aDialogTitle, aText, aValue, aCheckMsg, aCheckState); 1.209 + }, 1.210 + 1.211 + }; 1.212 + 1.213 + mockPromptServiceRegisterer = 1.214 + new MockObjectRegisterer("@mozilla.org/embedcomp/prompt-service;1", 1.215 + MockPromptService); 1.216 + mockPromptFactoryRegisterer = 1.217 + new MockObjectRegisterer("@mozilla.org/prompter;1", 1.218 + MockPromptService); 1.219 + 1.220 + mockPromptServiceRegisterer.register(); 1.221 + mockPromptFactoryRegisterer.register(); 1.222 +}; 1.223 + 1.224 +function enableDialogLoopBlocking() 1.225 +{ 1.226 + var prefs = SpecialPowers.Cc["@mozilla.org/preferences-service;1"]. 1.227 + getService(SpecialPowers.Ci.nsIPrefBranch); 1.228 + 1.229 + prefs.setIntPref("dom.successive_dialog_time_limit", 3); 1.230 +} 1.231 + 1.232 +function resetDialogLoopBlocking() 1.233 +{ 1.234 + var prefs = SpecialPowers.Cc["@mozilla.org/preferences-service;1"]. 1.235 + getService(SpecialPowers.Ci.nsIPrefBranch); 1.236 + 1.237 + prefs.setIntPref("dom.successive_dialog_time_limit", 0); 1.238 +} 1.239 + 1.240 +var expectedState; 1.241 + 1.242 +function runtests() 1.243 +{ 1.244 + registerMockPromptService(); 1.245 + enableDialogLoopBlocking(); 1.246 + 1.247 + // Test that alert() works normally and then gets blocked on the 1.248 + // second call. 1.249 + w = window.open(); 1.250 + w.alert("alert message 1"); 1.251 + is (promptState.method, "alert", "Wrong prompt method called"); 1.252 + is (promptState.parent, w, "Wrong alert parent"); 1.253 + is (promptState.msg, "alert message 1", "Wrong alert message"); 1.254 + promptState = void(0); 1.255 + 1.256 + w.alert("alert message 2"); 1.257 + is (promptState.method, "alertCheck", "Wrong prompt method called"); 1.258 + is (promptState.parent, w, "Wrong alert parent"); 1.259 + is (promptState.msg, "alert message 2", "Wrong alert message"); 1.260 + promptState = void(0); 1.261 + 1.262 + try { 1.263 + w.alert("alert message 3"); 1.264 + } catch(e) { 1.265 + is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 1.266 + } 1.267 + 1.268 + is (promptState, void(0), "Wrong prompt state after blocked alert()"); 1.269 + 1.270 + w.close(); 1.271 + 1.272 + // Test that confirm() works normally and then gets blocked on the 1.273 + // second call. 1.274 + w = window.open(); 1.275 + w.confirm("confirm message 1"); 1.276 + is (promptState.method, "confirm", "Wrong prompt method called"); 1.277 + is (promptState.parent, w, "Wrong confirm parent"); 1.278 + is (promptState.msg, "confirm message 1", "Wrong confirm message"); 1.279 + promptState = void(0); 1.280 + 1.281 + w.confirm("confirm message 2"); 1.282 + is (promptState.method, "confirmCheck", "Wrong prompt method called"); 1.283 + is (promptState.parent, w, "Wrong confirm parent"); 1.284 + is (promptState.msg, "confirm message 2", "Wrong confirm message"); 1.285 + promptState = void(0); 1.286 + 1.287 + try { 1.288 + w.confirm("confirm message 3"); 1.289 + } catch(e) { 1.290 + is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 1.291 + } 1.292 + 1.293 + is (promptState, void(0), "Wrong prompt state after blocked confirm()"); 1.294 + 1.295 + w.close(); 1.296 + 1.297 + // Test that prompt() works normally and then gets blocked on the 1.298 + // second call. 1.299 + w = window.open(); 1.300 + w.prompt("prompt message 1"); 1.301 + is (promptState.method, "prompt", "Wrong prompt method called"); 1.302 + is (promptState.parent, w, "Wrong prompt parent"); 1.303 + is (promptState.msg, "prompt message 1", "Wrong prompt message"); 1.304 + is (promptState.checkMsg, null, "Wrong dialog value"); 1.305 + promptState = void(0); 1.306 + 1.307 + w.prompt("prompt message 2"); 1.308 + is (promptState.method, "prompt", "Wrong prompt method called"); 1.309 + is (promptState.parent, w, "Wrong prompt parent"); 1.310 + is (promptState.msg, "prompt message 2", "Wrong prompt message"); 1.311 + is (promptState.checkMsg, "Prevent this page from creating additional dialogs", "Wrong dialog value"); 1.312 + promptState = void(0); 1.313 + 1.314 + try { 1.315 + w.prompt("prompt message 3"); 1.316 + } catch(e) { 1.317 + is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 1.318 + } 1.319 + 1.320 + is (promptState, void(0), "Wrong prompt state after blocked prompt()"); 1.321 + 1.322 + w.close(); 1.323 + 1.324 + // Test that showModalDialog() works normally and then gets blocked 1.325 + // on the second call. 1.326 + w = window.open(); 1.327 + w.showModalDialog("data:text/html,%3Cscript>window.close();%3C/script>") 1.328 + is (promptState, void(0), "Wrong prompt state"); 1.329 + 1.330 + // Test that showModalDialog() works normally and then gets blocked 1.331 + // on the second call. 1.332 + try { 1.333 + w.showModalDialog("data:text/html,%3Cscript>window.close();%3C/script>") 1.334 + } catch(e) { 1.335 + is(e.name, "NS_ERROR_NOT_AVAILABLE", "Wrong exception"); 1.336 + } 1.337 + is (promptState.method, "confirm", "Wrong prompt method called"); 1.338 + is (promptState.parent, w, "Wrong confirm parent"); 1.339 + is (promptState.msg, "Prevent this page from creating additional dialogs", 1.340 + "Wrong confirm message"); 1.341 + promptState = void(0); 1.342 + 1.343 + w.close(); 1.344 + 1.345 + resetDialogLoopBlocking(); 1.346 + 1.347 + mockPromptFactoryRegisterer.unregister(); 1.348 + mockPromptServiceRegisterer.unregister(); 1.349 + 1.350 + SimpleTest.finish(); 1.351 +} 1.352 + 1.353 +</script> 1.354 +</body> 1.355 +</html>