chromium/third_party/blink/web_tests/fast/forms/datetimelocal/input-valueasnumber-datetimelocal.html

<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description('Tests for .valueAsNumber with &lt;input type=datetime-local>.');

var input = document.createElement('input');
input.type = 'datetime-local';

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.789")', 'Date.UTC(1969, 11, 31, 12, 34, 56, 789)');
shouldBe('valueAsNumberFor("1970-01-01T00:00:00.000")', 'Date.UTC(1970, 0, 1, 0, 0, 0)');
shouldBe('valueAsNumberFor("2009-12-22T11:32:11")', 'Date.UTC(2009, 11, 22, 11, 32, 11)');

shouldBeEqualToString('setValueAsNumberAndGetValue(1969, 11, 1, 0, 0, 0, 0)', '1969-12-01T00:00');
shouldBeEqualToString('setValueAsNumberAndGetValue(1970, 0, 1, 10, 1, 0, 100)', '1970-01-01T10:01:00.1');
shouldBeEqualToString('setValueAsNumberAndGetValue(2009, 11, 31, 23, 59, 59, 999)', '2009-12-31T23:59:59.999');
shouldBeEqualToString('setValueAsNumberAndGetValue(10000, 0, 1, 12, 0, 1, 0)', '10000-01-01T12:00:01');

shouldBeEqualToString('setValueAsNumberAndGetValue(-1, 0, 1, 0, 0, 0, 0)', '');
shouldBeEqualToString('setValueAsNumberAndGetValue(0, 11, 31, 23, 59, 59, 999)', '');
shouldBeEqualToString('setValueAsNumberAndGetValue(1, 0, 1, 0, 0, 0, 0)', '0001-01-01T00:00');
shouldBeEqualToString('setValueAsNumberAndGetValue(275760, 8, 12, 0, 0, 0, 1)', '275760-09-12T00:00:00.001');
shouldBeEqualToString('setValueAsNumberAndGetValue(275760, 8, 13, 0, 0, 0, 0)', '275760-09-13T00:00');

debug('Tests to set invalid values to valueAsNumber:');
shouldBe('input.value = ""; input.valueAsNumber = null; input.value', '"1970-01-01T00:00"');
shouldBeNaN('input.valueAsNumber = "foo"; input.valueAsNumber');
shouldBeEqualToString('input.valueAsNumber = "foo"; input.value', '');
shouldBeNaN('input.valueAsNumber = NaN; input.valueAsNumber');
shouldBeEqualToString('input.valueAsNumber = NaN; input.value', '');
shouldBeNaN('input.valueAsNumber = Number.NaN; input.valueAsNumber');
shouldBeEqualToString('input.valueAsNumber = Number.NaN; input.value', '');
shouldThrow('input.valueAsNumber = Infinity', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
shouldThrow('input.valueAsNumber = Number.POSITIVE_INFINITY', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
shouldThrow('input.valueAsNumber = Number.NEGATIVE_INFINITY', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
shouldBeNaN('input.valueAsNumber = Date.UTC(275760, 8, 13, 0, 0, 0, 1); input.valueAsNumber');
shouldBeEqualToString('input.valueAsNumber = Date.UTC(275760, 8, 13, 0, 0, 0, 1); input.value', '');

debug('Step attribute value and string representation:');
// If the step attribute value is 1 second and the second part is 0, we should not show the second part.
shouldBe('input.step = "1"; setValueAsNumberAndGetValue(2010, 0, 21, 0, 0, 0, 0)', '"2010-01-21T00:00"');
// If the step attribute value is 0.001 second and the millisecond part is 0, we should not show the millisecond part.
shouldBe('input.step = "0.001"; setValueAsNumberAndGetValue(2010, 0, 21, 0, 0, 0, 0)', '"2010-01-21T00:00"');
</script>
</body>
</html>