test_setactionhandler.html (1992B)
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> 10 11 SimpleTest.waitForExplicitFinish(); 12 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", 25 ]; 26 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"); 32 33 let expectedDetails = generateActionDetails(action); 34 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 }); 41 42 generateAction(action); 43 ok(fired, "handler of " + action + " is cleared"); 44 } 45 46 SimpleTest.finish(); 47 })(); 48 49 function generateAction(action) { 50 let details = generateActionDetails(action); 51 SpecialPowers.wrap(navigator.mediaSession).notifyHandler(details); 52 } 53 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; 60 } 61 return details; 62 } 63 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; 73 } 74 75 function hasSameValue(a, b) { 76 // The order of the object matters when stringify the object. 77 return JSON.stringify(a) == JSON.stringify(b); 78 } 79 80 function clearActionHandler(action) { 81 navigator.mediaSession.setActionHandler(action, null); 82 } 83 84 </script> 85 </body> 86 </html>