Skip to content

Truly independent web browser

2.3 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<div id="elsewhere"></div>
<script>
    // Resolves once resource selection has failed on the source and waits for another one. The selection goes on to
    // wait after the error event's dispatch, in the same task, so check in a later one.
    async function selectionWaits(video, source) {
        await new Promise(resolve => source.addEventListener("error", resolve, { once: true }));
        await new Promise(resolve => setTimeout(resolve, 0));
        if (video.networkState !== HTMLMediaElement.NETWORK_NO_SOURCE)
            println(`not waiting: networkState ${video.networkState}`);
    }

    async function waitingVideo() {
        // Resource selection fails on a source without a src and waits for another one.
        const video = document.createElement("video");
        const source = document.createElement("source");
        const waits = selectionWaits(video, source);
        video.appendChild(source);
        document.body.appendChild(video);
        await waits;
        return video;
    }

    function playableSource() {
        const source = document.createElement("source");
        source.src = "../../../Assets/h264-reordered-frames.mp4";
        return source;
    }

    function loadedMetadata(video, label) {
        return new Promise(resolve => {
            video.addEventListener("loadedmetadata", () => {
                println(`${label}: loaded metadata: ${video.videoWidth}x${video.videoHeight}`);
                resolve();
            });
        });
    }

    asyncTest(async done => {
        const elsewhere = document.getElementById("elsewhere");

        // The failed source is moved out of the element with moveBefore(), and a new source appended.
        const movedOut = await waitingVideo();
        const movedOutLoaded = loadedMetadata(movedOut, "failed source moved out");
        elsewhere.moveBefore(movedOut.firstChild, null);
        movedOut.appendChild(playableSource());
        await movedOutLoaded;

        // A new source is moved into the element with moveBefore().
        const movedIn = await waitingVideo();
        const movedInLoaded = loadedMetadata(movedIn, "new source moved in");
        const source = playableSource();
        elsewhere.appendChild(source);
        movedIn.moveBefore(source, null);
        await movedInLoaded;

        done();
    });
</script>