<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>This test validates element timing information for cross-origin redirect chain images.</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/element-timing-helpers.js"></script>
<script src=/common/get-host-info.sub.js></script>
</head>
<body>
<script>
async_test(t => {
    assert_implements(window.PerformanceElementTiming, "PerformanceElementTiming is not implemented");
    let destUrl = get_host_info().HTTP_REMOTE_ORIGIN
            + '/resource-timing/resources/multi_redirect.py?';
    destUrl += 'page_origin=' +  get_host_info().HTTP_ORIGIN;
    destUrl += '&cross_origin=' + get_host_info().HTTP_REMOTE_ORIGIN;
    destUrl += '&final_resource=' + '/element-timing/resources/circle-tao.svg';
    destUrl += '&timing_allow=1';
    destUrl += '&tao_steps=';
    for (let taoSteps=0; taoSteps < 4; taoSteps++) {
        const image = document.createElement('img');
        image.src = destUrl + taoSteps;
        image.setAttribute('elementtiming', taoSteps);
        image.setAttribute('id', 'id' + taoSteps)
        document.body.appendChild(image);
    }
    let numObserved = 0;
    let observedMap = [false, false, false, false];
    const beforeRender = performance.now();
    new PerformanceObserver(t.step_func(entries => {
        entries.getEntries().forEach(entry => {
            const taoSteps = entry.identifier;
            assert_false(observedMap[taoSteps], 'Should observe each image once');
            observedMap[taoSteps] = true;
            if (taoSteps !== '3') {
                assert_equals(entry.renderTime, 0,
                    'renderTime should be 0 when there is ' + taoSteps + ' tao steps');
            }
            const bound = taoSteps === '3' ? beforeRender : 0;
            checkElement(entry, destUrl + taoSteps, taoSteps, 'id' + taoSteps, bound,
                    document.getElementById(taoSteps));
        });
        numObserved += entries.getEntries().length;
        if (numObserved === 4)
            t.done();
    })).observe({type: 'element', buffered: true});
}, 'Cross-origin image without TAO should not have its renderTime set, with full TAO it should.');
</script>
</body>
</html>

