<!DOCTYPE html>
<html>
    <head>
        <meta name="referrer" content="unsafe-url">
        <style>
            iframe { border: 1px solid black; }
        </style>
        <script src="../../../../resources/js-test-pre.js"></script>
        <script>
            window.jsTestIsAsync = true;

            if (internals)
                internals.setTrackingPreventionEnabled(false);

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

            function setup()
            {
                description("Tests that mediaDevices.enumerateDevices returns the same ids for devices in the same frame/subframe .");
            }

            let map = { };
            function checkID(origin, device)
            {
                let origins = `${self.origin}-${origin}`;

                if (!map[origins])
                    map[origins] = { };

                let originInfo = map[origins];
                if (!originInfo[device.deviceId])
                    originInfo[device.deviceId] = device.kind;
                
                if (originInfo[device.deviceId] != device.kind)
                    testFailed(`: duplicate device IDs for ${device.kind} and ${originInfo[device.deviceId]} in ${origin}/${self.origin}`);

                if (Object.keys(originInfo).length > 6)
                    testFailed(`: more then four unique device IDs in ${origin}/${self.origin}`);
            }

            let eventCount = 0;
            async function handler(event) 
            {
                event.data.devices.forEach(device => { checkID(event.data.origin, device); });

                if (++eventCount != 6)
                    return;

                await navigator.mediaDevices.getUserMedia({video: true, audio: true });
                let devices = await navigator.mediaDevices.enumerateDevices();
                devices.forEach(device => checkID(self.origin, device));

                debug('');
                if (internals)
                    internals.setTrackingPreventionEnabled(true);
                finishJSTest();
            }

            addEventListener("message", handler, false);

        </script> 
    </head>
    <body onload="setup()">
        <iframe allow="camera;microphone" src="http://localhost:8000/media/media-stream/resources/enumerate-devices-source-id-frame.html?1"></iframe>
        <br>
        <iframe allow="camera;microphone" src="http://127.0.0.1:8000/media/media-stream/resources/enumerate-devices-source-id-frame.html?2"></iframe>
        <br>
        <iframe allow="camera;microphone" src="http://localhost:8000/media/media-stream/resources/enumerate-devices-source-id-frame.html?3"></iframe>
        <div id="console"></div>
        <script src="../../../../resources/js-test-post.js"></script>
    </body>
</html>
