summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/event-timing/event-click-visibilitychange.html
blob: beb3ba3512d6e0ad800c8be041d0014a80ba375c (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
<!DOCTYPE html>
<html>
<meta charset=utf-8 />
<meta name="timeout" content="long">
<title>Event Timing: eventCounts.</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
<script src=/resources/testdriver-actions.js></script>
<script src=/page-visibility/resources/window_state_context.js></script>
<script src=resources/event-timing-test-utils.js></script>

<body>
  <button id='target'>Click me</button>

  <script>
    let observedEntries = [];
    const map = new Map();
    const events = ['pointerdown'];

    promise_test(async t => {
      assert_implements(window.PerformanceEventTiming, 'Event Timing is not supported.');

      const { minimize, restore } = window_state_context(t);
      const button = document.getElementById('target');

      const callback = (entryList) => { observedEntries = observedEntries.concat(entryList.getEntries().filter(filterAndAddToMap(events, map))); };
      const readyToResolve = () => { return observedEntries.length >= 1; };
      const observerPromise = createPerformanceObserverPromise(['event'], callback, readyToResolve);

      const tapEventPromise = new Promise(resolve => {
        button.addEventListener('pointerdown', async (event) => {
          document.body.innerText += "Adding content to force rendering";

          // await here will yield to event loop, and end event processing time,
          // which will allow rendering to continue.
          // The visibility change may happen before rendering has a chance
          // but it is not guarenteed which will happen first.
          await minimize();
          const timeAfterVisibilityFalse = performance.now();

          await restore();
          const timeAfterVisibilityTrue = performance.now();

          resolve({ timeAfterVisibilityFalse, timeAfterVisibilityTrue });
        });
      });

      // A buffered visibility-state PerformanceEntry would have made this test
      // cleaner, due to the variability of ordering of events, but it is not
      // yet available.
      const visibilityEventPromise = new Promise(resolve => {
        document.addEventListener('visibilitychange', (event) => {
          if (document.visibilityState !== 'visible') {
            resolve(performance.now());
          }
        });
      });

      const timeBeforeTap = performance.now();
      await interactAndObserve('tap', button, observerPromise);

      // The order that these events fire is non-deterministic, but we can await
      // the result of the promise in any order.
      const { timeAfterVisibilityFalse, timeAfterVisibilityTrue } = await tapEventPromise;
      const timeOfVisibilityFalse = await visibilityEventPromise;

      assert_equals(observedEntries.length, 1, "Pointerdown was measured");
      const entry = observedEntries[0];

      assert_not_equals(timeBeforeTap, undefined);
      assert_not_equals(timeAfterVisibilityFalse, undefined);
      assert_not_equals(timeAfterVisibilityTrue, undefined);
      assert_not_equals(timeOfVisibilityFalse, undefined);

      assert_less_than(
        entry.processingEnd,
        timeOfVisibilityFalse,
        "event handler ends before visibility event fires"
      );
      assert_less_than(
        timeOfVisibilityFalse,
        timeAfterVisibilityFalse,
        "visibility event fires before event handler continues"
      );
      assert_less_than_equal(
        entry.startTime + entry.duration,
        timeAfterVisibilityFalse,
        "event duration ends before visibility is changed"
      );

    }, "Event handlers which change visibility should not measure next paint.");

  </script>
</body>

</html>