<html>
<head>

<script>
    if (window.testRunner)
        testRunner.dumpAsText();

    function dispatchEvent(target, eventType, offsetX, offsetY)
    {
        var targetRect = target.getBoundingClientRect();
        var ev = document.createEvent("MouseEvent");
        ev.initMouseEvent(eventType, true, true, window, 1, 1, 1, targetRect.left + offsetX, targetRect.top + offsetY, false, false, false, false, 0, document);
        target.dispatchEvent(ev);
    }

    function log(s)
    {
        document.getElementById('result').innerHTML += s + '<br>';
    }

    function defaultPreventingHandler(e) {
        e.preventDefault();
    }

    function runTests()
    {
        var radio1 = document.getElementById("radio1");
        var radio2 = document.getElementById("radio2");

        var radio3 = document.getElementById("radio3");
        var radio4 = document.getElementById("radio4");

        var check1 = document.getElementById("check1");
        var check2 = document.getElementById("check2");

        radio1.checked = true;

        radio3.checked = true;
        radio3.indeterminate = true;
        radio4.indeterminate = true;

        check1.checked = true;
        check1.indeterminate = true;

        check2.checked = false;
        check2.indeterminate = true;

        radio3.addEventListener('click', defaultPreventingHandler, false);
        radio4.addEventListener('click', defaultPreventingHandler, false);
        check1.addEventListener('click', defaultPreventingHandler, false);
        check2.addEventListener('click', defaultPreventingHandler, false);

        // Clicking selected radio should not unselect them both
        dispatchEvent(radio1, "click", 2, 2);

        // Default-prevented click should not change checked or intermediate state
        dispatchEvent(radio3, "click", 2, 2);
        dispatchEvent(radio4, "click", 2, 2);

        // Default-prevented click should not change checked or intermediate state
        dispatchEvent(check1, "click", 2, 2);
        dispatchEvent(check2, "click", 2, 2);

        if (radio1.checked)
        log('Click did not unselect all radio buttons: PASS');
        else
        log('Click unselected all radio buttons: FAIL');

        log('<br>Checking whether default-prevented click clobbered indeterminate state:')
        log('On checked radio button: ' + (radio3.indeterminate ? 'PASS' : 'FAIL'));
        log('On unchecked radio button: ' + (radio4.indeterminate ? 'PASS' : 'FAIL'));
        log('On checked checkbox: ' + (check1.indeterminate ? 'PASS' : 'FAIL'));
        log('On unchecked checkbox: ' + (check2.indeterminate ? 'PASS' : 'FAIL'));

        log('<br>Checking whether default-prevented click clobbered checked state:')
        log('On checked radio button: ' + (radio3.checked ? 'PASS' : 'FAIL'));
        log('On unchecked radio button: ' + (!radio4.checked ? 'PASS' : 'FAIL'));
        log('On checked checkbox: ' + (check1.checked ? 'PASS' : 'FAIL'));
        log('On unchecked checkbox: ' + (!check2.checked ? 'PASS' : 'FAIL'));
    }
</script>

</head>

<body onload="runTests()">

    <form>
      <input type="radio" name="test" id="radio1" value="a">A
      <input type="radio" name="test" id="radio2" value="b">B
    </form>

    <form>
      <input type="radio" name="test2" id="radio3" value="a">Intermediate A
      <input type="radio" name="test2" id="radio4" value="b">Intermediate B
    </form>

    <form>
      <input type="checkbox" name="check1" id="check1" value="a">C
      <input type="checkbox" name="check2" id="check2" value="b">D
    </form>

    <div id="result"></div>
</body>
</html>
