<html>
<head>
    <title>SELECT pop-up page keys test</title>
    <script>
    function log(s)
    {
        document.getElementById('console').appendChild(document.createTextNode(s + "\n"));
    }
    
    function sendKey(element, key) {
        if (window.testRunner) {
            key = key.substr(0,1).toLowerCase() + key.substr(1); // lowercase 1st letter for eventSender
            eventSender.keyDown(key);
        } else {
            var event = document.createEvent("KeyboardEvents");
            event.initKeyboardEvent("keydown", true, true, document.defaultView, key, 0,
                                    false, false, false, false, false);
            element.dispatchEvent(event);
        }
    }

    function sendKeyAndExpectIndex(selectId, key, initialIndex, expectedIndex) {
        var select = document.getElementById(selectId);
        clearSelection(select);
        select.focus();
        select.selectedIndex = initialIndex;
        if (select.selectedIndex != initialIndex) {
            log("FAIL: can't set selectedIndex to " + initialIndex + ' (is '
            + select.selectedIndex + ')');
            return false;
        }
        sendKey(select, key);
        if (select.selectedIndex != expectedIndex) {
            log('FAIL: selectedIndex should be ' + expectedIndex + ' (is ' +
                select.selectedIndex + ') after a ' + key + 
                ' from index ' + initialIndex);
            return false;
        }
        return true;
    }
    
    function clearSelection(select) {
        for (i = 0; i < select.options.length; i++)
            select.options[i].selected = false;
    }
    
    function testPageDownNoDisabledElements() {
        var passed = true;

        passed = sendKeyAndExpectIndex('ss', "PageDown", 0, 3) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageDown", 1, 4) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageDown", 2, 5) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageDown", 3, 6) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageDown", 4, 6) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageDown", 5, 6) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageDown", 6, 6) && passed;

        if (passed)
            log('PASS: testPageDownNoDisabledElements passed');
        return passed;
    }

    function testPageUpNoDisabledElements() {
        var passed = true;

        passed = sendKeyAndExpectIndex('ss', "PageUp", 6, 3) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageUp", 5, 2) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageUp", 4, 1) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageUp", 3, 0) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageUp", 2, 0) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageUp", 1, 0) && passed;
        passed = sendKeyAndExpectIndex('ss', "PageUp", 0, 0) && passed;

        if (passed)
            log('PASS: testPageUpNoDisabledElements passed');
        return passed;
    }
    
    function testHomeNoDisabledElements() {
        var passed = true;

        passed = sendKeyAndExpectIndex('ss', "Home", 6, 0) && passed;
        passed = sendKeyAndExpectIndex('ss', "Home", 5, 0) && passed;
        passed = sendKeyAndExpectIndex('ss', "Home", 4, 0) && passed;
        passed = sendKeyAndExpectIndex('ss', "Home", 3, 0) && passed;
        passed = sendKeyAndExpectIndex('ss', "Home", 2, 0) && passed;
        passed = sendKeyAndExpectIndex('ss', "Home", 1, 0) && passed;
        passed = sendKeyAndExpectIndex('ss', "Home", 0, 0) && passed;

        if (passed)
            log('PASS: testHomeNoDisabledElements passed');
        return passed;
    }
    
    function testEndNoDisabledElements() {
        var passed = true;

        passed = sendKeyAndExpectIndex('ss', "End", 6, 6) && passed;
        passed = sendKeyAndExpectIndex('ss', "End", 5, 6) && passed;
        passed = sendKeyAndExpectIndex('ss', "End", 4, 6) && passed;
        passed = sendKeyAndExpectIndex('ss', "End", 3, 6) && passed;
        passed = sendKeyAndExpectIndex('ss', "End", 2, 6) && passed;
        passed = sendKeyAndExpectIndex('ss', "End", 1, 6) && passed;
        passed = sendKeyAndExpectIndex('ss', "End", 0, 6) && passed;

        if (passed)
            log('PASS: testEndNoDisabledElements passed');
        return passed;
    }

    function testPageDownWithDisabledElements() {
        var passed = true;

        passed = sendKeyAndExpectIndex('ssd', "PageDown", 1, 4) && passed;
        passed = sendKeyAndExpectIndex('ssd', "PageDown", 2, 5) && passed; 
        passed = sendKeyAndExpectIndex('ssd', "PageDown", 4, 6) && passed;
        passed = sendKeyAndExpectIndex('ssd', "PageDown", 5, 6) && passed;
        passed = sendKeyAndExpectIndex('ssd', "PageDown", 6, 6) && passed;

        if (passed)
            log('PASS: testPageDownWithDisabledElements passed');
        return passed;
    }
    
    function testPageUpWithDisabledElements() {
        var passed = true;

        passed = sendKeyAndExpectIndex('ssd', "PageUp", 6, 2) && passed;
        passed = sendKeyAndExpectIndex('ssd', "PageUp", 5, 2) && passed;
        passed = sendKeyAndExpectIndex('ssd', "PageUp", 4, 1) && passed; 
        passed = sendKeyAndExpectIndex('ssd', "PageUp", 2, 1) && passed;
        passed = sendKeyAndExpectIndex('ssd', "PageUp", 1, 1) && passed;

        if (passed)
            log('PASS: testPageUpWithDisabledElements passed');
        return passed;
    }
    
    function testHomeWithDisabledElements() {
        var passed = true;

        passed = sendKeyAndExpectIndex('ssd', "Home", 7, 1) && passed;
        passed = sendKeyAndExpectIndex('ssd', "Home", 6, 1) && passed;
        passed = sendKeyAndExpectIndex('ssd', "Home", 5, 1) && passed;
        passed = sendKeyAndExpectIndex('ssd', "Home", 4, 1) && passed;
        passed = sendKeyAndExpectIndex('ssd', "Home", 3, 1) && passed;
        passed = sendKeyAndExpectIndex('ssd', "Home", 2, 1) && passed;
        passed = sendKeyAndExpectIndex('ssd', "Home", 1, 1) && passed;
        passed = sendKeyAndExpectIndex('ssd', "Home", 0, 1) && passed;

        if (passed)
            log('PASS: testHomeWithDisabledElements passed');
        return passed;
    }
    
    function testEndWithDisabledElements() {
        var passed = true;

        passed = sendKeyAndExpectIndex('ssd', "End", 7, 6) && passed;
        passed = sendKeyAndExpectIndex('ssd', "End", 6, 6) && passed;
        passed = sendKeyAndExpectIndex('ssd', "End", 5, 6) && passed;
        passed = sendKeyAndExpectIndex('ssd', "End", 4, 6) && passed;
        passed = sendKeyAndExpectIndex('ssd', "End", 3, 6) && passed;
        passed = sendKeyAndExpectIndex('ssd', "End", 2, 6) && passed;
        passed = sendKeyAndExpectIndex('ssd', "End", 1, 6) && passed;
        passed = sendKeyAndExpectIndex('ssd', "End", 0, 6) && passed;

        if (passed)
            log('PASS: testEndWithDisabledElements passed');
        return passed;
    }
    
    function test()
    {
        try{
            if (window.testRunner)
                testRunner.dumpAsText();
            var passed = true;
            passed = testPageDownNoDisabledElements() && passed;
            passed = testPageUpNoDisabledElements() && passed;
            passed = testHomeNoDisabledElements() && passed;
            passed = testEndNoDisabledElements() && passed;

            passed = testPageDownWithDisabledElements() && passed;
            passed = testPageUpWithDisabledElements() && passed;
            passed = testHomeWithDisabledElements() && passed;
            passed = testEndWithDisabledElements() && passed;
        
            if (passed)
                log('\nPASS: ALL TESTS SUCCEEDED');
            else
                log('\nFAIL: SOME TESTS FAILED (SEE ABOVE)');
            log('');
        } catch (exception) {
            log('\nFAIL: Threw exception: ' + exception);
        }
    }
    </script>
</head>
<body onload="test()">
    <p>This test verifies that the Home/End/PageUp/PageDown keys work correctly
        for pop-up &lt;select&gt; elements that have focus but are not currently popped-up.</p>
    <p>Output below is a series of PASS or FAIL lines showing expected and actual selection indexes,
        followed by a single PASS or FAIL for the status of the entire test.<p>
    <p>NOTE: This test will fail on Mac OS, on which these keys are NOT supposed to change
        the selection of a focused pop-up.</p>
    <hr>
  <form>
        <select name="singleselect" id="ss">
          <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">
            <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>
    </form>
    <pre id="console"></pre>
</body>
</html>
