4 <script src=
"../../../resources/js-test.js"></script>
7 <p id=
"description"></p>
8 <div id=
"console"></div>
10 description('Tests for .valueAsNumber with <input type=time>.');
12 var input
= document
.createElement('input');
15 function valueAsNumberFor(stringValue
) {
16 input
.value
= stringValue
;
17 return input
.valueAsNumber
;
20 function setValueAsNumberAndGetValue(hour
, minute
, second
, ms
) {
21 input
.valueAsNumber
= ((hour
* 60 + minute
) * 60 + second
) * 1000 + ms
;
25 shouldBe('valueAsNumberFor("")', 'Number.NaN');
26 shouldBe('valueAsNumberFor("00:00:00.000")', 'Date.UTC(1970, 0, 1, 0, 0, 0, 0)');
27 shouldBe('valueAsNumberFor("04:35")', 'Date.UTC(1970, 0, 1, 4, 35, 0, 0)');
28 shouldBe('valueAsNumberFor("23:59:59.999")', 'Date.UTC(1970, 0, 1, 23, 59, 59, 999)');
30 shouldBe('setValueAsNumberAndGetValue(0, 0, 0, 0)', '"00:00"');
31 shouldBe('setValueAsNumberAndGetValue(0, 0, 1, 0)', '"00:00:01"');
32 shouldBe('setValueAsNumberAndGetValue(0, 0, 0, 2)', '"00:00:00.002"');
33 shouldBe('setValueAsNumberAndGetValue(11, 59, 59, 999)', '"11:59:59.999"');
34 shouldBe('setValueAsNumberAndGetValue(12, 0, 0, 0)', '"12:00"');
35 shouldBe('setValueAsNumberAndGetValue(23, 59, 59, 999)', '"23:59:59.999"');
36 shouldBe('setValueAsNumberAndGetValue(24, 0, 0, 0)', '"00:00"');
37 shouldBe('setValueAsNumberAndGetValue(48, 0, 13, 0)', '"00:00:13"');
38 shouldBe('setValueAsNumberAndGetValue(-23, -59, -59, 0)', '"00:00:01"');
40 debug('Tests to set invalid values to valueAsNumber:');
41 shouldBe('input.value = ""; input.valueAsNumber = null; input.value', '"00:00"');
42 shouldBeNaN('input.valueAsNumber = "foo"; input.valueAsNumber');
43 shouldBeEqualToString('input.valueAsNumber = "foo"; input.value', '');
44 shouldBeNaN('input.valueAsNumber = NaN; input.valueAsNumber');
45 shouldBeEqualToString('input.valueAsNumber = NaN; input.value', '');
46 shouldBeNaN('input.valueAsNumber = Number.NaN; input.valueAsNumber');
47 shouldBeEqualToString('input.valueAsNumber = Number.NaN; input.value', '');
48 shouldThrow('input.valueAsNumber = Infinity', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
49 shouldThrow('input.valueAsNumber = Number.POSITIVE_INFINITY', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
50 shouldThrow('input.valueAsNumber = Number.NEGATIVE_INFINITY', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
52 debug('Step attribute value and string representation:');
53 // If the step attribute value is 1 second and the second part is 0, we should show the second part.
54 shouldBe('input.step = "1"; setValueAsNumberAndGetValue(0, 0, 0, 0)', '"00:00:00"');
55 // If the step attribute value is 0.001 second and the millisecond part is 0, we should show the millisecond part.
56 shouldBe('input.step = "0.001"; setValueAsNumberAndGetValue(0, 0, 0, 0)', '"00:00:00.000"');