<!DOCTYPE html>
<meta charset="utf-8">
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/gc.js"></script>
<script>

function createContext() {
    let context = new OfflineAudioContext(2, 1000, 44100);
    if (window.internals)
        return internals.observeGC(context);
}

function createRenderingContext(completeCallback) {
    let context = new OfflineAudioContext(2, 1000, 44100);
    let node = context.createBufferSource();
    node.connect(context.destination);
    node.start();
    context.oncomplete = completeCallback;
    context.startRendering();
    if (window.internals)
        return internals.observeGC(context);
}

test(() => {
    let observer = createContext();
    gc();
    if (window.internals)
        assert_true(observer.wasCollected);
}, "GC a newly created offline audio context");

promise_test(async () => {
    let complete = false;
    let observer = createRenderingContext(() => complete = true);
    while (!complete) {
        gc();
        if (window.internals)
            assert_false(observer.wasCollected);
        await new Promise(resolve => setTimeout(resolve, 50));
    }
    gc();
    if (window.internals)
        assert_true(observer.wasCollected);
}, "GC a rendering offline audio context");
</script>
