1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_bug561636.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,474 @@ 1.4 +var gInvalidFormPopup = document.getElementById('invalid-form-popup'); 1.5 +ok(gInvalidFormPopup, 1.6 + "The browser should have a popup to show when a form is invalid"); 1.7 + 1.8 +function checkPopupShow() 1.9 +{ 1.10 + ok(gInvalidFormPopup.state == 'showing' || gInvalidFormPopup.state == 'open', 1.11 + "[Test " + testId + "] The invalid form popup should be shown"); 1.12 +} 1.13 + 1.14 +function checkPopupHide() 1.15 +{ 1.16 + ok(gInvalidFormPopup.state != 'showing' && gInvalidFormPopup.state != 'open', 1.17 + "[Test " + testId + "] The invalid form popup should not be shown"); 1.18 +} 1.19 + 1.20 +function checkPopupMessage(doc) 1.21 +{ 1.22 + is(gInvalidFormPopup.firstChild.textContent, 1.23 + doc.getElementById('i').validationMessage, 1.24 + "[Test " + testId + "] The panel should show the message from validationMessage"); 1.25 +} 1.26 + 1.27 +let gObserver = { 1.28 + QueryInterface : XPCOMUtils.generateQI([Ci.nsIFormSubmitObserver]), 1.29 + 1.30 + notifyInvalidSubmit : function (aFormElement, aInvalidElements) 1.31 + { 1.32 + } 1.33 +}; 1.34 + 1.35 +var testId = -1; 1.36 + 1.37 +function nextTest() 1.38 +{ 1.39 + testId++; 1.40 + if (testId >= tests.length) { 1.41 + finish(); 1.42 + return; 1.43 + } 1.44 + executeSoon(tests[testId]); 1.45 +} 1.46 + 1.47 +function test() 1.48 +{ 1.49 + waitForExplicitFinish(); 1.50 + waitForFocus(nextTest); 1.51 +} 1.52 + 1.53 +var tests = [ 1.54 + 1.55 +/** 1.56 + * In this test, we check that no popup appears if the form is valid. 1.57 + */ 1.58 +function() 1.59 +{ 1.60 + let uri = "data:text/html,<html><body><iframe name='t'></iframe><form target='t' action='data:text/html,'><input><input id='s' type='submit'></form></body></html>"; 1.61 + let tab = gBrowser.addTab(); 1.62 + 1.63 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.64 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.65 + let doc = gBrowser.contentDocument; 1.66 + 1.67 + doc.getElementById('s').click(); 1.68 + 1.69 + executeSoon(function() { 1.70 + checkPopupHide(); 1.71 + 1.72 + // Clean-up 1.73 + gBrowser.removeTab(gBrowser.selectedTab); 1.74 + nextTest(); 1.75 + }); 1.76 + }, true); 1.77 + 1.78 + gBrowser.selectedTab = tab; 1.79 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.80 +}, 1.81 + 1.82 +/** 1.83 + * In this test, we check that, when an invalid form is submitted, 1.84 + * the invalid element is focused and a popup appears. 1.85 + */ 1.86 +function() 1.87 +{ 1.88 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input required id='i'><input id='s' type='submit'></form>"; 1.89 + let tab = gBrowser.addTab(); 1.90 + 1.91 + gInvalidFormPopup.addEventListener("popupshown", function() { 1.92 + gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false); 1.93 + 1.94 + let doc = gBrowser.contentDocument; 1.95 + is(doc.activeElement, doc.getElementById('i'), 1.96 + "First invalid element should be focused"); 1.97 + 1.98 + checkPopupShow(); 1.99 + checkPopupMessage(doc); 1.100 + 1.101 + // Clean-up and next test. 1.102 + gBrowser.removeTab(gBrowser.selectedTab); 1.103 + nextTest(); 1.104 + }, false); 1.105 + 1.106 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.107 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.108 + 1.109 + gBrowser.contentDocument.getElementById('s').click(); 1.110 + }, true); 1.111 + 1.112 + gBrowser.selectedTab = tab; 1.113 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.114 +}, 1.115 + 1.116 +/** 1.117 + * In this test, we check that, when an invalid form is submitted, 1.118 + * the first invalid element is focused and a popup appears. 1.119 + */ 1.120 +function() 1.121 +{ 1.122 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input><input id='i' required><input required><input id='s' type='submit'></form>"; 1.123 + let tab = gBrowser.addTab(); 1.124 + 1.125 + gInvalidFormPopup.addEventListener("popupshown", function() { 1.126 + gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false); 1.127 + 1.128 + let doc = gBrowser.contentDocument; 1.129 + is(doc.activeElement, doc.getElementById('i'), 1.130 + "First invalid element should be focused"); 1.131 + 1.132 + checkPopupShow(); 1.133 + checkPopupMessage(doc); 1.134 + 1.135 + // Clean-up and next test. 1.136 + gBrowser.removeTab(gBrowser.selectedTab); 1.137 + nextTest(); 1.138 + }, false); 1.139 + 1.140 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.141 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.142 + 1.143 + gBrowser.contentDocument.getElementById('s').click(); 1.144 + }, true); 1.145 + 1.146 + gBrowser.selectedTab = tab; 1.147 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.148 +}, 1.149 + 1.150 +/** 1.151 + * In this test, we check that, we hide the popup by interacting with the 1.152 + * invalid element if the element becomes valid. 1.153 + */ 1.154 +function() 1.155 +{ 1.156 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>"; 1.157 + let tab = gBrowser.addTab(); 1.158 + 1.159 + gInvalidFormPopup.addEventListener("popupshown", function() { 1.160 + gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false); 1.161 + 1.162 + let doc = gBrowser.contentDocument; 1.163 + is(doc.activeElement, doc.getElementById('i'), 1.164 + "First invalid element should be focused"); 1.165 + 1.166 + checkPopupShow(); 1.167 + checkPopupMessage(doc); 1.168 + 1.169 + EventUtils.synthesizeKey("a", {}); 1.170 + 1.171 + executeSoon(function () { 1.172 + checkPopupHide(); 1.173 + 1.174 + // Clean-up and next test. 1.175 + gBrowser.removeTab(gBrowser.selectedTab); 1.176 + nextTest(); 1.177 + }); 1.178 + }, false); 1.179 + 1.180 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.181 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.182 + 1.183 + gBrowser.contentDocument.getElementById('s').click(); 1.184 + }, true); 1.185 + 1.186 + gBrowser.selectedTab = tab; 1.187 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.188 +}, 1.189 + 1.190 +/** 1.191 + * In this test, we check that, we don't hide the popup by interacting with the 1.192 + * invalid element if the element is still invalid. 1.193 + */ 1.194 +function() 1.195 +{ 1.196 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input type='email' id='i' required><input id='s' type='submit'></form>"; 1.197 + let tab = gBrowser.addTab(); 1.198 + 1.199 + gInvalidFormPopup.addEventListener("popupshown", function() { 1.200 + gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false); 1.201 + 1.202 + let doc = gBrowser.contentDocument; 1.203 + is(doc.activeElement, doc.getElementById('i'), 1.204 + "First invalid element should be focused"); 1.205 + 1.206 + checkPopupShow(); 1.207 + checkPopupMessage(doc); 1.208 + 1.209 + EventUtils.synthesizeKey("a", {}); 1.210 + 1.211 + executeSoon(function () { 1.212 + checkPopupShow(); 1.213 + 1.214 + // Clean-up and next test. 1.215 + gBrowser.removeTab(gBrowser.selectedTab); 1.216 + nextTest(); 1.217 + }); 1.218 + }, false); 1.219 + 1.220 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.221 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.222 + 1.223 + gBrowser.contentDocument.getElementById('s').click(); 1.224 + }, true); 1.225 + 1.226 + gBrowser.selectedTab = tab; 1.227 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.228 +}, 1.229 + 1.230 +/** 1.231 + * In this test, we check that we can hide the popup by blurring the invalid 1.232 + * element. 1.233 + */ 1.234 +function() 1.235 +{ 1.236 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>"; 1.237 + let tab = gBrowser.addTab(); 1.238 + 1.239 + gInvalidFormPopup.addEventListener("popupshown", function() { 1.240 + gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false); 1.241 + 1.242 + let doc = gBrowser.contentDocument; 1.243 + is(doc.activeElement, doc.getElementById('i'), 1.244 + "First invalid element should be focused"); 1.245 + 1.246 + checkPopupShow(); 1.247 + checkPopupMessage(doc); 1.248 + 1.249 + doc.getElementById('i').blur(); 1.250 + 1.251 + executeSoon(function () { 1.252 + checkPopupHide(); 1.253 + 1.254 + // Clean-up and next test. 1.255 + gBrowser.removeTab(gBrowser.selectedTab); 1.256 + nextTest(); 1.257 + }); 1.258 + }, false); 1.259 + 1.260 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.261 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.262 + 1.263 + gBrowser.contentDocument.getElementById('s').click(); 1.264 + }, true); 1.265 + 1.266 + gBrowser.selectedTab = tab; 1.267 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.268 +}, 1.269 + 1.270 +/** 1.271 + * In this test, we check that we can hide the popup by pressing TAB. 1.272 + */ 1.273 +function() 1.274 +{ 1.275 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>"; 1.276 + let tab = gBrowser.addTab(); 1.277 + 1.278 + gInvalidFormPopup.addEventListener("popupshown", function() { 1.279 + gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false); 1.280 + 1.281 + let doc = gBrowser.contentDocument; 1.282 + is(doc.activeElement, doc.getElementById('i'), 1.283 + "First invalid element should be focused"); 1.284 + 1.285 + checkPopupShow(); 1.286 + checkPopupMessage(doc); 1.287 + 1.288 + EventUtils.synthesizeKey("VK_TAB", {}); 1.289 + 1.290 + executeSoon(function () { 1.291 + checkPopupHide(); 1.292 + 1.293 + // Clean-up and next test. 1.294 + gBrowser.removeTab(gBrowser.selectedTab); 1.295 + nextTest(); 1.296 + }); 1.297 + }, false); 1.298 + 1.299 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.300 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.301 + 1.302 + gBrowser.contentDocument.getElementById('s').click(); 1.303 + }, true); 1.304 + 1.305 + gBrowser.selectedTab = tab; 1.306 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.307 +}, 1.308 + 1.309 +/** 1.310 + * In this test, we check that the popup will hide if we move to another tab. 1.311 + */ 1.312 +function() 1.313 +{ 1.314 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>"; 1.315 + let tab = gBrowser.addTab(); 1.316 + 1.317 + gInvalidFormPopup.addEventListener("popupshown", function() { 1.318 + gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false); 1.319 + 1.320 + let doc = gBrowser.contentDocument; 1.321 + is(doc.activeElement, doc.getElementById('i'), 1.322 + "First invalid element should be focused"); 1.323 + 1.324 + checkPopupShow(); 1.325 + checkPopupMessage(doc); 1.326 + 1.327 + // Create a new tab and move to it. 1.328 + gBrowser.selectedTab = gBrowser.addTab("about:blank", {skipAnimation: true}); 1.329 + 1.330 + executeSoon(function() { 1.331 + checkPopupHide(); 1.332 + 1.333 + // Clean-up and next test. 1.334 + gBrowser.removeTab(gBrowser.selectedTab); 1.335 + gBrowser.removeTab(gBrowser.selectedTab); 1.336 + nextTest(); 1.337 + }); 1.338 + }, false); 1.339 + 1.340 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.341 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.342 + 1.343 + gBrowser.contentDocument.getElementById('s').click(); 1.344 + }, true); 1.345 + 1.346 + gBrowser.selectedTab = tab; 1.347 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.348 +}, 1.349 + 1.350 +/** 1.351 + * In this test, we check that nothing happen (no focus nor popup) if the 1.352 + * invalid form is submitted in another tab than the current focused one 1.353 + * (submitted in background). 1.354 + */ 1.355 +function() 1.356 +{ 1.357 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>"; 1.358 + let tab = gBrowser.addTab(); 1.359 + 1.360 + gObserver.notifyInvalidSubmit = function() { 1.361 + executeSoon(function() { 1.362 + let doc = tab.linkedBrowser.contentDocument; 1.363 + isnot(doc.activeElement, doc.getElementById('i'), 1.364 + "We should not focus the invalid element when the form is submitted in background"); 1.365 + 1.366 + checkPopupHide(); 1.367 + 1.368 + // Clean-up 1.369 + Services.obs.removeObserver(gObserver, "invalidformsubmit"); 1.370 + gObserver.notifyInvalidSubmit = function () {}; 1.371 + gBrowser.removeTab(tab); 1.372 + 1.373 + nextTest(); 1.374 + }); 1.375 + }; 1.376 + 1.377 + Services.obs.addObserver(gObserver, "invalidformsubmit", false); 1.378 + 1.379 + tab.linkedBrowser.addEventListener("load", function(e) { 1.380 + // Ignore load events from the iframe. 1.381 + if (tab.linkedBrowser.contentDocument == e.target) { 1.382 + let browser = e.currentTarget; 1.383 + browser.removeEventListener("load", arguments.callee, true); 1.384 + 1.385 + isnot(gBrowser.selectedTab.linkedBrowser, browser, 1.386 + "This tab should have been loaded in background"); 1.387 + browser.contentDocument.getElementById('s').click(); 1.388 + } 1.389 + }, true); 1.390 + 1.391 + tab.linkedBrowser.loadURI(uri); 1.392 +}, 1.393 + 1.394 +/** 1.395 + * In this test, we check that the author defined error message is shown. 1.396 + */ 1.397 +function() 1.398 +{ 1.399 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input x-moz-errormessage='foo' required id='i'><input id='s' type='submit'></form>"; 1.400 + let tab = gBrowser.addTab(); 1.401 + 1.402 + gInvalidFormPopup.addEventListener("popupshown", function() { 1.403 + gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false); 1.404 + 1.405 + let doc = gBrowser.contentDocument; 1.406 + is(doc.activeElement, doc.getElementById('i'), 1.407 + "First invalid element should be focused"); 1.408 + 1.409 + checkPopupShow(); 1.410 + 1.411 + is(gInvalidFormPopup.firstChild.textContent, "foo", 1.412 + "The panel should show the author defined error message"); 1.413 + 1.414 + // Clean-up and next test. 1.415 + gBrowser.removeTab(gBrowser.selectedTab); 1.416 + nextTest(); 1.417 + }, false); 1.418 + 1.419 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.420 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.421 + 1.422 + gBrowser.contentDocument.getElementById('s').click(); 1.423 + }, true); 1.424 + 1.425 + gBrowser.selectedTab = tab; 1.426 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.427 +}, 1.428 + 1.429 +/** 1.430 + * In this test, we check that the message is correctly updated when it changes. 1.431 + */ 1.432 +function() 1.433 +{ 1.434 + let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input type='email' required id='i'><input id='s' type='submit'></form>"; 1.435 + let tab = gBrowser.addTab(); 1.436 + 1.437 + gInvalidFormPopup.addEventListener("popupshown", function() { 1.438 + gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false); 1.439 + 1.440 + let doc = gBrowser.contentDocument; 1.441 + let input = doc.getElementById('i'); 1.442 + is(doc.activeElement, input, "First invalid element should be focused"); 1.443 + 1.444 + checkPopupShow(); 1.445 + 1.446 + is(gInvalidFormPopup.firstChild.textContent, input.validationMessage, 1.447 + "The panel should show the current validation message"); 1.448 + 1.449 + input.addEventListener('input', function() { 1.450 + input.removeEventListener('input', arguments.callee, false); 1.451 + 1.452 + executeSoon(function() { 1.453 + // Now, the element suffers from another error, the message should have 1.454 + // been updated. 1.455 + is(gInvalidFormPopup.firstChild.textContent, input.validationMessage, 1.456 + "The panel should show the current validation message"); 1.457 + 1.458 + // Clean-up and next test. 1.459 + gBrowser.removeTab(gBrowser.selectedTab); 1.460 + nextTest(); 1.461 + }); 1.462 + }, false); 1.463 + 1.464 + EventUtils.synthesizeKey('f', {}); 1.465 + }, false); 1.466 + 1.467 + tab.linkedBrowser.addEventListener("load", function(aEvent) { 1.468 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.469 + 1.470 + gBrowser.contentDocument.getElementById('s').click(); 1.471 + }, true); 1.472 + 1.473 + gBrowser.selectedTab = tab; 1.474 + gBrowser.selectedTab.linkedBrowser.loadURI(uri); 1.475 +}, 1.476 + 1.477 +];