test_bug561634.html (2868B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=561634 5 --> 6 <head> 7 <title>Test for Bug 561634</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 10 </head> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=561634">Mozilla Bug 561634</a> 13 <p id="display"></p> 14 <div id="content" style="display: none;"> 15 <form> 16 </form> 17 </div> 18 <pre id="test"> 19 <script type="application/javascript"> 20 21 /** Test for Bug 561634 */ 22 23 function checkEmptyForm() 24 { 25 ok(document.forms[0].checkValidity(), "An empty form is valid"); 26 } 27 28 function checkBarredFromConstraintValidation() 29 { 30 var f = document.forms[0]; 31 var fs = document.createElement('fieldset'); 32 var i = document.createElement('input'); 33 34 f.appendChild(fs); 35 i.type = 'hidden'; 36 f.appendChild(i); 37 38 fs.setCustomValidity("foo"); 39 i.setCustomValidity("foo"); 40 41 ok(f.checkValidity(), 42 "A form with invalid element barred from constraint validation should be valid"); 43 44 f.removeChild(i); 45 f.removeChild(fs); 46 } 47 48 function checkValid() 49 { 50 var f = document.forms[0]; 51 var i = document.createElement('input'); 52 f.appendChild(i); 53 54 ok(f.checkValidity(), "A form with valid elements is valid"); 55 56 f.removeChild(i); 57 } 58 59 function checkInvalid() 60 { 61 var f = document.forms[0]; 62 var i = document.createElement('input'); 63 f.appendChild(i); 64 65 i.setCustomValidity("foo"); 66 ok(!f.checkValidity(), "A form with invalid elements is invalid"); 67 68 var i2 = document.createElement('input'); 69 f.appendChild(i2); 70 ok(!f.checkValidity(), 71 "A form with at least one invalid element is invalid"); 72 73 f.removeChild(i2); 74 f.removeChild(i); 75 } 76 77 function checkInvalidEvent() 78 { 79 var f = document.forms[0]; 80 var i = document.createElement('input'); 81 f.appendChild(i); 82 var i2 = document.createElement('input'); 83 f.appendChild(i2); 84 85 i.setCustomValidity("foo"); 86 87 var invalidEventForInvalidElement = false; 88 var invalidEventForValidElement = false; 89 90 i.addEventListener("invalid", function (e) { 91 invalidEventForInvalidElement = true; 92 ok(e.cancelable, "invalid event should be cancelable"); 93 ok(!e.bubbles, "invalid event should not bubble"); 94 }); 95 96 i2.addEventListener("invalid", function (e) { 97 invalidEventForValidElement = true; 98 }); 99 100 f.checkValidity(); 101 102 setTimeout(function() { 103 ok(invalidEventForInvalidElement, 104 "invalid event should be fired on invalid elements"); 105 ok(!invalidEventForValidElement, 106 "invalid event should not be fired on valid elements"); 107 108 f.removeChild(i2); 109 f.removeChild(i); 110 111 SimpleTest.finish(); 112 }, 0); 113 } 114 115 SimpleTest.waitForExplicitFinish(); 116 117 checkEmptyForm(); 118 checkBarredFromConstraintValidation(); 119 checkValid(); 120 checkInvalid(); 121 checkInvalidEvent(); // will call finish(). 122 123 </script> 124 </pre> 125 </body> 126 </html>