<!DOCTYPE html>
<html>
    <head>
        <title>Unique origin is unable to create MediaKeys</title>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <div id="log"></div>
        <script>
            // When the sandbox attribute is present on an iframe, it will
            // treat the content as being from a unique origin. So try to
            // call createMediaKeys() inside an iframe and it should fail.

            function load_iframe(src, sandbox) {
                return new Promise(function(resolve) {
                    var iframe = document.createElement('iframe');
                    iframe.onload = function() { resolve(iframe); };
                    iframe.sandbox = sandbox;
                    iframe.src = src;
                    document.documentElement.appendChild(iframe);
                });
            }

            function wait_for_message() {
                return new Promise(function(resolve) {
                    self.addEventListener('message', function listener(e) {
                        resolve(e.data);
                        self.removeEventListener(listener);
                    });
                });
            }

            promise_test(function(test) {
                var script = 'data:text/html,' +
                    '<script>' +
                    '    window.onmessage = function(e) {' +
                    '        navigator.requestMediaKeySystemAccess(\'org.w3.clearkey\', [{}]).then(function(access) {' +
                    '            return access.createMediaKeys();' +
                    '        }).then(function(mediaKeys) {' +
                    '            window.parent.postMessage({result: \'allowed\'}, \'*\');' +
                    '        }, function(error) {' +
                    '            window.parent.postMessage({result: \'failed\'}, \'*\');' +
                    '        });' +
                    '    };' +
                    '<\/script>';

                // Verify that this page can create a MediaKeys first.
                navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    // Success, so now create the iframe and try there.
                    return load_iframe(script, 'allow-scripts')
                }).then(function(iframe) {
                    iframe.contentWindow.postMessage({}, '*');
                    return wait_for_message();
                }).then(function(message) {
                    assert_equals(message.result, 'failed');
                });
            }, 'Unique origin is unable to create MediaKeys');
        </script>
    </body>
</html>

Verify unique origins can't create MediaKeys
Verify unique origins can't create MediaKeys

BUG=357837
TEST=new test passes

Review URL: https://codereview.chromium.org/1080803003

git-svn-id: bf5cd6ccde378db821296732a091cfbcf5285fbd@193587 bbb929c8-8fbe-4397-9dbb-9b2b20218538
