Skip to content

Commit

Permalink
Ensure that the size changes are picked up between visible/invisible …
Browse files Browse the repository at this point in the history
…states (ampproject#6251)
  • Loading branch information
Dima Voytenko authored and Vanessa Pasque committed Dec 22, 2016
1 parent 5831dd1 commit 211a27d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/service/viewport-impl.js
Expand Up @@ -205,6 +205,11 @@ export class Viewport {
this.visible_ = visible;
if (visible) {
this.binding_.connect();
if (this.size_) {
// If the size has already been intialized, check it again in case
// the size has changed between `disconnect` and `connect`.
this.resize_();
}
} else {
this.binding_.disconnect();
}
Expand Down
30 changes: 30 additions & 0 deletions test/functional/test-viewport.js
Expand Up @@ -158,6 +158,7 @@ describe('Viewport', () => {
// Hasn't been called at first.
expect(binding.connect).to.not.be.called;
expect(binding.disconnect).to.not.be.called;
expect(viewport.size_).to.be.null;

// When becomes visible - it gets called.
viewer.isVisible = () => true;
Expand All @@ -177,6 +178,35 @@ describe('Viewport', () => {
expect(binding.disconnect).to.be.calledOnce;
});

it('should resize only after size has been initialed', () => {
binding.connect = sandbox.spy();
binding.disconnect = sandbox.spy();
viewer.isVisible = () => true;
let onVisibilityHandler;
viewer.onVisibilityChanged = handler => onVisibilityHandler = handler;
viewport = new Viewport(ampdoc, binding, viewer);

// Size has not be initialized yet.
expect(binding.connect).to.be.calledOnce;
expect(binding.disconnect).to.not.be.called;
expect(viewport.size_).to.be.null;

// Disconnect: ignore resizing.
viewer.isVisible = () => false;
onVisibilityHandler();
expect(binding.connect).to.be.calledOnce;
expect(binding.disconnect).to.be.calledOnce;
expect(viewport.size_).to.be.null;

// Size has been initialized.
viewport.size_ = {width: 0, height: 0};
viewer.isVisible = () => true;
onVisibilityHandler();
expect(binding.connect).to.be.calledTwice;
expect(binding.disconnect).to.be.calledOnce;
expect(viewport.size_).to.deep.equal(viewportSize);
});

it('should pass through size and scroll', () => {
expect(viewport.getPaddingTop()).to.equal(19);
expect(updatedPaddingTop).to.equal(19);
Expand Down

0 comments on commit 211a27d

Please sign in to comment.