-
Notifications
You must be signed in to change notification settings - Fork 36
/
test.html
216 lines (173 loc) · 5.63 KB
/
test.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="shell.css">
<title>event-loop test</title>
</head>
<body style="height: 2000px;">
<button id="testButton" onclick="startTest()" style="font-size:xx-large">Redo test</script>
<div style="overflow:hidden">
<div id="slider"></div>
</div>
<div id="mutator"></div>
<script>
'use strict';
var RESULTS = [];
var TIMED_RESULTS = [];
function addResultToDocument(msg, type) {
var el = document.createElement('div');
if (type)
el.classList.add(type);
el.classList.add('result');
el.innerText = msg;
document.body.appendChild(el);
}
function reportError(msg) {
addResultToDocument(msg, 'error');
}
let startTime = 0;
function log(/*...args*/) {
var line = "";
for (var i=0; i<arguments.length; i++)
line += (i != 0 ? ' ' : '') + arguments[i];
if (startTime == 0)
startTime = performance.now();
var tick = Math.round((performance.now()-startTime)*100)/100;
console.log(tick + ' ' + line);
RESULTS.push(line);
TIMED_RESULTS.push( { event: line, time: tick});
}
function assertOrder(r1, r2, msg) {
var i1 = RESULTS.indexOf(r1);
var i2 = RESULTS.indexOf(r2);
if (i1 == -1)
reportError(r1 + " not found");
if (i2 == -1)
reportError(r2 + " not found");
if (i2 <= i1)
reportError('"' + r1 + '" before "' + r2 + '": ' + msg);
}
function endTest() {
// Promise/rAF asserts
assertOrder("script end", "promise A", "promise handlers executed after script");
assertOrder("promise A", "promise B", "promise handlers in order");
assertOrder("rAF A", "rAF B", "rAF in order");
assertOrder("promise A", "rAF A", "rAF after promise handlers");
assertOrder("promise A", "timeout A", "timeout after promise handlers");
assertOrder("rAF A promise", "rAF B", "promise handler immediately after rAF");
var p1 = RESULTS.indexOf('promise B'), p0 = RESULTS.indexOf('promise A');
if ((p1 - p0) > 2)
reportError("Microtasks queue not executed all at once, distance is " + (p1 - p0) );
assertOrder("promise A", "mutate", "mutate microtask after promise A")
assertOrder("mutate", "promise B", "mutate happens before promise B resolves");
// render events
assertOrder("matchMedia", "resize", "matchMedia before resize");
assertOrder("resize", "scroll", "resize before scroll");
assertOrder("scroll", "rAF A", "scroll before rAF");
assertOrder("animationstart", "rAF A", "css animation starts before rAF");
assertOrder("resize", "animationstart", "css animation starts before rAF");
// addResultToDocument("rAF and timeout order is unspecified", "warn");
if (!document.querySelector('.error'))
addResultToDocument('tests passed');
document.querySelector('#slider').classList.remove('animate');
addResultToDocument('output in console');
}
function startTest() {
RESULTS = [];
log("script start");
var oldResults = document.querySelectorAll('.result');
for (var i=0; i<oldResults.length; i++)
oldResults[i].parentNode.removeChild(oldResults[i]);
// for (var el of document.querySelectorAll('.result'))
// el.parentNode.removeChild(el);
// rAF
window.requestAnimationFrame( function() {
log("rAF A");
if (window.ResizeObserver) {
var ro = new ResizeObserver(function(entries) {
log("ResizeObserver raf");
});
ro.observe(document.querySelector('#mutator'));
}
new Promise( function(fulfill, reject) {
fulfill();
})
.then( function() {
log("rAF A promise");
});
});
window.requestAnimationFrame( function() {
log("rAF B");
});
// timeouts
window.setTimeout(function() { log('timeout A');}, 0);
window.setTimeout(function() { log('timeout B');}, 0);
// Promises
Promise.resolve()
.then(function() {
log("promise A");
return new Promise( function(fulfill) {
fulfill();
});
})
.then(function() {
log("promise B");
});
// CSS Animation
document.querySelector('#slider').classList.remove('animate');
document.querySelector('#slider').classList.add('animate');
// Scrolling
window.scrollBy(0,10);
// Resize
try {
window.parent.document.querySelector('iframe').width = "450px";
}
catch(ex) {
console.error('resize not tested, not an iframe');
}
// ResizeObserver
if (window.ResizeObserver) {
var ro = new ResizeObserver(function(entries) {
log("ResizeObserver script");
});
ro.observe(document.querySelector('#mutator'));
}
// Mutations
document.querySelector('#mutator').appendChild(
document.createElement('div')
);
// Test end
window.setTimeout(function() { endTest(); }, 500);
log("script end");
}
// Listeners
window.addEventListener('resize', function() {
log('resize');
});
window.addEventListener('scroll', function() {
log('scroll');
});
window.matchMedia("(min-width: 400px)").addListener( function() {
log('matchMedia');
});
document.querySelector('#slider').addEventListener('animationstart', function() {
log('animationstart');
});
var mutObs = new MutationObserver( function(entries) {
log('mutate');
});
mutObs.observe(document.querySelector('#mutator'),
{ childList: true, attributes: true });
window.addEventListener('message', function(ev) {
switch(ev.data) {
case 'startTest':
window.requestAnimationFrame(startTest);
break;
default:
break;
}
});
</script>
</body>
</html>