Bug 1945643 - Update to mozilla-nimbus-schemas 2025.1.1 r=chumphreys
[gecko.git] / dom / media / mediasession / test / test_setactionhandler.html
blob000e4c3b3766f8b9498492349df9f2bb98894759
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <title></title>
5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
7 </head>
8 <body>
9 <script>
11 SimpleTest.waitForExplicitFinish();
13 const ACTIONS = [
14 "play",
15 "pause",
16 "seekbackward",
17 "seekforward",
18 "previoustrack",
19 "nexttrack",
20 "skipad",
21 "seekto",
22 "seekforward",
23 "seekbackward",
24 "stop",
27 (async function testSetActionHandler() {
28 for (const action of ACTIONS) {
29 info(`Test setActionHandler for '${action}'`);
30 generateAction(action);
31 ok(true, "it's ok to do " + action + " without any handler");
33 let expectedDetails = generateActionDetails(action);
35 let fired = false;
36 await setHandlerAndTakeAction(action, details => {
37 ok(hasSameValue(details, expectedDetails), "get expected details for " + action);
38 fired = !fired;
39 clearActionHandler(action);
40 });
42 generateAction(action);
43 ok(fired, "handler of " + action + " is cleared");
46 SimpleTest.finish();
47 })();
49 function generateAction(action) {
50 let details = generateActionDetails(action);
51 SpecialPowers.wrap(navigator.mediaSession).notifyHandler(details);
54 function generateActionDetails(action) {
55 let details = { action };
56 if (action == "seekbackward" || action == "seekforward") {
57 details.seekOffset = 3.14159;
58 } else if (action == "seekto") {
59 details.seekTime = 1.618;
61 return details;
64 function setHandlerAndTakeAction(action, handler) {
65 let promise = new Promise(resolve => {
66 navigator.mediaSession.setActionHandler(action, details => {
67 handler(details);
68 resolve();
69 });
70 });
71 generateAction(action);
72 return promise;
75 function hasSameValue(a, b) {
76 // The order of the object matters when stringify the object.
77 return JSON.stringify(a) == JSON.stringify(b);
80 function clearActionHandler(action) {
81 navigator.mediaSession.setActionHandler(action, null);
84 </script>
85 </body>
86 </html>