<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Resource Timing script initiator types</title>
<link rel="author" title="Google" href="http://www.google.com/" />
<link rel="help" href="http://www.w3.org/TR/resource-timing/#dom-performanceresourcetiming-initiatortype"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/webperftestharness.js"></script>
<script src="resources/webperftestharnessextension.js"></script>
<script>
setup({explicit_done: true});
let iframe;
function setup_iframe() {
    const iframe_content =
        '<script src="empty_script.js?id=1"></scr' + 'ipt>' +
        '<script src="empty_script.js?id=2" async></scr' + 'ipt>' +
        '<script src="empty_script.js?id=3" async=false></scr' + 'ipt>' +
        '<script src="empty_script.js?id=4" defer></scr' + 'ipt>' +
        '<script>' +
        'document.write("<script src=\\"empty_script.js?id=5\\"></scr" + "ipt>");' +
        'const s1 = document.createElement("script");' +
        's1.src = "empty_script.js?id=6";' +
        'document.getElementsByTagName("head")[0].appendChild(s1);' +
        'const s2 = document.createElement("script");' +
        's2.src = "empty_script.js?id=7";' +
        's2.async = true;' +
        'document.getElementsByTagName("head")[0].appendChild(s2);' +
        'const s3 = document.createElement("script");' +
        's3.src = "empty_script.js?id=8";' +
        's3.async = false;' +
        'document.getElementsByTagName("head")[0].appendChild(s3);' +
        'const s4 = document.createElement("script");' +
        's4.src = "empty_script.js?id=9";' +
        's4.defer = true;' +
        'document.getElementsByTagName("head")[0].appendChild(s4);' +
        '</scr' + 'ipt>';
    iframe = document.getElementById('frameContext');
    iframe.contentWindow.document.write(iframe_content);
}
function onload_test() {
    const context = new PerformanceContext(iframe.contentWindow.performance);
    const entries = context.getEntriesByType('resource');

    const index = window.location.pathname.lastIndexOf('/');
    const pathname = window.location.pathname.substring(0, index) +
        '/resources/empty_script.js?id=';
    let expected_entries = {};
    expected_entries[pathname + '1'] = 'script';
    expected_entries[pathname + '2'] = 'script';
    expected_entries[pathname + '3'] = 'script';
    expected_entries[pathname + '4'] = 'script';
    expected_entries[pathname + '5'] = 'script';
    expected_entries[pathname + '6'] = 'script';
    expected_entries[pathname + '7'] = 'script';
    expected_entries[pathname + '8'] = 'script';
    expected_entries[pathname + '9'] = 'script';

    test_resource_entries(entries, expected_entries);
    done();
}
window.setup_iframe = setup_iframe;
</script>
</head>
<body>
<h1>Description</h1>
<p>This test validates that all of the different types of script loads are reported with the correct initiator.</p>
<div id="log"></div>
<iframe id="frameContext" onload="onload_test();" src="resources/inject_resource_test.html"></iframe>
</body>
</html>
