Skip to content

Commit 75f393d

Browse files
authored
Override pause/resumeCallback for STAMP needs. (ampproject#25422)
1 parent 465f762 commit 75f393d

File tree

3 files changed

+76
-21
lines changed

3 files changed

+76
-21
lines changed

extensions/amp-story/1.0/amp-story-page.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,13 @@ export class AmpStoryPage extends AMP.BaseElement {
393393
switch (state) {
394394
case PageState.NOT_ACTIVE:
395395
this.element.removeAttribute('active');
396-
this.pauseCallback();
396+
this.pause_();
397397
this.state_ = state;
398398
break;
399399
case PageState.PLAYING:
400400
if (this.state_ === PageState.NOT_ACTIVE) {
401401
this.element.setAttribute('active', '');
402-
this.resumeCallback();
402+
this.resume_();
403403
}
404404

405405
if (this.state_ === PageState.PAUSED) {
@@ -430,8 +430,10 @@ export class AmpStoryPage extends AMP.BaseElement {
430430
}
431431
}
432432

433-
/** @override */
434-
pauseCallback() {
433+
/**
434+
* @private
435+
*/
436+
pause_() {
435437
this.advancement_.stop();
436438

437439
this.stopMeasuringVideoPerformance_();
@@ -458,8 +460,10 @@ export class AmpStoryPage extends AMP.BaseElement {
458460
}
459461
}
460462

461-
/** @override */
462-
resumeCallback() {
463+
/**
464+
* @private
465+
*/
466+
resume_() {
463467
this.registerAllMedia_();
464468

465469
if (this.isActive()) {

extensions/amp-story/1.0/amp-story.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,16 +457,24 @@ export class AmpStory extends AMP.BaseElement {
457457
}
458458
}
459459

460-
/** @override */
461-
pauseCallback() {
460+
/**
461+
* Pauses the whole story on viewer visibilityState updates, or tab visibility
462+
* updates.
463+
* @private
464+
*/
465+
pause_() {
462466
this.pausedStateToRestore_ = !!this.storeService_.get(
463467
StateProperty.PAUSED_STATE
464468
);
465469
this.storeService_.dispatch(Action.TOGGLE_PAUSED, true);
466470
}
467471

468-
/** @override */
469-
resumeCallback() {
472+
/**
473+
* Resumes the whole story on viewer visibilityState updates, or tab
474+
* visibility updates.
475+
* @private
476+
*/
477+
resume_() {
470478
this.storeService_.dispatch(
471479
Action.TOGGLE_PAUSED,
472480
this.pausedStateToRestore_
@@ -1790,7 +1798,7 @@ export class AmpStory extends AMP.BaseElement {
17901798
* @private
17911799
*/
17921800
onVisibilityChanged_() {
1793-
this.getAmpDoc().isVisible() ? this.resumeCallback() : this.pauseCallback();
1801+
this.getAmpDoc().isVisible() ? this.resume_() : this.pause_();
17941802
}
17951803

17961804
/**

extensions/amp-story/1.0/test/test-amp-story.js

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {MediaType} from '../media-pool';
3333
import {PageState} from '../amp-story-page';
3434
import {PaginationButtons} from '../pagination-buttons';
3535
import {Services} from '../../../../src/services';
36+
import {VisibilityState} from '../../../../src/visibility-state';
3637
import {createElementWithAttributes} from '../../../../src/dom';
3738
import {poll} from '../../../../testing/iframe';
3839
import {registerServiceBuilder} from '../../../../src/service';
@@ -224,11 +225,15 @@ describes.realWin(
224225
const oldPage = pages[0];
225226
const newPage = pages[1];
226227

227-
const pauseOldPageStub = sandbox.stub(oldPage, 'pauseCallback');
228-
const resumeNewPageStub = sandbox.stub(newPage, 'resumeCallback');
228+
const setStateOldPageStub = sandbox.stub(oldPage, 'setState');
229+
const setStateNewPageStub = sandbox.stub(newPage, 'setState');
229230
await story.switchTo_('page-1');
230-
expect(pauseOldPageStub).to.have.been.calledOnce;
231-
expect(resumeNewPageStub).to.have.been.calledOnce;
231+
expect(setStateOldPageStub).to.have.been.calledOnceWithExactly(
232+
PageState.NOT_ACTIVE
233+
);
234+
expect(setStateNewPageStub).to.have.been.calledOnceWithExactly(
235+
PageState.PLAYING
236+
);
232237
});
233238

234239
// TODO(#11639): Re-enable this test.
@@ -698,28 +703,66 @@ describes.realWin(
698703
await createStoryWithPages(2, ['cover', 'page-1']);
699704

700705
await story.layoutCallback();
701-
story.pauseCallback();
706+
story.getAmpDoc().overrideVisibilityState(VisibilityState.INACTIVE);
702707
expect(story.storeService_.get(StateProperty.PAUSED_STATE)).to.be.true;
703708
});
704709

705-
it('should play the story when viewer becomes active', async () => {
710+
it('should pause the story when viewer becomes hidden', async () => {
706711
await createStoryWithPages(2, ['cover', 'page-1']);
707712

708-
story.storeService_.dispatch(Action.TOGGLE_PAUSED, true);
713+
await story.layoutCallback();
714+
story.getAmpDoc().overrideVisibilityState(VisibilityState.HIDDEN);
715+
expect(story.storeService_.get(StateProperty.PAUSED_STATE)).to.be.true;
716+
});
717+
718+
it('should pause the story when viewer becomes paused', async () => {
719+
await createStoryWithPages(2, ['cover', 'page-1']);
720+
721+
await story.layoutCallback();
722+
story.getAmpDoc().overrideVisibilityState(VisibilityState.PAUSED);
723+
expect(story.storeService_.get(StateProperty.PAUSED_STATE)).to.be.true;
724+
});
725+
726+
it('should pause the story page when viewer becomes paused', async () => {
727+
await createStoryWithPages(2, ['cover', 'page-1']);
709728

710729
await story.layoutCallback();
711-
story.resumeCallback();
730+
const setStateStub = sandbox.stub(story.activePage_, 'setState');
731+
story.getAmpDoc().overrideVisibilityState(VisibilityState.PAUSED);
732+
expect(setStateStub).to.have.been.calledOnceWithExactly(
733+
PageState.PAUSED
734+
);
735+
});
736+
737+
it('should play the story when viewer becomes active', async () => {
738+
await createStoryWithPages(2, ['cover', 'page-1']);
739+
740+
await story.layoutCallback();
741+
story.getAmpDoc().overrideVisibilityState(VisibilityState.PAUSED);
742+
story.getAmpDoc().overrideVisibilityState(VisibilityState.ACTIVE);
712743
expect(story.storeService_.get(StateProperty.PAUSED_STATE)).to.be.false;
713744
});
714745

746+
it('should play the story page when viewer becomes active', async () => {
747+
await createStoryWithPages(2, ['cover', 'page-1']);
748+
749+
await story.layoutCallback();
750+
const setStateStub = sandbox.stub(story.activePage_, 'setState');
751+
story.getAmpDoc().overrideVisibilityState(VisibilityState.PAUSED);
752+
story.getAmpDoc().overrideVisibilityState(VisibilityState.ACTIVE);
753+
expect(setStateStub.getCall(1)).to.have.been.calledWithExactly(
754+
PageState.PLAYING
755+
);
756+
});
757+
715758
it('should keep the story paused on resume when previously paused', async () => {
716759
await createStoryWithPages(2, ['cover', 'page-1']);
717760

718761
story.storeService_.dispatch(Action.TOGGLE_PAUSED, true);
719762

720763
await story.layoutCallback();
721-
story.pauseCallback();
722-
story.resumeCallback();
764+
story.getAmpDoc().overrideVisibilityState(VisibilityState.PAUSED);
765+
story.getAmpDoc().overrideVisibilityState(VisibilityState.ACTIVE);
723766
expect(story.storeService_.get(StateProperty.PAUSED_STATE)).to.be.true;
724767
});
725768

0 commit comments

Comments
 (0)