<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Resource Timing: test behavior for reused resources</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 src="/common/get-host-info.sub.js"></script>
<script>
setup({explicit_done: true});
let d;
let iframe;
let iframeBody;
let count = 0;
const host = get_host_info();
const img_src = 'blue.png?id=cached';
function onload_prep() {
    iframe = document.getElementById('frameContext');
    d = iframe.contentWindow.document;
    iframeBody = d.body;

    const image = d.createElement('IMG');
    image.addEventListener('load', function() {
        step_timeout(onload_test, 0); });
    image.src = img_src;
    iframeBody.appendChild(image);

    const image2 = d.createElement('IMG');
    image2.addEventListener('load', function() {
        step_timeout(onload_test, 0); });
    image2.src = img_src;
    iframeBody.appendChild(image2);
}

function onload_test() {
    ++count;
    if (count < 2)
        return;

    const context = new PerformanceContext(iframe.contentWindow.performance);
    const entries = context.getEntriesByType('resource');
    test_equals(entries.length, 1, "There should be only one entry");

    const index = window.location.pathname.lastIndexOf('/');
    const pathname = window.location.pathname.substring(0, index);
    let expected_entries = {};
    expected_entries[pathname + '/resources/' + img_src] = 'img';
    test_resource_entries(entries, expected_entries);
    test_greater_than(entries[0].requestStart, 0, 'requestStart should be non-zero on the same-origin request');
    test_greater_or_equals(entries[0].responseEnd, entries[0].startTime, 'responseEnd should not be before startTime');
    test_greater_or_equals(entries[0].duration, 0, 'duration should not be negative');

    context.clearResourceTimings();
    start_crossorigin_test();
}
function start_crossorigin_test() {
    const image3 = d.createElement('IMG');
    image3.addEventListener("load", function() { step_timeout(finish_crossorigin_test, 0); });
    image3.src = 'http://' + host.REMOTE_HOST + ':{{ports[http][1]}}{{location[path]}}/../resources/' + img_src;
    iframeBody.appendChild(image3);
}
function finish_crossorigin_test() {
    const context = new PerformanceContext(iframe.contentWindow.performance);
    const entries = context.getEntriesByType('resource');
    test_equals(entries.length, 1, 'There should be one entry in second test');
    test_true(entries[0].name.startsWith('http://' + host.REMOTE_HOST + ':{{ports[http][1]}}'), 'Entry name should start with cross-origin domain');
    test_true(entries[0].name.endsWith('/resources/' + img_src), 'Entry name should end with file name');
    test_equals(entries[0].requestStart, 0, 'requestStart should be 0 on the cross-origin request');
    done();
}
window.setup_iframe = () => {};
window.addEventListener('load', onload_prep);
</script>
</head>
<body>
<h1>Description</h1>
<p>This test validates that a reused resource appears in the buffer once.</p>
<div id="log"></div>
<iframe id="frameContext" src="resources/inject_resource_test.html"></iframe>
<img src="resources/blue.png?id=cached"></img>
<script>
    const img = document.createElement('IMG');
    img.src = "http://" + host.REMOTE_HOST + ":{{ports[http][1]}}{{location[path]}}/../resources/blue.png?id=cached";
    document.currentScript.parentNode.insertBefore(img, document.currentScript);
</script>
</body>
</html>
