servo/tests/wpt/web-platform-tests/largest-contentful-paint/image-TAO.sub.html

60 lines
2.5 KiB
HTML

<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Largest Contentful Paint: observe cross origin images with various Timing-Allow-Origin headers</title>
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/element-timing-helpers.js"></script>
<div id='my_div'></div>
<script>
async_test(t => {
if (!window.PerformanceElementTiming) {
assert_unreached("PerformanceElementTiming is not implemented");
}
const remote_img = 'http://{{domains[www]}}:{{ports[http][1]}}/element-timing/resources/TAOImage.py?'
+ 'origin=' + window.location.origin +'&tao=';
const valid_tao = ['wildcard', 'origin', 'multi', 'multi_wildcard', 'match_origin', 'match_wildcard'];
const invalid_tao = ['null', 'space', 'uppercase'];
const div = document.getElementById('my_div');
let img_size = 20;
function addImage(tao) {
const img = document.createElement('img');
img.src = remote_img + tao;
img.id = tao;
img.height = img_size;
img.width = img_size;
// Set increasing size so that largest-contentful-paint captures all of them.
img_size += 1;
div.appendChild(img);
}
let img_count = 0;
const total_images = valid_tao.length + invalid_tao.length;
new PerformanceObserver(
t.step_func(entryList => {
assert_equals(entryList.getEntries().length, 1);
const entry = entryList.getEntries()[0];
assert_greater_than(entry.loadTime, 0);
const tao = entry.id;
if (valid_tao.includes(tao))
assert_greater_than(entry.renderTime, 0, 'Image with valid TAO should have renderTime');
else if (invalid_tao.includes(tao))
assert_equals(entry.renderTime, 0, 'Image with invalid TAO should not have renderTime');
else
assert_unreached('Should be in one of valid_tao OR invalid_tao');
img_count++;
// Process valid TAO images first.
if (img_count < valid_tao.length)
addImage(valid_tao[img_count]);
// Then add invalid TAO images.
else if (img_count < total_images)
addImage(invalid_tao[img_count - valid_tao.length]);
// Once we've seen all the images, end the test.
else
t.done();
})
).observe({type: 'largest-contentful-paint'});
// Add first image, the rest will be added on each observer callback.
addImage(valid_tao[0]);
}, 'Cross-origin elements with valid TAO have correct renderTime, with invalid TAO have renderTime set to 0.');
</script>
</body>