test_input_number_validation.html (4488B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=827161 5 --> 6 <head> 7 <title>Test validation of number control input</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <script src="/tests/SimpleTest/EventUtils.js"></script> 10 <script type="text/javascript" src="test_input_number_data.js"></script> 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 12 <meta charset="UTF-8"> 13 </head> 14 <body> 15 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=827161">Mozilla Bug 827161</a> 16 <p id="display"></p> 17 <div id="content"> 18 <input id="input" type="number" step="0.01" oninvalid="invalidEventHandler(event);"> 19 <input id="requiredinput" type="number" step="0.01" required 20 oninvalid="invalidEventHandler(event);"> 21 </div> 22 <pre id="test"> 23 <script type="application/javascript"> 24 25 /** 26 * Test for Bug 827161. 27 * This test checks that validation works correctly for <input type=number>. 28 */ 29 SimpleTest.waitForExplicitFinish(); 30 31 SimpleTest.waitForFocus(function() { 32 startTests(); 33 SimpleTest.finish(); 34 }); 35 36 var elem; 37 38 function runTest(test) { 39 elem.lang = test.langTag; 40 41 gInvalid = false; // reset 42 var desc = `${test.desc} (lang='${test.langTag}', id='${elem.id}')`; 43 elem.value = 0; 44 elem.focus(); 45 elem.select(); 46 sendString(test.inputWithGrouping); 47 checkIsInvalid(elem, `${desc} with grouping separator`); 48 sendChar("a"); 49 checkIsInvalid(elem, `${desc} with grouping separator`); 50 51 gInvalid = false; // reset 52 elem.value = 0; 53 elem.select(); 54 sendString(test.inputWithoutGrouping); 55 checkIsValid(elem, `${desc} without grouping separator`); 56 sendChar("a"); 57 checkIsInvalid(elem, `${desc} without grouping separator`); 58 } 59 60 function runInvalidInputTest(test) { 61 elem.lang = test.langTag; 62 63 gInvalid = false; // reset 64 var desc = `${test.desc} (lang='${test.langTag}', id='${elem.id}')`; 65 elem.value = 0; 66 elem.focus(); 67 elem.select(); 68 sendString(test.input); 69 checkIsInvalid(elem, `${desc} with invalid input ${test.input}`); 70 } 71 72 function startTests() { 73 elem = document.getElementById("input"); 74 for (var test of tests) { 75 runTest(test); 76 } 77 for (var test of invalidTests) { 78 runInvalidInputTest(test); 79 } 80 elem = document.getElementById("requiredinput"); 81 for (var test of tests) { 82 runTest(test); 83 } 84 85 gInvalid = false; // reset 86 elem.value = ""; 87 checkIsInvalidEmptyValue(elem, "empty value"); 88 } 89 90 var gInvalid = false; 91 92 function invalidEventHandler(e) 93 { 94 is(e.type, "invalid", "Invalid event type should be 'invalid'"); 95 gInvalid = true; 96 } 97 98 function checkIsValid(element, infoStr) 99 { 100 ok(!element.validity.badInput, 101 "Element should not suffer from bad input for " + infoStr); 102 ok(element.validity.valid, "Element should be valid for " + infoStr); 103 ok(element.checkValidity(), "checkValidity() should return true for " + infoStr); 104 ok(!gInvalid, "The invalid event should not have been thrown for " + infoStr); 105 is(element.validationMessage, '', 106 "Validation message should be the empty string for " + infoStr); 107 ok(element.matches(":valid"), ":valid pseudo-class should apply for " + infoStr); 108 } 109 110 function checkIsInvalid(element, infoStr) 111 { 112 ok(element.validity.badInput, 113 "Element should suffer from bad input for " + infoStr); 114 ok(!element.validity.valid, "Element should not be valid for " + infoStr); 115 ok(!element.checkValidity(), "checkValidity() should return false for " + infoStr); 116 ok(gInvalid, "The invalid event should have been thrown for " + infoStr); 117 is(element.validationMessage, "Please enter a number.", 118 "Validation message is not the expected message for " + infoStr); 119 ok(element.matches(":invalid"), ":invalid pseudo-class should apply for " + infoStr); 120 } 121 122 function checkIsInvalidEmptyValue(element, infoStr) 123 { 124 ok(!element.validity.badInput, 125 "Element should not suffer from bad input for " + infoStr); 126 ok(element.validity.valueMissing, 127 "Element should suffer from value missing for " + infoStr); 128 ok(!element.validity.valid, "Element should not be valid for " + infoStr); 129 ok(!element.checkValidity(), "checkValidity() should return false for " + infoStr); 130 ok(gInvalid, "The invalid event should have been thrown for " + infoStr); 131 is(element.validationMessage, "Please enter a number.", 132 "Validation message is not the expected message for " + infoStr); 133 ok(element.matches(":invalid"), ":invalid pseudo-class should apply for " + infoStr); 134 } 135 136 </script> 137 </pre> 138 </body> 139 </html>