@@ -33,6 +33,7 @@ import {MediaType} from '../media-pool';
3333import { PageState } from '../amp-story-page' ;
3434import { PaginationButtons } from '../pagination-buttons' ;
3535import { Services } from '../../../../src/services' ;
36+ import { VisibilityState } from '../../../../src/visibility-state' ;
3637import { createElementWithAttributes } from '../../../../src/dom' ;
3738import { poll } from '../../../../testing/iframe' ;
3839import { 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