1414 * limitations under the License.
1515 */
1616
17+ import { Action , AmpStoryStoreService } from '../amp-story-store-service' ;
1718import { AmpDocSingle } from '../../../../src/service/ampdoc-impl' ;
1819import { AmpStoryPage , PageState , Selectors } from '../amp-story-page' ;
19- import { AmpStoryStoreService } from '../amp-story-store-service' ;
2020import { Deferred } from '../../../../src/utils/promise' ;
2121import { LocalizationService } from '../../../../src/service/localization' ;
2222import { MediaType } from '../media-pool' ;
@@ -34,6 +34,7 @@ describes.realWin('amp-story-page', {amp: true}, (env) => {
3434 let element ;
3535 let gridLayerEl ;
3636 let page ;
37+ let storeService ;
3738 let isPerformanceTrackingOn ;
3839
3940 const nextTick = ( ) => new Promise ( ( resolve ) => win . setTimeout ( resolve , 0 ) ) ;
@@ -55,7 +56,7 @@ describes.realWin('amp-story-page', {amp: true}, (env) => {
5556 . stub ( Services , 'localizationForDoc' )
5657 . returns ( localizationService ) ;
5758
58- const storeService = new AmpStoryStoreService ( win ) ;
59+ storeService = new AmpStoryStoreService ( win ) ;
5960 registerServiceBuilder ( win , 'story-store' , function ( ) {
6061 return storeService ;
6162 } ) ;
@@ -207,6 +208,34 @@ describes.realWin('amp-story-page', {amp: true}, (env) => {
207208 } ) ;
208209 } ) ;
209210
211+ it ( 'should unmute audio when state becomes active' , ( done ) => {
212+ env . sandbox . stub ( page , 'loadPromise' ) . returns ( Promise . resolve ( ) ) ;
213+
214+ storeService . dispatch ( Action . TOGGLE_MUTED , false ) ;
215+
216+ const videoEl = win . document . createElement ( 'video' ) ;
217+ videoEl . setAttribute ( 'src' , 'https://example.com/video.mp3' ) ;
218+ gridLayerEl . appendChild ( videoEl ) ;
219+
220+ let mediaPoolMock ;
221+
222+ page . buildCallback ( ) ;
223+ page
224+ . layoutCallback ( )
225+ . then ( ( ) => page . mediaPoolPromise_ )
226+ . then ( ( mediaPool ) => {
227+ mediaPoolMock = env . sandbox . mock ( mediaPool ) ;
228+ mediaPoolMock . expects ( 'unmute' ) . once ( ) ;
229+
230+ page . setState ( PageState . PLAYING ) ;
231+
232+ win . requestAnimationFrame ( ( ) => {
233+ mediaPoolMock . verify ( ) ;
234+ done ( ) ;
235+ } ) ;
236+ } ) ;
237+ } ) ;
238+
210239 it ( 'should perform media operations on fie video when active' , ( done ) => {
211240 const iframe = win . document . createElement ( 'iframe' ) ;
212241 const fiePromise = installFriendlyIframeEmbed ( iframe , gridLayerEl , {
@@ -394,6 +423,32 @@ describes.realWin('amp-story-page', {amp: true}, (env) => {
394423 } ) ;
395424 } ) ;
396425
426+ it ( 'should mute audio when state becomes active' , ( done ) => {
427+ storeService . dispatch ( Action . TOGGLE_MUTED , false ) ;
428+
429+ const videoEl = win . document . createElement ( 'video' ) ;
430+ videoEl . setAttribute ( 'src' , 'https://example.com/video.mp3' ) ;
431+ gridLayerEl . appendChild ( videoEl ) ;
432+
433+ let mediaPoolMock ;
434+
435+ page . buildCallback ( ) ;
436+ page
437+ . layoutCallback ( )
438+ . then ( ( ) => page . mediaPoolPromise_ )
439+ . then ( ( mediaPool ) => {
440+ mediaPoolMock = env . sandbox . mock ( mediaPool ) ;
441+ mediaPoolMock . expects ( 'mute' ) . withExactArgs ( videoEl ) . once ( ) ;
442+
443+ page . setState ( PageState . NOT_ACTIVE ) ;
444+
445+ win . requestAnimationFrame ( ( ) => {
446+ mediaPoolMock . verify ( ) ;
447+ done ( ) ;
448+ } ) ;
449+ } ) ;
450+ } ) ;
451+
397452 it ( 'should stop the advancement when state becomes paused' , async ( ) => {
398453 page . buildCallback ( ) ;
399454 const advancementStopStub = env . sandbox . stub ( page . advancement_ , 'stop' ) ;
0 commit comments