/
test-viewport-binding.js
546 lines (467 loc) · 19.1 KB
/
test-viewport-binding.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/**
* Copyright 2017 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 {Services} from '../../src/services';
import {ViewportBindingIosEmbedWrapper_} from '../../src/service/viewport/viewport-binding-ios-embed-wrapper';
import {ViewportBindingNatural_} from '../../src/service/viewport/viewport-binding-natural';
import {installDocService} from '../../src/service/ampdoc-impl';
import {installPlatformService} from '../../src/service/platform-impl';
import {installVsyncService} from '../../src/service/vsync-impl';
import {toggleExperiment} from '../../src/experiments';
import {whenDocumentReady} from '../../src/document-ready';
describes.realWin('ViewportBindingNatural', {ampCss: true}, env => {
let binding;
let win;
let ampdoc;
let child;
beforeEach(() => {
env.iframe.style.width = '100px';
env.iframe.style.height = '200px';
win = env.win;
win.document.documentElement.classList.add('i-amphtml-singledoc');
child = win.document.createElement('div');
child.style.width = '200px';
child.style.height = '300px';
win.document.body.appendChild(child);
installPlatformService(win);
installVsyncService(win);
installDocService(win, /* isSingleDoc */ true);
ampdoc = Services.ampdocServiceFor(win).getSingleDoc();
binding = new ViewportBindingNatural_(ampdoc);
binding.connect();
});
it('should setup overflow:visible on body', () => {
expect(win.getComputedStyle(win.document.body).overflow).to.equal(
'visible'
);
});
it('should configure body as relative', () => {
binding = new ViewportBindingNatural_(ampdoc);
expect(win.document.body.style.display).to.not.be.ok;
const bodyStyles = win.getComputedStyle(win.document.body);
expect(bodyStyles.position).to.equal('relative');
// It's important that this experiment does NOT override the previously
// set `overflow`.
expect(bodyStyles.overflow).to.equal('visible');
});
it('should override body overflow for iOS webview', () => {
win.document.documentElement.classList.add('i-amphtml-webview');
binding = new ViewportBindingNatural_(ampdoc);
const bodyStyles = win.getComputedStyle(win.document.body);
expect(bodyStyles.position).to.equal('relative');
expect(bodyStyles.overflowX).to.equal('hidden');
expect(bodyStyles.overflowY).to.not.equal('hidden');
});
it('should NOT require fixed layer transferring', () => {
expect(binding.requiresFixedLayerTransfer()).to.be.false;
});
it('should NOT require override of the global scrollTo', () => {
expect(binding.overrideGlobalScrollTo()).to.be.false;
});
it('should connect events: subscribe to scroll and resize events', () => {
expect(win.eventListeners.count('resize')).to.equal(1);
expect(win.eventListeners.count('scroll')).to.equal(1);
});
it('should disconnect events', () => {
// After disconnect, there are no more listeners on window.
binding.disconnect();
expect(win.eventListeners.count('resize')).to.equal(0);
expect(win.eventListeners.count('scroll')).to.equal(0);
});
it('should update padding', () => {
binding.updatePaddingTop(31);
expect(win.document.documentElement.style.paddingTop).to.equal('31px');
});
// TODO(zhouyx, #11827): Make this test work on Safari.
it.configure()
.skipSafari()
.run('should calculate size', () => {
const size = binding.getSize();
expect(size.width).to.equal(100);
expect(size.height).to.equal(200);
});
it('should calculate scrollTop from scrollElement', () => {
win.pageYOffset = 11;
win.document.scrollingElement.scrollTop = 17;
expect(binding.getScrollTop()).to.equal(17);
});
it('should calculate scrollWidth from scrollElement', () => {
expect(binding.getScrollWidth()).to.equal(200);
});
it('should calculate scrollHeight from scrollElement', () => {
expect(binding.getScrollHeight()).to.equal(300);
});
it('should calculate contentHeight from body height', () => {
// Set content to be smaller than viewport.
child.style.height = '50px';
expect(binding.getContentHeight()).to.equal(50);
});
it('should include padding top in contentHeight', () => {
binding.updatePaddingTop(10);
binding.setScrollTop(20); // should have no effect on height
expect(binding.getContentHeight()).to.equal(310);
});
it('should account for child margin-top', () => {
child.style.marginTop = '15px';
expect(binding.getContentHeight()).to.equal(315);
});
it('should account for child margin-top (WebKit)', () => {
env.sandbox.stub(win.document, 'scrollingElement').value(null);
env.sandbox.stub(binding.platform_, 'isWebKit').returns(true);
child.style.marginTop = '15px';
expect(binding.getContentHeight()).to.equal(315);
});
it('should update scrollTop on scrollElement', () => {
win.pageYOffset = 11;
win.document.scrollingElement.scrollTop = 17;
binding.setScrollTop(21);
expect(win.document.scrollingElement./*OK*/ scrollTop).to.equal(21);
});
it('should fallback scrollTop to pageYOffset', () => {
win.pageYOffset = 11;
win.document.scrollingElement.scrollTop = 0;
expect(binding.getScrollTop()).to.equal(11);
});
it('should offset client rect for layout', () => {
win.pageXOffset = 0;
win.pageYOffset = 200;
win.document.scrollingElement.scrollTop = 0;
const el = {
getBoundingClientRect: () => {
return {left: 11.5, top: 12.5, width: 13.5, height: 14.5};
},
};
const rect = binding.getLayoutRect(el);
expect(rect.left).to.equal(12); // round(0 + 11.5)
expect(rect.top).to.equal(213); // round(200 + 12.5)
expect(rect.width).to.equal(14); // round(13.5)
expect(rect.height).to.equal(15); // round(14.5)
});
it('should offset client rect for layout and position passed in', () => {
win.pageXOffset = 0;
win.pageYOffset = 2000;
win.document.scrollingElement.scrollTop = 0;
const el = {
getBoundingClientRect: () => {
return {left: 11.5, top: 12.5, width: 13.5, height: 14.5};
},
};
const rect = binding.getLayoutRect(el, 100, 200);
expect(rect.left).to.equal(112); // round(100 + 11.5)
expect(rect.top).to.equal(213); // round(200 + 12.5)
expect(rect.width).to.equal(14); // round(13.5)
expect(rect.height).to.equal(15); // round(14.5)
});
it('should disable scroll temporarily and reset scroll', () => {
let htmlCss = win.getComputedStyle(win.document.documentElement);
expect(htmlCss.overflowX).to.equal('hidden');
expect(htmlCss.overflowY).to.equal('auto');
binding.disableScroll();
expect(win.document.documentElement).to.have.class(
'i-amphtml-scroll-disabled'
);
htmlCss = win.getComputedStyle(win.document.documentElement);
expect(htmlCss.overflowX).to.equal('hidden');
expect(htmlCss.overflowY).to.equal('hidden');
binding.resetScroll();
expect(win.document.documentElement).to.not.have.class(
'i-amphtml-scroll-disabled'
);
htmlCss = win.getComputedStyle(win.document.documentElement);
expect(htmlCss.overflowX).to.equal('hidden');
expect(htmlCss.overflowY).to.equal('auto');
});
});
describes.realWin('ViewportBindingNatural on iOS', {ampCss: true}, env => {
let binding;
let win;
let ampdoc;
let child;
beforeEach(() => {
env.iframe.style.width = '100px';
env.iframe.style.height = '200px';
win = env.win;
win.document.documentElement.classList.add('i-amphtml-singledoc');
child = win.document.createElement('div');
child.style.width = '200px';
child.style.height = '300px';
win.document.body.appendChild(child);
installPlatformService(win);
installVsyncService(win);
installDocService(win, /* isSingleDoc */ true);
ampdoc = Services.ampdocServiceFor(win).getSingleDoc();
env.sandbox.stub(Services.platformFor(win), 'isIos').returns(true);
binding = new ViewportBindingNatural_(ampdoc);
binding.connect();
});
it('should reset overscroll on X-axis', () => {
win.scrollTo(1, 0);
expect(win.pageXOffset).to.equal(1);
return new Promise(resolve => {
win.addEventListener('scroll', () => {
if (win.pageXOffset == 0) {
resolve();
}
});
});
});
});
describes.realWin('ViewportBindingIosEmbedWrapper', {ampCss: true}, env => {
let win;
let binding;
let vsync;
let child;
beforeEach(() => {
env.iframe.style.width = '100px';
env.iframe.style.height = '100px';
win = env.win;
win.document.documentElement.className = 'top i-amphtml-singledoc';
child = win.document.createElement('div');
child.style.width = '200px';
child.style.height = '300px';
child.textContent = 'test';
win.document.body.appendChild(child);
installDocService(win, /* isSingleDoc */ true);
installVsyncService(win);
installPlatformService(win);
vsync = Services.vsyncFor(win);
binding = new ViewportBindingIosEmbedWrapper_(win);
binding.connect();
});
it('should NOT setup body min-height w/o experiment', () => {
const style = win.getComputedStyle(win.document.body);
expect(style.minHeight).to.equal('0px');
});
it('should require override of the global scrollTo', () => {
expect(binding.overrideGlobalScrollTo()).to.be.true;
});
// TODO(#22220): Remove when "ios-fixed-no-transfer" experiment is cleaned up.
it('should require fixed layer transferring', () => {
expect(binding.requiresFixedLayerTransfer()).to.be.true;
});
// TODO(#22220): Remove when "ios-fixed-no-transfer" experiment is cleaned up.
it('should require fixed layer transferring for later iOS w/o experiment', () => {
env.sandbox
.stub(Services.platformFor(win), 'getIosVersionString')
.callsFake(() => '12.2');
expect(binding.requiresFixedLayerTransfer()).to.be.true;
});
it('should configure fixed layer transferring based on iOS version', () => {
toggleExperiment(win, 'ios-fixed-no-transfer');
let version;
env.sandbox
.stub(Services.platformFor(win), 'getIosVersionString')
.callsFake(() => version);
// 12.1 is still out.
version = '12.1';
expect(binding.requiresFixedLayerTransfer()).to.be.true;
// 12.2 and up are fixed.
version = '12.2';
expect(binding.requiresFixedLayerTransfer()).to.be.false;
version = '12.3';
expect(binding.requiresFixedLayerTransfer()).to.be.false;
version = '13.0';
expect(binding.requiresFixedLayerTransfer()).to.be.false;
});
it('should start w/o overscroll and set it on doc ready', () => {
const root = win.document.documentElement;
expect(root).to.not.have.class('i-amphtml-ios-overscroll');
expect(root.style.webkitOverflowScrolling).to.not.equal('touch');
expect(binding.wrapper_.style.webkitOverflowScrolling).to.not.equal(
'touch'
);
return whenDocumentReady(win.document).then(() => {
expect(root).to.have.class('i-amphtml-ios-overscroll');
});
});
it('should have UI setup', () => {
expect(binding.setupDone_).to.be.true;
expect(win.document.documentElement).to.have.class('i-amphtml-ios-embed');
expect(win.document.body).to.exist;
expect(win.document.body.parentNode).to.not.equal(
win.document.documentElement
);
expect(win.document.body.parentNode).to.equal(binding.wrapper_);
expect(binding.wrapper_.parentNode).to.equal(win.document.documentElement);
expect(binding.wrapper_.tagName).to.equal('HTML');
expect(binding.wrapper_.id).to.equal('i-amphtml-wrapper');
expect(win.document.body.contains(child)).to.be.true;
expect(binding.wrapper_.contains(child)).to.be.true;
expect(win.document.contains(child)).to.be.true;
expect(child.textContent).to.equal('test');
// Top-level classes moved to the wrapper element.
expect(win.document.documentElement).to.have.class('top');
expect(binding.wrapper_).to.have.class('top');
});
it.configure()
.skipFirefox()
.run('should have CSS setup', () => {
win.document.body.style.display = 'table';
const htmlCss = win.getComputedStyle(win.document.documentElement);
const wrapperCss = win.getComputedStyle(binding.wrapper_);
const bodyCss = win.getComputedStyle(win.document.body);
// `<html>` must have `position: static` or layout is broken.
expect(htmlCss.position).to.equal('static');
// `<html>` and `<i-amphtml-wrapper>` must be scrollable, but not `body`.
// Unfortunately, we can't test here `-webkit-overflow-scrolling`.
expect(htmlCss.overflowY).to.equal('auto');
expect(htmlCss.overflowX).to.equal('hidden');
expect(wrapperCss.overflowY).to.equal('auto');
expect(wrapperCss.overflowX).to.equal('hidden');
expect(bodyCss.overflowY).to.equal('visible');
expect(bodyCss.overflowX).to.equal('visible');
// Wrapper must be a block and positioned absolute at 0/0/0/0.
expect(wrapperCss.display).to.equal('block');
expect(wrapperCss.position).to.equal('absolute');
expect(wrapperCss.top).to.equal('0px');
expect(wrapperCss.left).to.equal('0px');
expect(wrapperCss.right).to.equal('0px');
expect(wrapperCss.bottom).to.equal('0px');
expect(wrapperCss.margin).to.equal('0px');
// `body` must have `relative` positioning and `block` display.
expect(bodyCss.position).to.equal('relative');
// Preserve the customized `display` value.
expect(bodyCss.display).to.equal('table');
// `body` must have a 1px transparent border for two purposes:
// (1) to cancel out margin collapse in body's children;
// (2) to offset scroll adjustment to 1 to avoid scroll freeze problem.
expect(
bodyCss.borderTop.replace('rgba(0, 0, 0, 0)', 'transparent')
).to.equal('1px solid transparent');
expect(bodyCss.margin).to.equal('0px');
});
it('should be immediately scrolled to 1 to avoid freeze', () => {
expect(binding.wrapper_.scrollTop).to.equal(1);
});
it('should connect events: subscribe to scroll and resize events', () => {
expect(win.eventListeners.count('resize')).to.equal(1);
// Note that scroll event is on the wrapper, and NOT on root or body.
expect(win.eventListeners.count('scroll')).to.equal(0);
expect(win.document.eventListeners.count('scroll')).to.equal(0);
expect(
win.document.documentElement.eventListeners.count('scroll')
).to.equal(0);
expect(win.document.body.eventListeners.count('scroll')).to.equal(0);
});
it('should disconnect events', () => {
// After disconnect, there are no more listeners on window.
binding.disconnect();
expect(win.eventListeners.count('resize')).to.equal(0);
});
it('should update padding', () => {
binding.updatePaddingTop(31);
expect(binding.wrapper_.style.paddingTop).to.equal('31px');
// Notice, root is not touched.
expect(win.document.documentElement.style.paddingTop).to.equal('');
});
// TODO(zhouyx, #11827): Make this test work on Safari.
it.configure()
.skipSafari()
.run('should calculate size', () => {
const size = binding.getSize();
expect(size.width).to.equal(100);
expect(size.height).to.equal(100);
});
it('should calculate scrollTop from wrapper', () => {
binding.wrapper_.scrollTop = 17;
expect(binding.getScrollTop()).to.equal(17);
});
it('should calculate scrollWidth from wrapper', () => {
expect(binding.getScrollWidth()).to.equal(200);
});
it('should calculate scrollHeight from wrapper', () => {
expect(binding.getScrollHeight()).to.equal(301); // +1px for border-top.
});
it('should calculate contentHeight from body height', () => {
// Set content to be smaller than viewport.
child.style.height = '50px';
expect(binding.getContentHeight()).to.equal(51);
});
it('should include padding top in contentHeight', () => {
binding.updatePaddingTop(10);
binding.setScrollTop(20); // should have no effect on height
expect(binding.getContentHeight()).to.equal(311); // +1px for border-top.
});
it('should account for child margin-top', () => {
child.style.marginTop = '15px';
expect(binding.getContentHeight()).to.equal(316); // +1px for border-top.
});
it('should update scrollTop on wrapper', () => {
binding.setScrollTop(21);
expect(binding.wrapper_.scrollTop).to.equal(21);
});
it('should adjust scrollTop to avoid scroll freeze', () => {
binding.setScrollTop(21);
expect(binding.wrapper_.scrollTop).to.equal(21);
// `scrollTop=0` is normally not allowed.
binding.setScrollTop(0);
expect(binding.wrapper_.scrollTop).to.equal(1);
});
it('should offset client rect for layout', () => {
binding.wrapper_.scrollTop = 200;
const el = {
getBoundingClientRect: () => {
return {left: 11.5, top: 12.5, width: 13.5, height: 14.5};
},
};
const rect = binding.getLayoutRect(el);
expect(rect.top).to.equal(213); // round(200 + 12.5)
expect(rect.width).to.equal(14); // round(13.5)
expect(rect.height).to.equal(15); // round(14.5)
});
it('should offset client rect for layout and position passed in', () => {
binding.wrapper_.scrollTop = 10;
const el = {
getBoundingClientRect: () => {
return {left: 11.5, top: 12.5, width: 13.5, height: 14.5};
},
};
const rect = binding.getLayoutRect(el, 100, 200);
expect(rect.left).to.equal(112); // round(100 + 11.5)
expect(rect.top).to.equal(213); // round(200 + 12.5)
expect(rect.width).to.equal(14); // round(13.5)
expect(rect.height).to.equal(15); // round(14.5)
});
it('should call scroll event', () => {
return new Promise(resolve => {
binding.onScroll(resolve);
binding.wrapper_.scrollTop = 11;
}).then(() => {
expect(binding.getScrollTop()).to.equal(11);
});
});
it('should disable scroll temporarily and reset scroll', () => {
let wrapperCss = win.getComputedStyle(binding.wrapper_);
expect(wrapperCss.overflowX).to.equal('hidden');
expect(wrapperCss.overflowY).to.equal('auto');
binding.disableScroll();
expect(binding.wrapper_).to.have.class('i-amphtml-scroll-disabled');
wrapperCss = win.getComputedStyle(binding.wrapper_);
expect(wrapperCss.overflowX).to.equal('hidden');
expect(wrapperCss.overflowY).to.equal('hidden');
binding.resetScroll();
expect(binding.wrapper_).to.not.have.class('i-amphtml-scroll-disabled');
wrapperCss = win.getComputedStyle(binding.wrapper_);
expect(wrapperCss.overflowX).to.equal('hidden');
expect(wrapperCss.overflowY).to.equal('auto');
});
it('should NOT refresh overscroll w/o experiment', () => {
binding.contentHeightChanged();
const root = win.document.documentElement;
return vsync.mutatePromise().then(() => {
expect(root).to.have.class('i-amphtml-ios-overscroll');
});
});
});