blob: e29f6f953462e51d4e7c03cce9e25359efce8ec0 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description('Tests for .valueAsNumber with &lt;input type=datetime>.');
var input = document.createElement('input');
input.type = 'datetime';
function valueAsNumberFor(stringValue) {
input.value = stringValue;
return input.valueAsNumber;
}
function setValueAsNumberAndGetValue(year, month, day, hour, minute, second, msec) {
var date = new Date();
date.setTime(Date.UTC(year, month, day, hour, minute, second, msec));
if (year < 100)
date.setUTCFullYear(year);
input.valueAsNumber = date.getTime();
return input.value;
}
shouldBe('valueAsNumberFor("")', 'Number.NaN');
shouldBe('valueAsNumberFor("1969-12-31T12:34:56.789Z")', 'Date.UTC(1969, 11, 31, 12, 34, 56, 789)');
shouldBe('valueAsNumberFor("1970-01-01T00:00:00.000Z")', 'Date.UTC(1970, 0, 1, 0, 0, 0)');
shouldBe('valueAsNumberFor("2009-12-22T11:32:11Z")', 'Date.UTC(2009, 11, 22, 11, 32, 11)');
shouldBe('setValueAsNumberAndGetValue(1969, 11, 1, 0, 0, 0, 0)', '"1969-12-01T00:00Z"');
shouldBe('setValueAsNumberAndGetValue(1970, 0, 1, 10, 1, 0, 100)', '"1970-01-01T10:01:00.100Z"');
shouldBe('setValueAsNumberAndGetValue(2009, 11, 31, 23, 59, 59, 999)', '"2009-12-31T23:59:59.999Z"');
shouldBe('setValueAsNumberAndGetValue(10000, 0, 1, 12, 0, 1, 0)', '"10000-01-01T12:00:01Z"');
shouldBe('setValueAsNumberAndGetValue(-1, 0, 1, 0, 0, 0, 0)', '""');
shouldBe('setValueAsNumberAndGetValue(0, 11, 31, 23, 59, 59, 999)', '""');
shouldBe('setValueAsNumberAndGetValue(1, 0, 1, 0, 0, 0, 0)', '"0001-01-01T00:00Z"');
shouldBe('setValueAsNumberAndGetValue(275760, 8, 12, 0, 0, 0, 1)', '"275760-09-12T00:00:00.001Z"');
shouldBe('setValueAsNumberAndGetValue(275760, 8, 13, 0, 0, 0, 0)', '"275760-09-13T00:00Z"');
debug('Tests to set invalid values to valueAsNumber:');
shouldBe('input.value = ""; input.valueAsNumber = null; input.value', '"1970-01-01T00:00Z"');
shouldThrow('input.valueAsNumber = "foo"', '"NotSupportedError (DOM Exception 9): The operation is not supported."');
shouldThrow('input.valueAsNumber = NaN', '"NotSupportedError (DOM Exception 9): The operation is not supported."');
shouldThrow('input.valueAsNumber = Number.NaN', '"NotSupportedError (DOM Exception 9): The operation is not supported."');
shouldThrow('input.valueAsNumber = Infinity', '"NotSupportedError (DOM Exception 9): The operation is not supported."');
shouldThrow('input.valueAsNumber = Number.POSITIVE_INFINITY', '"NotSupportedError (DOM Exception 9): The operation is not supported."');
shouldThrow('input.valueAsNumber = Number.NEGATIVE_INFINITY', '"NotSupportedError (DOM Exception 9): The operation is not supported."');
shouldThrow('input.valueAsNumber = Date.UTC(275760, 8, 13, 0, 0, 0, 1)', '"NotSupportedError (DOM Exception 9): The operation is not supported."');
debug('Step attribute value and string representation:');
// String representation should not depend on the step attribute.
shouldBe('input.step = "1"; setValueAsNumberAndGetValue(2010, 0, 21, 0, 0, 0, 0)', '"2010-01-21T00:00Z"');
shouldBe('input.step = "0.001"; setValueAsNumberAndGetValue(2010, 0, 21, 0, 0, 0, 0)', '"2010-01-21T00:00Z"');
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>