<!DOCTYPE html>
<html>
<body>

<p>Test for <a href="https://bugs.webkit.org/show_bug.cgi?id=32013">https://bugs.webkit.org/show_bug.cgi?id=32013</a>.

<p>For manual testing, focus a radio button in the first group and use the arrow keys. Changing the checked
radio button should fire change events.

<p>
<input type=radio name=aaa value=a checked onchange="handleChange(event)" onclick="handleClick(event)" oninput="handleInput(event)">a
<input type=radio name=aaa value=b onchange="handleChange(event)" onclick="handleClick(event)" oninput="handleInput(event)">b
<input type=radio name=aaa value=c onchange="handleChange(event)" onclick="handleClick(event)" oninput="handleInput(event)">c

<p>For manual testing, focus a radio button in the second group and use the arrow keys. Change events
should still be dispatched but the checked radio should not change.

<p>
<input type=radio name=bbb value=d checked onchange="handleChange(event)" onclick="handleClick(event)" oninput="handleInput(event)">d
<input type=radio name=bbb value=e onchange="handleChange(event)" onclick="handleClick(event)" oninput="handleInput(event)">e
<input type=radio name=bbb value=f onchange="handleChange(event)" onclick="handleClick(event)" oninput="handleInput(event)">f

<pre id=out></pre>

<script>

var preventClickValues = 'def';

function handleInput(e)
{
    var value = e.target.value;
    print(value + ' dispatched input event');
    assertChecked(e.target.value);
}

function handleChange(e)
{
    var value = e.target.value;
    print(value + ' dispatched change event');
    assertChecked(e.target.value);
}

function handleClick(e)
{
    var value = e.target.value;
    print(value + ' dispatched click event');
    assertChecked(e.target.value);
    if (preventClickValues.indexOf(value) !== -1)
        e.preventDefault();
}

function print(s)
{
    document.getElementById('out').textContent += s + '\n';
}

function pass(s) {
    print('PASS: ' + s);
}

function fail(s) {
    print('FAIL: ' + s);
}

function getRadio(value)
{
    return document.querySelector('input[value="' + value + '"]');
}

function assertChecked(value)
{
    if (getRadio(value).checked)
        pass(value + ' is checked');
    else
        fail(value + ' should be checked');
}

function assertNotChecked(value)
{
    if (!getRadio(value).checked)
        pass(value + ' is not checked');
    else
        fail(value + ' should not be checked');
}

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

if (window.eventSender) {
    getRadio('a').focus();
    eventSender.keyDown('downArrow');
    eventSender.keyDown('downArrow');

    getRadio('d').focus();
    eventSender.keyDown('downArrow');
    eventSender.keyDown('downArrow');

    assertNotChecked('a');
    assertNotChecked('b');
    assertChecked('c');

    assertChecked('d');
    assertNotChecked('e');
    assertNotChecked('f');
}

</script>
</body>
</html>
