blob: d66dc4a520f92ab89276fcd49f276418ce3d5815 [file] [log] [blame]
Tom McKee69105b62019-09-13 19:16:411<!DOCTYPE HTML>
2<meta charset=utf-8>
Sean Feng005e3e92023-11-02 21:30:173<meta name="viewport" content="width=device-width, minimum-scale=1">
Tom McKee69105b62019-09-13 19:16:414<title>Largest Contentful Paint: size when image overflows</title>
Tom McKee69105b62019-09-13 19:16:415<body>
6<style>
7body {
8 /* Preventing a scrollbar from showing and removing any margins simplifies
9 the calculations below. */
10 overflow: hidden;
11 margin: 0px;
12}
13</style>
14<script src="/resources/testharness.js"></script>
15<script src="/resources/testharnessreport.js"></script>
16<script src="resources/largest-contentful-paint-helpers.js"></script>
17<script>
Sean Fenga3cb53d2023-04-21 16:11:0718 setup({"hide_test_state": true});
Tom McKee69105b62019-09-13 19:16:4119 let beforeRender;
20 const viewportWidth = document.documentElement.clientWidth;
21 const viewportHeight = document.documentElement.clientHeight;
Blink WPT Bot8e56de12020-10-02 20:11:4022 const imgWidth = 100;
23 const imgHeight = 50;
Tom McKee69105b62019-09-13 19:16:4124 async_test(function (t) {
Blink WPT Bot8e56de12020-10-02 20:11:4025 assert_implements(window.LargestContentfulPaint, "LargestContentfulPaint is not implemented");
Tom McKee69105b62019-09-13 19:16:4126 const beforeLoad = performance.now();
27 new PerformanceObserver(
28 t.step_func_done(function(entryList) {
29 assert_equals(entryList.getEntries().length, 1);
30 const entry = entryList.getEntries()[0];
Ian Clelland5cac6562023-05-24 18:18:0031 const url = window.location.origin + '/images/lcp-100x50.png';
Tom McKee69105b62019-09-13 19:16:4132 // To compute the size, compute the percentage of the image visible and
Blink WPT Bot8e56de12020-10-02 20:11:4033 // scale by its natural dimensions. In this test, the image is expanded to twice its size
34 // but place towards the bottom right corner of the viewport, so it is
Tom McKee69105b62019-09-13 19:16:4135 // effectively clipped to 50% by 50% of its display size. Scaling by
36 // its natural width and height of 100px and 50px respectively, leads
37 // to a weighted size of 50 by 25.
Blink WPT Bot8e56de12020-10-02 20:11:4038 const truncatedWidth = imgWidth / 2;
39 const truncatedHeight = imgHeight / 2;
Tom McKee69105b62019-09-13 19:16:4140 const weightedSize = truncatedWidth * truncatedHeight;
41 checkImage(entry, url, 'image_id', weightedSize, beforeLoad);
42 })
43 ).observe({type: 'largest-contentful-paint', buffered: true});
44 // Add an image, setting width and height equal to viewport.
45 img = document.createElement('img');
Ian Clelland5cac6562023-05-24 18:18:0046 img.src = '/images/lcp-100x50.png';
Tom McKee69105b62019-09-13 19:16:4147 img.id = 'image_id';
Blink WPT Bot8e56de12020-10-02 20:11:4048 img.width = imgWidth * 2;
49 img.height = imgHeight * 2;
50 img.style.position = 'absolute';
51 img.style.left = viewportWidth - imgWidth + 'px';
52 img.style.top = viewportHeight - imgHeight + 'px';
Tom McKee69105b62019-09-13 19:16:4153 document.body.appendChild(img);
54 }, 'The intersectionRect of an img element overflowing is computed correctly');
55</script>
56</body>