<!-- webkit-test-runner [ UsesBackForwardCache=true ] -->
<head>
<style>
html {
    font-size: 32pt;
}
</style>
<script src="resources/swipe-test.js"></script>
<script>

async function runTest()
{
    // The first swipe should fail because we're scrolled to the middle of the document
    // and this is all a single gesture. We can only start swipes if we *begin*
    // pinned to the edge.
    await startSwipeGesture();
    await completeSwipeGesture();
    
    testRunner.clearTestRunnerCallbacks();
    testRunner.installDidBeginSwipeCallback(didBeginSwipeCallback);
    testRunner.installWillEndSwipeCallback(willEndSwipeCallback);
    testRunner.installDidEndSwipeCallback(didEndSwipeCallback);
    testRunner.installDidRemoveSwipeSnapshotCallback(didRemoveSwipeSnapshotCallback);    

    // The second swipe should succeed because we are now scrolled to the left edge.
    await startSwipeGesture();
    await completeSwipeGesture();
}

function didBeginSwipeNotReachedCallback()
{
    log("Failure. Should never begin a swipe, because we were in the middle of a scrolling gesture that started when the main frame was not pinned to the left.");
}

function didBeginSwipeCallback()
{
    log("didBeginSwipe");
}

function willEndSwipeCallback()
{
    log("willEndSwipe");

    shouldBe(false, isFirstPage(), "The swipe should not yet have navigated away from the second page.");
}

function didEndSwipeCallback()
{
    log("didEndSwipe");

    startMeasuringDuration("snapshotRemoval");
}

function didRemoveSwipeSnapshotCallback()
{
    log("didRemoveSwipeSnapshot");

    shouldBe(true, isFirstPage(), "The swipe should have navigated back to the first page.");
    measuredDurationShouldBeLessThan("snapshotRemoval", 1000, "Because we're using the page cache, it shouldn't be long between the gesture completing and the snapshot being removed.")

    testComplete();
}

function isFirstPage()
{
    return window.location.href.indexOf("second") == -1;
}

window.onload = async function () {
    if (!window.eventSender || !window.testRunner) {
        document.getElementById("large").innerHTML = "This test must be run in WebKitTestRunner.";
        return;
    }

    document.getElementById("large").innerHTML = isFirstPage() ? "first" : "second";

    if (isFirstPage()) {
        initializeSwipeTest();

        testRunner.installDidBeginSwipeCallback(didBeginSwipeNotReachedCallback);
        testRunner.installWillEndSwipeCallback(willEndSwipeCallback);
        testRunner.installDidEndSwipeCallback(didEndSwipeCallback);
        testRunner.installDidRemoveSwipeSnapshotCallback(didRemoveSwipeSnapshotCallback);

        testRunner.dumpAsText();
        testRunner.waitUntilDone();

        setTimeout(function () {
            window.location.href = window.location.href + "?second";
        }, 0);
        return;
    }

    // Second page loaded.
    window.scrollTo(100, 0);

    await runTest();
};
</script>
</head>
<body>
<div id="large" style="width: 2000px; height: 2000px;"></div>
</body>
