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

<input id="before" type="text">
<input id="input" type="date">
<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=date&gt;");

    beginTest("Digit keys");                               // [mm]/dd/yyyy
    UIHelper.keyDown("9");                                 // -> [09]/dd/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 09/[dd]/yyyy
    UIHelper.keyDown("2");                                 // -> 09/[02]/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 09/02/[yyyy]
    UIHelper.keyDown("2");                                 // -> 09/02/[0002]
    UIHelper.keyDown("0");                                 // -> 09/02/[0020]
    UIHelper.keyDown("1");                                 // -> 09/02/[0201]
    UIHelper.keyDown("2");                                 // -> 09/02/[2012]
    UIHelper.keyDown("A");                                 // Ignored.
    shouldBeEqualToString("input.value", "2012-09-02");
    shouldBe("changeEventsFired", "4");
    shouldBe("inputEventsFired", "4");

    beginTest("Digit keys with leading zero");             // [mm]/dd/yyyy
    UIHelper.keyDown("0");                                 // -> [00]/dd/yyyy
    UIHelper.keyDown("5");                                 // -> [05]/dd/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 05/[dd]/yyyy
    UIHelper.keyDown("0");                                 // -> 05/[00]/yyyy
    UIHelper.keyDown("6");                                 // -> 05/[06]/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 05/06/[yyyy]
    UIHelper.keyDown("0");                                 // -> 05/06/[0001]
    UIHelper.keyDown("0");                                 // -> 05/06/[0001]
    UIHelper.keyDown("3");                                 // -> 05/06/[0003]
    UIHelper.keyDown("4");                                 // -> 05/06/[0034]
    shouldBeEqualToString("input.value", "0034-05-06");
    shouldBe("changeEventsFired", "3");
    shouldBe("inputEventsFired", "3");

    beginTest("Digit keys and backspace key");             // [mm]/dd/yyyy
    UIHelper.keyDown("1");                                 // -> [01]/dd/yyyy
    UIHelper.keyDown("\b");                                // -> [mm]/dd/yyyy
    UIHelper.keyDown("2");                                 // -> [02]/dd/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 02/[dd]/yyyy
    UIHelper.keyDown("3");                                 // -> 02/[03]/yyyy
    UIHelper.keyDown("\b");                                // -> 02/[dd]/yyyy
    UIHelper.keyDown("4");                                 // -> 02/[04]/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 02/04/[yyyy]
    UIHelper.keyDown("4");                                 // -> 02/04/[0004]
    UIHelper.keyDown("\b");                                // -> 02/04/[yyyy]
    UIHelper.keyDown("2");                                 // -> 02/04/[0002]
    UIHelper.keyDown("0");                                 // -> 02/04/[0020]
    UIHelper.keyDown("2");                                 // -> 02/04/[0202]
    UIHelper.keyDown("0");                                 // -> 02/04/[2020]
    shouldBeEqualToString("input.value", "2020-02-04");
    shouldBe("changeEventsFired", "6");
    shouldBe("inputEventsFired", "6");

    beginTest("Digit keys with timeout");                  // [mm]/dd/yyyy
    UIHelper.keyDown("2");                                 // -> [02]/dd/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 02/[dd]/yyyy
    UIHelper.keyDown("4");                                 // -> 02/[04]/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 02/04/[yyyy]
    UIHelper.keyDown("4");                                 // -> 02/04/[0004]
    await UIHelper.delayFor(1500);                         // Wait.
    UIHelper.keyDown("1");                                 // -> 02/04/[0001]
    shouldBeEqualToString("input.value", "0001-02-04");
    shouldBe("changeEventsFired", "2");
    shouldBe("inputEventsFired", "2");

    beginTest("Digit keys clamp value");                   // [mm]/dd/yyyy
    UIHelper.keyDown("9");                                 // -> [09]/dd/yyyy
    UIHelper.keyDown("9");                                 // -> [12]/dd/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 12/[dd]/yyyy
    UIHelper.keyDown("9");                                 // -> 12/[09]/yyyy
    UIHelper.keyDown("9");                                 // -> 12/[31]/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 12/31/[yyyy]
    UIHelper.keyDown("9");                                 // -> 12/31/[0009]
    UIHelper.keyDown("9");                                 // -> 12/31/[0099]
    UIHelper.keyDown("9");                                 // -> 12/31/[0999]
    UIHelper.keyDown("9");                                 // -> 12/31/[9999]
    shouldBeEqualToString("input.value", "9999-12-31");
    shouldBe("changeEventsFired", "4");
    shouldBe("inputEventsFired", "4");

    beginTest("Left/Right arrow keys");                    // [mm]/dd/yyyy
    UIHelper.keyDown("2");                                 // -> [02]/dd/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 02/[dd]/yyyy
    UIHelper.keyDown("2");                                 // -> 02/[02]/yyyy
    UIHelper.keyDown("rightArrow");                        // -> 02/02/[yyyy]
    UIHelper.keyDown("2");                                 // -> 02/02/[0002]
    shouldBeEqualToString("input.value", "0002-02-02");
    UIHelper.keyDown("leftArrow");                         // -> 02/[02]/0002
    UIHelper.keyDown("3");                                 // -> 02/[03]/0002
    UIHelper.keyDown("leftArrow");                         // -> [02]/03/0002
    UIHelper.keyDown("3");                                 // -> [03]/03/0002
    shouldBeEqualToString("input.value", "0002-03-03");
    shouldBe("changeEventsFired", "3");
    shouldBe("inputEventsFired", "3");

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

    beginTest("Up/Down arrow keys", "2020-12-20");         // [12]/20/2020
    UIHelper.keyDown("upArrow");                           // [01]/20/2020
    shouldBeEqualToString("input.value", "2020-01-20");
    UIHelper.keyDown("upArrow");                           // [02]/20/2020
    shouldBeEqualToString("input.value", "2020-02-20");
    UIHelper.keyDown("downArrow");                         // [01]/20/2020
    shouldBeEqualToString("input.value", "2020-01-20");
    UIHelper.keyDown("downArrow");                         // [12]/20/2020
    shouldBeEqualToString("input.value", "2020-12-20");
    shouldBe("changeEventsFired", "4");
    shouldBe("inputEventsFired", "4");

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

    beginTest("Backspace key", "2020-08-26");              // [08]/26/2020
    UIHelper.keyDown("\b");                                // -> [mm]/26/2020
    shouldBeEqualToString("input.value", "");
    UIHelper.keyDown("7");                                 // -> [07]/26/2020
    shouldBeEqualToString("input.value", "2020-07-26");
    shouldBe("changeEventsFired", "2");
    shouldBe("inputEventsFired", "2");

    beginTest("Delete key", "2020-08-26");                 // [08]/26/2021
    UIHelper.keyDown("delete");                            // -> [mm]/26/2021
    shouldBeEqualToString("input.value", "");
    shouldBe("changeEventsFired", "1");
    shouldBe("inputEventsFired", "1");

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

    input.setAttribute('value', '2012-02-01');
    beginTest("EmptyString Test", "2012-02-01");
    UIHelper.keyDown("\t"); // -> 02/[01]/2012
    UIHelper.keyDown("downArrow"); // -> 02/[31]/2012
    shouldBeEqualToString('input.value', ''); // 2012-02-31 is not a valid date.
    input.removeAttribute('value');

    finishJSTest();
});

</script>
</body>
</html>
