summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/is-input-pending/resources/pending-input-utils.js
blob: 489c16827a111a561eb1c2cccb72a4db526ca3e1 (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
// Dispatches the given sequence of actions and verifies isInputPending state
// after dispatch according to expectations. Returns when all dispatched input
// has been handled.
const pendingActionTest = async (label, target, actionCallback, expectations) => {
  promise_test(async () => {
    // Give focus to the page first, before running the test.
    await new test_driver.Actions()
      .pointerMove(0, 0)
      .pointerDown()
      .pointerUp()
      .send();

    // Register a handler to fetch the result of isInputPending from the target
    // window.
    const resultPromise = new Promise(res => {
      window.addEventListener('message', function handler(e) {
        if (e.data === 'check-input') return;
        res(e.data);
        window.removeEventListener('message', handler);
      });
    });

    // Signal to the target window to monitor isInputPending.
    target.postMessage('check-input', '*');

    const actions = actionCallback();
    const actionsPromise = actions.send();

    const {discrete, continuous} = await resultPromise;

    assert_equals(discrete, expectations.discrete, 'detected discrete input');
    assert_equals(continuous, expectations.continuous, 'detected continuous input');

    await actionsPromise;
  }, label);
}

const PendingInputUtils = {
  testDetectNoPendingInput(target, actionCallback, label) {
    pendingActionTest(label, target, actionCallback, {
      discrete: false,
      continuous: false,
    });
  },

  testDetectDiscretePendingInput(target, actionCallback, label) {
    pendingActionTest(label, target, actionCallback, {
      discrete: true,
      continuous: true,
    });
  },

  testDetectContinuousPendingInput(target, actionCallback, label) {
    pendingActionTest(label, target, actionCallback, {
      discrete: false,
      continuous: true,
    });
  },

  // Simulates a pointer event at the given coordinates, and tests that the
  // given target window cannot access it. Intended for cross-origin compliance
  // tests.
  testCannotAccessPendingInputAt(target, x, y, label) {
    PendingInputUtils.testDetectNoPendingInput(target, () => {
      return new test_driver.Actions().pointerMove(x, y).pointerDown().pointerUp();
    }, label);
  },
}