test_maxlength_attribute.html (5400B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=345624 5 --> 6 <head> 7 <title>Test for Bug 345624</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <script src="/tests/SimpleTest/EventUtils.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 <style> 12 input, textarea { background-color: rgb(0,0,0) !important; } 13 :-moz-any(input,textarea):valid { background-color: rgb(0,255,0) !important; } 14 :-moz-any(input,textarea):invalid { background-color: rgb(255,0,0) !important; } 15 </style> 16 </head> 17 <body> 18 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=345624">Mozilla Bug 345624</a> 19 <p id="display"></p> 20 <div id="content"> 21 <input id='i'> 22 <textarea id='t'></textarea> 23 </div> 24 <pre id="test"> 25 <script type="application/javascript"> 26 27 /** Test for Bug 345624 */ 28 29 /** 30 * This test is checking only tooLong related features 31 * related to constraint validation. 32 */ 33 34 function checkTooLongValidity(element) 35 { 36 element.value = "foo"; 37 ok(!element.validity.tooLong, 38 "Element should not be too long when maxlength is not set"); 39 is(window.getComputedStyle(element).getPropertyValue('background-color'), 40 "rgb(0, 255, 0)", ":valid pseudo-class should apply"); 41 ok(element.validity.valid, "Element should be valid"); 42 ok(element.checkValidity(), "The element should be valid"); 43 44 element.maxLength = 1; 45 ok(!element.validity.tooLong, 46 "Element should not be too long unless the user edits it"); 47 is(window.getComputedStyle(element).getPropertyValue('background-color'), 48 "rgb(0, 255, 0)", ":valid pseudo-class should apply"); 49 ok(element.validity.valid, "Element should be valid"); 50 ok(element.checkValidity(), "The element should be valid"); 51 52 element.focus(); 53 54 synthesizeKey("KEY_Backspace"); 55 is(element.value, "fo", "value should have changed"); 56 ok(element.validity.tooLong, 57 "Element should be too long after a user edit that does not make it short enough"); 58 is(window.getComputedStyle(element).getPropertyValue('background-color'), 59 "rgb(255, 0, 0)", ":invalid pseudo-class should apply"); 60 ok(!element.validity.valid, "Element should be invalid"); 61 ok(!element.checkValidity(), "The element should not be valid"); 62 is(element.validationMessage, 63 "Please shorten this text to 1 characters or less (you are currently using 2 characters).", 64 "The validation message text is not correct"); 65 66 synthesizeKey("KEY_Backspace"); 67 is(element.value, "f", "value should have changed"); 68 ok(!element.validity.tooLong, 69 "Element should not be too long after a user edit makes it short enough"); 70 is(window.getComputedStyle(element).getPropertyValue('background-color'), 71 "rgb(0, 255, 0)", ":valid pseudo-class should apply"); 72 ok(element.validity.valid, "Element should be valid"); 73 74 element.maxLength = 2; 75 ok(!element.validity.tooLong, 76 "Element should remain valid if maxlength changes but maxlength > length"); 77 is(window.getComputedStyle(element).getPropertyValue('background-color'), 78 "rgb(0, 255, 0)", ":valid pseudo-class should apply"); 79 ok(element.validity.valid, "Element should be valid"); 80 81 element.maxLength = 1; 82 ok(!element.validity.tooLong, 83 "Element should remain valid if maxlength changes but maxlength = length"); 84 is(window.getComputedStyle(element).getPropertyValue('background-color'), 85 "rgb(0, 255, 0)", ":valid pseudo-class should apply"); 86 ok(element.validity.valid, "Element should be valid"); 87 ok(element.checkValidity(), "The element should be valid"); 88 89 element.maxLength = 0; 90 ok(element.validity.tooLong, 91 "Element should become invalid if maxlength changes and maxlength < length"); 92 is(window.getComputedStyle(element).getPropertyValue('background-color'), 93 "rgb(255, 0, 0)", ":invalid pseudo-class should apply"); 94 ok(!element.validity.valid, "Element should be invalid"); 95 ok(!element.checkValidity(), "The element should not be valid"); 96 is(element.validationMessage, 97 "Please shorten this text to 0 characters or less (you are currently using 1 characters).", 98 "The validation message text is not correct"); 99 100 element.maxLength = 1; 101 ok(!element.validity.tooLong, 102 "Element should become valid if maxlength changes and maxlength = length"); 103 is(window.getComputedStyle(element).getPropertyValue('background-color'), 104 "rgb(0, 255, 0)", ":valid pseudo-class should apply"); 105 ok(element.validity.valid, "Element should be valid"); 106 ok(element.checkValidity(), "The element should be valid"); 107 108 element.value = "test"; 109 ok(!element.validity.tooLong, 110 "Element should stay valid after programmatic edit (even if value is too long)"); 111 is(window.getComputedStyle(element).getPropertyValue('background-color'), 112 "rgb(0, 255, 0)", ":valid pseudo-class should apply"); 113 ok(element.validity.valid, "Element should be valid"); 114 ok(element.checkValidity(), "The element should be valid"); 115 116 element.setCustomValidity("custom message"); 117 is(window.getComputedStyle(element).getPropertyValue('background-color'), 118 "rgb(255, 0, 0)", ":invalid pseudo-class should apply"); 119 is(element.validationMessage, "custom message", 120 "Custom message should be shown instead of too long one"); 121 } 122 123 checkTooLongValidity(document.getElementById('i')); 124 checkTooLongValidity(document.getElementById('t')); 125 126 </script> 127 </pre> 128 </body> 129 </html>