tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

observe-multiple-images.html (5004B)


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