summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/event-timing/click-timing.html
blob: f2b6922f5e585069932d6d2e71a9f1f9bed7ce65 (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
<!DOCTYPE html>
<html>
<meta charset=utf-8 />
<title>Event Timing: compare timing of two long clicks
</title>
<button id='button'>Generate a 'click' event</button>
<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/event-timing-test-utils.js></script>
<script>
  /* Timeline:
     Observer starts
     Long click 1
     Long click 2
     Once two clicks have been received by observer, compare the timestamps.
  */
  let timeBeforeFirstClick;
  let timeAfterFirstClick;
  let timeAfterSecondClick;

  function testExpectations(observedEntries) {
    assert_not_equals(timeBeforeFirstClick, undefined);
    assert_not_equals(timeAfterFirstClick, undefined);
    assert_not_equals(timeAfterSecondClick, undefined);

    // First click.
    verifyClickEvent(observedEntries[0], 'button');
    assert_between_exclusive(observedEntries[0].processingStart,
        timeBeforeFirstClick,
        timeAfterFirstClick,
        "First click's processingStart");
    assert_greater_than(timeAfterFirstClick, observedEntries[0].startTime,
        "timeAfterFirstClick should be later than first click's start time.");

    // Second click.
    verifyClickEvent(observedEntries[1], 'button');
    assert_between_exclusive(observedEntries[1].processingStart,
        timeAfterFirstClick,
        timeAfterSecondClick,
        "Second click's processingStart");
    assert_greater_than(timeAfterSecondClick, observedEntries[1].startTime,
          "timeAfterSecondClick should be later than second click's start time.");
  }

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

    const observerPromise = new Promise(resolve => {
      const observedEntries = [];
      new PerformanceObserver(entryList => {
        observedEntries.push(...entryList.getEntriesByName("pointerdown"));
        if (observedEntries.length < 2)
          return;
        resolve(observedEntries);
      }).observe({type: 'event'});
    })

    timeBeforeFirstClick = performance.now();
    await clickAndBlockMain('button');
    timeAfterFirstClick = performance.now();
    await clickAndBlockMain('button');
    timeAfterSecondClick = performance.now();

    const observedEntries = await observerPromise;
    testExpectations(observedEntries);

  }, "Event Timing: compare click timings.");
</script>
</html>