Bug 1733902 - part4 : add a test. r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D128165
This commit is contained in:
alwu 2021-11-01 22:26:41 +00:00
Родитель 5e5944d9ec
Коммит aa1981beb0
3 изменённых файлов: 118 добавлений и 1 удалений

Просмотреть файл

@ -6,6 +6,7 @@ support-files =
empty.png
file_contentTitle.html
file_empty.html
file_iframe_media.html
file_findinframe.html
file_mediaPlayback2.html
file_multipleAudio.html
@ -15,6 +16,7 @@ support-files =
file_redirect_to.html
file_silentAudioTrack.html
file_webAudio.html
gizmo.mp4
head.js
image.jpg
image_page.html
@ -24,6 +26,8 @@ support-files =
skip-if = true # Bug 1312652
[browser_autoscroll_disabled_on_editable_content.js]
[browser_autoscroll_disabled_on_links.js]
[browser_delay_autoplay_cross_origin_iframe.js]
tags = audiochannel
[browser_delay_autoplay_media.js]
tags = audiochannel
skip-if =
@ -130,7 +134,7 @@ skip-if = e10s # Bug ?????? - test directly manipulates content (gBrowser.conten
[browser_saveImageURL.js]
[browser_starting_autoscroll_in_about_content.js]
[browser_resume_bkg_video_on_tab_hover.js]
skip-if =
skip-if =
os == "win" && processor == "aarch64" # Bug 1536573
debug # Bug 1388959
[browser_about_networking.js]

Просмотреть файл

@ -0,0 +1,99 @@
/**
* After the tab has been visited, all media should be able to start playing.
* This test is used to ensure that playing media from a cross-origin iframe in
* a tab that has been already visited won't fail.
*/
"use strict";
add_task(async function setupTestEnvironment() {
await SpecialPowers.pushPrefEnv({
set: [
["media.autoplay.default", 0],
["media.block-autoplay-until-in-foreground", true],
],
});
});
add_task(async function testCrossOriginIframeShouldBeAbleToStart() {
info("Create a new foreground tab");
const originalTab = gBrowser.selectedTab;
const tab = await BrowserTestUtils.openNewForegroundTab(
window.gBrowser,
"about:blank"
);
info("As tab has been visited, media should be allowed to start");
const MEDIA_FILE = "gizmo.mp4";
await SpecialPowers.spawn(
tab.linkedBrowser,
[getTestWebBasedURL(MEDIA_FILE)],
async url => {
let vid = content.document.createElement("video");
vid.src = url;
ok(
await vid.play().then(
_ => true,
_ => false
),
"video started playing"
);
}
);
info("Make the tab to background");
await BrowserTestUtils.switchTab(gBrowser, originalTab);
info(
"As tab has been visited, a cross-origin iframe should be able to start media"
);
const IFRAME_FILE = "file_iframe_media.html";
await createIframe(
tab.linkedBrowser,
getTestWebBasedURL(IFRAME_FILE, { crossOrigin: true })
);
await ensureCORSIframeCanStartPlayingMedia(tab.linkedBrowser);
info("Remove tab");
BrowserTestUtils.removeTab(tab);
});
/**
* Following are helper functions
*/
function getTestWebBasedURL(fileName, { crossOrigin = false } = {}) {
const origin = crossOrigin ? "http://example.org" : "http://example.com";
return (
getRootDirectory(gTestPath).replace("chrome://mochitests/content", origin) +
fileName
);
}
function createIframe(browser, iframeUrl) {
return SpecialPowers.spawn(browser, [iframeUrl], async url => {
info(`Create iframe and wait until it finishes loading`);
const iframe = content.document.createElement("iframe");
const iframeLoaded = new Promise(r => (iframe.onload = r));
iframe.src = url;
content.document.body.appendChild(iframe);
await iframeLoaded;
});
}
function ensureCORSIframeCanStartPlayingMedia(browser) {
return SpecialPowers.spawn(browser, [], async _ => {
info(`check if media in iframe can start playing`);
const iframe = content.document.querySelector("iframe");
if (!iframe) {
ok(false, `can not get the iframe!`);
return;
}
const playPromise = new Promise(r => {
content.onmessage = event => {
is(event.data, "played", `started playing media from CORS iframe`);
r();
};
});
iframe.contentWindow.postMessage("play", "*");
await playPromise;
});
}

Просмотреть файл

@ -0,0 +1,14 @@
<!DOCTYPE html>
<video id="video" src="gizmo.mp4" loop></video>
<script type="text/javascript">
window.onmessage = async event => {
const video = document.getElementById("video");
const w = window.opener || window.parent;
if (event.data == "play") {
await video.play();
w.postMessage("played", "*");
}
}
</script>