@@ -729,6 +729,17 @@ describes.realWin(
729729 expect ( story . storeService_ . get ( StateProperty . PAUSED_STATE ) ) . to . be . true ;
730730 } ) ;
731731
732+ it ( 'should rewind the story page when viewer becomes inactive' , async ( ) => {
733+ await createStoryWithPages ( 2 , [ 'cover' , 'page-1' ] ) ;
734+
735+ await story . layoutCallback ( ) ;
736+ const setStateStub = window . sandbox . stub ( story . activePage_ , 'setState' ) ;
737+ story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . INACTIVE ) ;
738+ expect ( setStateStub . getCall ( 1 ) ) . to . have . been . calledWithExactly (
739+ PageState . NOT_ACTIVE
740+ ) ;
741+ } ) ;
742+
732743 it ( 'should pause the story when viewer becomes hidden' , async ( ) => {
733744 await createStoryWithPages ( 2 , [ 'cover' , 'page-1' ] ) ;
734745
@@ -737,6 +748,17 @@ describes.realWin(
737748 expect ( story . storeService_ . get ( StateProperty . PAUSED_STATE ) ) . to . be . true ;
738749 } ) ;
739750
751+ it ( 'should pause the story page when viewer becomes hidden' , async ( ) => {
752+ await createStoryWithPages ( 2 , [ 'cover' , 'page-1' ] ) ;
753+
754+ await story . layoutCallback ( ) ;
755+ const setStateStub = window . sandbox . stub ( story . activePage_ , 'setState' ) ;
756+ story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . HIDDEN ) ;
757+ expect ( setStateStub ) . to . have . been . calledOnceWithExactly (
758+ PageState . PAUSED
759+ ) ;
760+ } ) ;
761+
740762 it ( 'should pause the story when viewer becomes paused' , async ( ) => {
741763 await createStoryWithPages ( 2 , [ 'cover' , 'page-1' ] ) ;
742764
@@ -756,7 +778,7 @@ describes.realWin(
756778 ) ;
757779 } ) ;
758780
759- it ( 'should play the story when viewer becomes active' , async ( ) => {
781+ it ( 'should play the story when viewer becomes active after paused ' , async ( ) => {
760782 await createStoryWithPages ( 2 , [ 'cover' , 'page-1' ] ) ;
761783
762784 await story . layoutCallback ( ) ;
@@ -765,14 +787,33 @@ describes.realWin(
765787 expect ( story . storeService_ . get ( StateProperty . PAUSED_STATE ) ) . to . be . false ;
766788 } ) ;
767789
768- it ( 'should play the story page when viewer becomes active' , async ( ) => {
790+ it ( 'should play the story page when viewer becomes active after paused' , async ( ) => {
791+ await createStoryWithPages ( 2 , [ 'cover' , 'page-1' ] ) ;
792+
793+ await story . layoutCallback ( ) ;
794+ const setStateStub = window . sandbox . stub ( story . activePage_ , 'setState' ) ;
795+ story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . PAUSED ) ;
796+ story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . ACTIVE ) ;
797+ expect ( setStateStub . getCall ( 1 ) ) . to . have . been . calledWithExactly (
798+ PageState . PLAYING
799+ ) ;
800+ } ) ;
801+
802+ it ( 'should play the story page when viewer becomes active after paused + inactive' , async ( ) => {
769803 await createStoryWithPages ( 2 , [ 'cover' , 'page-1' ] ) ;
770804
771805 await story . layoutCallback ( ) ;
772806 const setStateStub = window . sandbox . stub ( story . activePage_ , 'setState' ) ;
773807 story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . PAUSED ) ;
808+ story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . INACTIVE ) ;
774809 story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . ACTIVE ) ;
810+ expect ( setStateStub . getCall ( 0 ) ) . to . have . been . calledWithExactly (
811+ PageState . PAUSED
812+ ) ;
775813 expect ( setStateStub . getCall ( 1 ) ) . to . have . been . calledWithExactly (
814+ PageState . NOT_ACTIVE
815+ ) ;
816+ expect ( setStateStub . getCall ( 2 ) ) . to . have . been . calledWithExactly (
776817 PageState . PLAYING
777818 ) ;
778819 } ) ;
@@ -788,6 +829,18 @@ describes.realWin(
788829 expect ( story . storeService_ . get ( StateProperty . PAUSED_STATE ) ) . to . be . true ;
789830 } ) ;
790831
832+ it ( 'should keep the story paused on resume when previously paused + inactive' , async ( ) => {
833+ await createStoryWithPages ( 2 , [ 'cover' , 'page-1' ] ) ;
834+
835+ story . storeService_ . dispatch ( Action . TOGGLE_PAUSED , true ) ;
836+
837+ await story . layoutCallback ( ) ;
838+ story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . PAUSED ) ;
839+ story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . INACTIVE ) ;
840+ story . getAmpDoc ( ) . overrideVisibilityState ( VisibilityState . ACTIVE ) ;
841+ expect ( story . storeService_ . get ( StateProperty . PAUSED_STATE ) ) . to . be . true ;
842+ } ) ;
843+
791844 describe ( 'amp-story continue anyway' , ( ) => {
792845 it ( 'should not display layout' , async ( ) => {
793846 await createStoryWithPages ( 2 , [ 'cover' , 'page-4' ] ) ;
0 commit comments