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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
'use strict'
// Runs a set of tests for a given prefixed/unprefixed animation event (e.g.
// animationstart/webkitAnimationStart).
//
// The eventDetails object must have the following form:
// {
// isTransition: false, <-- can be omitted, default false
// unprefixedType: 'animationstart',
// prefixedType: 'webkitAnimationStart',
// animationCssStyle: '1ms', <-- must NOT include animation name or
// transition property
// }
function runAnimationEventTests(eventDetails) {
const {
isTransition,
unprefixedType,
prefixedType,
animationCssStyle
} = eventDetails;
// Derive the DOM event handler names, e.g. onanimationstart.
const unprefixedHandler = `on${unprefixedType}`;
const prefixedHandler = `on${prefixedType.toLowerCase()}`;
const style = document.createElement('style');
document.head.appendChild(style);
if (isTransition) {
style.sheet.insertRule(
`.baseStyle { width: 100px; transition: width ${animationCssStyle}; }`);
style.sheet.insertRule('.transition { width: 200px !important; }');
} else {
style.sheet.insertRule('@keyframes anim {}');
}
function triggerAnimation(div) {
if (isTransition) {
div.classList.add('transition');
} else {
div.style.animation = `anim ${animationCssStyle}`;
}
}
test(t => {
const div = createDiv(t);
assert_equals(div[unprefixedHandler], null,
`${unprefixedHandler} should initially be null`);
assert_equals(div[prefixedHandler], null,
`${prefixedHandler} should initially be null`);
// Setting one should not affect the other.
div[unprefixedHandler] = () => { };
assert_not_equals(div[unprefixedHandler], null,
`setting ${unprefixedHandler} should make it non-null`);
assert_equals(div[prefixedHandler], null,
`setting ${unprefixedHandler} should not affect ${prefixedHandler}`);
div[prefixedHandler] = () => { };
assert_not_equals(div[prefixedHandler], null,
`setting ${prefixedHandler} should make it non-null`);
assert_not_equals(div[unprefixedHandler], div[prefixedHandler],
'the setters should be different');
}, `${unprefixedHandler} and ${prefixedHandler} are not aliases`);
// The below tests primarily test the interactions of prefixed animation
// events in the algorithm for invoking events:
// https://dom.spec.whatwg.org/#concept-event-listener-invoke
promise_test(async t => {
const div = createDiv(t);
let receivedEventCount = 0;
addTestScopedEventHandler(t, div, prefixedHandler, () => {
receivedEventCount++;
});
addTestScopedEventListener(t, div, prefixedType, () => {
receivedEventCount++;
});
// The HTML spec[0] specifies that the prefixed event handlers have an
// 'Event handler event type' of the appropriate prefixed event type. E.g.
// onwebkitanimationend creates a listener for the event type
// 'webkitAnimationEnd'.
//
// [0]: https://html.spec.whatwg.org/multipage/webappapis.html#event-handlers-on-elements,-document-objects,-and-window-objects
div.dispatchEvent(new AnimationEvent(prefixedType));
assert_equals(receivedEventCount, 2,
'prefixed listener and handler received event');
}, `dispatchEvent of a ${prefixedType} event does trigger a ` +
`prefixed event handler or listener`);
promise_test(async t => {
const div = createDiv(t);
let receivedEvent = false;
addTestScopedEventHandler(t, div, unprefixedHandler, () => {
receivedEvent = true;
});
addTestScopedEventListener(t, div, unprefixedType, () => {
receivedEvent = true;
});
div.dispatchEvent(new AnimationEvent(prefixedType));
assert_false(receivedEvent,
'prefixed listener or handler received event');
}, `dispatchEvent of a ${prefixedType} event does not trigger an ` +
`unprefixed event handler or listener`);
promise_test(async t => {
const div = createDiv(t);
let receivedEvent = false;
addTestScopedEventHandler(t, div, prefixedHandler, () => {
receivedEvent = true;
});
addTestScopedEventListener(t, div, prefixedType, () => {
receivedEvent = true;
});
// The rewrite rules from
// https://dom.spec.whatwg.org/#concept-event-listener-invoke step 8 do not
// apply because isTrusted will be false.
div.dispatchEvent(new AnimationEvent(unprefixedType));
assert_false(receivedEvent, 'prefixed listener or handler received event');
}, `dispatchEvent of an ${unprefixedType} event does not trigger a ` +
`prefixed event handler or listener`);
promise_test(async t => {
const div = createDiv(t);
let receivedEvent = false;
addTestScopedEventHandler(t, div, prefixedHandler, () => {
receivedEvent = true;
});
triggerAnimation(div);
await waitForEventThenAnimationFrame(t, unprefixedType);
assert_true(receivedEvent, `received ${prefixedHandler} event`);
}, `${prefixedHandler} event handler should trigger for an animation`);
promise_test(async t => {
const div = createDiv(t);
let receivedPrefixedEvent = false;
addTestScopedEventHandler(t, div, prefixedHandler, () => {
receivedPrefixedEvent = true;
});
let receivedUnprefixedEvent = false;
addTestScopedEventHandler(t, div, unprefixedHandler, () => {
receivedUnprefixedEvent = true;
});
triggerAnimation(div);
await waitForEventThenAnimationFrame(t, unprefixedType);
assert_true(receivedUnprefixedEvent, `received ${unprefixedHandler} event`);
assert_false(receivedPrefixedEvent, `received ${prefixedHandler} event`);
}, `${prefixedHandler} event handler should not trigger if an unprefixed ` +
`event handler also exists`);
promise_test(async t => {
const div = createDiv(t);
let receivedPrefixedEvent = false;
addTestScopedEventHandler(t, div, prefixedHandler, () => {
receivedPrefixedEvent = true;
});
let receivedUnprefixedEvent = false;
addTestScopedEventListener(t, div, unprefixedType, () => {
receivedUnprefixedEvent = true;
});
triggerAnimation(div);
await waitForEventThenAnimationFrame(t, unprefixedHandler);
assert_true(receivedUnprefixedEvent, `received ${unprefixedHandler} event`);
assert_false(receivedPrefixedEvent, `received ${prefixedHandler} event`);
}, `${prefixedHandler} event handler should not trigger if an unprefixed ` +
`listener also exists`);
promise_test(async t => {
// We use a parent/child relationship to be able to register both prefixed
// and unprefixed event handlers without the deduplication logic kicking in.
const parent = createDiv(t);
const child = createDiv(t);
parent.appendChild(child);
// After moving the child, we have to clean style again.
getComputedStyle(child).transition;
getComputedStyle(child).width;
let observedUnprefixedType;
addTestScopedEventHandler(t, parent, unprefixedHandler, e => {
observedUnprefixedType = e.type;
});
let observedPrefixedType;
addTestScopedEventHandler(t, child, prefixedHandler, e => {
observedPrefixedType = e.type;
});
triggerAnimation(child);
await waitForEventThenAnimationFrame(t, unprefixedType);
assert_equals(observedUnprefixedType, unprefixedType);
assert_equals(observedPrefixedType, prefixedType);
}, `event types for prefixed and unprefixed ${unprefixedType} event ` +
`handlers should be named appropriately`);
promise_test(async t => {
const div = createDiv(t);
let receivedEvent = false;
addTestScopedEventListener(t, div, prefixedType, () => {
receivedEvent = true;
});
triggerAnimation(div);
await waitForEventThenAnimationFrame(t, unprefixedHandler);
assert_true(receivedEvent, `received ${prefixedType} event`);
}, `${prefixedType} event listener should trigger for an animation`);
promise_test(async t => {
const div = createDiv(t);
let receivedPrefixedEvent = false;
addTestScopedEventListener(t, div, prefixedType, () => {
receivedPrefixedEvent = true;
});
let receivedUnprefixedEvent = false;
addTestScopedEventListener(t, div, unprefixedType, () => {
receivedUnprefixedEvent = true;
});
triggerAnimation(div);
await waitForEventThenAnimationFrame(t, unprefixedHandler);
assert_true(receivedUnprefixedEvent, `received ${unprefixedType} event`);
assert_false(receivedPrefixedEvent, `received ${prefixedType} event`);
}, `${prefixedType} event listener should not trigger if an unprefixed ` +
`listener also exists`);
promise_test(async t => {
const div = createDiv(t);
let receivedPrefixedEvent = false;
addTestScopedEventListener(t, div, prefixedType, () => {
receivedPrefixedEvent = true;
});
let receivedUnprefixedEvent = false;
addTestScopedEventHandler(t, div, unprefixedHandler, () => {
receivedUnprefixedEvent = true;
});
triggerAnimation(div);
await waitForEventThenAnimationFrame(t, unprefixedHandler);
assert_true(receivedUnprefixedEvent, `received ${unprefixedType} event`);
assert_false(receivedPrefixedEvent, `received ${prefixedType} event`);
}, `${prefixedType} event listener should not trigger if an unprefixed ` +
`event handler also exists`);
promise_test(async t => {
// We use a parent/child relationship to be able to register both prefixed
// and unprefixed event listeners without the deduplication logic kicking in.
const parent = createDiv(t);
const child = createDiv(t);
parent.appendChild(child);
// After moving the child, we have to clean style again.
getComputedStyle(child).transition;
getComputedStyle(child).width;
let observedUnprefixedType;
addTestScopedEventListener(t, parent, unprefixedType, e => {
observedUnprefixedType = e.type;
});
let observedPrefixedType;
addTestScopedEventListener(t, child, prefixedType, e => {
observedPrefixedType = e.type;
});
triggerAnimation(child);
await waitForEventThenAnimationFrame(t, unprefixedHandler);
assert_equals(observedUnprefixedType, unprefixedType);
assert_equals(observedPrefixedType, prefixedType);
}, `event types for prefixed and unprefixed ${unprefixedType} event ` +
`listeners should be named appropriately`);
promise_test(async t => {
const div = createDiv(t);
let receivedEvent = false;
addTestScopedEventListener(t, div, prefixedType.toLowerCase(), () => {
receivedEvent = true;
});
addTestScopedEventListener(t, div, prefixedType.toUpperCase(), () => {
receivedEvent = true;
});
triggerAnimation(div);
await waitForEventThenAnimationFrame(t, unprefixedHandler);
assert_false(receivedEvent, `received ${prefixedType} event`);
}, `${prefixedType} event listener is case sensitive`);
}
// Below are utility functions.
// Creates a div element, appends it to the document body and removes the
// created element during test cleanup.
function createDiv(test) {
const element = document.createElement('div');
element.classList.add('baseStyle');
document.body.appendChild(element);
test.add_cleanup(() => {
element.remove();
});
// Flush style before returning. Some browsers only do partial style re-calc,
// so ask for all important properties to make sure they are applied.
getComputedStyle(element).transition;
getComputedStyle(element).width;
return element;
}
// Adds an event handler for |handlerName| (calling |callback|) to the given
// |target|, that will automatically be cleaned up at the end of the test.
function addTestScopedEventHandler(test, target, handlerName, callback) {
assert_regexp_match(
handlerName, /^on/, 'Event handler names must start with "on"');
assert_equals(target[handlerName], null,
`${handlerName} must be supported and not previously set`);
target[handlerName] = callback;
// We need this cleaned up even if the event handler doesn't run.
test.add_cleanup(() => {
if (target[handlerName])
target[handlerName] = null;
});
}
// Adds an event listener for |type| (calling |callback|) to the given
// |target|, that will automatically be cleaned up at the end of the test.
function addTestScopedEventListener(test, target, type, callback) {
target.addEventListener(type, callback);
// We need this cleaned up even if the event handler doesn't run.
test.add_cleanup(() => {
target.removeEventListener(type, callback);
});
}
// Returns a promise that will resolve once the passed event (|eventName|) has
// triggered and one more animation frame has happened. Automatically chooses
// between an event handler or event listener based on whether |eventName|
// begins with 'on'.
//
// We always listen on window as we don't want to interfere with the test via
// triggering the prefixed event deduplication logic.
function waitForEventThenAnimationFrame(test, eventName) {
return new Promise((resolve, _) => {
const eventFunc = eventName.startsWith('on')
? addTestScopedEventHandler : addTestScopedEventListener;
eventFunc(test, window, eventName, () => {
// rAF once to give the event under test time to come through.
requestAnimationFrame(resolve);
});
});
}
|