<html> <!-- webkit-test-runner [ JavaScriptCanOpenWindowsAutomatically=false ] -->
    <head>
        <script src="../../resources/js-test-pre.js"></script>
    </head>
    <body>
        <button id="button1" onclick="testButton1()">Click Here</button>
        <button id="button2" onclick="testButton2()">Click Here Too</button>
        <button id="test" onclick="popup()" style="display:none"></button>
        <div id="console"></div>
        <script>
            var testNum = 0;
            var win;

            function popup() {
                win = window.open("about:blank", "blank");
                shouldBeNonNull("win");
            }

            function testButton1() {
                document.getElementById("test").click();
            }

            function testButton2() {
                var clickEvent = document.createEvent("MouseEvents");
                clickEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                document.getElementById("test").dispatchEvent(clickEvent);
            }

            function closeWindowAndRunNextTest() {
                if (!win)
                    nextTest();

                win.close();
                setTimeout(doneHandler, 1);

                function doneHandler() {
                    if (win.closed) {
                        setTimeout(nextTest, 1);
                        return;
                    }
                    setTimeout(doneHandler, 1);
                }
            }

            function nextTest() {
                if (testNum == 0) {
                    var button1 = document.getElementById("button1");
                    eventSender.mouseMoveTo(button1.offsetLeft + button1.offsetWidth / 2, button1.offsetTop + button1.offsetHeight / 2);
                    eventSender.mouseDown();
                    eventSender.mouseUp();
                } else if (testNum == 1) {
                    var button2 = document.getElementById("button2");
                    eventSender.mouseMoveTo(button2.offsetLeft + button2.offsetWidth / 2, button2.offsetTop + button2.offsetHeight / 2);
                    eventSender.mouseDown();
                    eventSender.mouseUp();
                } else {
                    testRunner.notifyDone();
                }
                ++testNum;
                closeWindowAndRunNextTest();
            }

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

                nextTest();
            }
        </script> 
    </body> 
</html>
