browser_media_control_supported_keys.js (3985B)
1 const PAGE_NON_AUTOPLAY = 2 "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_non_autoplay.html"; 3 4 const testVideoId = "video"; 5 const sDefaultSupportedKeys = [ 6 "focus", 7 "play", 8 "pause", 9 "playpause", 10 "stop", 11 "seekto", 12 "seekforward", 13 "seekbackward", 14 ]; 15 16 add_task(async function setupTestingPref() { 17 await SpecialPowers.pushPrefEnv({ 18 set: [["media.mediacontrol.testingevents.enabled", true]], 19 }); 20 }); 21 22 /** 23 * Supported media keys are used for indicating what UI button should be shown 24 * on the virtual control interface. All supported media keys are listed in 25 * `MediaKey` in `MediaController.webidl`. Some media keys are defined as 26 * default media keys which are always supported. Otherwise, other media keys 27 * have to have corresponding action handler on the active media session in 28 * order to be added to the supported keys. 29 */ 30 add_task(async function testDefaultSupportedKeys() { 31 info(`open media page`); 32 const tab = await createLoadedTabWrapper(PAGE_NON_AUTOPLAY); 33 34 info(`start media`); 35 await playMedia(tab, testVideoId); 36 37 info(`should use default supported keys`); 38 await supportedKeysShouldEqualTo(tab, sDefaultSupportedKeys); 39 40 info(`remove tab`); 41 await tab.close(); 42 }); 43 44 add_task(async function testNoActionHandlerBeingSet() { 45 info(`open media page`); 46 const tab = await createLoadedTabWrapper(PAGE_NON_AUTOPLAY); 47 48 info(`start media`); 49 await playMedia(tab, testVideoId); 50 51 info(`create media session but not set any action handler`); 52 await setMediaSessionSupportedAction(tab, []); 53 54 info( 55 `should use default supported keys even if ` + 56 `media session doesn't have any action handler` 57 ); 58 await supportedKeysShouldEqualTo(tab, sDefaultSupportedKeys); 59 60 info(`remove tab`); 61 await tab.close(); 62 }); 63 64 add_task(async function testSettingActionsWhichAreAlreadyDefaultKeys() { 65 info(`open media page`); 66 const tab = await createLoadedTabWrapper(PAGE_NON_AUTOPLAY); 67 68 info(`start media`); 69 await playMedia(tab, testVideoId); 70 71 info(`create media session but not set any action handler`); 72 await setMediaSessionSupportedAction(tab, ["play", "pause", "stop"]); 73 74 info( 75 `those actions has already been included in default supported keys, so ` + 76 `the result should still be default supported keys` 77 ); 78 await supportedKeysShouldEqualTo(tab, sDefaultSupportedKeys); 79 80 info(`remove tab`); 81 await tab.close(); 82 }); 83 84 add_task(async function testSettingActionsWhichAreNotDefaultKeys() { 85 info(`open media page`); 86 const tab = await createLoadedTabWrapper(PAGE_NON_AUTOPLAY); 87 88 info(`start media`); 89 await playMedia(tab, testVideoId); 90 91 info(`create media session but not set any action handler`); 92 let nonDefaultActions = ["previoustrack", "nexttrack"]; 93 await setMediaSessionSupportedAction(tab, nonDefaultActions); 94 95 info( 96 `supported keys should include those actions which are not default supported keys` 97 ); 98 let expectedKeys = sDefaultSupportedKeys.concat(nonDefaultActions); 99 await supportedKeysShouldEqualTo(tab, expectedKeys); 100 101 info(`remove tab`); 102 await tab.close(); 103 }); 104 105 /** 106 * The following are helper functions. 107 */ 108 async function supportedKeysShouldEqualTo(tab, expectedKeys) { 109 const controller = tab.linkedBrowser.browsingContext.mediaController; 110 const supportedKeys = controller.supportedKeys; 111 while (JSON.stringify(expectedKeys) != JSON.stringify(supportedKeys)) { 112 await new Promise(r => (controller.onsupportedkeyschange = r)); 113 } 114 for (let idx = 0; idx < expectedKeys.length; idx++) { 115 is( 116 supportedKeys[idx], 117 expectedKeys[idx], 118 `'${supportedKeys[idx]}' should equal to '${expectedKeys[idx]}'` 119 ); 120 } 121 } 122 123 function setMediaSessionSupportedAction(tab, actions) { 124 return SpecialPowers.spawn(tab.linkedBrowser, [actions], actionArr => { 125 for (let action of actionArr) { 126 content.navigator.mediaSession.setActionHandler(action, () => { 127 info(`set '${action}' action handler`); 128 }); 129 } 130 }); 131 }