<!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="time">
<input id="after" type="text">

<script>

jsTestIsAsync = true;

changeEventsFired = 0;
function onChangeEvent() {
    changeEventsFired++;
}

inputEventsFired = 0;
function onInputEvent() {
    inputEventsFired++;
}

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

    changeEventsFired = 0;
    inputEventsFired = 0;
}

input.addEventListener("change", onChangeEvent);
input.addEventListener("input", onInputEvent);

addEventListener("load", async () => {
    description("Test for keyboard operations for &lt;input type=time&gt;");

    beginTest("Digit keys");                               // [hh]:mm tt
    UIHelper.keyDown("1");                                 // -> [01]:mm tt
    UIHelper.keyDown("2");                                 // -> [12]:mm tt
    UIHelper.keyDown("rightArrow");                        // -> 12:[mm] tt
    UIHelper.keyDown("2");                                 // -> 12:[02] tt
    UIHelper.keyDown("rightArrow");                        // -> 12:02 [tt]
    UIHelper.keyDown("A");                                 // -> 12:02 [AM]
    shouldBeEqualToString("input.value", "00:02");
    shouldBe("changeEventsFired", "1");
    shouldBe("inputEventsFired", "1");

    beginTest("Digit keys with timeout");                  // [hh]:mm tt
    UIHelper.keyDown("2");                                 // [02]:mm tt
    UIHelper.keyDown("rightArrow");                        // -> 02:[mm] tt
    UIHelper.keyDown("4");                                 // -> 02:[04] tt
    await UIHelper.delayFor(1500);                         // Wait.
    UIHelper.keyDown("5");                                 // -> 02:[05] tt
    UIHelper.keyDown("rightArrow");                        // -> 02:05 [tt]
    UIHelper.keyDown("P");                                 // -> 02:05 [PM]
    shouldBeEqualToString("input.value", "14:05");
    shouldBe("changeEventsFired", "1");
    shouldBe("inputEventsFired", "1");

    beginTest("Digit keys clamp value", "12:22");          // [12]:22 PM
    UIHelper.keyDown("1");                                 // -> [01]:22 PM
    shouldBeEqualToString("input.value", "13:22");
    UIHelper.keyDown("3");                                 // -> [12]:22 PM
    shouldBeEqualToString("input.value", "12:22");
    UIHelper.keyDown("0");                                 // -> [01]:22 PM
    shouldBeEqualToString("input.value", "13:22");
    UIHelper.keyDown("rightArrow");                        // -> 13:[22] PM
    UIHelper.keyDown("6");                                 // -> 13:[06] PM
    shouldBeEqualToString("input.value", "13:06");
    UIHelper.keyDown("1");                                 // -> 13:[59] PM
    shouldBeEqualToString("input.value", "13:59");
    shouldBe("changeEventsFired", "5");
    shouldBe("inputEventsFired", "5");

    beginTest("Left/Right arrow keys", "15:27");           // [03]:27 PM
    UIHelper.keyDown("2");                                 // -> [02]:27 PM
    UIHelper.keyDown("rightArrow");                        // -> 02:[27] PM
    UIHelper.keyDown("2");                                 // -> 02:[02] PM
    UIHelper.keyDown("rightArrow");                        // -> 02:02 [PM]
    UIHelper.keyDown("A");                                 // -> 02:02 [AM]
    shouldBeEqualToString("input.value", "02:02");
    UIHelper.keyDown("leftArrow");                         // -> 02:[02] AM
    UIHelper.keyDown("3");                                 // -> 02:[03] AM
    UIHelper.keyDown("leftArrow");                         // -> [02]:03 AM
    UIHelper.keyDown("3");                                 // -> [03]:03 AM
    shouldBeEqualToString("input.value", "03:03");
    shouldBe("changeEventsFired", "5");
    shouldBe("inputEventsFired", "5");

    beginTest("Advance field keys", "01:28");              // [01]:28 AM
    UIHelper.keyDown("/");                                 // -> 01:[28] AM
    UIHelper.keyDown("3");                                 // -> 01:[03] AM
    shouldBeEqualToString("input.value", "01:03");
    UIHelper.keyDown("leftArrow");                         // -> [01]:03 AM
    UIHelper.keyDown("-");                                 // -> 01:[03] AM
    UIHelper.keyDown("4");                                 // -> 01:[04] AM
    shouldBeEqualToString("input.value", "01:04");
    UIHelper.keyDown("leftArrow");                         // -> [01]:04 AM
    UIHelper.keyDown(".");                                 // -> 01:[04] AM
    UIHelper.keyDown("5");                                 // -> 01:[05] AM
    shouldBeEqualToString("input.value", "01:05");
    UIHelper.keyDown("leftArrow");                         // -> [01]:05 AM
    UIHelper.keyDown(":");                                 // -> 01:[05] AM
    UIHelper.keyDown("6");                                 // -> 01:[06] AM
    shouldBeEqualToString("input.value", "01:06");
    UIHelper.keyDown("leftArrow");                         // -> [01]:06 AM
    UIHelper.keyDown(";");                                 // -> 01:[06] AM
    UIHelper.keyDown("7");                                 // -> 01:[07] AM
    shouldBeEqualToString("input.value", "01:07");
    UIHelper.keyDown("leftArrow");                         // -> [01]:07 AM
    UIHelper.keyDown(",");                                 // -> 01:[07] AM
    UIHelper.keyDown("8");                                 // -> 01:[08] AM
    shouldBeEqualToString("input.value", "01:08");
    shouldBe("changeEventsFired", "6");
    shouldBe("inputEventsFired", "6");

    beginTest("Up/Down arrow keys", "23:59");              // [11]:59 PM
    UIHelper.keyDown("upArrow");                           // -> [12]:59 PM
    shouldBeEqualToString("input.value", "12:59");
    UIHelper.keyDown("upArrow");                           // -> [01]:59 PM
    shouldBeEqualToString("input.value", "13:59");
    UIHelper.keyDown("downArrow");                         // -> [12]:59 PM
    shouldBeEqualToString("input.value", "12:59");
    UIHelper.keyDown("downArrow");                         // -> [11]:59 PM
    shouldBeEqualToString("input.value", "23:59");
    UIHelper.keyDown("rightArrow");                        // -> 11:[59] PM
    UIHelper.keyDown("rightArrow");                        // -> 11:59 [PM]
    UIHelper.keyDown("upArrow");                           // -> 11:59 [AM]
    shouldBeEqualToString("input.value", "11:59");
    UIHelper.keyDown("downArrow");                         // -> 11:59 [PM]
    shouldBeEqualToString("input.value", "23:59");
    UIHelper.keyDown("downArrow");                         // -> 11:59 [AM]
    shouldBeEqualToString("input.value", "11:59");
    shouldBe("changeEventsFired", "7");
    shouldBe("inputEventsFired", "7");

    beginTest("Tab key");                                  // [hh]:mm tt
    UIHelper.keyDown("2");                                 // -> [02]:mm tt
    UIHelper.keyDown("\t");                                // -> 02:[mm] tt
    UIHelper.keyDown("2");                                 // -> 02:[02] tt
    UIHelper.keyDown("\t");                                // -> 02:02 [tt]
    UIHelper.keyDown("A");                                 // -> 02:02 [AM]
    shouldBeEqualToString("input.value", "02:02");
    UIHelper.keyDown("\t");                                // Focus out.
    shouldBeEqualToString("document.activeElement.id", "after");
    UIHelper.keyDown("\t", ["shiftKey"]);                  // -> 02:02 [AM]
    UIHelper.keyDown("\t", ["shiftKey"]);                  // -> 02:[02] AM
    UIHelper.keyDown("3");                                 // -> 02:[03] AM
    UIHelper.keyDown("\t", ["shiftKey"]);                  // -> [02]:03 AM
    UIHelper.keyDown("3");                                 // -> [03]:03 AM
    shouldBeEqualToString("input.value", "03:03");
    UIHelper.keyDown("\t", ["shiftKey"]);                  // Focus out.
    shouldBeEqualToString("document.activeElement.id", "before");
    shouldBe("changeEventsFired", "3");
    shouldBe("inputEventsFired", "3");

    beginTest("Backspace key", "16:30");                   // [04]:30 PM
    UIHelper.keyDown("\b");                                // -> [hh]:30 PM
    shouldBeEqualToString("input.value", "");
    UIHelper.keyDown("7");                                 // -> [07]:30 PM
    shouldBeEqualToString("input.value", "19:30");
    shouldBe("changeEventsFired", "2");
    shouldBe("inputEventsFired", "2");

    beginTest("Delete key", "18:20");                      // [06]:20 PM
    UIHelper.keyDown("delete");                            // -> [hh]:20 PM
    shouldBeEqualToString("input.value", "");
    shouldBe("changeEventsFired", "1");
    shouldBe("inputEventsFired", "1");

    beginTest("Disabled/readonly", "09:01");
    input.disabled = true;
    UIHelper.keyDown("1");
    shouldBeEqualToString("input.value", "09:01");
    input.disabled = false;
    input.focus();
    UIHelper.keyDown("1");
    shouldBeEqualToString("input.value", "01:01");
    input.readOnly = true;
    UIHelper.keyDown("rightArrow");
    UIHelper.keyDown("2");
    shouldBeEqualToString("input.value", "01:01");
    input.readOnly = false;
    UIHelper.keyDown("2");
    shouldBeEqualToString("input.value", "01:02");
    shouldBe("changeEventsFired", "2");
    shouldBe("inputEventsFired", "2");

    finishJSTest();
});

</script>

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