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

content / test / data / performance_timeline / event_timing.html [blame]

<script>
  let allEventsPromise;

  let kDurationThreshold = 104;

  const mainThreadBusy = (ms) => {
    const target = performance.now() + ms;
    while (performance.now() < target);
  }

  const registerEventListeners = () => {
    mouseupPromise = new Promise(resolve => {
      window.addEventListener('mouseup', () => { mainThreadBusy(kDurationThreshold + 4); resolve(); }, { once: true });
    });

    pointerupPromise = new Promise(resolve => {
      window.addEventListener('pointerup', () => { mainThreadBusy(kDurationThreshold + 4); resolve(); }, { once: true });
    });

    clickPromise = new Promise(resolve => {
      window.addEventListener('click', () => { mainThreadBusy(kDurationThreshold + 4); resolve(); }, { once: true });
    });

    allEventsPromise = Promise.all([mouseupPromise, pointerupPromise, clickPromise])
    return true;
  };

  const waitForEvent = async () => {
    await allEventsPromise;
    return true;
  }

  // Assuming the PerformanceObserver callback would not fire until the `click` is finished.
  // This could be flaky depending on the loading state of the test page and render scheduling.
  const getEntriesCntAndDroppedEntriesCnt = async () => {
    return await new Promise(resolve => {
      const observer = new PerformanceObserver((list, _, options) => {
        resolve([list.getEntries().length, options['droppedEntriesCount']]);
      }).observe({ type: "event", durationThreshold: kDurationThreshold, buffered: true });
    });
  }
</script>