<!DOCTYPE html>
<html>
    <head>
        <style>
            iframe { border: 1px solid black; }
        </style>
        <script src="../../../../resources/js-test-pre.js"></script>
        <script>

            function numberOfTimesGetUserMediaPromptHasBeenCalled()
            {
                return testRunner.userMediaPermissionRequestCountForOrigin(document.location.href, document.location.href);
            }

            function gotStream3(s)
            {
                stream = s;
                shouldBe("numberOfTimesGetUserMediaPromptHasBeenCalled()", "2");
                shouldBe("stream.getAudioTracks().length", "1");
                shouldBe("stream.getVideoTracks().length", "1");

                debug("");
                finishJSTest();
            }

            function gotStream2(s)
            {
                stream = s;
                shouldBe("numberOfTimesGetUserMediaPromptHasBeenCalled()", "2");
                shouldBe("stream.getAudioTracks().length", "0");
                shouldBe("stream.getVideoTracks().length", "1");

                debug("<br>** Request a stream with video and audio, the user should not be prompted again **");
                shouldNotThrow("navigator.mediaDevices.getUserMedia({audio:true, video:true}).then(gotStream3);");
            }

            function gotStream1(s)
            {
                stream = s;
                shouldBe("numberOfTimesGetUserMediaPromptHasBeenCalled()", "1");
                shouldBe("stream.getAudioTracks().length", "1");
                shouldBe("stream.getVideoTracks().length", "0");

                debug("<br>** Request a video-only stream, the user should be prompted **");
                shouldNotThrow("navigator.mediaDevices.getUserMedia({video:true}).then(gotStream2);")
            }

            function setup()
            {
                description("Tests that user is only prompted once per device in the current browsing context.");

                testRunner.resetUserMediaPermissionRequestCountForOrigin(document.location.href, document.location.href);
                shouldBe("numberOfTimesGetUserMediaPromptHasBeenCalled()", "0");

                debug("<br>** Request an audio-only stream, the user should be prompted **");
                shouldNotThrow("navigator.mediaDevices.getUserMedia({audio:true}).then(gotStream1);");
            }

            if (window.testRunner)
                testRunner.setUserMediaPermission(true);
            window.jsTestIsAsync = true;
            window.successfullyParsed = true;
        </script> 
        <script src="../../../../resources/js-test-post.js"></script>
    </head>
    <body onload="setup()">
    </body>
</html>
