summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/pointerevents/coalesced_events_attributes_under_load.html
blob: 442aecb73ee6e65433afd848459edfae153cae75 (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
<!doctype html>
<title>Coalesced events under load</title>
<meta name="variant" content="?mouse">
<meta name="variant" content="?pen">
<meta name="variant" content="?touch">
<meta name="viewport" content="width=device-width">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="pointerevent_support.js"></script>
<style>
  #target {
    width: 100px;
    height: 100px;
    touch-action: none;
  }
</style>
<div id="target"></div>

<script>
  "use strict";
  const pointer_type = location.search.substring(1);
  const target = document.getElementById("target");

  // https://w3c.github.io/pointerevents/#coalesced-events
  function checkCoalescedMoveEventAttributes(event) {
    let coalesced_events = event.getCoalescedEvents();
    assert_greater_than_equal(coalesced_events.length, 1,
        "pointermove.getCoalescedEvents() has at least 1 entry");

    for (let i = 0; i < coalesced_events.length; i++) {
      let coalesced_event = coalesced_events[i];

      assert_equals(coalesced_event.isTrusted, true,
          "coalesced_event.isTrusted is true");
      assert_equals(coalesced_event.bubbles, false,
          "coalesced_event.bubbles is false");
      assert_equals(coalesced_event.cancelable, false,
          "coalesced_event.cancelable is false");

      if (i > 0) {
        assert_greater_than_equal(coalesced_event.timeStamp,
            coalesced_events[i-1].timeStamp,
            "coalesced_event.timeStamp must be ascending");
      }
    }
  }

  let coalesced_event_received = false;

  promise_test(async t => {
    let current_busyloop_ms = 5;

    target.addEventListener("pointerdown", event => {
      // Every pointerdown blocks the main thread for a certain time limit,
      // and then increases the time limit for next round in case the
      // current limit fails to cause event coalescing.
      let now = performance.now();
      while (performance.now() < now + current_busyloop_ms)
        continue;
      current_busyloop_ms *= 2;
    });

    target.addEventListener("pointermove", t.step_func(event => {
      checkCoalescedMoveEventAttributes(event);
      if (event.getCoalescedEvents().length > 1)
        coalesced_event_received = true;
    }));

    // Repeatedly send a long action sequence until either a coalesced event is
    // encountered or the busyloop becomes too long.
    while (!coalesced_event_received && current_busyloop_ms < 500) {
      let pointerup_promise = getEvent("pointerup", target);

      let actions = new test_driver.Actions()
          .addPointer("TestPointer", pointer_type)
          .pointerMove(0, 0, { origin: target })
          .pointerDown();
      for (let i = 0; i < 5; i++) {
        actions = actions.pointerMove(20, 20, { origin: target })
            .pointerMove(0, 0, { origin: target });
      }
      actions = actions.pointerUp();

      await actions.send();
      await await pointerup_promise;
    }

    assert_true(coalesced_event_received, "Coalesed pointermoves received");
  }, "Coalesced pointermoves under load");
</script>