input-valueasdate-stepping.html (3083B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>valueAsDate 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_valueAsDate_stepping</h3> 11 <!-- This test verifies that valueAsDate reads and writes Date values, 12 that those values step by the correct default step, and that the values 13 represent the correct times. 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 </form> 30 31 <script> 32 function test_stepping(inputType, stringValue, steppedString, baseMillis, stepAmount) { 33 test(function() { 34 // put date in, constructed from a UTC timestamp so the test doesn't 35 // vary by local timezone 36 input = document.getElementById("input_" + inputType); 37 input.valueAsDate = new Date(baseMillis) 38 39 // get string out (using startsWith here to allow for optional 40 // seconds and milliseconds) 41 var sanitizedStr = input.value; 42 assert_true(sanitizedStr.startsWith(stringValue), 43 "The input value [" + sanitizedStr + "] must resemble [" + stringValue + "]"); 44 45 // get date out 46 var sanitized = input.valueAsDate; 47 assert_equals(sanitized.getTime(), baseMillis, "The input valueAsDate must represent the same time as the original Date.") 48 49 // step up, get new date out 50 input.stepUp() 51 var steppedDate = input.valueAsDate; 52 assert_equals(steppedDate.getTime(), baseMillis + stepAmount, "Stepping must be by the correct amount") 53 54 // get new string out 55 var steppedStrOut = input.value; 56 assert_true(steppedStrOut.startsWith(steppedString), 57 "The changed input value [" + steppedStrOut + "] must resemble ["+steppedString+"]"); 58 59 // step back down, get first date out again 60 input.stepDown() 61 var backDown = input.valueAsDate; 62 assert_equals(backDown.getTime(), baseMillis, "Stepping back down must return the date to its original value"); 63 64 }, inputType + " should step correctly"); 65 } 66 67 var millis_per_day = 24 * 60 * 60 * 1000; 68 69 // jan 1 midnight, step 1 day to jan 2 70 test_stepping("date", "1970-01-01", "1970-01-02", 0, millis_per_day); 71 72 // jan 1 midnight, step 1 minute to 00:01:00 73 test_stepping("time", "00:00", "00:01", 0, 60 * 1000); 74 75 // jan 1 midnight, step 31 days to feb 1 76 test_stepping("month", "1970-01", "1970-02", 0, 31 * millis_per_day); 77 78 // monday jan 5 1970 midnight, step 7 days to jan 12 79 // (this has to start on a monday for stepping up and down to return) 80 test_stepping("week", "1970-W02", "1970-W03", 4 * millis_per_day, 7 * millis_per_day); 81 </script> 82 </body> 83 </html>