<!DOCTYPE html>
<html>
<script src="../../js-test-resources/js-test-pre.js"></script>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description('Tests that isolated worlds can have XHRs run in a different origin.');

jsTestIsAsync = true;

var tests = [
    function() {
        debug('XHR from main world');
        xhr();
    },
    function() {
        debug('XHR from isolated world with unchanged security origin');
        runTestInWorld(1, 'xhr');
    },
    function() {
        debug('XHR from isolated world with same security origin as XHR target');
        testRunner.setIsolatedWorldSecurityOrigin(1, 'http://localhost:8000');
        runTestInWorld(1, 'xhr');
    },
    function() {
        debug('XHR from isolated world with security origin that is allowlisted for XHR target');
        testRunner.setIsolatedWorldSecurityOrigin(2, 'chrome-extension://123');
        testRunner.addOriginAccessAllowListEntry('chrome-extension://123', 'http', 'localhost', false);
        runTestInWorld(2, 'xhr');
    },
    function() {
        debug('XHR from main world is not affected by the isolated world origin or allowlist');
        xhr();
    }
];
var currentTest = 0;

// This test is meaningless without testRunner.
if (window.testRunner) {
    window.addEventListener(
        'message',
        function(event) {
            var message = JSON.parse(event.data);
            switch (message.type) {
                case 'test-done':
                    currentTest++;
                    if (currentTest == tests.length) {
                        testRunner.setIsolatedWorldSecurityOrigin(1, null);
                        testRunner.setIsolatedWorldSecurityOrigin(2, null);
                        finishJSTest();
                    }
                    else
                        tests[currentTest]();
                    break;
                case 'debug':
                    debug(message.message);
                    break;
                default:
                    testFailed('Unknown message: ' + event.data);
                    break;
            }
        },
        false);

    tests[0]();
} else {
    testFailed('Test depends on LayoutTestController and must be run by DRT');
}

function runTestInWorld(worldId, funcName)
{
    testRunner.evaluateScriptInIsolatedWorld(
        worldId, String(eval(funcName)) + "\n" + funcName + "();");
}

function xhr()
{
    function debug(message) {
        window.postMessage(JSON.stringify({
                'type': 'debug',
                'message': message
            }),
            '*');
    }

    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://localhost:8000/security/isolatedWorld/resources/cross-origin-xhr.txt', true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState != 4)
            return;

        debug('XHR completed with status ' + xhr.status);
        window.postMessage(JSON.stringify({'type': 'test-done'}), '*');
    };
    xhr.send(null);
}

</script>

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