<!DOCTYPE html>
<html>
<body>
<script src="../../resources/js-test-pre.js"></script>
<p id="description"></p>
<div id="sandbox"></div>
<pre id="console"></pre>
<script>
description("Tests to ensure that accesskey works in iframes and other iframes don't effect current accesskey maps.");

function pressAccessKey(key)
{
    if (navigator.userAgent.search(/\bMac OS X\b/) != -1)
        modifiers = ["ctrlKey", "altKey"];
    else
        modifiers = ["altKey"];
    eventSender.keyDown(key, modifiers);
}

var targetsOfFocusEvents = [];

function clearEventRecords()
{
    targetsOfFocusEvents = [];
}

function recordFocusEvent(event)
{
    if (event.type == 'focus')
        targetsOfFocusEvents.push(event.target.id);
}

function createDomInDocument(doc, tagName, attributes)
{
    var element = doc.createElement(tagName);
    for (var name in attributes)
        element.setAttribute(name, attributes[name]);

    if (attributes['id'])
        element.addEventListener('focus', recordFocusEvent, false);
    return element;
}

function test()
{
    if (window.testRunner)
        testRunner.dumpAsText();

    var sandbox = document.getElementById('sandbox');
    sandbox.appendChild(createDomInDocument(document, 'input', {'id': 'inputB'}));
    sandbox.appendChild(createDomInDocument(document, 'input', {'id': 'inputC', 'accesskey': 'a'}));
    sandbox.appendChild(createDomInDocument(document, 'input', {'id': 'inputD', 'accesskey': 'b'}));
    sandbox.appendChild(createDomInDocument(document, 'input', {'id': 'inputE', 'accesskey': 'c'}));

    window.iframe1 = document.createElement('iframe');
    sandbox.appendChild(iframe1);
    iframe1.contentDocument.body.appendChild(createDomInDocument(iframe1.contentDocument, 'input', {'id': 'inputG', 'accesskey': 'a'}));
    iframe1.contentDocument.body.appendChild(createDomInDocument(iframe1.contentDocument, 'input', {'id': 'inputH', 'accesskey': 'c'}));
    iframe1.contentDocument.body.appendChild(createDomInDocument(iframe1.contentDocument, 'input', {'id': 'inputI', 'accesskey': 'd'}));

    window.iframe2 = iframe1.contentDocument.createElement('iframe');
    iframe1.contentDocument.body.appendChild(iframe2);
    iframe2.contentDocument.body.appendChild(createDomInDocument(iframe2.contentDocument, 'input', {'id': 'inputK'}));
    iframe2.contentDocument.body.appendChild(createDomInDocument(iframe2.contentDocument, 'input', {'id': 'inputL', 'accesskey': 'a'}));

    debug('Accesskeys should work in an iframe. "iframe1" has both "inputG" and "inputH" (accesskey="c") elements.');
    clearEventRecords();
    shouldBe('iframe1.contentDocument.getElementById("inputG").focus(); pressAccessKey("c"); targetsOfFocusEvents', '["inputG", "inputH"]');

    debug('\n"inputC" element has an accessKey of "a" and other iframes also have elements with accesskey of "a". An acccesskey should not be overridden by other iframes, so "inputC" should be selected.');
    clearEventRecords();
    shouldBe('document.getElementById("inputB").focus(); pressAccessKey("a"); targetsOfFocusEvents;', '["inputB", "inputC"]');

    debug('\nA child iframe, iframe1, has an element with accesskey of "d", which should be ignored.');
    clearEventRecords();
    shouldBe('document.getElementById("inputB").focus(); pressAccessKey("d"); targetsOfFocusEvents', '["inputB"]');

    debug('\nAn accesskey defined in an ancestor iframe should be ignored. "inputD" has accesskey of "b", which should not be selected from descendant iframes, iframe1 and iframe2.');
    clearEventRecords();
    shouldBe('iframe1.contentDocument.getElementById("inputG").focus(); pressAccessKey("b"); targetsOfFocusEvents', '["inputG"]');
    clearEventRecords();
    shouldBe('iframe2.contentDocument.getElementById("inputK").focus(); pressAccessKey("b"); targetsOfFocusEvents', '["inputK"]');
}

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