<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>MediaStreamTrack with event listener should not be collected.</title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
    <script>

    if (window.testRunner)
        testRunner.setUserMediaPermission(true);

    function forceGC()
    {
        if (window.GCController)
            return GCController.collect();
    
        // Force garbage collection
        for (var ndx = 0; ndx < 99000; ndx++)
            var str = new String("1234");
    }

    promise_test((test) => {
        return navigator.mediaDevices.getUserMedia({ video: true })
            .then((stream) => {

                promise_test((test) => {
                    return new Promise((resolve, reject) => {
                        let track = stream.getVideoTracks()[0];

                        track.onmute = (evt) => {
                            if (window.internals)
                                internals.setMediaStreamTrackMuted(track, false);
                        }
                        track.onunmute = resolve;

                        forceGC();

                        if (window.internals)
                            internals.setMediaStreamTrackMuted(track, true);

                        setTimeout(() => reject("Track muted state did not change in .5 second"), 500);
                    });
                }, "Test track");

                promise_test((test) => {
                    return new Promise((resolve, reject) => {
                        stream.onremovetrack = resolve;

                        forceGC();

                        if (window.internals)
                            window.internals.removeMediaStreamTrack(stream, stream.getVideoTracks()[0]);

                        setTimeout(() => reject("stream.onremovetrack did not fire in .5 second"), 500);
                    });
                }, "Test stream");

            });
    }, "Create stream");


    </script>
</head>
<body>
</body>
</html>
