Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/fake_events.js
4150 views
1
/*
2
* Helper function used in browser tests to simulate HTML5 events
3
*/
4
5
function simulateKeyEvent(eventType, keyCode, code, key, target) {
6
var props = { keyCode, charCode: keyCode, view: window, bubbles: true, cancelable: true };
7
if (code) props['code'] = code;
8
if (key) props['key'] = key;
9
var event = new KeyboardEvent(eventType, props);
10
if (!target) target = document;
11
return target.dispatchEvent(event);
12
}
13
14
function simulateKeyDown(keyCode, code = undefined, key = undefined, target = undefined) {
15
var doDefault = simulateKeyEvent('keydown', keyCode, code, key, target);
16
// As long as not handler called `preventDefault` we also send a keypress
17
// event.
18
if (doDefault) {
19
simulateKeyEvent('keypress', keyCode, code, key, target);
20
}
21
}
22
23
function simulateKeyUp(keyCode, code = undefined, target = undefined) {
24
simulateKeyEvent('keyup', keyCode, code, target);
25
}
26
27
function simulateKeyDownUp(keyCode, code = undefined, target = undefined) {
28
simulateKeyDown(keyCode, code, target);
29
simulateKeyUp(keyCode, code, target);
30
}
31
32
function simulateMouseEvent(eventType, x, y, button, absolute) {
33
if (!absolute) {
34
x += Module['canvas'].offsetLeft;
35
y += Module['canvas'].offsetTop;
36
}
37
var event = document.createEvent("MouseEvents");
38
event.initMouseEvent(eventType, true, true, window,
39
1, x, y, x, y,
40
0, 0, 0, 0,
41
button, null);
42
Module['canvas'].dispatchEvent(event);
43
}
44
45
function simulateMouseDown(x, y, button, absolute) {
46
simulateMouseEvent('mousedown', x, y, button, absolute);
47
}
48
49
function simulateMouseUp(x, y, button, absolute) {
50
simulateMouseEvent('mouseup', x, y, button, absolute);
51
}
52
53
function simulateMouseMove(x, y, absolute) {
54
simulateMouseEvent('mousemove', x, y, 0, absolute);
55
}
56
57
function simulateMouseClick(x, y, button, absolute) {
58
simulateMouseDown(x, y, button, absolute);
59
simulateMouseUp(x, y, button, absolute);
60
}
61
62