4 <title>Test window for triggering media session's action handler
</title>
5 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
6 <script src=
"MediaSessionTestUtils.js"></script>
9 <video id=
"testVideo" src=
"gizmo.mp4" loop
></video>
10 <iframe id=
"childFrame"></iframe>
13 var triggeredActionNums
= 0;
15 nextWindowMessage().then(
17 const testInfo
= event
.data
;
18 await
createSession(testInfo
);
19 // Media session would only become active if there is any media currently
20 // playing. Non-active media session won't receive any actions. Therefore,
21 // we start media playback before testing media session.
22 await
startMediaPlayback(testInfo
);
23 for (const action
of gMediaSessionActions
) {
24 await
waitUntilActionHandlerTriggered(action
, testInfo
);
26 endTestAndReportResult();
30 * The following are helper functions
32 async
function startMediaPlayback({shouldCreateFrom
}) {
33 info(`wait until media starts playing`);
34 if (shouldCreateFrom
== "main-frame") {
35 const video
= document
.getElementById("testVideo");
37 // As we can't observe `media-displayed-playback-changed` notification,
38 // that can only be observed in the chrome process. Therefore, we use a
39 // workaround instead which is to wait for a while to ensure that the
40 // controller has already been created in the chrome process.
41 let timeupdatecount
= 0;
42 await
new Promise(r
=> video
.ontimeupdate
= () => {
43 if (++timeupdatecount
== 3) {
44 video
.ontimeupdate
= null;
49 const iframe
= document
.getElementById("childFrame");
50 iframe
.contentWindow
.postMessage("play", "*");
51 await
new Promise(r
=> {
52 window
.onmessage
= event
=> {
53 is(event
.data
, "played", `media started playing in child-frame`);
60 async
function createSession({shouldCreateFrom
, origin
}) {
61 info(`create media session in ${shouldCreateFrom}`);
62 if (shouldCreateFrom
== "main-frame") {
63 // Simply referencing media session will create media session.
64 navigator
.mediaSession
;
67 const frame
= document
.getElementById("childFrame");
68 const originURL
= origin
== "same-origin"
69 ? "http://mochi.test:8888" : "http://example.org";
70 frame
.src
= originURL
+ "/tests/dom/media/mediasession/test/file_trigger_actionhandler_frame.html";
71 await
new Promise(r
=> frame
.onload
= r
);
74 async
function waitUntilActionHandlerTriggered(action
, {shouldCreateFrom
}) {
75 info(`wait until '${action}' handler of media session created in ` +
76 `${shouldCreateFrom} is triggered`);
77 if (shouldCreateFrom
== "main-frame") {
78 let promise
= new Promise(resolve
=> {
79 navigator
.mediaSession
.setActionHandler(action
, () => {
80 ok(true, `Triggered ${action} handler`);
81 triggeredActionNums
++;
85 SpecialPowers
.generateMediaControlKeyTestEvent(action
);
89 SpecialPowers
.generateMediaControlKeyTestEvent(action
);
90 if ((await
nextWindowMessage()).data
== action
) {
91 info(`Triggered ${action} handler in child-frame`);
92 triggeredActionNums
++;
96 function endTestAndReportResult() {
97 const w
= window
.opener
|| window
.parent
;
98 if (triggeredActionNums
== gMediaSessionActions
.length
) {
99 w
.postMessage("success", "*");
101 w
.postMessage("fail", "*");