head.js (4667B)
1 /** 2 * Return a web-based URL for a given file based on the testing directory. 3 * 4 * @param {string} fileName 5 * file that caller wants its web-based url 6 * @param {boolean} crossOrigin [optional] 7 * if set, then return a url with different origin. The default value is 8 * false. 9 */ 10 function GetTestWebBasedURL(fileName, { crossOrigin = false } = {}) { 11 const origin = crossOrigin ? "http://example.org" : "http://example.com"; 12 return ( 13 getRootDirectory(gTestPath).replace("chrome://mochitests/content", origin) + 14 fileName 15 ); 16 } 17 18 /** 19 * Runs a content script that creates an autoplay video. 20 * 21 * @param {browserElement} browser 22 * the browser to run the script in 23 * @param {object} args 24 * test case definition, required members 25 * { 26 * mode: String, "autoplay attribute" or "call play". 27 * } 28 */ 29 function loadAutoplayVideo(browser, args) { 30 return SpecialPowers.spawn(browser, [args], async args => { 31 info("- create a new autoplay video -"); 32 let video = content.document.createElement("video"); 33 video.id = "v1"; 34 video.didPlayPromise = new Promise(resolve => { 35 video.addEventListener( 36 "playing", 37 () => { 38 video.didPlay = true; 39 resolve(); 40 }, 41 { once: true } 42 ); 43 video.addEventListener( 44 "blocked", 45 () => { 46 video.didPlay = false; 47 resolve(); 48 }, 49 { once: true } 50 ); 51 }); 52 if (args.mode == "autoplay attribute") { 53 info("autoplay attribute set to true"); 54 video.autoplay = true; 55 } else if (args.mode == "call play") { 56 info("will call play() when reached loadedmetadata"); 57 video.addEventListener( 58 "loadedmetadata", 59 () => { 60 video.play().then( 61 () => { 62 info("video play() resolved"); 63 }, 64 () => { 65 info("video play() rejected"); 66 } 67 ); 68 }, 69 { once: true } 70 ); 71 } else { 72 ok(false, "Invalid 'mode' arg"); 73 } 74 if (args.muted) { 75 video.muted = true; 76 } 77 video.src = "gizmo.mp4"; 78 content.document.body.appendChild(video); 79 }); 80 } 81 82 /** 83 * Runs a content script that checks whether the video created by 84 * loadAutoplayVideo() started playing. 85 * 86 * @param {browserElement} browser 87 * the browser to run the script in 88 * @param {object} args 89 * test case definition, required members 90 * { 91 * name: String, description of test. 92 * mode: String, "autoplay attribute" or "call play". 93 * shouldPlay: boolean, whether video should play. 94 * } 95 */ 96 function checkVideoDidPlay(browser, args) { 97 return SpecialPowers.spawn(browser, [args], async args => { 98 let video = content.document.getElementById("v1"); 99 await video.didPlayPromise; 100 is( 101 video.didPlay, 102 args.shouldPlay, 103 args.name + 104 " should " + 105 (!args.shouldPlay ? "not " : "") + 106 "be able to autoplay" 107 ); 108 video.src = ""; 109 content.document.body.remove(video); 110 }); 111 } 112 113 /** 114 * Create a tab that will load the given url, and define an autoplay policy 115 * check function inside the content window in that tab. This function should 116 * only be used when `dom.media.autoplay-policy-detection.enabled` is true. 117 * 118 * @param {url} url 119 * the url which the created tab should load 120 */ 121 async function createTabAndSetupPolicyAssertFunc(url) { 122 let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, url); 123 await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { 124 content.video = content.document.createElement("video"); 125 content.ac = new content.AudioContext(); 126 content.assertAutoplayPolicy = ({ 127 resultForElementType, 128 resultForElement, 129 resultForContextType, 130 resultForContext, 131 }) => { 132 is( 133 content.navigator.getAutoplayPolicy("mediaelement"), 134 resultForElementType, 135 "getAutoplayPolicy('mediaelement') returns correct value" 136 ); 137 is( 138 content.navigator.getAutoplayPolicy(content.video), 139 resultForElement, 140 "getAutoplayPolicy(content.video) returns correct value" 141 ); 142 // note, per spec "allowed-muted" won't be used for audio context. 143 is( 144 content.navigator.getAutoplayPolicy("audiocontext"), 145 resultForContextType, 146 "getAutoplayPolicy('audiocontext') returns correct value" 147 ); 148 is( 149 content.navigator.getAutoplayPolicy(content.ac), 150 resultForContext, 151 "getAutoplayPolicy(content.ac) returns correct value" 152 ); 153 }; 154 }); 155 return tab; 156 }