test_suspend_media_by_inactive_docshell.html (2584B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test suspending media by inactive docShell</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="manifest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 8 </head> 9 <body> 10 <video id="testVideo" src="gizmo.mp4" loop></video> 11 <script class="testbody" type="text/javascript"> 12 /** 13 * When calling `browser.suspendMediaWhenInactive`, it can set the docShell's 14 * corresponding flag that is used to suspend media when the docShell is 15 * inactive. This test is used to check if we can suspend/resume the media 16 * correctly when changing docShell's active state. 17 */ 18 async function startTest() { 19 const video = document.getElementById("testVideo"); 20 21 info(`start video`); 22 await video.play(); 23 24 info(`set docShell inactive which would suspend media`); 25 await setDocShellActive(false); 26 27 info(`set docShell active which would resume media`); 28 await setDocShellActive(true); 29 30 SimpleTest.finish(); 31 } 32 33 SimpleTest.waitForExplicitFinish(); 34 SpecialPowers.pushPrefEnv( 35 {"set": [["media.testing-only-events", true]]}, startTest); 36 37 /** 38 * The following are test helper functions. 39 */ 40 function mediaSuspendedStateShouldEqualTo(expected) { 41 const video = document.getElementById("testVideo"); 42 const result = SpecialPowers.wrap(video).isSuspendedByInactiveDocOrDocShell; 43 is(result, expected, `media's suspended state is correct`); 44 } 45 46 function waitForEvent(target, name, condition) { 47 return new Promise(r => { 48 const listener = function(event) { 49 if (condition(event)) { 50 target.removeEventListener(name, listener); 51 r(); 52 } 53 }; 54 target.addEventListener(name, listener); 55 }); 56 } 57 58 async function setDocShellActive(isActive) { 59 const win = SpecialPowers.wrap(window); 60 const docShell = win.docShell; 61 62 const video = document.getElementById("testVideo"); 63 let mediaSuspendChangedPromise = waitForEvent( 64 docShell.chromeEventHandler, "MozMediaSuspendChanged", 65 event => event.target === video 66 ); 67 68 await SpecialPowers.spawnChrome([isActive], active => { 69 // This flag is used to prevent media from playing when docShell is 70 // inactive. After updating `docshell.isActive`, it would suspend/resume 71 // media and we // wait suspending/resuming finishing by listening to 72 // `MozMediaSuspendChanged`. 73 this.browsingContext.top.suspendMediaWhenInactive = true; 74 this.browsingContext.top.isActive = active; 75 }); 76 77 await mediaSuspendChangedPromise; 78 79 mediaSuspendedStateShouldEqualTo(!isActive); 80 } 81 82 </script> 83 </body> 84 </html>