blob: 8990fd14c0787caa07225017108edb8031eca899 [file] [log] [blame]
Tom McKee69105b62019-09-13 19:16:411<!DOCTYPE HTML>
2<meta charset=utf-8>
3<title>Largest Contentful Paint: size when image overflows</title>
Tom McKee69105b62019-09-13 19:16:414<body>
5<style>
6body {
7 /* Preventing a scrollbar from showing and removing any margins simplifies
8 the calculations below. */
9 overflow: hidden;
10 margin: 0px;
11}
12</style>
13<script src="/resources/testharness.js"></script>
14<script src="/resources/testharnessreport.js"></script>
15<script src="resources/largest-contentful-paint-helpers.js"></script>
16<script>
17 let beforeRender;
18 const viewportWidth = document.documentElement.clientWidth;
19 const viewportHeight = document.documentElement.clientHeight;
Blink WPT Bot8e56de12020-10-02 20:11:4020 const imgWidth = 100;
21 const imgHeight = 50;
Tom McKee69105b62019-09-13 19:16:4122 async_test(function (t) {
Blink WPT Bot8e56de12020-10-02 20:11:4023 assert_implements(window.LargestContentfulPaint, "LargestContentfulPaint is not implemented");
Tom McKee69105b62019-09-13 19:16:4124 const beforeLoad = performance.now();
25 new PerformanceObserver(
26 t.step_func_done(function(entryList) {
27 assert_equals(entryList.getEntries().length, 1);
28 const entry = entryList.getEntries()[0];
29 const url = window.location.origin + '/images/green-100x50.png';
30 // To compute the size, compute the percentage of the image visible and
Blink WPT Bot8e56de12020-10-02 20:11:4031 // scale by its natural dimensions. In this test, the image is expanded to twice its size
32 // but place towards the bottom right corner of the viewport, so it is
Tom McKee69105b62019-09-13 19:16:4133 // effectively clipped to 50% by 50% of its display size. Scaling by
34 // its natural width and height of 100px and 50px respectively, leads
35 // to a weighted size of 50 by 25.
Blink WPT Bot8e56de12020-10-02 20:11:4036 const truncatedWidth = imgWidth / 2;
37 const truncatedHeight = imgHeight / 2;
Tom McKee69105b62019-09-13 19:16:4138 const weightedSize = truncatedWidth * truncatedHeight;
39 checkImage(entry, url, 'image_id', weightedSize, beforeLoad);
40 })
41 ).observe({type: 'largest-contentful-paint', buffered: true});
42 // Add an image, setting width and height equal to viewport.
43 img = document.createElement('img');
44 img.src = '/images/green-100x50.png';
45 img.id = 'image_id';
Blink WPT Bot8e56de12020-10-02 20:11:4046 img.width = imgWidth * 2;
47 img.height = imgHeight * 2;
48 img.style.position = 'absolute';
49 img.style.left = viewportWidth - imgWidth + 'px';
50 img.style.top = viewportHeight - imgHeight + 'px';
Tom McKee69105b62019-09-13 19:16:4151 document.body.appendChild(img);
52 }, 'The intersectionRect of an img element overflowing is computed correctly');
53</script>
54</body>