forked from ampproject/amphtml
/
test-resource.js
1706 lines (1421 loc) · 59.3 KB
/
test-resource.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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 {AmpDocSingle} from '../../src/service/ampdoc-impl';
import {Resources} from '../../src/service/resources-impl';
import {Resource, ResourceState} from '../../src/service/resource';
import {layoutRectLtwh} from '../../src/layout-rect';
import * as sinon from 'sinon';
describe('Resource', () => {
let sandbox;
let element;
let elementMock;
let resources;
let resource;
let viewportMock;
beforeEach(() => {
sandbox = sinon.sandbox.create();
element = {
ownerDocument: {defaultView: window},
tagName: 'AMP-AD',
style: {},
isBuilt: () => false,
isUpgraded: () => false,
prerenderAllowed: () => false,
renderOutsideViewport: () => true,
build: () => false,
getBoundingClientRect: () => null,
updateLayoutBox: () => {},
isRelayoutNeeded: () => false,
layoutCallback: () => {},
changeSize: () => {},
unlayoutOnPause: () => false,
unlayoutCallback: () => true,
pauseCallback: () => false,
resumeCallback: () => false,
viewportCallback: () => {},
togglePlaceholder: () => sandbox.spy(),
getPriority: () => 2,
dispatchCustomEvent: () => {},
};
elementMock = sandbox.mock(element);
resources = new Resources(new AmpDocSingle(window));
resource = new Resource(1, element, resources);
viewportMock = sandbox.mock(resources.viewport_);
});
afterEach(() => {
viewportMock.verify();
elementMock.verify();
sandbox.restore();
});
it('should initialize correctly', () => {
expect(resource.getId()).to.equal(1);
expect(resource.debugid).to.equal('amp-ad#1');
expect(resource.getPriority()).to.equal(2);
expect(resource.getState()).to.equal(ResourceState.NOT_BUILT);
expect(resource.getLayoutBox().width).to.equal(0);
expect(resource.getLayoutBox().height).to.equal(0);
expect(resource.isInViewport()).to.equal(false);
});
it('should initialize correctly when already built', () => {
elementMock.expects('isBuilt').returns(true).once();
expect(new Resource(1, element).getState()).to.equal(
ResourceState.NOT_LAID_OUT);
});
it('should not build before upgraded', () => {
elementMock.expects('isUpgraded').returns(false).atLeast(1);
elementMock.expects('build').never();
resource.build();
expect(resource.getState()).to.equal(ResourceState.NOT_BUILT);
});
it('should build after upgraded', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('build').once();
resource.build();
expect(resource.getState()).to.equal(ResourceState.NOT_LAID_OUT);
});
it('should blacklist on build failure', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('build').throws('Failed').once();
resource.build();
expect(resource.blacklisted_).to.equal(true);
expect(resource.getState()).to.equal(ResourceState.NOT_BUILT);
});
it('should mark as ready for layout if already measured', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('build').once();
const stub = sandbox.stub(resource, 'hasBeenMeasured').returns(true);
resource.build(false);
expect(stub.calledOnce).to.be.true;
expect(resource.getState()).to.equal(ResourceState.READY_FOR_LAYOUT);
});
it('should mark as not laid out if not yet measured', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('build').once();
const stub = sandbox.stub(resource, 'hasBeenMeasured').returns(false);
resource.build(false);
expect(stub.calledOnce).to.be.true;
expect(resource.getState()).to.equal(ResourceState.NOT_LAID_OUT);
});
it('should allow to measure when not upgraded', () => {
elementMock.expects('isUpgraded').returns(false).atLeast(1);
const viewport = {
getLayoutRect() {
return layoutRectLtwh(0, 100, 300, 100);
},
isDeclaredFixed() {
return false;
},
};
resource.resources_ = {
win: window,
getViewport: () => viewport,
};
expect(() => {
resource.measure();
}).to.not.throw();
expect(resource.getLayoutBox()).to.eql(layoutRectLtwh(0, 100, 300, 100));
});
it('should allow measure even when not built', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect').returns(
layoutRectLtwh(0, 0, 0, 0)).once();
resource.measure();
expect(resource.getState()).to.equal(ResourceState.NOT_BUILT);
expect(resource.isFixed()).to.be.false;
});
it('should measure and update state', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('build').once();
resource.build();
elementMock.expects('getBoundingClientRect')
.returns({left: 11, top: 12, width: 111, height: 222})
.once();
elementMock.expects('updateLayoutBox')
.withExactArgs(sinon.match(data => {
return data.width == 111 && data.height == 222;
}))
.once();
resource.measure();
expect(resource.getState()).to.equal(ResourceState.READY_FOR_LAYOUT);
expect(resource.getLayoutBox().left).to.equal(11);
expect(resource.getLayoutBox().top).to.equal(12);
expect(resource.getLayoutBox().width).to.equal(111);
expect(resource.getLayoutBox().height).to.equal(222);
expect(resource.isFixed()).to.be.false;
});
it('should update initial box only on first measure', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('build').once();
resource.build();
element.getBoundingClientRect = () =>
({left: 11, top: 12, width: 111, height: 222});
resource.measure();
expect(resource.getLayoutBox().top).to.equal(12);
expect(resource.getInitialLayoutBox().top).to.equal(12);
element.getBoundingClientRect = () =>
({left: 11, top: 22, width: 111, height: 222});
resource.measure();
expect(resource.getLayoutBox().top).to.equal(22);
expect(resource.getInitialLayoutBox().top).to.equal(12);
});
it('should noop request measure when not built', () => {
expect(resource.isMeasureRequested()).to.be.false;
elementMock.expects('getBoundingClientRect').never();
resource.requestMeasure();
expect(resource.isMeasureRequested()).to.be.false;
});
it('should request measure when built', () => {
expect(resource.isMeasureRequested()).to.be.false;
elementMock.expects('getBoundingClientRect').never();
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.requestMeasure();
expect(resource.isMeasureRequested()).to.be.true;
});
it('should always layout if has not been laid out before', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
resource.state_ = ResourceState.NOT_LAID_OUT;
resource.layoutBox_ = {left: 11, top: 12, width: 111, height: 222};
elementMock.expects('getBoundingClientRect')
.returns(resource.layoutBox_).once();
resource.measure();
expect(resource.getState()).to.equal(ResourceState.READY_FOR_LAYOUT);
});
it('should not relayout if has box has not changed', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
resource.layoutBox_ = {left: 11, top: 12, width: 111, height: 222};
// Left is not part of validation.
elementMock.expects('getBoundingClientRect')
.returns({left: 11 + 10, top: 12, width: 111, height: 222}).once();
resource.measure();
expect(resource.getState()).to.equal(ResourceState.LAYOUT_COMPLETE);
expect(resource.getLayoutBox().left).to.equal(11 + 10);
});
it('should not relayout if box changed but element didn\'t opt in', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
resource.state_ = ResourceState.LAYOUT_COMPLETE;
resource.layoutBox_ = {left: 11, top: 12, width: 111, height: 222};
// Width changed.
elementMock.expects('getBoundingClientRect')
.returns({left: 11, top: 12, width: 111 + 10, height: 222}).once();
elementMock.expects('isRelayoutNeeded').returns(false).atLeast(1);
resource.measure();
expect(resource.getState()).to.equal(ResourceState.LAYOUT_COMPLETE);
expect(resource.getLayoutBox().width).to.equal(111 + 10);
});
it('should relayout if box changed when element opted in', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
resource.state_ = ResourceState.LAYOUT_COMPLETE;
resource.layoutBox_ = {left: 11, top: 12, width: 111, height: 222};
// Width changed.
elementMock.expects('getBoundingClientRect')
.returns({left: 11, top: 12, width: 111 + 10, height: 222}).once();
elementMock.expects('isRelayoutNeeded').returns(true).atLeast(1);
resource.measure();
expect(resource.getState()).to.equal(ResourceState.READY_FOR_LAYOUT);
expect(resource.getLayoutBox().width).to.equal(111 + 10);
});
it('should calculate NOT fixed for non-displayed elements', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect').returns(
layoutRectLtwh(0, 0, 0, 0)).once();
element.isAlwaysFixed = () => true;
resource.measure();
expect(resource.isFixed()).to.be.false;
});
it('should calculate fixed for always-fixed parent', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect').returns(
layoutRectLtwh(0, 0, 10, 10)).once();
element.offsetParent = {
isAlwaysFixed: () => true,
};
resource.measure();
expect(resource.isFixed()).to.be.true;
});
it('should calculate fixed for fixed-style parent', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect').returns(
layoutRectLtwh(0, 0, 10, 10)).once();
const fixedParent = document.createElement('div');
fixedParent.style.position = 'fixed';
document.body.appendChild(fixedParent);
element.offsetParent = fixedParent;
viewportMock.expects('isDeclaredFixed')
.withExactArgs(element)
.returns(false)
.once();
viewportMock.expects('isDeclaredFixed')
.withExactArgs(fixedParent)
.returns(true)
.once();
resource.measure();
expect(resource.isFixed()).to.be.true;
});
it('should hide and update layout box on collapse', () => {
resource.layoutBox_ = {left: 11, top: 12, width: 111, height: 222};
resource.isFixed_ = true;
elementMock.expects('updateLayoutBox')
.withExactArgs(sinon.match(data => {
return data.width == 0 && data.height == 0;
}))
.once();
resource.completeCollapse();
expect(resource.element.style.display).to.equal('none');
expect(resource.getLayoutBox().width).to.equal(0);
expect(resource.getLayoutBox().height).to.equal(0);
expect(resource.isFixed()).to.be.false;
});
it('should ignore startLayout if already completed or failed or going',
() => {
elementMock.expects('layoutCallback').never();
resource.state_ = ResourceState.LAYOUT_COMPLETE;
resource.startLayout(true);
resource.state_ = ResourceState.LAYOUT_FAILED;
resource.startLayout(true);
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.layoutPromise_ = {};
resource.startLayout(true);
});
it('should fail startLayout if not built', () => {
elementMock.expects('layoutCallback').never();
resource.state_ = ResourceState.NOT_BUILT;
expect(() => {
resource.startLayout(true);
}).to.throw(/Not ready to start layout/);
});
it('should ignore startLayout if not visible', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect')
.returns({left: 1, top: 1, width: 1, height: 1}).once();
elementMock.expects('layoutCallback').never();
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.layoutBox_ = {left: 11, top: 12, width: 0, height: 0};
resource.startLayout(true);
});
it('should force startLayout for first layout', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect')
.returns({left: 1, top: 1, width: 1, height: 1}).once();
elementMock.expects('layoutCallback').returns(Promise.resolve()).once();
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.layoutBox_ = {left: 11, top: 12, width: 10, height: 10};
resource.startLayout(true);
expect(resource.getState()).to.equal(ResourceState.LAYOUT_SCHEDULED);
});
it('should ignore startLayout for re-layout when not opt-in', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect')
.returns({left: 1, top: 1, width: 1, height: 1}).once();
elementMock.expects('layoutCallback').never();
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.layoutBox_ = {left: 11, top: 12, width: 10, height: 10};
resource.layoutCount_ = 1;
elementMock.expects('isRelayoutNeeded').returns(false).atLeast(1);
resource.startLayout(true);
expect(resource.getState()).to.equal(ResourceState.LAYOUT_COMPLETE);
});
it('should force startLayout for re-layout when opt-in', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect')
.returns({left: 1, top: 1, width: 1, height: 1}).once();
elementMock.expects('layoutCallback').returns(Promise.resolve()).once();
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.layoutBox_ = {left: 11, top: 12, width: 10, height: 10};
resource.layoutCount_ = 1;
elementMock.expects('isRelayoutNeeded').returns(true).atLeast(1);
resource.startLayout(true);
expect(resource.getState()).to.equal(ResourceState.LAYOUT_SCHEDULED);
});
it('should ignore startLayout when document is hidden' +
' and prerender not allowed', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(0);
elementMock.expects('getBoundingClientRect')
.returns({left: 1, top: 1, width: 1, height: 1}).atLeast(0);
elementMock.expects('prerenderAllowed').returns(false).atLeast(1);
elementMock.expects('layoutCallback').never();
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.layoutBox_ = {left: 11, top: 12, width: 10, height: 10};
resource.layoutCount_ = 0;
resource.startLayout(false);
expect(resource.getState()).to.equal(ResourceState.READY_FOR_LAYOUT);
});
it('should proceed startLayout when document is hidden' +
' and prerender is allowed', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(0);
elementMock.expects('getBoundingClientRect')
.returns({left: 1, top: 1, width: 1, height: 1}).atLeast(0);
elementMock.expects('prerenderAllowed').returns(true).atLeast(1);
elementMock.expects('layoutCallback').returns(Promise.resolve()).once();
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.layoutBox_ = {left: 11, top: 12, width: 10, height: 10};
resource.layoutCount_ = 0;
resource.startLayout(false);
expect(resource.getState()).to.equal(ResourceState.LAYOUT_SCHEDULED);
});
it('should complete startLayout', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect')
.returns({left: 1, top: 1, width: 1, height: 1}).once();
elementMock.expects('layoutCallback').returns(Promise.resolve()).once();
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.layoutBox_ = {left: 11, top: 12, width: 10, height: 10};
const loaded = resource.loadedOnce();
const promise = resource.startLayout(true);
expect(resource.layoutPromise_).to.not.equal(null);
expect(resource.getState()).to.equal(ResourceState.LAYOUT_SCHEDULED);
return promise.then(() => {
expect(resource.getState()).to.equal(ResourceState.LAYOUT_COMPLETE);
expect(resource.layoutPromise_).to.equal(null);
return loaded; // Just making sure this doesn't time out.
});
});
it('should fail startLayout', () => {
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect')
.returns({left: 1, top: 1, width: 1, height: 1}).once();
elementMock.expects('layoutCallback').returns(Promise.reject()).once();
resource.state_ = ResourceState.READY_FOR_LAYOUT;
resource.layoutBox_ = {left: 11, top: 12, width: 10, height: 10};
const promise = resource.startLayout(true);
expect(resource.layoutPromise_).to.not.equal(null);
expect(resource.getState()).to.equal(ResourceState.LAYOUT_SCHEDULED);
return promise.then(() => {
/* global fail: false */
fail('should not be here');
}, () => {
expect(resource.getState()).to.equal(ResourceState.LAYOUT_FAILED);
expect(resource.layoutPromise_).to.equal(null);
});
});
it('should change size and update state', () => {
resource.state_ = ResourceState.READY_FOR_LAYOUT;
elementMock.expects('changeSize').withExactArgs(111, 222).once();
resource.changeSize(111, 222);
expect(resource.getState()).to.equal(ResourceState.NOT_LAID_OUT);
});
it('should change size but not state', () => {
resource.state_ = ResourceState.NOT_BUILT;
elementMock.expects('changeSize').withExactArgs(111, 222).once();
resource.changeSize(111, 222);
expect(resource.getState()).to.equal(ResourceState.NOT_BUILT);
});
describe('setInViewport', () => {
it('should call viewportCallback when not built', () => {
resource.state_ = ResourceState.NOT_BUILT;
elementMock.expects('viewportCallback').withExactArgs(true).once();
resource.setInViewport(true);
expect(resource.isInViewport()).to.equal(true);
});
it('should call viewportCallback when built', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('viewportCallback').withExactArgs(true).once();
resource.setInViewport(true);
expect(resource.isInViewport()).to.equal(true);
});
it('should call viewportCallback only once', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('viewportCallback').withExactArgs(true).once();
resource.setInViewport(true);
resource.setInViewport(true);
resource.setInViewport(true);
});
});
describe('Resource set/get ownership', () => {
let child;
let parentResource;
let resources;
let grandChild;
beforeEach(() => {
const parent = {
ownerDocument: {defaultView: window},
tagName: 'PARENT',
isBuilt: () => false,
contains: () => true,
};
child = {
ownerDocument: {defaultView: window},
tagName: 'CHILD',
isBuilt: () => false,
contains: () => true,
parentElement: parent,
};
grandChild = {
ownerDocument: {defaultView: window},
tagName: 'GRANDCHILD',
isBuilt: () => false,
contains: () => true,
getElementsByClassName: () => {return [];},
parentElement: child,
};
parent.getElementsByClassName = () => {return [child, grandChild];};
child.getElementsByClassName = () => {return [grandChild];};
resources = new Resources(new AmpDocSingle(window));
parentResource = new Resource(1, parent, resources);
});
it('should set resource before Resource created for child element', () => {
resources.setOwner(child, parentResource.element);
const childResource = new Resource(1, child, resources);
expect(childResource.getOwner()).to.equal(parentResource.element);
});
it('should always get the lastest owner value', () => {
const childResource = new Resource(1, child, resources);
expect(childResource.getOwner()).to.be.null;
resources.setOwner(childResource.element, parentResource.element);
expect(childResource.owner_).to.equal(parentResource.element);
expect(childResource.getOwner()).to.equal(parentResource.element);
});
it('should remove cached value for grandchild', () => {
const childResource = new Resource(1, child, resources);
const grandChildResource = new Resource(1, grandChild, resources);
expect(grandChildResource.getOwner()).to.be.null;
resources.setOwner(childResource.element, parentResource.element);
expect(childResource.getOwner()).to.equal(parentResource.element);
expect(grandChildResource.getOwner()).to.equal(parentResource.element);
});
it('should not change owner if it is set via setOwner', () => {
const childResource = new Resource(1, child, resources);
const grandChildResource = new Resource(1, grandChild, resources);
resources.setOwner(grandChildResource.element, parentResource.element);
expect(grandChildResource.getOwner()).to.equal(parentResource.element);
resources.setOwner(childResource.element, parentResource.element);
expect(grandChildResource.getOwner()).to.equal(parentResource.element);
});
});
describe('unlayoutCallback', () => {
it('should NOT call unlayoutCallback on unbuilt element', () => {
resource.state_ = ResourceState.NOT_BUILT;
elementMock.expects('viewportCallback').never();
elementMock.expects('unlayoutCallback').never();
resource.unlayout();
expect(resource.getState()).to.equal(ResourceState.NOT_BUILT);
});
it('should call unlayoutCallback on built element and update state',
() => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('unlayoutCallback').returns(true).once();
elementMock.expects('togglePlaceholder').withArgs(true).once();
resource.unlayout();
expect(resource.getState()).to.equal(ResourceState.NOT_LAID_OUT);
});
it('updated state should bypass isRelayoutNeeded', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('unlayoutCallback').returns(true).once();
elementMock.expects('togglePlaceholder').withArgs(true).once();
elementMock.expects('isUpgraded').returns(true).atLeast(1);
elementMock.expects('getBoundingClientRect')
.returns({left: 1, top: 1, width: 1, height: 1}).once();
resource.unlayout();
elementMock.expects('layoutCallback').returns(Promise.resolve()).once();
resource.startLayout(true);
});
it('should call unlayoutCallback on built element' +
' but NOT update state', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('unlayoutCallback').returns(false).once();
elementMock.expects('togglePlaceholder').withArgs(true).never();
resource.unlayout();
expect(resource.getState()).to.equal(ResourceState.LAYOUT_COMPLETE);
});
it('should NOT call viewportCallback when resource not in viewport', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
resource.isInViewport_ = false;
elementMock.expects('viewportCallback').never();
resource.unlayout();
});
it('should call viewportCallback when resource in viewport', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
resource.isInViewport_ = true;
elementMock.expects('viewportCallback').withExactArgs(false).once();
resource.unlayout();
});
it('should delegate unload to unlayoutCallback', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('unlayoutCallback').returns(false).once();
elementMock.expects('togglePlaceholder').withArgs(true).never();
resource.unload();
expect(resource.getState()).to.equal(ResourceState.LAYOUT_COMPLETE);
});
});
describe('pauseCallback', () => {
it('should NOT call pauseCallback on unbuilt element', () => {
resource.state_ = ResourceState.NOT_BUILT;
elementMock.expects('pauseCallback').never();
resource.pause();
});
it('should NOT call pauseCallback on paused element', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
resource.paused_ = true;
elementMock.expects('pauseCallback').never();
resource.pause();
});
it('should call pauseCallback on built element', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('pauseCallback').once();
resource.pause();
});
it('should NOT call unlayoutCallback', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('pauseCallback').once();
elementMock.expects('unlayoutCallback').never();
resource.pause();
});
describe('when unlayoutOnPause', () => {
beforeEach(() => {
elementMock.expects('unlayoutOnPause').returns(true).once();
});
it('should call unlayoutCallback and update state', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('pauseCallback').once();
elementMock.expects('unlayoutCallback').returns(true).once();
resource.pause();
expect(resource.getState()).to.equal(ResourceState.NOT_LAID_OUT);
});
it('should call unlayoutCallback but NOT update state', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('pauseCallback').once();
elementMock.expects('unlayoutCallback').returns(false).once();
resource.pause();
expect(resource.getState()).to.equal(ResourceState.LAYOUT_COMPLETE);
});
});
describe('when remove from DOM', () => {
it('should not call pauseCallback on remove for unbuilt ele', () => {
resource.state_ = ResourceState.NOT_BUILT;
resource.pauseOnRemove();
elementMock.expects('pauseCallback').never();
elementMock.expects('viewportCallback').never();
});
it('should call pauseCallback on remove for built ele', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
resource.isInViewport_ = true;
resource.paused_ = false;
elementMock.expects('pauseCallback').once();
elementMock.expects('viewportCallback').once();
resource.pauseOnRemove();
expect(resource.isInViewport_).to.equal(false);
expect(resource.paused_).to.equal(true);
});
});
});
describe('resumeCallback', () => {
it('should NOT call resumeCallback on unbuilt element', () => {
resource.state_ = ResourceState.NOT_BUILT;
elementMock.expects('resumeCallback').never();
resource.resume();
});
it('should NOT call resumeCallback on un-paused element', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
elementMock.expects('resumeCallback').never();
resource.resume();
});
it('should call resumeCallback on built element', () => {
resource.state_ = ResourceState.LAYOUT_COMPLETE;
resource.paused_ = true;
elementMock.expects('resumeCallback').once();
resource.resume();
});
});
describe('getResourcesInViewport', () => {
let resource1;
let resource2;
beforeEach(() => {
resource1 = {
hasOwner: () => false,
isDisplayed: () => true,
isFixed: () => false,
prerenderAllowed: () => true,
overlaps: () => true,
};
resource2 = {
hasOwner: () => false,
isDisplayed: () => true,
isFixed: () => false,
prerenderAllowed: () => true,
overlaps: () => false,
};
resources.resources_ = [resource1, resource2];
});
it('should return a subset of resources that are currently ' +
'in the viewport', () => {
expect(resources.get().length).to.equal(2);
expect(resources.getResourcesInViewport().length).to.equal(1);
});
it('should not return resources that are not allowed to prerender if ' +
'in prerender mode', () => {
resource1.prerenderAllowed = () => false;
expect(resources.get().length).to.equal(2);
expect(resources.getResourcesInViewport(false).length).to.equal(1);
expect(resources.getResourcesInViewport(true).length).to.equal(0);
});
});
});
describe('Resource renderOutsideViewport', () => {
let sandbox;
let element;
let resources;
let resource;
let viewport;
let renderOutsideViewport;
beforeEach(() => {
sandbox = sinon.sandbox.create();
element = {
ownerDocument: {defaultView: window},
tagName: 'AMP-AD',
isBuilt: () => false,
isUpgraded: () => false,
prerenderAllowed: () => false,
renderOutsideViewport: () => true,
build: () => false,
getBoundingClientRect: () => null,
updateLayoutBox: () => {},
isRelayoutNeeded: () => false,
layoutCallback: () => {},
changeSize: () => {},
unlayoutOnPause: () => false,
unlayoutCallback: () => true,
pauseCallback: () => false,
resumeCallback: () => false,
viewportCallback: () => {},
getPriority: () => 0,
};
resources = new Resources(new AmpDocSingle(window));
resource = new Resource(1, element, resources);
viewport = resources.viewport_;
renderOutsideViewport = sandbox.stub(element, 'renderOutsideViewport');
sandbox.stub(viewport, 'getRect').returns(layoutRectLtwh(0, 0, 100, 100));
});
afterEach(() => {
sandbox.restore();
});
describe('boolean API', () => {
describe('when element returns true', () => {
beforeEach(() => {
renderOutsideViewport.returns(true);
});
describe('when element is inside viewport', () => {
it('should allow rendering when bottom falls outside', () => {
resource.layoutBox_ = layoutRectLtwh(0, 10, 100, 100);
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when top falls outside', () => {
resource.layoutBox_ = layoutRectLtwh(0, -10, 100, 100);
expect(resource.renderOutsideViewport()).to.equal(true);
});
describe('when element is owned', () => {
beforeEach(() => {
sandbox.stub(resource, 'hasOwner', () => true);
});
it('should allow rendering when bottom falls outside', () => {
resource.layoutBox_ = layoutRectLtwh(0, 10, 100, 100);
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when top falls outside', () => {
resource.layoutBox_ = layoutRectLtwh(0, -10, 100, 100);
expect(resource.renderOutsideViewport()).to.equal(true);
});
});
});
describe('when element is just below viewport', () => {
beforeEach(() => {
resource.layoutBox_ = layoutRectLtwh(0, 110, 100, 100);
});
it('should allow rendering when scrolling towards', () => {
resources.lastVelocity_ = 2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling away', () => {
resources.lastVelocity_ = -2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
describe('when element is owned', () => {
beforeEach(() => {
sandbox.stub(resource, 'hasOwner', () => true);
});
it('should allow rendering when scrolling towards', () => {
resources.lastVelocity_ = 2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling away', () => {
resources.lastVelocity_ = -2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
});
});
describe('when element is marginally below viewport', () => {
beforeEach(() => {
resource.layoutBox_ = layoutRectLtwh(0, 250, 100, 100);
});
it('should allow rendering when scrolling towards', () => {
resources.lastVelocity_ = 2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling away', () => {
resources.lastVelocity_ = -2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
describe('when element is owned', () => {
beforeEach(() => {
sandbox.stub(resource, 'hasOwner', () => true);
});
it('should allow rendering when scrolling towards', () => {
resources.lastVelocity_ = 2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling away', () => {
resources.lastVelocity_ = -2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
});
});
describe('when element is wayyy below viewport', () => {
beforeEach(() => {
resource.layoutBox_ = layoutRectLtwh(0, 1000, 100, 100);
});
it('should allow rendering', () => {
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling towards', () => {
resources.lastVelocity_ = 2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling away', () => {
resources.lastVelocity_ = -2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
describe('when element is owned', () => {
beforeEach(() => {
sandbox.stub(resource, 'hasOwner', () => true);
});
it('should allow rendering', () => {
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling towards', () => {
resources.lastVelocity_ = 2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling away', () => {
resources.lastVelocity_ = -2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
});
});
describe('when element is just above viewport', () => {
beforeEach(() => {
resource.layoutBox_ = layoutRectLtwh(0, -10, 100, 100);
});
it('should allow rendering when scrolling towards', () => {
resources.lastVelocity_ = -2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling away', () => {
resources.lastVelocity_ = 2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
describe('when element is owned', () => {
beforeEach(() => {
sandbox.stub(resource, 'hasOwner', () => true);
});
it('should allow rendering when scrolling towards', () => {
resources.lastVelocity_ = -2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling away', () => {
resources.lastVelocity_ = 2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
});
});
describe('when element is marginally above viewport', () => {
beforeEach(() => {
resource.layoutBox_ = layoutRectLtwh(0, -250, 100, 100);
});
it('should allow rendering when scrolling towards', () => {
resources.lastVelocity_ = -2;
expect(resource.renderOutsideViewport()).to.equal(true);
});
it('should allow rendering when scrolling away', () => {
resources.lastVelocity_ = 2;