browser_autoplay_policy_touchScroll.js (2593B)
1 /** 2 * This test is used to ensure that touch in point can activate document and 3 * allow autoplay, but touch scroll can't activate document. 4 */ 5 /* eslint-disable mozilla/no-arbitrary-setTimeout */ 6 "use strict"; 7 8 const PAGE = GetTestWebBasedURL("file_nonAutoplayAudio.html"); 9 10 function checkMediaPlayingState(isPlaying) { 11 let audio = content.document.getElementById("testAudio"); 12 if (!audio) { 13 ok(false, "can't get the audio element!"); 14 } 15 16 is(!audio.paused, isPlaying, "media playing state is correct."); 17 } 18 19 async function callMediaPlay(shouldStartPlaying) { 20 let audio = content.document.getElementById("testAudio"); 21 if (!audio) { 22 ok(false, "can't get the audio element!"); 23 } 24 25 info(`call media.play().`); 26 let playPromise = new Promise((resolve, reject) => { 27 audio.play().then(() => { 28 audio.isPlayStarted = true; 29 resolve(); 30 }); 31 content.setTimeout(() => { 32 if (audio.isPlayStarted) { 33 return; 34 } 35 reject(); 36 }, 3000); 37 }); 38 39 let isStartPlaying = await playPromise.then( 40 () => true, 41 () => false 42 ); 43 is( 44 isStartPlaying, 45 shouldStartPlaying, 46 "media is " + (isStartPlaying ? "" : "not ") + "playing." 47 ); 48 } 49 50 async function synthesizeTouchScroll(target, browser) { 51 const offset = 100; 52 await BrowserTestUtils.synthesizeTouch( 53 target, 54 0, 55 0, 56 { type: "touchstart" }, 57 browser 58 ); 59 await BrowserTestUtils.synthesizeTouch( 60 target, 61 offset / 2, 62 offset / 2, 63 { type: "touchmove" }, 64 browser 65 ); 66 await BrowserTestUtils.synthesizeTouch( 67 target, 68 offset, 69 offset, 70 { type: "touchend" }, 71 browser 72 ); 73 } 74 75 add_task(async function setup_test_preference() { 76 return SpecialPowers.pushPrefEnv({ 77 set: [ 78 ["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED], 79 ["media.autoplay.blocking_policy", 0], 80 ], 81 }); 82 }); 83 84 add_task(async function testTouchScroll() { 85 await BrowserTestUtils.withNewTab( 86 { 87 gBrowser, 88 url: PAGE, 89 }, 90 async browser => { 91 info(`- media should not start playing -`); 92 await SpecialPowers.spawn(browser, [false], checkMediaPlayingState); 93 94 info(`- simulate touch scroll which should not activate document -`); 95 await synthesizeTouchScroll("#testAudio", browser); 96 await SpecialPowers.spawn(browser, [false], callMediaPlay); 97 98 info(`- simulate touch at a point which should activate document -`); 99 await BrowserTestUtils.synthesizeTouch("#testAudio", 0, 0, {}, browser); 100 await SpecialPowers.spawn(browser, [true], callMediaPlay); 101 } 102 ); 103 });