<script src="../../resources/ui-helper.js"></script>
<script>
if (window.testRunner) {
    testRunner.waitUntilDone();
    testRunner.dumpAsText();
    testRunner.setShouldStayOnPageAfterHandlingBeforeUnload(true);
}

_confirmationDialogDisplayedOnce = false;

function submitForm()
{
    document.forms[0].submit();
}

onbeforeunload = function() {

    if (window._confirmationDialogDisplayedOnce)
        return "Click 'Leave Page'";

    window.setTimeout(function() {
        if (window.testRunner)
            testRunner.setShouldStayOnPageAfterHandlingBeforeUnload(false);

        window.setTimeout(function() {
            document.getElementById("console").innerHTML = "FAIL";
            if (window.testRunner)
                testRunner.notifyDone();
        }, 1000);

        document.forms[0].submit();
    }, 0);
    
    window._confirmationDialogDisplayedOnce = true;
    
    return "Click 'Stay on Page'";
}
</script>

<p>This tests that submitting a form a second time after canceling the first submission in a onbeforeunload handler is allowed. To test manually, follow the instructions in the JavaScript confirmation dialogs.</p>

<div id="console"></div>
<input id="testButton" type="button" value="Click to submit form" onclick="submitForm()">
<form action="resources/pass-and-notify-done.html" method="POST">
</form>

<script>
// Simulate a user interaction with the page so that the beforeunload alert shows.
const testButton = document.getElementById("testButton");
UIHelper.activateAt(testButton.offsetLeft + 5, testButton.offsetTop + 5);
</script>
