From 6557ceeb6636d5466dfb00d39dc2b2abb48d1b16 Mon Sep 17 00:00:00 2001 From: alwu Date: Fri, 6 Mar 2020 17:53:28 +0000 Subject: [PATCH] Bug 1617033 - part9 : add test. r=chunmin Differential Revision: https://phabricator.services.mozilla.com/D63929 --HG-- extra : moz-landing-system : lando --- dom/media/mediacontrol/tests/browser.ini | 5 + ...rowser_media_control_non_eligible_media.js | 127 ++++++++++++++++++ .../tests/file_non_eligible_media.html | 13 ++ dom/media/moz.build | 1 + 4 files changed, 146 insertions(+) create mode 100644 dom/media/mediacontrol/tests/browser_media_control_non_eligible_media.js create mode 100644 dom/media/mediacontrol/tests/file_non_eligible_media.html diff --git a/dom/media/mediacontrol/tests/browser.ini b/dom/media/mediacontrol/tests/browser.ini index 859b3c5c1e4d..3eaa13c9843a 100644 --- a/dom/media/mediacontrol/tests/browser.ini +++ b/dom/media/mediacontrol/tests/browser.ini @@ -3,9 +3,14 @@ support-files = file_autoplay.html file_muted_autoplay.html file_non_autoplay.html + file_non_eligible_media.html head.js ../../test/gizmo.mp4 + ../../test/gizmo-noaudio.webm + ../../test/gizmo-short.mp4 + ../../../../toolkit/content/tests/browser/silentAudioTrack.webm [browser_audio_focus_management.js] [browser_media_control_metadata.js] [browser_media_control_keys_event.js] +[browser_media_control_non_eligible_media.js] diff --git a/dom/media/mediacontrol/tests/browser_media_control_non_eligible_media.js b/dom/media/mediacontrol/tests/browser_media_control_non_eligible_media.js new file mode 100644 index 000000000000..a42f2e496601 --- /dev/null +++ b/dom/media/mediacontrol/tests/browser_media_control_non_eligible_media.js @@ -0,0 +1,127 @@ +/* eslint-disable no-undef */ +const PAGE_NON_ELIGIBLE_MEDIA = + "https://example.com/browser/dom/media/mediacontrol/tests/file_non_eligible_media.html"; + +// This array contains the elements' id in `file_non_eligible_media.html`. +const gNonEligibleElementIds = [ + "muted", + "volume-0", + "silent-audio-track", + "no-audio-track", + "short-duration", +]; + +/** + * This test is used to test couples of things about what kinds of media is + * eligible for being controlled by media control keys. + * (1) If media is inaudible all the time, then we would not control it. + * (2) If media starts inaudibly, we would not try to control it. But once it + * becomes audible later, we would keep controlling it until it's detroyed. + * (3) If media's duration is too short (<3s), then we would not control it. + */ +add_task(async function setupTestingPref() { + await SpecialPowers.pushPrefEnv({ + set: [["media.mediacontrol.testingevents.enabled", true]], + }); +}); + +add_task(async function testPlayPauseAndStop() { + for (const elementId of gNonEligibleElementIds) { + info(`- open new tab and start non eligible media ${elementId} -`); + const tab = await createTabAndLoad(PAGE_NON_ELIGIBLE_MEDIA); + await startNonEligibleMedia(tab, elementId); + + // Generate media control event should be postponed for a while to ensure + // that we didn't create any controller. + info(`- let media play for a while -`); + await checkIfMediaIsStillPlaying(tab, elementId); + + info(`- simulate pressing 'pause' media control key -`); + ChromeUtils.generateMediaControlKeysTestEvent("pause"); + + info(`- non eligible media won't be controlled by media control -`); + await checkIfMediaIsStillPlaying(tab, elementId); + + if (couldElementBecomeEligible(elementId)) { + info(`- make element ${elementId} audible -`); + await makeElementEligible(tab, elementId); + + info(`- simulate pressing 'pause' media control key -`); + ChromeUtils.generateMediaControlKeysTestEvent("pause"); + + info(`- audible media should be controlled by media control -`); + await waitUntilMediaPaused(tab, elementId); + } + + info(`remove tab`); + await BrowserTestUtils.removeTab(tab); + } +}); + +/** + * The following are helper functions. + */ +function startNonEligibleMedia(tab, elementId) { + return SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => { + const video = content.document.getElementById(Id); + if (!video) { + ok(false, `can't get the media element!`); + } + if (Id == "volume-0") { + video.volume = 0.0; + } + return video.play(); + }); +} + +function checkIfMediaIsStillPlaying(tab, elementId) { + return SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => { + const video = content.document.getElementById(Id); + if (!video) { + ok(false, `can't get the media element!`); + } + return new Promise(r => { + // In order to test "media isn't affected by media control", we would not + // only check `mPaused`, we would also oberve "timeupdate" event multiple + // times to ensure that video is still playing continually. + let timeUpdateCount = 0; + ok(!video.paused); + video.ontimeupdate = () => { + if (++timeUpdateCount == 3) { + video.ontimeupdate = null; + r(); + } + }; + }); + }); +} + +function couldElementBecomeEligible(elementId) { + return elementId == "muted" || elementId == "volume-0"; +} + +function makeElementEligible(tab, elementId) { + return SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => { + const video = content.document.getElementById(Id); + if (!video) { + ok(false, `can't get the media element!`); + } + // to turn inaudible media become audible in order to be controlled. + video.volume = 1.0; + video.muted = false; + }); +} + +function waitUntilMediaPaused(tab, elementId) { + return SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => { + const video = content.document.getElementById(Id); + if (!video) { + ok(false, `can't get the media element!`); + } + if (video.paused) { + ok(true, "media has been paused"); + return Promise.resolve(); + } + return new Promise(r => (video.onpaused = r)); + }); +} diff --git a/dom/media/mediacontrol/tests/file_non_eligible_media.html b/dom/media/mediacontrol/tests/file_non_eligible_media.html new file mode 100644 index 000000000000..d64ed3abf759 --- /dev/null +++ b/dom/media/mediacontrol/tests/file_non_eligible_media.html @@ -0,0 +1,13 @@ + + + +Media are not eligible to be controlled + + + + + + + + + diff --git a/dom/media/moz.build b/dom/media/moz.build index 902b08db4052..3c02e551390d 100644 --- a/dom/media/moz.build +++ b/dom/media/moz.build @@ -356,6 +356,7 @@ LOCAL_INCLUDES += [ '/layout/xul', '/media/libyuv/libyuv/include', '/netwerk/base', + '/toolkit/content/tests/browser/', ] if CONFIG['MOZ_WEBRTC']: