summaryrefslogtreecommitdiffstats
path: root/dom/events/test/pointerevents/mochitest_support_internal.js
blob: cdc10a31813daa11aaa42d86816c7921bee1d18b (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
115
116
117
118
119
120
121
122
123
124
125
// This file supports translating W3C tests
// to tests on auto MochiTest system with minimum changes.
// Author: Maksim Lebedev <alessarik@gmail.com>

const PARENT_ORIGIN = "http://mochi.test:8888/";

// Since web platform tests don't check pointerId, we have to use some heuristic
// to test them. and thus pointerIds are send to mochitest_support_external.js
// before we start sending synthesized widget events. Here, we avoid using
// default values used in Gecko to insure everything works as expected.
const POINTER_MOUSE_ID = 7;
const POINTER_PEN_ID = 8;
const POINTER_TOUCH_ID = 9; // Extend for multiple touch points if needed.

// Setup environment.
addListeners(document.getElementById("target0"));
addListeners(document.getElementById("target1"));

// Setup communication between mochitest_support_external.js.
// Function allows to initialize prerequisites before testing
// and adds some callbacks to support mochitest system.
function resultCallback(aTestObj) {
  var message = aTestObj.name + " (";
  message += "Get: " + JSON.stringify(aTestObj.status) + ", ";
  message += "Expect: " + JSON.stringify(aTestObj.PASS) + ")";
  window.opener.postMessage(
    {
      type: "RESULT",
      message,
      result: aTestObj.status === aTestObj.PASS,
    },
    PARENT_ORIGIN
  );
}

add_result_callback(resultCallback);
add_completion_callback(() => {
  window.opener.postMessage({ type: "FIN" }, PARENT_ORIGIN);
});

window.addEventListener("load", () => {
  // Start testing.
  var startMessage = {
    type: "START",
    message: {
      mouseId: POINTER_MOUSE_ID,
      penId: POINTER_PEN_ID,
      touchId: POINTER_TOUCH_ID,
    },
  };
  window.opener.postMessage(startMessage, PARENT_ORIGIN);
});

function addListeners(elem) {
  if (!elem) {
    return;
  }
  var All_Events = [
    "pointerdown",
    "pointerup",
    "pointercancel",
    "pointermove",
    "pointerover",
    "pointerout",
    "pointerenter",
    "pointerleave",
    "gotpointercapture",
    "lostpointercapture",
  ];
  All_Events.forEach(function(name) {
    elem.addEventListener(name, function(event) {
      console.log("(" + event.type + ")-(" + event.pointerType + ")");

      // Perform checks only for trusted events.
      if (!event.isTrusted) {
        return;
      }

      // Compute the desired event.pointerId from event.pointerType.
      var pointerId = {
        mouse: POINTER_MOUSE_ID,
        pen: POINTER_PEN_ID,
        touch: POINTER_TOUCH_ID,
      }[event.pointerType];

      // Compare the pointerId.
      resultCallback({
        name: "Mismatched event.pointerId recieved.",
        status: event.pointerId,
        PASS: pointerId,
      });
    });
  });
}

// mock the touchScrollInTarget to make the test work.
function touchScrollInTarget() {
  return Promise.resolve();
}

// mock test_driver to make the test work.
function Actions() {}
Actions.prototype = {
  addPointer() {
    return this;
  },
  pointerMove() {
    return this;
  },
  pointerDown() {
    return this;
  },
  pause() {
    return this;
  },
  pointerUp() {
    return this;
  },
  send() {
    return Promise.resolve();
  },
};
const test_driver = {
  Actions,
};