summaryrefslogtreecommitdiffstats
path: root/dom/events/test/pointerevents/wpt/pointerevent_support.js
blob: 4b8c83cbe062ab3bc8778ac11b6921bbd0679665 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
var All_Pointer_Events = [
  "pointerdown",
  "pointerup",
  "pointercancel",
  "pointermove",
  "pointerover",
  "pointerout",
  "pointerenter",
  "pointerleave",
  "gotpointercapture",
  "lostpointercapture",
];

// Check for conformance to PointerEvent interface
// TA: 1.1, 1.2, 1.6, 1.7, 1.8, 1.9, 1.10, 1.11, 1.12, 1.13
function check_PointerEvent(event, testNamePrefix) {
  if (testNamePrefix === undefined) {
    testNamePrefix = "";
  }

  // Use expectedPointerType if set otherwise just use the incoming event pointerType in the test name.
  var pointerTestName =
    testNamePrefix +
    " " +
    (expectedPointerType == null ? event.pointerType : expectedPointerType) +
    " " +
    event.type;

  if (expectedPointerType != null) {
    test(function() {
      assert_equals(
        event.pointerType,
        expectedPointerType,
        "pointerType should be the one specified in the test page."
      );
    }, pointerTestName + " event pointerType is correct.");
  }

  test(function() {
    assert_true(
      event instanceof event.target.ownerDocument.defaultView.PointerEvent,
      "event is a PointerEvent event"
    );
  }, pointerTestName + " event is a PointerEvent event");

  // Check attributes for conformance to WebIDL:
  // * attribute exists
  // * has proper type
  // * if the attribute is "readonly", it cannot be changed
  // TA: 1.1, 1.2
  var idl_type_check = {
    long(v) {
      return typeof v === "number" && Math.round(v) === v;
    },
    float(v) {
      return typeof v === "number";
    },
    string(v) {
      return typeof v === "string";
    },
    boolean(v) {
      return typeof v === "boolean";
    },
  };
  [
    ["readonly", "long", "pointerId"],
    ["readonly", "float", "width"],
    ["readonly", "float", "height"],
    ["readonly", "float", "pressure"],
    ["readonly", "long", "tiltX"],
    ["readonly", "long", "tiltY"],
    ["readonly", "string", "pointerType"],
    ["readonly", "boolean", "isPrimary"],
    ["readonly", "long", "detail", 0],
  ].forEach(function(attr) {
    var readonly = attr[0];
    var type = attr[1];
    var name = attr[2];
    var value = attr[3];

    // existence check
    test(function() {
      assert_true(
        name in event,
        name + " attribute in " + event.type + " event"
      );
    }, pointerTestName + "." + name + " attribute exists");

    // readonly check
    if (readonly === "readonly") {
      test(function() {
        assert_readonly(
          event.type,
          name,
          event.type + "." + name + " cannot be changed"
        );
      }, pointerTestName + "." + name + " is readonly");
    }

    // type check
    test(function() {
      assert_true(
        idl_type_check[type](event[name]),
        name + " attribute of type " + type
      );
    }, pointerTestName +
      "." +
      name +
      " IDL type " +
      type +
      " (JS type was " +
      typeof event[name] +
      ")");

    // value check if defined
    if (value != undefined) {
      test(function() {
        assert_equals(event[name], value, name + " attribute value");
      }, pointerTestName + "." + name + " value is " + value + ".");
    }
  });

  // Check the pressure value
  // TA: 1.6, 1.7, 1.8
  test(function() {
    // TA: 1.6
    assert_greater_than_equal(
      event.pressure,
      0,
      "pressure is greater than or equal to 0"
    );
    assert_less_than_equal(
      event.pressure,
      1,
      "pressure is less than or equal to 1"
    );

    if (event.type === "pointerup") {
      assert_equals(event.pressure, 0, "pressure is 0 during pointerup");
    }

    // TA: 1.7, 1.8
    if (event.pointerType === "mouse") {
      if (event.buttons === 0) {
        assert_equals(
          event.pressure,
          0,
          "pressure is 0 for mouse with no buttons pressed"
        );
      } else {
        assert_equals(
          event.pressure,
          0.5,
          "pressure is 0.5 for mouse with a button pressed"
        );
      }
    }
  }, pointerTestName + ".pressure value is valid");

  // Check mouse-specific properties
  if (event.pointerType === "mouse") {
    // TA: 1.9, 1.10, 1.13
    test(function() {
      assert_equals(event.width, 1, "width of mouse should be 1");
      assert_equals(event.height, 1, "height of mouse should be 1");
      assert_equals(event.tiltX, 0, event.type + ".tiltX is 0 for mouse");
      assert_equals(event.tiltY, 0, event.type + ".tiltY is 0 for mouse");
      assert_true(event.isPrimary, event.type + ".isPrimary is true for mouse");
    }, pointerTestName + " properties for pointerType = mouse");
    // Check properties for pointers other than mouse
  }
}

function showPointerTypes() {
  var complete_notice = document.getElementById("complete-notice");
  var pointertype_log = document.getElementById("pointertype-log");
  var pointertypes = Object.keys(detected_pointertypes);
  pointertype_log.innerHTML = pointertypes.length
    ? pointertypes.join(",")
    : "(none)";
  complete_notice.style.display = "block";
}

function showLoggedEvents() {
  var event_log_elem = document.getElementById("event-log");
  event_log_elem.innerHTML = event_log.length ? event_log.join(", ") : "(none)";

  var complete_notice = document.getElementById("complete-notice");
  complete_notice.style.display = "block";
}

function log(msg, el) {
  if (++count > 10) {
    count = 0;
    el.innerHTML = " ";
  }
  el.innerHTML = msg + "; " + el.innerHTML;
}

function failOnScroll() {
  assert_true(false, "scroll received while shouldn't");
}

function updateDescriptionNextStep() {
  document.getElementById("desc").innerHTML =
    "Test Description: Try to scroll text RIGHT.";
}

function updateDescriptionComplete() {
  document.getElementById("desc").innerHTML = "Test Description: Test complete";
}

function updateDescriptionSecondStepTouchActionElement(
  target,
  scrollReturnInterval
) {
  window.setTimeout(function() {
    objectScroller(target, "up", 0);
  }, scrollReturnInterval);
  document.getElementById("desc").innerHTML =
    "Test Description: Try to scroll element RIGHT moving your outside of the red border";
}

function updateDescriptionThirdStepTouchActionElement(
  target,
  scrollReturnInterval,
  callback = null
) {
  window.setTimeout(function() {
    objectScroller(target, "left", 0);
    if (callback) {
      callback();
    }
  }, scrollReturnInterval);
  document.getElementById("desc").innerHTML =
    "Test Description: Try to scroll element DOWN then RIGHT starting your touch inside of the element. Then tap complete button";
}

function updateDescriptionFourthStepTouchActionElement(
  target,
  scrollReturnInterval
) {
  document.getElementById("desc").innerHTML =
    "Test Description: Try to scroll element RIGHT starting your touch inside of the element";
}

function objectScroller(target, direction, value) {
  if (direction == "up") {
    target.scrollTop = 0;
  } else if (direction == "left") {
    target.scrollLeft = 0;
  }
}

function sPointerCapture(e) {
  try {
    target0.setPointerCapture(e.pointerId);
  } catch (ex) {}
}

function rPointerCapture(e) {
  try {
    captureButton.value = "Set Capture";
    target0.releasePointerCapture(e.pointerId);
  } catch (ex) {}
}

var globalPointerEventTest = null;
var expectedPointerType = null;
const ALL_POINTERS = ["mouse", "touch", "pen"];
const HOVERABLE_POINTERS = ["mouse", "pen"];
const NOHOVER_POINTERS = ["touch"];

function MultiPointerTypeTest(testName, types) {
  this.testName = testName;
  this.types = types;
  this.currentTypeIndex = 0;
  this.currentTest = null;
  this.createNextTest();
}

MultiPointerTypeTest.prototype.skip = function() {
  var prevTest = this.currentTest;
  this.createNextTest();
  prevTest.timeout();
};

MultiPointerTypeTest.prototype.done = function() {
  var prevTest = this.currentTest;
  this.createNextTest();
  if (prevTest != null) {
    prevTest.done();
  }
};

MultiPointerTypeTest.prototype.step = function(stepFunction) {
  this.currentTest.step(stepFunction);
};

MultiPointerTypeTest.prototype.createNextTest = function() {
  if (this.currentTypeIndex < this.types.length) {
    var pointerTypeDescription = document.getElementById(
      "pointerTypeDescription"
    );
    document.getElementById("pointerTypeDescription").innerHTML =
      "Follow the test instructions with <span style='color: red'>" +
      this.types[this.currentTypeIndex] +
      "</span>. If you don't have the device <a href='javascript:;' onclick='globalPointerEventTest.skip()'>skip it</a>.";
    this.currentTest = async_test(
      this.types[this.currentTypeIndex] + " " + this.testName
    );
    expectedPointerType = this.types[this.currentTypeIndex];
    this.currentTypeIndex++;
  } else {
    document.getElementById("pointerTypeDescription").innerHTML = "";
  }
  resetTestState();
};

function setup_pointerevent_test(testName, supportedPointerTypes) {
  return (globalPointerEventTest = new MultiPointerTypeTest(
    testName,
    supportedPointerTypes
  ));
}

function checkPointerEventType(event) {
  assert_equals(
    event.pointerType,
    expectedPointerType,
    "pointerType should be the same as the requested device."
  );
}