summaryrefslogtreecommitdiffstats
path: root/widget/tests/test_mouse_event_with_control_on_mac.html
blob: 85e206ea957293f5885e2b47b2201988cb8cf5e1 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<html>
<head>
  <title>Test control+click on Mac</title>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/SpecialPowers.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
  <style>
    #target {
      width: 100px;
      height: 100px;
      background-color: lightgreen;
    };
  </style>
</head>
<body>
<div id="target"></div>
<script type="application/javascript">

function synthesizeNativeMouseClickWithControl(aTarget, aOffsetX, aOffsetY) {
  const NSEventTypeLeftMouseDown = 1;
  const NSEventTypeLeftMouseUp = 2;

  const NSEventModifierFlagControl = 1 << 18;

  let utils = SpecialPowers.getDOMWindowUtils(window);

  let rect = aTarget.getBoundingClientRect();
  let x = aOffsetX + window.mozInnerScreenX + rect.left;
  let y = aOffsetY + window.mozInnerScreenY + rect.top;
  let scale = utils.screenPixelsPerCSSPixel;

  utils.sendNativeMouseEvent(x * scale, y * scale, NSEventTypeLeftMouseDown, NSEventModifierFlagControl, aTarget);
  utils.sendNativeMouseEvent(x * scale, y * scale, NSEventTypeLeftMouseUp, NSEventModifierFlagControl, aTarget);
}

function waitAndCheckMouseEvents(aTarget, aExpectedEvents) {
  return new Promise((aResolve, aReject) => {
    let timer;
    let cleanup = function() {
      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
      aTarget.removeEventListener("mousedown", listener);
      aTarget.removeEventListener("mouseup", listener);
      aTarget.removeEventListener("contextmenu", listener);
      aTarget.removeEventListener("click", listener);
      aTarget.removeEventListener("auxclick", listener);
    };

    let listener = function(aEvent) {
      aEvent.preventDefault();
      let expectedEvent = aExpectedEvents.shift();
      if (!expectedEvent) {
        cleanup();
        ok(false, `receive unexpected ${aEvent.type} event`);
        aReject(new Error(`receive unexpected ${aEvent.type} event`));
        return;
      }

      isDeeply([aEvent.type, aEvent.button, aEvent.ctrlKey], expectedEvent,
               `check received ${aEvent.type} event`);
      if (!aExpectedEvents.length) {
        // Wait a bit to see if there is any unexpected event.
        timer = setTimeout(function() {
          cleanup();
          aResolve();
        }, 0);
      }
    };

    aTarget.addEventListener("mousedown", listener);
    aTarget.addEventListener("mouseup", listener);
    aTarget.addEventListener("contextmenu", listener);
    aTarget.addEventListener("click", listener);
    aTarget.addEventListener("auxclick", listener);
  });
}

add_task(async function Init() {
  await SimpleTest.promiseFocus();
});

add_task(async function TestMouseClickWithControl() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.event.treat_ctrl_click_as_right_click.disabled", true]],
  });

  let target = document.getElementById("target");
  let promise = waitAndCheckMouseEvents(target, [["mousedown", 0, true],
                                                 ["contextmenu", 0, true],
                                                 ["mouseup", 0, true],
                                                 ["click", 0, true]]);
  synthesizeNativeMouseClickWithControl(target, 10, 10);
  await promise;
});

add_task(async function TestOldBehavior() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.event.treat_ctrl_click_as_right_click.disabled", false]],
  });

  let target = document.getElementById("target");
  let promise = waitAndCheckMouseEvents(target, [["mousedown", 2, true],
                                                 ["contextmenu", 2, true],
                                                 ["mouseup", 2, true],
                                                 ["auxclick", 2, true]]);
  synthesizeNativeMouseClickWithControl(target, 10, 10);
  await promise;
});
</script>
</body>
</html>