summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/element-timing/observe-multiple-images.html
blob: c5ea700553b4b2337bbcbb3925293992758f791d (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Element Timing: multiple images</title>
<body>
<style>
body {
  margin: 0;
}
#img1 {
  display: block;
  margin-left: auto;
  margin-right: auto;
}
#img2 {
  margin-top:150px;
  margin-left:50px;
}
</style>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/element-timing-helpers.js"></script>
<script>
  let beforeRender, image1Observed=0, image2Observed=0, image3Observed=0;
  async_test(function (t) {
    assert_implements(window.PerformanceElementTiming, "PerformanceElementTiming is not implemented");
    const observer = new PerformanceObserver(
      t.step_func(function(entryList) {
        entryList.getEntries().forEach( entry => {
          if (entry.identifier === 'image1') {
            if (image1Observed) {
              assert_unreached("Observer received image1 more than once");
              t.done();
            }
            image1Observed = 1;
            const pathname1 = window.location.origin + '/element-timing/resources/square100.png';
            // The images do not contain ID, so expect an empty ID.
            checkElement(entry, pathname1, 'image1', 'img1', beforeRender,
                document.getElementById('img1'));
            // This image is horizontally centered.
            // Using abs and comparing to 1 because the viewport sizes could be odd.
            // If a size is odd, then image cannot be in the pure center, but left
            // and right should still be very close to their estimated coordinates.
            assert_less_than_equal(Math.abs(entry.intersectionRect.left -
              (document.documentElement.clientWidth / 2 - 50)), 1,
              'left of rect for image1');
            assert_less_than_equal(Math.abs(entry.intersectionRect.right -
              (document.documentElement.clientWidth / 2 + 50)), 1,
              'right of rect for image1');
            assert_equals(entry.intersectionRect.top, 0, 'top of rect for image1');
            assert_equals(entry.intersectionRect.bottom,
              100, 'bottom of rect for image1');
            checkNaturalSize(entry, 100, 100);
          }
          else if (entry.identifier === 'image2') {
            if (image2Observed) {
              assert_unreached("Observer received image2 more than once");
              t.done();
            }
            image2Observed = 1;
            const pathname2 = window.location.origin + '/element-timing/resources/square20.png';
            checkElement(entry, pathname2, 'image2', 'img2', beforeRender,
                document.getElementById('img2'));
            // This image should be below image 1, and should respect the margin.
            checkRect(entry, [50, 250, 250, 450], "of image2");
            checkNaturalSize(entry, 20, 20);
          }
          else if (entry.identifier === 'image3') {
            if (image3Observed) {
              assert_unreached("Observer received image3 more than once");
              t.done();
            }
            image3Observed = 1;
            const pathname3 = window.location.origin + '/element-timing/resources/circle.svg';
            checkElement(entry, pathname3, 'image3', 'img3', beforeRender,
                document.getElementById('img3'));
            // This image is just to the right of image2.
            checkRect(entry, [250, 450, 250, 450], "of image3");
            checkNaturalSize(entry, 200, 200);
          }
          else {
            assert_unreached("Received an unexpected identifier.");
            t.done();
          }
          if (image1Observed && image2Observed && image3Observed) {
            t.done();
          }
        });
      })
    );
    observer.observe({entryTypes: ['element']});
    function addImage(number, source, width=0) {
      const img = document.createElement('img');
      img.src = source;
      // Set a different id and elementtiming value for each image.
      img.id = 'img' + number;
      img.setAttribute('elementtiming', 'image' + number);
      if (width !== 0)
        img.width = width;
      document.body.appendChild(img);
    }
    // Add the images during onload to be sure that the observer is registered in
    // time to observe the element timing.
    window.onload = () => {
      addImage(1, 'resources/square100.png');
      // Use requestAnimationFrame and a timeout to ensure that the images are
      // processed in the order we want.
      requestAnimationFrame( () => {
        t.step_timeout( () => {
          // Set the size equal to that of image3 to make positioning easier.
          addImage(2, 'resources/square20.png', 200);
          requestAnimationFrame( () => {
            t.step_timeout( () => {
              addImage(3, 'resources/circle.svg');
            }, 0);
          });
        }, 0);
      });
      beforeRender = performance.now();
    };
  }, 'PerformanceObserver can observe multiple image elements.');
</script>
</body>