<!DOCTYPE html>
<html>
<head>
<script src="../../../../resources/js-test-pre.js"></script>
</head>
<body>
<script>
description("Tests that when a getUserMedia request is made, permission is denied and its Frame is disconnected before the promise is resolved, the promise never resolves.");
window.jsTestIsAsync = true;

if (window.testRunner)
    testRunner.setUserMediaPermission(false);
else
    debug('This test can not be run without the testRunner');

var error;
var options = {audio: true, video: true};
function onIframeLoaded() {
    iframeNavigator = iframe.contentWindow.navigator;
    iframe.contentWindow.onunload = onIframeUnloaded;
    iframeNavigator.mediaDevices.getUserMedia(options)
        .then(stream => {
            testFailed('Success callback invoked unexpectedly');
            finishJSTest();
        })
        .catch(err => {
            error = err;
            shouldBeEqualToString('error.name', 'NotAllowedError');
            debug('');
            iframe.src = 'data:text/html,This frame should be visible when the test completes';
        });
}

function onIframeUnloaded() {
    // Make another request, with permission already denied.
    iframeNavigator.mediaDevices.getUserMedia(options)
        .then(stream => {
            testFailed('Success callback invoked unexpectedly');
            finishJSTest();
        })
        .catch(err => {
            testFailed('Error callback invoked');
            finishJSTest();
        });
    setTimeout(function() {
        testPassed('No callbacks invoked');
        finishJSTest();
    }, 100);
}

var iframe = document.createElement('iframe');
iframe.src = 'resources/disconnected-frame-inner.html';
document.body.appendChild(iframe);

</script>
<script src="../../../../resources/js-test-post.js"></script>
</body>
</html>
