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
|
<!DOCTYPE HTML>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script type="text/javascript" src="../pointerevent_support.js"></script>
<style>
div.box {
margin: 10px;
padding: 50px;
float: left;
}
</style>
<h1>Verifies the effect of pointer event prevent-default on following pointer & mouse events.</h1>
<div id="target" class="box" style="background-color:green;">
</div>
<script>
var receivedEvents = [];
var receivedMouseEvents = [];
var receivedPointerEvents = [];
var mouseEventList = ["mousedown", "mouseup", "mouseenter", "mouseleave", "mouseover", "mouseout", "mousemove"];
var pointerEventList = ["pointerdown", "pointerup", "pointerenter", "pointerleave", "pointerover", "pointerout", "pointermove"];
var preventDefaultList = ["none"].concat(pointerEventList);
var eventToPreventDefault = "";
function init() {
var targetDiv = document.getElementById("target");
mouseEventList.forEach(function (eventName) {
targetDiv.addEventListener(eventName, function (event) {
receivedEvents.push(eventName);
receivedMouseEvents.push(eventName);
});
});
pointerEventList.forEach(function (eventName) {
targetDiv.addEventListener(eventName, function (event) {
if (event.type == eventToPreventDefault) {
event.preventDefault();
}
receivedEvents.push(eventName);
receivedPointerEvents.push(eventName);
});
});
}
function expectedPointerEvents() {
return [
"pointerover", "pointerenter", "pointermove", "pointerdown", "pointerup",
"pointerdown", "pointermove", "pointerout", "pointerleave", "pointerover",
"pointerenter", "pointermove", "pointerup", "pointerout", "pointerleave"
];
}
// When "pointerdown" is not prevented, all compat mouse events are fired.
// Otherwise, the "mousedown/move/up" events between "pointerdown" and
// "pointerup" are suppressed.
function expectedMouseEvents(prevented_event) {
return prevented_event === "pointerdown"
? ["mouseover", "mouseenter", "mousemove",
"mouseout", "mouseleave", "mouseover", "mouseenter",
"mouseout", "mouseleave"]
: ["mouseover", "mouseenter", "mousemove",
"mousedown", "mouseup", "mousedown", "mousemove",
"mouseout", "mouseleave", "mouseover", "mouseenter",
"mousemove", "mouseup",
"mouseout", "mouseleave"];
}
async function runTestForDefaultEvent(preventDefaultEvent) {
promise_test((test)=>
new Promise(async (resolve, reject)=>{
test.add_cleanup(()=>{
receivedEvents = [];
receivedPointerEvents = [];
receivedMouseEvents = [];
eventToPreventDefault = "";
});
eventToPreventDefault = preventDefaultEvent;
try{
// if awaited Promise rejects then fail the test
await performActions(test, preventDefaultEvent);
}
catch(e){
reject(e);
}
test.step(()=>{
if(eventToPreventDefault === 'none'){
assert_true(arePointerEventsBeforeCompatMouseEvents(receivedEvents), "There are no compatible mouse events for all pointer events:" + receivedEvents);
}
assert_array_equals(receivedPointerEvents, expectedPointerEvents());
assert_array_equals(receivedMouseEvents, expectedMouseEvents(preventDefaultEvent));
});
resolve();
}), `Verifies the effect of pointer event prevent-default on ${preventDefaultEvent}`);
}
async function runTests(){
// create one test for each event in preventDefaultList
// run all tests one by one
preventDefaultList.forEach(preventDefaultEvent=>runTestForDefaultEvent(preventDefaultEvent));
}
function performActions(test, preventDefaultEvent){
let targetDiv = document.getElementById("target");
let rect = targetDiv.getBoundingClientRect();
let x1 = Math.ceil(rect.left - 3);
let y1 = Math.ceil(rect.top - 5);
let x2 = Math.ceil(rect.left + 3);
let y2 = Math.ceil(rect.top + 5);
let eventWatcher = new EventWatcher(test, targetDiv, ["mouseleave"]);
// wait for all expected events to happen.
// Because each test ends with mouseleave and there are 2 mouseleave events,
// we use EventWatcher to wait for the two mouseleave events to happen
let donePromise = eventWatcher.wait_for(["mouseleave", "mouseleave"], { record: 'all' });
let actionsPromise = new test_driver.Actions()
.pointerMove(0, 0)
// start with mouse outside target
.pointerMove(x1, y1)
// move into target and click
.pointerMove(x2, y2)
.pointerDown()
.pointerUp()
// drag out of and into target & release within target
.pointerDown()
.pointerMove(x2+5, y2+5)
.pointerMove(x1, y1)
.pointerMove(x2+1, y2+1)
.pointerUp()
// move outside target again
.pointerMove(x1, y1)
.send();
return Promise.all([donePromise, actionsPromise]);
}
init();
runTests();
</script>
|