chromium/third_party/blink/web_tests/fast/forms/resources/multiple-fields-blur-and-focus-events.js

var testInput;
var blurCounter = 0;
var focusCounter = 0;

function keyDown(key, modifiers)
{
    eventSender.keyDown(key, modifiers);
}

function state()
{
    return 'blur=' + blurCounter + ' focus=' + focusCounter;
}

function beginTestCase(testCaseName)
{
    debug(testCaseName);
    blurCounter = 0;
    focusCounter = 0;
}

function startTestFor(typeName)
{
    description('Check blur and focus events for multiple fields ' + typeName + ' input UI');
    document.getElementById('container').innerHTML = '<input id="before">'
        + '<input id="test" type="' + typeName + '">'
        + '<input id="after">';

    testInput = document.getElementById('test');
    testInput.addEventListener('blur', function () { ++blurCounter; });
    testInput.addEventListener('focus', function () { ++focusCounter; });

    beginTestCase('focus() and blur()');
    shouldBeEqualToString('testInput.focus(); state()', 'blur=0 focus=1');
    shouldBeEqualToString('testInput.blur(); state()', 'blur=1 focus=1');

    if (window.eventSender) {
        // Note: the number of tab stops is the number of fields + 1 for the
        // drop down button.
        var numberOfTabStops;
        switch (typeName) {
        case 'week':
        case 'month':
            numberOfTabStops = 3;
            break;
        case 'datetime':
        case 'datetime-local':
            numberOfTabStops = 7;
            break;
        default:
            numberOfTabStops = 4;
            break;
        }

        beginTestCase('focus and Tab key to blur');
        document.getElementById("before").focus();
        for (var i = 0; i < numberOfTabStops; i++)
            shouldBeEqualToString('keyDown("\t"); state()', 'blur=0 focus=1');
        shouldBeEqualToString('keyDown("\t"); state()', 'blur=1 focus=1');
    } else {
        debug('Please run in DumpRenderTree for focus and Tab-key test case');
    }
    debug('');
    document.body.removeChild(document.getElementById("container"));
}