-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtest-chunk.js
422 lines (376 loc) · 12 KB
/
test-chunk.js
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
activateChunkingForTesting,
chunkInstanceForTesting,
deactivateChunking,
onIdle,
startupChunk,
} from '../../src/chunk';
import {installDocService} from '../../src/service/ampdoc-impl';
import {Services} from '../../src/services';
import * as sinon from 'sinon';
describe('chunk', () => {
beforeEach(() => {
activateChunkingForTesting();
});
const resolvingIdleCallbackWithTimeRemaining = timeRemaining => fn => {
Promise.resolve({
timeRemaining: () => timeRemaining,
}).then(fn);
};
function basicTests(env) {
let fakeWin;
beforeEach(() => {
fakeWin = env.win;
// If there is a viewer, wait for it, so we run with it being
// installed.
if (env.win.services.viewer) {
return Services.viewerPromiseForDoc(env.win.document).then(() => {
// Make sure we make a chunk instance, so all runs
// have a viewer.
chunkInstanceForTesting(env.win.document);
});
}
});
it('should execute a chunk', done => {
startupChunk(fakeWin.document, unusedIdleDeadline => {
done();
});
});
it('should execute chunks', () => {
let count = 0;
let progress = '';
return new Promise(resolve => {
function complete(str) {
return function(unusedIdleDeadline) {
progress += str;
if (++count == 6) {
resolve();
}
};
}
startupChunk(fakeWin.document, complete('a'));
startupChunk(fakeWin.document, complete('b'));
startupChunk(fakeWin.document, function() {
complete('c')();
startupChunk(fakeWin.document, function() {
complete('d')();
startupChunk(fakeWin.document, complete('e'));
startupChunk(fakeWin.document, complete('f'));
});
});
}).then(() => {
expect(progress).to.equal('abcdef');
});
});
}
describes.fakeWin('visible no amp', {
amp: false,
}, env => {
beforeEach(() => {
installDocService(env.win, /* isSingleDoc */ true);
expect(env.win.services.viewer).to.be.undefined;
env.win.document.hidden = false;
});
basicTests(env);
});
describes.fakeWin('invisible no amp', {
amp: false,
}, env => {
beforeEach(() => {
installDocService(env.win, /* isSingleDoc */ true);
expect(env.win.services.viewer).to.be.undefined;
env.win.document.hidden = true;
env.win.requestIdleCallback = function() {
throw new Error('Should not be called');
};
env.win.postMessage = function(data, targetOrigin) {
expect(targetOrigin).to.equal('*');
Promise.resolve().then(() => {
const event = {
data,
type: 'message',
};
env.win.eventListeners.fire(event);
});
};
});
basicTests(env);
});
describes.fakeWin('with viewer', {
amp: true,
}, env => {
beforeEach(() => {
expect(env.win.services.viewer).to.exist;
env.win.document.hidden = false;
});
describe('visible', () => {
beforeEach(() => {
const viewer = Services.viewerForDoc(env.win.document);
env.sandbox.stub(viewer, 'isVisible').callsFake(() => {
return true;
});
});
basicTests(env);
});
describe.configure().skip(() => !('onunhandledrejection' in window))
.run('error handling', () => {
let fakeWin;
let done;
function onReject(event) {
expect(event.reason.message).to.match(/test async/);
done();
}
beforeEach(() => {
fakeWin = env.win;
const viewer = Services.viewerForDoc(env.win.document);
env.sandbox.stub(viewer, 'isVisible').callsFake(() => {
return true;
});
window.addEventListener('unhandledrejection', onReject);
});
afterEach(() => {
window.removeEventListener('unhandledrejection', onReject);
});
it('should proceed on error and rethrowAsync', d => {
startupChunk(fakeWin.document, () => {
throw new Error('test async');
});
startupChunk(fakeWin.document, () => {
done = d;
});
});
});
describe('invisible', () => {
beforeEach(() => {
const viewer = Services.viewerForDoc(env.win.document);
env.sandbox.stub(viewer, 'isVisible').callsFake(() => {
return false;
});
env.win.requestIdleCallback =
resolvingIdleCallbackWithTimeRemaining(15);
const chunks = chunkInstanceForTesting(env.win.document);
env.sandbox.stub(chunks, 'executeAsap_').callsFake(() => {
throw new Error('No calls expected: executeAsap_');
});
env.win.location.resetHref('test#visibilityState=hidden');
});
basicTests(env);
});
describe('invisible but deactivated', () => {
beforeEach(() => {
deactivateChunking();
const viewer = Services.viewerForDoc(env.win.document);
env.sandbox.stub(viewer, 'isVisible').callsFake(() => {
return false;
});
env.win.requestIdleCallback = () => {
throw new Error('No calls expected');
};
env.win.location.resetHref('test#visibilityState=hidden');
});
basicTests(env);
});
describe('invisible via document.hidden', () => {
beforeEach(() => {
const viewer = Services.viewerForDoc(env.win.document);
env.sandbox.stub(viewer, 'isVisible').callsFake(() => {
return false;
});
env.win.requestIdleCallback =
resolvingIdleCallbackWithTimeRemaining(15);
const chunks = chunkInstanceForTesting(env.win.document);
env.sandbox.stub(chunks, 'executeAsap_').callsFake(() => {
throw new Error('No calls expected: executeAsap_');
});
env.win.document.hidden = true;
});
basicTests(env);
});
describe('invisible to visible', () => {
beforeEach(() => {
env.win.location.resetHref('test#visibilityState=hidden');
const viewer = Services.viewerForDoc(env.win.document);
let visible = false;
env.sandbox.stub(viewer, 'isVisible').callsFake(() => {
return visible;
});
env.win.requestIdleCallback = () => {
// Don't call the callback, but transition to visible
visible = true;
viewer.onVisibilityChange_();
};
});
basicTests(env);
});
describe('invisible to visible', () => {
beforeEach(() => {
env.win.location.resetHref('test#visibilityState=prerender');
const viewer = Services.viewerForDoc(env.win.document);
let visible = false;
env.sandbox.stub(viewer, 'isVisible').callsFake(() => {
return visible;
});
env.win.requestIdleCallback = () => {
// Don't call the callback, but transition to visible
visible = true;
viewer.onVisibilityChange_();
};
});
basicTests(env);
});
describe('invisible to visible after a while', () => {
beforeEach(() => {
env.win.location.resetHref('test#visibilityState=hidden');
const viewer = Services.viewerForDoc(env.win.document);
let visible = false;
env.sandbox.stub(viewer, 'isVisible').callsFake(() => {
return visible;
});
env.win.requestIdleCallback = () => {
// Don't call the callback, but transition to visible
setTimeout(() => {
visible = true;
viewer.onVisibilityChange_();
}, 10);
};
});
basicTests(env);
});
});
describes.realWin('realWin', {
amp: true,
}, env => {
beforeEach(() => {
Object.defineProperty(env.win.document, 'hidden', {
get: () => false,
});
});
basicTests(env);
});
describes.realWin('realWin noIdleCallback', {
amp: true,
}, env => {
beforeEach(() => {
env.win.requestIdleCallback = null;
expect(env.win.requestIdleCallback).to.be.null;
const viewer = Services.viewerForDoc(env.win.document);
env.sandbox.stub(viewer, 'isVisible').callsFake(() => {
return false;
});
Object.defineProperty(env.win.document, 'hidden', {
get: () => false,
});
});
basicTests(env);
});
});
describe('onIdle', () => {
let win;
let calls;
let callbackCalled;
let sandbox;
let clock;
beforeEach(() => {
sandbox = sinon.sandbox.create();
clock = sandbox.useFakeTimers();
calls = [];
callbackCalled = false;
win = {
requestIdleCallback: (fn, options) => {
calls.push({
invoke: (timeRemaining, didTimeout) => {
fn({
timeRemaining: () => timeRemaining,
didTimeout: !!didTimeout,
});
},
options,
});
},
};
});
afterEach(() => {
sandbox.restore();
});
function markCalled() {
callbackCalled = true;
}
it('should fire for sufficient remaining time', () => {
onIdle(win, 66, 1000, markCalled);
expect(calls).to.have.length(1);
expect(callbackCalled).to.be.false;
expect(calls[0].options.timeout).to.equal(1000);
calls[0].invoke(66);
expect(callbackCalled).to.be.true;
expect(calls).to.have.length(1);
});
it('should try again with not enough time', () => {
onIdle(win, 66, 1000, markCalled);
expect(calls).to.have.length(1);
expect(callbackCalled).to.be.false;
expect(calls[0].options.timeout).to.equal(1000);
clock.tick(100);
calls[0].invoke(65);
expect(callbackCalled).to.be.false;
expect(calls).to.have.length(2);
expect(calls[1].options.timeout).to.equal(900);
calls[1].invoke(66);
expect(callbackCalled).to.be.true;
expect(calls).to.have.length(2);
});
it('should try again with not enough time (2 recursions)', () => {
onIdle(win, 66, 1000, markCalled);
expect(calls).to.have.length(1);
expect(callbackCalled).to.be.false;
expect(calls[0].options.timeout).to.equal(1000);
clock.tick(100);
calls[0].invoke(65);
expect(callbackCalled).to.be.false;
expect(calls).to.have.length(2);
expect(calls[1].options.timeout).to.equal(900);
clock.tick(50);
calls[1].invoke(0);
expect(callbackCalled).to.be.false;
expect(calls).to.have.length(3);
expect(calls[2].options.timeout).to.equal(850);
calls[2].invoke(66);
expect(callbackCalled).to.be.true;
expect(calls).to.have.length(3);
});
it('should timeout when callback is called after timeout', () => {
onIdle(win, 66, 1000, markCalled);
expect(calls).to.have.length(1);
expect(callbackCalled).to.be.false;
expect(calls[0].options.timeout).to.equal(1000);
clock.tick(1000);
// Not enough time remaining but timed out via time.
calls[0].invoke(1);
expect(callbackCalled).to.be.true;
expect(calls).to.have.length(1);
});
it('should timeout when callback is called with didTimeout', () => {
onIdle(win, 66, 1000, markCalled);
expect(calls).to.have.length(1);
expect(callbackCalled).to.be.false;
expect(calls[0].options.timeout).to.equal(1000);
// Not enough time remaining but timed out via didTimeout.
calls[0].invoke(1, /* didTimeout */ true);
expect(callbackCalled).to.be.true;
expect(calls).to.have.length(1);
});
});