summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/largest-contentful-paint/larger-image.html
blob: 948f00d0c61d4bd5e78b6c577fa007c5f3519562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Largest Contentful Paint: largest image is reported.</title>

<body>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  <script src="resources/largest-contentful-paint-helpers.js"></script>
  <!-- There is some text and some images. We care about blue.png being reported, as it is the largest. -->
  <p>This is some text! :)</p>
  <img src='' id='red' />
  <img src='' id='blue' />
  <img src='' id='black' />
  <p>More text!</p>
  <script>
    // Add listener for load event that is fired when image is loaded.
    function image_load_promise(image_element) {
      return new Promise(resolve => {
        image_element.addEventListener('load', resolve);
      });
    }

    promise_test(async (t) => {
      assert_implements(window.LargestContentfulPaint, "LargestContentfulPaint is not implemented");

      let promise = image_load_promise(document.getElementById('red'));
      document.getElementById('red').src = '/images/red.png';
      await promise;

      const beforeLoad = performance.now();

      promise = image_load_promise(document.getElementById('blue'));
      document.getElementById('blue').src = '/images/blue.png';
      await promise;

      promise = image_load_promise(document.getElementById('black'));
      document.getElementById('black').src = '/images/black-rectangle.png';
      await promise;

      const observer = new PerformanceObserver(
        t.step_func(entryList => {
          entryList.getEntries().forEach(entry => {
            // The text or other image could be reported as LCP if it is rendered before the blue image.
            if (entry.id !== 'blue')
              return;

            const url = window.location.origin + '/images/blue.png';
            // blue.png is 133 by 106.
            const size = 133 * 106;
            checkImage(entry, url, 'blue', size, beforeLoad);
            t.done();
          })
        })
      );
      observer.observe({ type: 'largest-contentful-paint', buffered: true });
    }, 'Largest Contentful Paint: largest image is reported.');
  </script>
</body>