<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<p>This test verifies that the Home/End/PageUp/PageDown keys work correctly for &lt;select&gt; elements.
Since it requires <CODE>eventSender.keyDown</CODE>, it will not run solo in the web browser; it must be run with <KBD>run-webkit-tests</KBD>.</p>
<hr>
<form>
    <select name="singleselect" id="ss" size="4" multiple="true">
        <option value="0">0 </option>
        <option value="1">1 </option>
        <option value="2">2 </option>
        <option value="3">3 </option>
        <option value="4">4 </option>
        <option value="5">5 </option>
        <option value="6">6 </option>
    </select>
    <select name="singleselectwithdisabled" id="ssd" size="4" multiple="false">
        <option value="0" disabled="true">0 </option>
        <option value="1">1 </option>
        <option value="2">2 </option>
        <option value="3" disabled="true">3 </option>
        <option value="4">4 </option>
        <option value="5">5 </option>
        <option value="6">6 </option>
        <option value="7" disabled = "true">7 </option>
    </select>
    <select name="singleselectwithgroup" id="ssg" size="4" multiple="false">
        <optgroup label="gp0">
            <option value="0">0 </option>
            <option value="1">1 </option>
            <option value="2">2 </option>
        </optgroup>
        <optgroup label="gp1">
            <option value="3">3 </option>
        </optgroup>
        <option value="4">4 </option>
        <optgroup label="gp2">
            <option value="5">5 </option>
        </optgroup>
        <option value="6">6 </option>
    </select>
</form>

<p id="description"></p>
<div id="console"></div>
<div id="log"></div>

<script>

function log(message) {
    document.getElementById('log').appendChild(document.createTextNode(message + "\n"));
}

function sendKeyAndExpectIndex(selectId, key, initialIndex, expectedIndex) {
    var select = document.getElementById(selectId);
    clearSelection(select);
    select.focus();
    select.selectedIndex = initialIndex;
    if (select.selectedIndex != initialIndex) {
        log("can't set selectedIndex to " + initialIndex + ' (is ' + select.selectedIndex + ')');
        return false;
    }
    if (window.testRunner)
        eventSender.keyDown(key);
    if (select.selectedIndex != expectedIndex) {
        log('selectedIndex should be ' + expectedIndex + ' (is ' + select.selectedIndex + ') after a ' + key + ' from index ' + initialIndex);
        return false;
    }
    return true;
}

function equalArrays(a1, a2) {
    if (a1.length != a2.length)
        return false;
    for (i = 0; i < a1.length; i++) {
        if (a1[i] != a2[i])
            return false;
    }
    return true;
}

function dumpArray(a) {
    s = "[";
    for (i = 0; i < a.length; i++) {
        s = s + a[i];
        if (i < a.length - 1)
             s = s + ", ";
    }
    return s + "]";
}

function getSelectedIndices(select) {
    nowSelected = [];
    for (i = 0; i < select.options.length; i++)
        if (select.options[i].selected)
              nowSelected.push(i);
    return nowSelected;
}

function clearSelection(select) {
    for (i = 0; i < select.options.length; i++)
        select.options[i].selected = false;
}

// expectedIndices should be in sorted order
function sendWithShiftKeyAndExpectIndices(selectId, key, expectedIndices) {
    var select = document.getElementById(selectId);
    select.focus();
    if (window.testRunner)
        eventSender.keyDown(key, ["shiftKey"]);
    nowSelected = getSelectedIndices(select);
    if (!equalArrays(nowSelected, expectedIndices)) {
        log('selected indices should be ' + dumpArray(expectedIndices) + ' (is ' +
            dumpArray(nowSelected) + ') after a ' + key);
        return false;
    }
    return true;
}

function testPageDownNoDisabledElements() {
    shouldBe('sendKeyAndExpectIndex("ss", "pageDown", 0, 3)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageDown", 1, 4)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageDown", 2, 5)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageDown", 3, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageDown", 4, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageDown", 5, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageDown", 6, 6)', 'true');
}

function testPageUpNoDisabledElements() {
    shouldBe('sendKeyAndExpectIndex("ss", "pageUp", 6, 3)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageUp", 5, 2)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageUp", 4, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageUp", 3, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageUp", 2, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageUp", 1, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "pageUp", 0, 0)', 'true');
}

function testHomeNoDisabledElements() {
    shouldBe('sendKeyAndExpectIndex("ss", "home", 6, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "home", 5, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "home", 4, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "home", 3, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "home", 2, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "home", 1, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "home", 0, 0)', 'true');
}

function testEndNoDisabledElements() {
    shouldBe('sendKeyAndExpectIndex("ss", "end", 6, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "end", 5, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "end", 4, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "end", 3, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "end", 2, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "end", 1, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ss", "end", 0, 6)', 'true');
}

function testPageDownWithDisabledElements() {
    shouldBe('sendKeyAndExpectIndex("ssd", "pageDown", 0, 4)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageDown", 1, 4)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageDown", 2, 5)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageDown", 4, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageDown", 5, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageDown", 6, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageDown", 7, 6)', 'true');
}

function testPageUpWithDisabledElements() {
    shouldBe('sendKeyAndExpectIndex("ssd", "pageUp", 7, 4)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageUp", 6, 2)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageUp", 5, 2)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageUp", 4, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageUp", 2, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageUp", 1, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "pageUp", 0, 1)', 'true');
}

function testHomeWithDisabledElements() {
    shouldBe('sendKeyAndExpectIndex("ssd", "home", 7, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "home", 6, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "home", 5, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "home", 4, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "home", 3, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "home", 2, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "home", 1, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "home", 0, 1)', 'true');
}

function testEndWithDisabledElements() {
    shouldBe('sendKeyAndExpectIndex("ssd", "end", 7, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "end", 6, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "end", 5, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "end", 4, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "end", 3, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "end", 2, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "end", 1, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssd", "end", 0, 6)', 'true');
}

function testVariousShiftKeysNoDisabledElements() {
    var select = document.getElementById("ss");
    select.focus();
    clearSelection(select);
    select.selectedIndex = 0;

    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageDown", [0, 1, 2, 3])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageDown", [0, 1, 2, 3, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageDown", [0, 1, 2, 3, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageUp", [0, 1, 2, 3])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageUp", [0])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageUp", [0])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "end", [0, 1, 2, 3, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "end", [0, 1, 2, 3, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "home", [0])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "home", [0])', 'true');

    clearSelection(select);
    select.selectedIndex = 3;
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageDown", [3, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageDown", [3, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageUp", [3])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageUp", [0, 1, 2, 3])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageUp", [0, 1, 2, 3])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "pageUp", [0, 1, 2, 3])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "end", [3, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "end", [3, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "home", [0, 1, 2, 3])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ss", "home", [0, 1, 2, 3])', 'true');
}

function testVariousShiftKeysWithDisabledElements() {
    var select = document.getElementById('ssd');
    select.focus();
    clearSelection(select);
    select.selectedIndex = 1;

    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageDown", [1, 2, 4])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageDown", [1, 2, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageDown", [1, 2, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageUp", [1, 2])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageUp", [1])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageUp", [1])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "end", [1, 2, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "end", [1, 2, 4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "home", [1])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "home", [1])', 'true');

    clearSelection(select);
    select.selectedIndex = 4;
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageDown", [4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageDown", [4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageUp", [2, 4])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageUp", [1, 2, 4])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "pageUp", [1, 2, 4])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "end", [4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "end", [4, 5, 6])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "home", [1, 2, 4])', 'true');
    shouldBe('sendWithShiftKeyAndExpectIndices("ssd", "home", [1, 2, 4])', 'true');
}

function testPageDownWithGroup() {
    shouldBe('sendKeyAndExpectIndex("ssg", "pageDown", 0, 3)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageDown", 1, 3)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageDown", 2, 4)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageDown", 3, 5)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageDown", 4, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageDown", 5, 6)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageDown", 6, 6)', 'true');
}

function testPageUpWithGroup() {
    shouldBe('sendKeyAndExpectIndex("ssg", "pageUp", 6, 4)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageUp", 5, 3)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageUp", 4, 2)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageUp", 3, 1)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageUp", 2, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageUp", 1, 0)', 'true');
    shouldBe('sendKeyAndExpectIndex("ssg", "pageUp", 0, 0)', 'true');
}

testPageDownNoDisabledElements();
testPageUpNoDisabledElements();
testHomeNoDisabledElements();
testEndNoDisabledElements();
testPageDownWithDisabledElements();
testPageUpWithDisabledElements();
testHomeWithDisabledElements();
testEndWithDisabledElements();
testVariousShiftKeysNoDisabledElements();
testVariousShiftKeysWithDisabledElements();
testPageDownWithGroup();
testPageUpWithGroup();
</script>

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