<!DOCTYPE html>
<html>
<body>
<iframe id="iframe"></iframe>
<script src="../resources/js-test-pre.js"></script>
<script>
description("Tests that canvas context fillStyle does not leak documents.");

jsTestIsAsync = true;
frameDocumentID = 0;
checkCount = 0;

function iframeLoaded(frameDocument) {
    frameDocumentID = internals.documentIdentifier(frameDocument);
    shouldBeTrue("internals.isDocumentAlive(frameDocumentID)");

    iframe.addEventListener("load", () => {
        handle = setInterval(() => {
            gc();
            if (!internals.isDocumentAlive(frameDocumentID)) {
                clearInterval(handle);
                testPassed("The iframe document didn't leak.");
                finishJSTest();
            }
            checkCount++;
            if (checkCount > 500) {
                clearInterval(handle);
                testFailed("The iframe document leaked.");
                finishJSTest();
            }
        }, 10);
    }, {once: true});

    iframe.src = "about:blank";
}

function test() {
    ["color", "gradient", "pattern"].forEach(variant => {
        window.addEventListener("message", (message) => {
            if (message.data === "frameLoaded")
                iframeLoaded(iframe.contentWindow.document);
        }, {once: true});

        iframe.src = `resources/ctx.2d-fillStyle-${variant}.html`;
    });
}

onload = () => {
    if (!window.testRunner || !window.internals)
        testFailed("Test requires internals.");

    test();
};

</script>
<script src="../resources/js-test-post.js"></script>
</body>
</html>
