Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that the size changes are picked up between visible/invisible states #6251

Merged
merged 1 commit into from Nov 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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