<!DOCTYPE html>
<html>
<head>
<script src="../../../../resources/js-test-pre.js"></script>
<script src="../../../../resources/ui-helper.js"></script>
</head>
<body>

<input id="before" type="text">
<input id="input" type="datetime-local">
<input id="after" type="text">

<script>

jsTestIsAsync = true;

function beginTest(title, value, step) {
    debug("\n" + title);
    input.value = value || "";
    input.step = step || "";
    input.blur();
    input.focus();
}

function verifyNumberOfFields(expected) {
    numberOfFields = 0;

    // Focus first field.
    input.blur();
    input.focus();

    while (document.activeElement !== after) {
        numberOfFields++;
        UIHelper.keyDown("\t");
    }

    // Return focus to first field.
    input.blur();
    input.focus();

    shouldBe("numberOfFields", expected.toString());
}

addEventListener("load", async () => {
    description("Test for presence of second and millisecond fields in &lt;input type=datetime-local&gt;");

    defaultNumberOfFields = 6; // Year, month, day, hour, minute, and AM/PM fields
    numberOfFieldsWithSeconds = defaultNumberOfFields + 1;
    numberOfFieldsWithMilliseconds = numberOfFieldsWithSeconds + 1;

    beginTest("Default", "2020-09-08T10:20");
    verifyNumberOfFields(defaultNumberOfFields);

    beginTest("Zero seconds", "2020-09-08T10:20:00");
    verifyNumberOfFields(defaultNumberOfFields);

    beginTest("Zero seconds and zero milliseconds", "2020-09-08T10:20:00.000");
    verifyNumberOfFields(defaultNumberOfFields);

    beginTest("Zero seconds and non-zero milliseconds", "2020-09-08T10:20:00.567");
    verifyNumberOfFields(numberOfFieldsWithMilliseconds);

    beginTest("Non-zero seconds", "2020-09-08T10:20:45");
    verifyNumberOfFields(numberOfFieldsWithSeconds);

    beginTest("Non-zero seconds and zero milliseconds", "2020-09-08T10:20:45.000");
    verifyNumberOfFields(numberOfFieldsWithSeconds);

    beginTest("Non-zero seconds and non-zero milliseconds", "2020-09-08T10:20:45.567");
    verifyNumberOfFields(numberOfFieldsWithMilliseconds);

    beginTest("Step attribute minute precision", "2020-09-08T10:20", "60");
    verifyNumberOfFields(defaultNumberOfFields);

    beginTest("Step attribute second precision", "2020-09-08T10:20", "2");
    verifyNumberOfFields(numberOfFieldsWithSeconds);

    beginTest("Step attribute millisecond precision", "2020-09-08T10:20", "0.5");
    verifyNumberOfFields(numberOfFieldsWithMilliseconds);

    debug("");
    finishJSTest();
});

</script>

<script src="../../../../resources/js-test-post.js"></script>
</body>
</html>
