| Tom McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 1 | <!DOCTYPE HTML> |
| 2 | <meta charset=utf-8> |
| Sean Feng | 005e3e9 | 2023-11-02 21:30:17 | [diff] [blame] | 3 | <meta name="viewport" content="width=device-width, minimum-scale=1"> |
| Tom McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 4 | <title>Largest Contentful Paint: size when image overflows</title> |
| Tom McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 5 | <body> |
| 6 | <style> |
| 7 | body { |
| 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 Feng | a3cb53d | 2023-04-21 16:11:07 | [diff] [blame] | 18 | setup({"hide_test_state": true}); |
| Tom McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 19 | let beforeRender; |
| 20 | const viewportWidth = document.documentElement.clientWidth; |
| 21 | const viewportHeight = document.documentElement.clientHeight; |
| Blink WPT Bot | 8e56de1 | 2020-10-02 20:11:40 | [diff] [blame] | 22 | const imgWidth = 100; |
| 23 | const imgHeight = 50; |
| Tom McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 24 | async_test(function (t) { |
| Blink WPT Bot | 8e56de1 | 2020-10-02 20:11:40 | [diff] [blame] | 25 | assert_implements(window.LargestContentfulPaint, "LargestContentfulPaint is not implemented"); |
| Tom McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 26 | 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 Clelland | 5cac656 | 2023-05-24 18:18:00 | [diff] [blame] | 31 | const url = window.location.origin + '/images/lcp-100x50.png'; |
| Tom McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 32 | // To compute the size, compute the percentage of the image visible and |
| Blink WPT Bot | 8e56de1 | 2020-10-02 20:11:40 | [diff] [blame] | 33 | // 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 McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 35 | // 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 Bot | 8e56de1 | 2020-10-02 20:11:40 | [diff] [blame] | 38 | const truncatedWidth = imgWidth / 2; |
| 39 | const truncatedHeight = imgHeight / 2; |
| Tom McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 40 | 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 Clelland | 5cac656 | 2023-05-24 18:18:00 | [diff] [blame] | 46 | img.src = '/images/lcp-100x50.png'; |
| Tom McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 47 | img.id = 'image_id'; |
| Blink WPT Bot | 8e56de1 | 2020-10-02 20:11:40 | [diff] [blame] | 48 | 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 McKee | 69105b6 | 2019-09-13 19:16:41 | [diff] [blame] | 53 | document.body.appendChild(img); |
| 54 | }, 'The intersectionRect of an img element overflowing is computed correctly'); |
| 55 | </script> |
| 56 | </body> |