Skip to content

Truly independent web browser

2.9 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script>
    function play() {
        const context = new AudioContext();
        const oscillator = new OscillatorNode(context);
        oscillator.connect(context.destination);
        oscillator.start();
        return context;
    }

    promiseTest(async () => {
        internals.setTestTimeout(30_000);
        // The tab can still be remembered as playing a previous test's sound.
        await waitForCondition(() => !internals.tabIsPlayingAudio());
        const earlierTransitions = internals.tabAudibilityTransitions().length;
        const httpServer = httpTestServer();

        // This document's audio runs silently from the start, so its sound is heard as soon as it is let through.
        const context = new AudioContext();
        const gain = new GainNode(context, { gain: 0 });
        const oscillator = new OscillatorNode(context);
        oscillator.connect(gain).connect(context.destination);
        oscillator.start();
        await waitForCondition(() => context.currentTime >= 0.5);

        const childUrl = new URL(
            await httpServer.createEcho("GET", "/tab-keeps-playing-audio-heard-after-an-isolated-iframe-leaves-child", {
                status: 200,
                headers: { "Content-Type": "text/html" },
                body: `<!DOCTYPE html><script>${play}play();<\/script>`,
            })
        );
        childUrl.hostname = uniqueLocalhostHostname("tab-keeps-playing-audio-heard-after-an-isolated-iframe-leaves");

        const iframe = document.createElement("iframe");
        iframe.src = childUrl;
        document.body.appendChild(iframe);
        println(`plays audio heard in the isolated iframe: ${await waitForCondition(() => internals.tabIsPlayingAudio())}`);

        // This document's sound starts while the tab goes on playing the sound of the iframe's page that left it, and
        // keeps the tab playing audio past when it would have stopped.
        iframe.remove();
        println(`stops being heard once the iframe is gone: ${await waitForCondition(() => !internals.tabIsHeardPlayingAudio())}`);
        const leftAt = performance.now();
        gain.gain.value = 1;
        println(`is heard playing audio in this document: ${await waitForCondition(() => internals.tabIsHeardPlayingAudio())}`);

        // The tab would have stopped playing audio a while after the iframe left, which only time passing can show.
        await new Promise(resolve => setTimeout(resolve, Math.max(0, leftAt + 4000 - performance.now())));
        println(`plays audio after it would have stopped: ${internals.tabIsPlayingAudio()}`);

        await context.close();
        println(`stops playing audio once nothing is heard: ${await waitForCondition(() => !internals.tabIsPlayingAudio())}`);
        println(`playing audio indicator: ${internals.tabAudibilityTransitions().slice(earlierTransitions).join(", ")}`);
    });
</script>