<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script>
function loadIFrame(src) {
    return new Promise(function(resolve, reject) {
        var iframe = document.createElement('iframe');
        iframe.onload = function() { resolve(iframe); };

        iframe.src = src;

        document.documentElement.appendChild(iframe);
    });
}

async function imageLoadCount(uuid)
{
    const response = await fetch('resources/image-load.py?uuid=' + uuid, { method: 'POST' });
    return response.text();
}

async function navigateIFrame(iframe, uuid)
{
    const promise = new Promise(resolve => iframe.onload = resolve);
    iframe.src = "resources/iframe-with-image.py?uuid=" + uuid;
    await promise;
    return imageLoadCount(uuid);
}

async function reloadIFrame(iframe, uuid)
{
    const promise = new Promise(resolve => iframe.onload = resolve);
    iframe.contentWindow.location.reload();
    await promise;
    return imageLoadCount(uuid);
}

function resetIFrame(iframe)
{
    const promise = new Promise(resolve => { iframe.onload = resolve; });
    iframe.src = "about:blank";
    return promise;
}

promise_test(async (test) => {
    const uuid = token();
    const iframe = await loadIFrame("about:blank");

    const token1 = await navigateIFrame(iframe, uuid);
    await resetIFrame(iframe);
    const token2 = await navigateIFrame(iframe, uuid);
    const token3 = await reloadIFrame(iframe, uuid);

    assert_equals(token1, "1", "navigating to");
    assert_equals(token2, "2", "after new navigation");
    assert_equals(token3, "3", "after reload");

    iframe.remove();
}, "Ensure image is not speculatively loaded after a reload");
</script>
