summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/uievents/click/contextmenu_event.html
blob: 7a33d0aa38c47af832d57304f4b804e19952953c (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
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Contextmenu event</title>
  <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>
</head>
<body>
  <h1>Test contextmenu event</h1>
  <p>Tests that right-clicking fires a contextmenu event.</p>
  <ol id="instructions">
    <li>Right-click here.
  </ol>
  <script>
    let event_log = [];

    function cancel_and_log_event(ename) {
        return new Promise(resolve => {
            document.body.addEventListener(ename, e => {
                e.preventDefault();
                event_log.push(e.type);
                resolve(e);
            }, {once: true});
        });
    }

    promise_test(async () => {
        const event_promises = ["mousedown", "mouseup", "contextmenu"]
              .map(ename => cancel_and_log_event(ename));

        let target = document.getElementById("instructions");
        let actions = new test_driver.Actions();
        actions.pointerMove(0, 0, {origin: target})
            .pointerDown({button: actions.ButtonType.RIGHT})
            .pointerUp({button: actions.ButtonType.RIGHT})
            .send();

        await Promise.all(event_promises);

        assert_equals(event_log.length, 3, "Three events are received");
        assert_true(event_log.includes("contextmenu"), "contextmenu event is received");
        assert_true(event_log.includes("mouseup"), "mouseup event is received");
        assert_equals(event_log[0], "mousedown", "mousedown event is the first event received");
    }, "Test contextmenu dispatched after mousedown");
  </script>
</body>
</html>