Skip to content

Commit

Permalink
Cannot unlayout without being paused first
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Mar 18, 2016
1 parent ef0dc02 commit 44cc1be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
27 changes: 9 additions & 18 deletions src/service/resources-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1306,11 +1306,8 @@ export class Resources {
setupVisibilityStateMachine_(vsm) {
const prerender = VisibilityState.PRERENDER;
const visible = VisibilityState.VISIBLE;
// Viewer has told us we are no longer active
const hidden = VisibilityState.HIDDEN;
// Viewer has told us to pause media but don't unload it.
const paused = VisibilityState.PAUSED;
// The browser tab is no longer active
const inactive = VisibilityState.INACTIVE;

const doPass = () => {
Expand Down Expand Up @@ -1339,14 +1336,8 @@ export class Resources {
const pause = () => {
this.resources_.forEach(r => r.pause());
};
const unlayout = () => {
this.resources_.forEach(r => r.unlayout());
};
const pauseAndUnlayout = () => {
this.resources_.forEach(r => {
r.pause();
r.unlayout();
});
const unload = () => {
this.resources_.forEach(r => r.unload());
};
const resume = () => {
this.resources_.forEach(r => r.resume());
Expand All @@ -1355,18 +1346,18 @@ export class Resources {

vsm.addTransition(prerender, prerender, doPass);
vsm.addTransition(prerender, visible, doPass);
vsm.addTransition(prerender, hidden, unlayout);
vsm.addTransition(prerender, inactive, unlayout);
vsm.addTransition(prerender, hidden, doPass);
vsm.addTransition(prerender, inactive, doPass);
vsm.addTransition(prerender, paused, doPass);

vsm.addTransition(visible, visible, doPass);
vsm.addTransition(visible, hidden, unlayout);
vsm.addTransition(visible, inactive, pauseAndUnlayout);
vsm.addTransition(visible, hidden, doPass);
vsm.addTransition(visible, inactive, unload);
vsm.addTransition(visible, paused, pause);

vsm.addTransition(hidden, visible, doPass);
vsm.addTransition(hidden, hidden, doPass);
vsm.addTransition(hidden, inactive, pause);
vsm.addTransition(hidden, inactive, unload);
vsm.addTransition(hidden, paused, pause);

vsm.addTransition(inactive, visible, doPass);
Expand All @@ -1375,8 +1366,8 @@ export class Resources {
vsm.addTransition(inactive, paused, doPass);

vsm.addTransition(paused, visible, resume);
vsm.addTransition(paused, hidden, unlayout);
vsm.addTransition(paused, inactive, unlayout);
vsm.addTransition(paused, hidden, doPass);
vsm.addTransition(paused, inactive, unload);
vsm.addTransition(paused, paused, doPass);
}
}
Expand Down
29 changes: 15 additions & 14 deletions test/integration/test-visibility-states.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe('Viewer Visibility State', () => {
let notifyPass = noop;
function doPass() {
if (shouldPass) {
if (window.debug) debugger;
doPass_.call(this);
shouldPass = false;
notifyPass();
Expand Down Expand Up @@ -103,7 +102,7 @@ describe('Viewer Visibility State', () => {
return createFixtureIframe('test/fixtures/visibility-state.html', 10000)
.then(f => {
fixture = f;
fixture.win.name = "__AMP__visibilityState=prerender";
fixture.win.name = '__AMP__visibilityState=prerender';
return expectBodyToBecomeVisible(fixture.win);
}).then(() => {
viewer = viewerFor(fixture.win);
Expand All @@ -129,14 +128,14 @@ describe('Viewer Visibility State', () => {
fixture.iframe.parentNode.removeChild(fixture.iframe);
});

describe('from in the PRERENDER state', () => {
describe.skip('from in the PRERENDER state', () => {
beforeEach(() => {
return waitForNextPass().then(setupSpys);
});

// TODO(jridgewell): Need to test non-prerenderable element doesn't
// prerender, and prerenderable does.
it.skip('does not call callbacks when going to PRERENDER', () => {
it('does not call callbacks when going to PRERENDER', () => {
return waitForNextPass().then(() => {
expect(layoutCallback).not.to.have.been.called;
expect(unlayoutCallback).not.to.have.been.called;
Expand All @@ -147,7 +146,7 @@ describe('Viewer Visibility State', () => {

// TODO(jridgewell): Need to test non-prerenderable element already
// laid-out, and prerenderable is not.
it.skip('calls layout when going to VISIBLE', () => {
it('calls layout when going to VISIBLE', () => {
viewer.setVisibilityState_(VisibilityState.VISIBLE);
return waitForNextPass().then(() => {
expect(layoutCallback).to.have.been.called;
Expand All @@ -159,7 +158,7 @@ describe('Viewer Visibility State', () => {

// TODO(jridgewell): Need to test non-prerenderable element calls
// unlayout, and prerenderable does not.
it.skip('calls unlayout when going to HIDDEN', () => {
it('calls unlayout when going to HIDDEN', () => {
viewer.setVisibilityState_(VisibilityState.VISIBLE);
changeVisibility('hidden');
return waitForNextPass().then(() => {
Expand All @@ -172,7 +171,7 @@ describe('Viewer Visibility State', () => {

// TODO(jridgewell): Need to test non-prerenderable element calls
// unlayout, and prerenderable does not.
it.skip('calls unlayout when going to INACTIVE', () => {
it('calls unlayout when going to INACTIVE', () => {
viewer.setVisibilityState_(VisibilityState.INACTIVE);
return waitForNextPass().then(() => {
expect(layoutCallback).not.to.have.been.called;
Expand All @@ -182,6 +181,8 @@ describe('Viewer Visibility State', () => {
});
});

// TODO(jridgewell): Need to test non-prerenderable element calls
// unlayout, and prerenderable does not.
it('does not call callbacks when going to PAUSED', () => {
viewer.setVisibilityState_(VisibilityState.PAUSED);
return waitForNextPass().then(() => {
Expand All @@ -208,11 +209,11 @@ describe('Viewer Visibility State', () => {
});
});

it('calls unlayout when going to HIDDEN', () => {
it('does not call callbacks when going to HIDDEN', () => {
changeVisibility('hidden');
return waitForNextPass().then(() => {
expect(layoutCallback).not.to.have.been.called;
expect(unlayoutCallback).to.have.been.called;
expect(unlayoutCallback).not.to.have.been.called;
expect(pauseCallback).not.to.have.been.called;
expect(resumeCallback).not.to.have.been.called;
});
Expand Down Expand Up @@ -248,10 +249,10 @@ describe('Viewer Visibility State', () => {
}).then(setupSpys);
});

it('calls layout when going to VISIBLE', () => {
it('does not call callbacks going to VISIBLE', () => {
changeVisibility('visible');
return waitForNextPass().then(() => {
expect(layoutCallback).to.have.been.called;
expect(layoutCallback).not.to.have.been.called;
expect(unlayoutCallback).not.to.have.been.called;
expect(pauseCallback).not.to.have.been.called;
expect(resumeCallback).not.to.have.been.called;
Expand All @@ -267,11 +268,11 @@ describe('Viewer Visibility State', () => {
});
});

it('calls pause when going to INACTIVE', () => {
it('calls unload when going to INACTIVE', () => {
viewer.setVisibilityState_(VisibilityState.INACTIVE);
return waitForNextPass().then(() => {
expect(layoutCallback).not.to.have.been.called;
expect(unlayoutCallback).not.to.have.been.called;
expect(unlayoutCallback).to.have.been.called;
expect(pauseCallback).to.have.been.called;
expect(resumeCallback).not.to.have.been.called;
});
Expand Down Expand Up @@ -362,7 +363,7 @@ describe('Viewer Visibility State', () => {
changeVisibility('hidden');
return waitForNextPass().then(() => {
expect(layoutCallback).not.to.have.been.called;
expect(unlayoutCallback).to.have.been.called;
expect(unlayoutCallback).not.to.have.been.called;
expect(pauseCallback).not.to.have.been.called;
expect(resumeCallback).not.to.have.been.called;
});
Expand Down

0 comments on commit 44cc1be

Please sign in to comment.