browser_autoplay_userinteraction.js (2931B)
1 "use strict"; 2 3 const TEST_URI = 4 getRootDirectory(gTestPath).replace( 5 "chrome://mochitests/content", 6 "https://example.com" 7 ) + "file_empty.html"; 8 const gPermissionName = "autoplay-media"; 9 10 add_setup(async () => { 11 await SpecialPowers.pushPrefEnv({ 12 set: [["browser.navigation.requireUserInteraction", true]], 13 }); 14 }); 15 16 async function assertMenulist(entries) { 17 // Wait for the session data to be flushed before continuing the test 18 await new Promise(resolve => 19 SessionStore.getSessionHistory(gBrowser.selectedTab, resolve) 20 ); 21 22 let backButton = document.getElementById("back-button"); 23 let contextMenu = document.getElementById("backForwardMenu"); 24 25 info("waiting for the history menu to open"); 26 27 let popupShownPromise = BrowserTestUtils.waitForEvent( 28 contextMenu, 29 "popupshown" 30 ); 31 EventUtils.synthesizeMouseAtCenter(backButton, { 32 type: "contextmenu", 33 button: 2, 34 }); 35 await popupShownPromise; 36 37 info("history menu opened"); 38 39 let nodes = contextMenu.childNodes; 40 41 is( 42 nodes.length, 43 entries.length, 44 "Has the expected number of contextMenu entries" 45 ); 46 47 for (let i = 0; i < entries.length; i++) { 48 let node = nodes[i]; 49 is( 50 node.getAttribute("uri"), 51 entries[i], 52 "contextMenu node has the correct uri" 53 ); 54 } 55 56 let popupHiddenPromise = BrowserTestUtils.waitForEvent( 57 contextMenu, 58 "popuphidden" 59 ); 60 contextMenu.hidePopup(); 61 await popupHiddenPromise; 62 } 63 64 async function playVideoAndCheckForUserInteraction(aMuteVideo) { 65 for (const mode of ["call play", "autoplay attribute"]) { 66 await BrowserTestUtils.withNewTab( 67 { 68 gBrowser, 69 url: TEST_URI, 70 }, 71 async browser => { 72 let args = { 73 name: `${mode} causes BBI to ${!aMuteVideo ? "not" : ""} skip`, 74 shouldPlay: true, 75 mode, 76 muted: aMuteVideo, 77 }; 78 await loadAutoplayVideo(browser, args); 79 await checkVideoDidPlay(browser, args); 80 81 let loaded = BrowserTestUtils.waitForLocationChange( 82 gBrowser, 83 TEST_URI + "#1" 84 ); 85 await SpecialPowers.spawn(browser, [], async () => { 86 content.history.pushState(null, "", "#1"); 87 }); 88 await loaded; 89 90 if (!aMuteVideo) { 91 await assertMenulist([TEST_URI + "#1", TEST_URI]); 92 } else { 93 await assertMenulist([TEST_URI + "#1"]); 94 } 95 } 96 ); 97 } 98 } 99 100 add_task(async function played_video_should_cause_userinteraction() { 101 await SpecialPowers.pushPrefEnv({ 102 set: [["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.ALLOWED]], 103 }); 104 await playVideoAndCheckForUserInteraction(false); 105 }); 106 107 add_task(async function muted_video_should_not_cause_userinteraction() { 108 await SpecialPowers.pushPrefEnv({ 109 set: [["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED]], 110 }); 111 await playVideoAndCheckForUserInteraction(true); 112 });