4 <script src="../../js/resources/js-test-pre.js"></script>
7 <p id="description"></p>
8 <div id="console"></div>
10 description('Tests for .valueAsNumber with <input type=datetime-local>.');
12 var input = document.createElement('input');
13 input.type = 'datetime-local';
15 function valueAsNumberFor(stringValue) {
16 input.value = stringValue;
17 return input.valueAsNumber;
20 function setValueAsNumberAndGetValue(year, month, day, hour, minute, second, msec) {
21 var date = new Date();
22 date.setTime(Date.UTC(year, month, day, hour, minute, second, msec));
24 date.setUTCFullYear(year);
25 input.valueAsNumber = date.getTime();
29 shouldBe('valueAsNumberFor("")', 'Number.NaN');
30 shouldBe('valueAsNumberFor("1969-12-31T12:34:56.789")', 'Date.UTC(1969, 11, 31, 12, 34, 56, 789)');
31 shouldBe('valueAsNumberFor("1970-01-01T00:00:00.000")', 'Date.UTC(1970, 0, 1, 0, 0, 0)');
32 shouldBe('valueAsNumberFor("2009-12-22T11:32:11")', 'Date.UTC(2009, 11, 22, 11, 32, 11)');
34 shouldBe('setValueAsNumberAndGetValue(1969, 11, 1, 0, 0, 0, 0)', '"1969-12-01T00:00"');
35 shouldBe('setValueAsNumberAndGetValue(1970, 0, 1, 10, 1, 0, 100)', '"1970-01-01T10:01:00.100"');
36 shouldBe('setValueAsNumberAndGetValue(2009, 11, 31, 23, 59, 59, 999)', '"2009-12-31T23:59:59.999"');
37 shouldBe('setValueAsNumberAndGetValue(10000, 0, 1, 12, 0, 1, 0)', '"10000-01-01T12:00:01"');
39 shouldBe('setValueAsNumberAndGetValue(-1, 0, 1, 0, 0, 0, 0)', '""');
40 shouldBe('setValueAsNumberAndGetValue(0, 11, 31, 23, 59, 59, 999)', '""');
41 shouldBe('setValueAsNumberAndGetValue(1, 0, 1, 0, 0, 0, 0)', '"0001-01-01T00:00"');
42 shouldBe('setValueAsNumberAndGetValue(275760, 8, 12, 0, 0, 0, 1)', '"275760-09-12T00:00:00.001"');
43 shouldBe('setValueAsNumberAndGetValue(275760, 8, 13, 0, 0, 0, 0)', '"275760-09-13T00:00"');
45 debug('Tests to set invalid values to valueAsNumber:');
46 shouldBe('input.value = ""; input.valueAsNumber = null; input.value', '"1970-01-01T00:00"');
47 shouldThrow('input.valueAsNumber = "foo"', '"Error: NOT_SUPPORTED_ERR: DOM Exception 9"');
48 shouldThrow('input.valueAsNumber = NaN', '"Error: NOT_SUPPORTED_ERR: DOM Exception 9"');
49 shouldThrow('input.valueAsNumber = Number.NaN', '"Error: NOT_SUPPORTED_ERR: DOM Exception 9"');
50 shouldThrow('input.valueAsNumber = Infinity', '"Error: NOT_SUPPORTED_ERR: DOM Exception 9"');
51 shouldThrow('input.valueAsNumber = Number.POSITIVE_INFINITY', '"Error: NOT_SUPPORTED_ERR: DOM Exception 9"');
52 shouldThrow('input.valueAsNumber = Number.NEGATIVE_INFINITY', '"Error: NOT_SUPPORTED_ERR: DOM Exception 9"');
53 shouldThrow('input.valueAsNumber = Date.UTC(275760, 8, 13, 0, 0, 0, 1)', '"Error: NOT_SUPPORTED_ERR: DOM Exception 9"');
55 debug('Step attribute value and string representation:');
56 // If the step attribute value is 1 second and the second part is 0, we should show the second part.
57 shouldBe('input.step = "1"; setValueAsNumberAndGetValue(2010, 0, 21, 0, 0, 0, 0)', '"2010-01-21T00:00:00"');
58 // If the step attribute value is 0.001 second and the millisecond part is 0, we should show the millisecond part.
59 shouldBe('input.step = "0.001"; setValueAsNumberAndGetValue(2010, 0, 21, 0, 0, 0, 0)', '"2010-01-21T00:00:00.000"');
61 <script src="../../js/resources/js-test-post.js"></script>