From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../mediasession/test/MediaSessionTestUtils.js | 30 ++++ dom/media/mediasession/test/browser.toml | 9 + .../test/browser_active_mediasession_among_tabs.js | 198 +++++++++++++++++++++ .../mediasession/test/crashtests/crashtests.list | 1 + .../test/crashtests/inactive-mediasession.html | 16 ++ .../mediasession/test/file_media_session.html | 30 ++++ .../test/file_trigger_actionhanlder_frame.html | 40 +++++ .../test/file_trigger_actionhanlder_window.html | 107 +++++++++++ dom/media/mediasession/test/mochitest.toml | 14 ++ .../mediasession/test/test_setactionhandler.html | 84 +++++++++ .../test/test_trigger_actionhanlder.html | 56 ++++++ 11 files changed, 585 insertions(+) create mode 100644 dom/media/mediasession/test/MediaSessionTestUtils.js create mode 100644 dom/media/mediasession/test/browser.toml create mode 100644 dom/media/mediasession/test/browser_active_mediasession_among_tabs.js create mode 100644 dom/media/mediasession/test/crashtests/crashtests.list create mode 100644 dom/media/mediasession/test/crashtests/inactive-mediasession.html create mode 100644 dom/media/mediasession/test/file_media_session.html create mode 100644 dom/media/mediasession/test/file_trigger_actionhanlder_frame.html create mode 100644 dom/media/mediasession/test/file_trigger_actionhanlder_window.html create mode 100644 dom/media/mediasession/test/mochitest.toml create mode 100644 dom/media/mediasession/test/test_setactionhandler.html create mode 100644 dom/media/mediasession/test/test_trigger_actionhanlder.html (limited to 'dom/media/mediasession/test') diff --git a/dom/media/mediasession/test/MediaSessionTestUtils.js b/dom/media/mediasession/test/MediaSessionTestUtils.js new file mode 100644 index 0000000000..1ab0e1fe9b --- /dev/null +++ b/dom/media/mediasession/test/MediaSessionTestUtils.js @@ -0,0 +1,30 @@ +const gMediaSessionActions = [ + "play", + "pause", + "seekbackward", + "seekforward", + "previoustrack", + "nexttrack", + "skipad", + "seekto", + "stop", +]; + +// gCommands and gResults are used in `test_active_mediasession_within_page.html` +const gCommands = { + createMainFrameSession: "create-main-frame-session", + createChildFrameSession: "create-child-frame-session", + destroyChildFrameSessions: "destroy-child-frame-sessions", + destroyActiveChildFrameSession: "destroy-active-child-frame-session", + destroyInactiveChildFrameSession: "destroy-inactive-child-frame-session", +}; + +const gResults = { + mainFrameSession: "main-frame-session", + childFrameSession: "child-session-unchanged", + childFrameSessionUpdated: "child-session-changed", +}; + +function nextWindowMessage() { + return new Promise(r => (window.onmessage = event => r(event))); +} diff --git a/dom/media/mediasession/test/browser.toml b/dom/media/mediasession/test/browser.toml new file mode 100644 index 0000000000..1732d096a0 --- /dev/null +++ b/dom/media/mediasession/test/browser.toml @@ -0,0 +1,9 @@ +[DEFAULT] +subsuite = "media-bc" +tags = "mediacontrol" +support-files = [ + "file_media_session.html", + "../../test/gizmo.mp4", +] + +["browser_active_mediasession_among_tabs.js"] diff --git a/dom/media/mediasession/test/browser_active_mediasession_among_tabs.js b/dom/media/mediasession/test/browser_active_mediasession_among_tabs.js new file mode 100644 index 0000000000..a70d4502b6 --- /dev/null +++ b/dom/media/mediasession/test/browser_active_mediasession_among_tabs.js @@ -0,0 +1,198 @@ +/* eslint-disable no-undef */ +"use strict"; + +const PAGE = + "https://example.com/browser/dom/media/mediasession/test/file_media_session.html"; + +const ACTION = "previoustrack"; + +add_task(async function setupTestingPref() { + await SpecialPowers.pushPrefEnv({ + set: [["media.mediacontrol.testingevents.enabled", true]], + }); +}); + +/** + * When multiple tabs are all having media session, the latest created one would + * become an active session. When the active media session is destroyed via + * closing the tab, the previous active session would become current active + * session again. + */ +add_task(async function testActiveSessionWhenClosingTab() { + info(`open tab1 and load media session test page`); + const tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE); + await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab1); + + info(`pressing '${ACTION}' key`); + MediaControlService.generateMediaControlKey(ACTION); + + info(`session in tab1 should become active session`); + await checkIfActionReceived(tab1, ACTION); + + info(`open tab2 and load media session test page`); + const tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE); + await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab2); + + info(`pressing '${ACTION}' key`); + MediaControlService.generateMediaControlKey(ACTION); + + info(`session in tab2 should become active session`); + await checkIfActionReceived(tab2, ACTION); + await checkIfActionNotReceived(tab1, ACTION); + + info(`remove tab2`); + const controllerChanged = waitUntilMainMediaControllerChanged(); + BrowserTestUtils.removeTab(tab2); + await controllerChanged; + + info(`pressing '${ACTION}' key`); + MediaControlService.generateMediaControlKey(ACTION); + + info(`session in tab1 should become active session again`); + await checkIfActionReceived(tab1, ACTION); + + info(`remove tab1`); + BrowserTestUtils.removeTab(tab1); +}); + +/** + * This test is similar with `testActiveSessionWhenClosingTab`, the difference + * is that the way we use to destroy active session is via naviagation, not + * closing tab. + */ +add_task(async function testActiveSessionWhenNavigatingTab() { + info(`open tab1 and load media session test page`); + const tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE); + await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab1); + + info(`pressing '${ACTION}' key`); + MediaControlService.generateMediaControlKey(ACTION); + + info(`session in tab1 should become active session`); + await checkIfActionReceived(tab1, ACTION); + + info(`open tab2 and load media session test page`); + const tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE); + await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab2); + + info(`pressing '${ACTION}' key`); + MediaControlService.generateMediaControlKey(ACTION); + + info(`session in tab2 should become active session`); + await checkIfActionReceived(tab2, ACTION); + await checkIfActionNotReceived(tab1, ACTION); + + info(`navigate tab2 to blank page`); + const controllerChanged = waitUntilMainMediaControllerChanged(); + BrowserTestUtils.startLoadingURIString(tab2.linkedBrowser, "about:blank"); + await controllerChanged; + + info(`pressing '${ACTION}' key`); + MediaControlService.generateMediaControlKey(ACTION); + + info(`session in tab1 should become active session`); + await checkIfActionReceived(tab1, ACTION); + + info(`remove tabs`); + BrowserTestUtils.removeTab(tab1); + BrowserTestUtils.removeTab(tab2); +}); + +/** + * If we create a media session in a tab where no any playing media exists, then + * that session would not involve in global active media session selection. The + * current active media session would remain unchanged. + */ +add_task(async function testCreatingSessionWithoutPlayingMedia() { + info(`open tab1 and load media session test page`); + const tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE); + await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab1); + + info(`pressing '${ACTION}' key`); + MediaControlService.generateMediaControlKey(ACTION); + + info(`session in tab1 should become active session`); + await checkIfActionReceived(tab1, ACTION); + + info(`open tab2 and load media session test page`); + const tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE); + + info(`pressing '${ACTION}' key`); + MediaControlService.generateMediaControlKey(ACTION); + + info( + `session in tab1 is still an active session because there is no media playing in tab2` + ); + await checkIfActionReceived(tab1, ACTION); + await checkIfActionNotReceived(tab2, ACTION); + + info(`remove tabs`); + BrowserTestUtils.removeTab(tab1); + BrowserTestUtils.removeTab(tab2); +}); + +/** + * The following are helper functions + */ +async function startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab) { + await Promise.all([ + BrowserUtils.promiseObserved("active-media-session-changed"), + SpecialPowers.spawn(tab.linkedBrowser, [], () => { + const video = content.document.getElementById("testVideo"); + if (!video) { + ok(false, `can't get the media element!`); + } + video.play(); + }), + ]); +} + +async function checkIfActionReceived(tab, action) { + await SpecialPowers.spawn(tab.linkedBrowser, [action], expectedAction => { + return new Promise(resolve => { + const result = content.document.getElementById("result"); + if (!result) { + ok(false, `can't get the element for showing result!`); + } + + function checkAction() { + is( + result.innerHTML, + expectedAction, + `received '${expectedAction}' correctly` + ); + // Reset the result after finishing checking result, then we can dispatch + // same action again without worrying about previous result. + result.innerHTML = ""; + resolve(); + } + + if (result.innerHTML == "") { + info(`wait until receiving action`); + result.addEventListener("actionChanged", () => checkAction(), { + once: true, + }); + } else { + checkAction(); + } + }); + }); +} + +async function checkIfActionNotReceived(tab, action) { + await SpecialPowers.spawn(tab.linkedBrowser, [action], expectedAction => { + return new Promise(resolve => { + const result = content.document.getElementById("result"); + if (!result) { + ok(false, `can't get the element for showing result!`); + } + is(result.innerHTML, "", `should not receive any action`); + ok(result.innerHTML != expectedAction, `not receive '${expectedAction}'`); + resolve(); + }); + }); +} + +function waitUntilMainMediaControllerChanged() { + return BrowserUtils.promiseObserved("main-media-controller-changed"); +} diff --git a/dom/media/mediasession/test/crashtests/crashtests.list b/dom/media/mediasession/test/crashtests/crashtests.list new file mode 100644 index 0000000000..9ca4956ab6 --- /dev/null +++ b/dom/media/mediasession/test/crashtests/crashtests.list @@ -0,0 +1 @@ +load inactive-mediasession.html diff --git a/dom/media/mediasession/test/crashtests/inactive-mediasession.html b/dom/media/mediasession/test/crashtests/inactive-mediasession.html new file mode 100644 index 0000000000..b24fb887ff --- /dev/null +++ b/dom/media/mediasession/test/crashtests/inactive-mediasession.html @@ -0,0 +1,16 @@ + + + + diff --git a/dom/media/mediasession/test/file_media_session.html b/dom/media/mediasession/test/file_media_session.html new file mode 100644 index 0000000000..7cff7f4f69 --- /dev/null +++ b/dom/media/mediasession/test/file_media_session.html @@ -0,0 +1,30 @@ + + + + Media Session and non-autoplay media + + + +

+ + + diff --git a/dom/media/mediasession/test/file_trigger_actionhanlder_frame.html b/dom/media/mediasession/test/file_trigger_actionhanlder_frame.html new file mode 100644 index 0000000000..4d55db2189 --- /dev/null +++ b/dom/media/mediasession/test/file_trigger_actionhanlder_frame.html @@ -0,0 +1,40 @@ + + + + Test frame for triggering media session's action handler + + + + + + + diff --git a/dom/media/mediasession/test/file_trigger_actionhanlder_window.html b/dom/media/mediasession/test/file_trigger_actionhanlder_window.html new file mode 100644 index 0000000000..f3316d6dad --- /dev/null +++ b/dom/media/mediasession/test/file_trigger_actionhanlder_window.html @@ -0,0 +1,107 @@ + + + + Test window for triggering media session's action handler + + + + + + + + + diff --git a/dom/media/mediasession/test/mochitest.toml b/dom/media/mediasession/test/mochitest.toml new file mode 100644 index 0000000000..130c08ab00 --- /dev/null +++ b/dom/media/mediasession/test/mochitest.toml @@ -0,0 +1,14 @@ +[DEFAULT] +subsuite = "media" +tags = "mediasession mediacontrol" + +support-files = [ + "../../test/gizmo.mp4", + "file_trigger_actionhanlder_frame.html", + "file_trigger_actionhanlder_window.html", + "MediaSessionTestUtils.js", +] + +["test_setactionhandler.html"] + +["test_trigger_actionhanlder.html"] diff --git a/dom/media/mediasession/test/test_setactionhandler.html b/dom/media/mediasession/test/test_setactionhandler.html new file mode 100644 index 0000000000..77d4703a3b --- /dev/null +++ b/dom/media/mediasession/test/test_setactionhandler.html @@ -0,0 +1,84 @@ + + + + + + + + + + + diff --git a/dom/media/mediasession/test/test_trigger_actionhanlder.html b/dom/media/mediasession/test/test_trigger_actionhanlder.html new file mode 100644 index 0000000000..a0e170c868 --- /dev/null +++ b/dom/media/mediasession/test/test_trigger_actionhanlder.html @@ -0,0 +1,56 @@ + + + + Test of triggering media session's action handlers + + + + + + + + -- cgit v1.2.3