<p>This test tabs with different combinations of modifiers pressed, where some should advance focus and some should not.</p>
<div id="testDiv"></div>
<pre id="console"></pre>

<script>

if (window.testRunner)
    testRunner.dumpAsText();

function log(s)
{
    document.getElementById('console').appendChild(document.createTextNode(s));
}

function description(element)
{
    return element.getAttribute("id");
}

function keydownListener(event)
{
    log("- keydown event: " + description(event.target)  + "\n");
}

function blurListener(event)
{
    log("- blur event: " + description(event.target) + "\n");
}

var lastFocusedElement = null;
function focusListener(event)
{
    log('- focus event: ' + description(event.target) + '\n');
    lastFocusedElement = event.target;
}

function addEventListeners(element)
{
    element.addEventListener('keydown', keydownListener, false);
    element.addEventListener('focus', focusListener, false);
    element.addEventListener('blur', blurListener, false);
}

function addElements(parentElement)
{
    // Add 3 input elements
    var input = parentElement.ownerDocument.createElement('input');
    addEventListeners(input);
    input.setAttribute("id", "a");
    parentElement.appendChild(input);
    input = parentElement.ownerDocument.createElement('input');
    addEventListeners(input);
    input.setAttribute("id", "b");
    parentElement.appendChild(input);
    input = parentElement.ownerDocument.createElement('input');
    addEventListeners(input);
    input.setAttribute("id", "c");
    parentElement.appendChild(input);
}

function dispatchTab(element, shiftKey, metaKey, ctrlKey, altGraphKey)
{
    var event = document.createEvent("KeyboardEvents");
    var tabKeyIdentifier = "U+0009";
    event.initKeyboardEvent("keydown", true, true, document.defaultView, tabKeyIdentifier, 0, ctrlKey, false, shiftKey, metaKey, altGraphKey);
    element.dispatchEvent(event);
}

function setup()
{
    var testDiv = document.getElementById('testDiv');
    addElements(testDiv);
}

setup();

var middleInput = document.getElementsByTagName('input')[1];

for (i = 0; i < 2; ++i) {
    log("resetting focus to middle input\n");
    middleInput.focus();
    log("test tab case that should advance focus\n");
    dispatchTab(middleInput, (i & 1) != 0, false, false, false);
}
for (i = 2; i < 16; ++i) {
    log("resetting focus to middle input\n");
    middleInput.focus();
    log("test tab case that should not advance focus\n");
    dispatchTab(middleInput, (i & 1) != 0, (i & 2) != 0, (i & 4) != 0, (i & 8) != 0);
}

</script>
