input-valueasnumber-stepping.html (3667B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>valueAsNumber stepping</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 </head> 8 <body> 9 <p> 10 <h3>input_valueAsNumber_stepping</h3> 11 <!-- This test verifies that valueAsNumber reads and writes number values, 12 that those values step by the correct default step, and that the values 13 represent the correct milliseconds/months. 14 --> 15 </p> 16 17 <hr> 18 19 <div id="log"></div> 20 21 <form method="post" 22 enctype="application/x-www-form-urlencoded" 23 action="" 24 name="input_form"> 25 <p><input type='date' id='input_date'></p> 26 <p><input type='time' id='input_time'></p> 27 <p><input type='week' id='input_week'></p> 28 <p><input type='month' id='input_month'></p> 29 <p><input type='datetime-local' id='input_datetime-local'></p> 30 <p><input type='range' id='input_range'></p> 31 <p><input type='number' id='input_number'></p> 32 </form> 33 34 <script> 35 function test_stepping(inputType, stringValue, steppedString, baseNumber, stepAmount) { 36 test(function() { 37 // put number in 38 input = document.getElementById("input_" + inputType); 39 input.valueAsNumber = baseNumber 40 41 // get string out 42 // startsWith is here to allow for optional seconds and milliseconds. 43 // the replace("T", " ") fallback is for https://github.com/web-platform-tests/wpt/issues/20994 44 var sanitizedStr = input.value; 45 assert_true(sanitizedStr.startsWith(stringValue) || sanitizedStr.startsWith(stringValue.replace("T", " ")), 46 "The input value [" + sanitizedStr + "] must resemble [" + stringValue + "]"); 47 48 // get number out 49 var sanitized = input.valueAsNumber; 50 assert_equals(sanitized, baseNumber, "The input valueAsNumber must equal the original number.") 51 52 // step up, get new date out 53 input.stepUp() 54 var steppedNumber = input.valueAsNumber; 55 assert_equals(steppedNumber, baseNumber + stepAmount, "Stepping must be by the correct amount") 56 57 // get new string out 58 var steppedStrOut = input.value; 59 assert_true(steppedStrOut.startsWith(steppedString) || steppedStrOut.startsWith(steppedString.replace("T", " ")), 60 "The changed input value [" + steppedStrOut + "] must resemble [" + steppedString + "]"); 61 62 // step back down, get first date out again 63 input.stepDown() 64 var backDown = input.valueAsNumber; 65 assert_equals(backDown, baseNumber, "Stepping back down must return the number to its original value"); 66 67 }, inputType + " should step correctly"); 68 } 69 70 var millis_per_day = 24 * 60 * 60 * 1000; 71 72 // jan 1 midnight, step 1 day to jan 2 73 test_stepping("date", "1970-01-01", "1970-01-02", 0, millis_per_day); 74 75 // jan 1 midnight, step 1 minute to 00:01:00 76 test_stepping("time", "00:00", "00:01", 0, 60 * 1000); 77 78 // jan 1 midnight, step 1 month (not counting by milliseconds) to feb 1 79 test_stepping("month", "1970-01", "1970-02", 0, 1); 80 81 // monday jan 5 1970 midnight, step 7 days to jan 12 82 // (this has to start on a monday for stepping up and down to return) 83 test_stepping("week", "1970-W02", "1970-W03", 4 * millis_per_day, 7 * millis_per_day); 84 85 // jan 1 midnight, step 1 minute to 00:01:00 86 test_stepping("datetime-local", "1970-01-01T00:00", "1970-01-01T00:01", 0, 60 * 1000); 87 88 // numbers, for which the default step is 1 89 test_stepping("range", "22", "23", 22, 1); 90 test_stepping("number", "24", "25", 24, 1); 91 </script> 92 93 </body> 94 </html>