Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / forms / number / number-valueasnumber.html
blobe403d0802a7b6f87a881be97f8bee009ddd42c55
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../../resources/js-test.js"></script>
5 </head>
6 <body>
7 <script>
8 description('Tests for .valueAsNumber with &lt;input type=number>.');
10 var input = document.createElement('input');
11 input.type = 'number';
13 function valueAsNumberFor(stringValue) {
14 input.value = stringValue;
15 return input.valueAsNumber;
18 function setValueAsNumberAndGetValue(num) {
19 input.valueAsNumber = num;
20 return input.value;
23 shouldBe('valueAsNumberFor("0")', '0');
24 shouldBe('valueAsNumberFor("0.2")', '0.2');
25 shouldBe('valueAsNumberFor(".2")', '0.2');
26 shouldBe('valueAsNumberFor("10")', '10');
27 shouldBe('valueAsNumberFor("01")', '1');
28 shouldBe('valueAsNumberFor("-0")', '0'); // "-0" is 0 in HTML5.
29 shouldBe('valueAsNumberFor("-1.2")', '-1.2');
30 shouldBe('valueAsNumberFor("1.2E10")', '1.2E10');
31 shouldBe('valueAsNumberFor("1.2E-10")', '1.2E-10');
32 shouldBe('valueAsNumberFor("1.2E+10")', '1.2E10');
33 shouldBe('valueAsNumberFor("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789")', '1.2345678901234568E+308');
34 shouldBe('valueAsNumberFor("0.12345678901234567890123456789012345678901234567890")', '0.123456789012345678');
36 debug('valueAsNumber for invalid string values:');
37 shouldBeTrue('isNaN(valueAsNumberFor(""))');
38 shouldBeTrue('isNaN(valueAsNumberFor("abc"))');
39 shouldBeTrue('isNaN(valueAsNumberFor("0xff"))');
40 shouldBeTrue('isNaN(valueAsNumberFor("+1"))');
41 shouldBeTrue('isNaN(valueAsNumberFor(" 10"))');
42 shouldBeTrue('isNaN(valueAsNumberFor("10 "))');
43 shouldBeTrue('isNaN(valueAsNumberFor("."))');
44 shouldBeTrue('isNaN(valueAsNumberFor("1E"))');
45 shouldBeTrue('isNaN(valueAsNumberFor("NaN"))');
46 shouldBeTrue('isNaN(valueAsNumberFor("nan"))');
47 shouldBeTrue('isNaN(valueAsNumberFor("Inf"))');
48 shouldBeTrue('isNaN(valueAsNumberFor("inf"))');
49 shouldBeTrue('isNaN(valueAsNumberFor("Infinity"))');
50 shouldBeTrue('isNaN(valueAsNumberFor("infinity"))');
52 debug('Too huge exponent to support');
53 shouldBeTrue('isNaN(valueAsNumberFor("1.2E65535"))');
55 debug('Tests for the valueAsNumber setter:');
56 shouldBeEqualToString('setValueAsNumberAndGetValue(0)', '0');
57 shouldBeEqualToString('setValueAsNumberAndGetValue(10)', '10');
58 shouldBeEqualToString('setValueAsNumberAndGetValue(01)', '1');
59 shouldBeEqualToString('setValueAsNumberAndGetValue(-0)', '0');
60 shouldBeEqualToString('setValueAsNumberAndGetValue(-1.2)', '-1.2');
61 shouldBeEqualToString('setValueAsNumberAndGetValue(1.2e10)', '12000000000');
62 shouldBeEqualToString('setValueAsNumberAndGetValue(1.2e-10)', '1.2e-10');
63 shouldBeEqualToString('setValueAsNumberAndGetValue(1.2345678901234567e+308)', '1.2345678901234567e+308');
64 shouldBeEqualToString('setValueAsNumberAndGetValue("-1.797693134862315e+308")', '-1.797693134862315e+308');
65 shouldThrow('setValueAsNumberAndGetValue("-1.797693134862316e+308")', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
66 shouldBe('setValueAsNumberAndGetValue("1.797693134862315e+308")', '"1.797693134862315e+308"');
67 shouldThrow('setValueAsNumberAndGetValue("1.797693134862316e+308")', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
69 debug('Tests to set invalid values to valueAsNumber:');
70 shouldBeEqualToString('setValueAsNumberAndGetValue(null)', '0');
71 shouldBeEqualToString('setValueAsNumberAndGetValue("foo")', '');
72 shouldBeEqualToString('setValueAsNumberAndGetValue(NaN)', '');
73 shouldBeEqualToString('setValueAsNumberAndGetValue(Number.NaN)', '');
74 shouldThrow('setValueAsNumberAndGetValue(Infinity)', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
75 shouldThrow('setValueAsNumberAndGetValue(Number.POSITIVE_INFINITY)', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
76 shouldThrow('setValueAsNumberAndGetValue(Number.NEGATIVE_INFINITY)', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
77 </script>
78 </body>
79 </html>