<!DOCTYPE html> <!-- webkit-test-runner [ useFlexibleViewport=true IOSFormControlRefreshEnabled=true ] -->
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <script src="../../../../../resources/js-test.js"></script>
        <script src="../../../../../resources/ui-helper.js"></script>
    </head>
<body>
<select id="select">
    <option>January</option>
    <option>February</option>
    <option>March</option>
    <option>April</option>
    <option>May</option>
    <option>June</option>
    <option>July</option>
    <option>August</option>
    <option>September</option>
    <option>October</option>
    <option>November</option>
    <option>December</option>
</select>
</body>
<script>
jsTestIsAsync = true;

async function verifyCheckedItemInContextMenu(expectedItem)
{
    checkedItem = "";
    for (const [index, option] of Array.from(select.options).entries()) {
        let isChecked = await UIHelper.selectFormAccessoryHasCheckedItemAtRow(index);
        if (isChecked) {
            if (checkedItem)
                testFailed("More than one checked item.");

            checkedItem = option.value;
        }
    }

    shouldBeEqualToString("checkedItem", expectedItem);
}

addEventListener("load", async () => {
    description("This test verifies that tapping on a select element, choosing an option, and then tapping on the same element presents a context menu with the correct checked item.");

    shouldBeEqualToString("select.value", "January");
    await UIHelper.activateElement(select);
    await UIHelper.waitForContextMenuToShow();
    await verifyCheckedItemInContextMenu("January");
    await UIHelper.selectFormAccessoryPickerRow(3);
    await UIHelper.waitForContextMenuToHide();
    shouldBeEqualToString("select.value", "April");

    await UIHelper.activateElement(select);
    await UIHelper.waitForContextMenuToShow();
    await verifyCheckedItemInContextMenu("April");
    await UIHelper.selectFormAccessoryPickerRow(0);
    await UIHelper.waitForContextMenuToHide();

    finishJSTest();
});
</script>
</html>
