tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_media_control_stop_timer.js (2636B)


      1 // Import this in order to use `triggerPictureInPicture()`.
      2 Services.scriptloader.loadSubScript(
      3  "chrome://mochitests/content/browser/toolkit/components/pictureinpicture/tests/head.js",
      4  this
      5 );
      6 
      7 const PAGE_NON_AUTOPLAY =
      8  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_non_autoplay.html";
      9 
     10 const testVideoId = "video";
     11 
     12 add_task(async function setupTestingPref() {
     13  await SpecialPowers.pushPrefEnv({
     14    set: [
     15      ["media.mediacontrol.testingevents.enabled", true],
     16      ["media.mediacontrol.stopcontrol.timer", true],
     17      ["media.mediacontrol.stopcontrol.timer.ms", 0],
     18    ],
     19  });
     20 });
     21 
     22 /**
     23 * This test is used to check the stop timer for media element, which would stop
     24 * media control for the specific element when the element has been paused over
     25 * certain length of time. (That is controlled by the value of the pref
     26 * `media.mediacontrol.stopcontrol.timer.ms`) In this test, we set the pref to 0
     27 * which means the stop timer would be triggered after the media is paused.
     28 * However, if the media is being used in PIP mode, we won't start the stop
     29 * timer for it.
     30 */
     31 add_task(async function testStopMediaControlAfterPausingMedia() {
     32  info(`open media page`);
     33  const tab = await createLoadedTabWrapper(PAGE_NON_AUTOPLAY);
     34 
     35  info(`start media`);
     36  await playMedia(tab, testVideoId);
     37 
     38  info(`pause media and the stop timer would stop media control`);
     39  await pauseMediaAndMediaControlShouldBeStopped(tab, testVideoId);
     40 
     41  info(`remove tab`);
     42  await tab.close();
     43 });
     44 
     45 add_task(async function testNotToStopMediaControlForPIPVideo() {
     46  info(`open media page`);
     47  const tab = await createLoadedTabWrapper(PAGE_NON_AUTOPLAY);
     48 
     49  info(`start media`);
     50  await playMedia(tab, testVideoId);
     51 
     52  info(`trigger PIP mode`);
     53  const winPIP = await triggerPictureInPicture(tab.linkedBrowser, testVideoId);
     54 
     55  info(`pause media and the stop timer would not stop media control`);
     56  await pauseMedia(tab, testVideoId);
     57 
     58  info(`pressing 'play' key should start PIP video again`);
     59  await generateMediaControlKeyEvent("play");
     60  await checkOrWaitUntilMediaStartedPlaying(tab, testVideoId);
     61 
     62  info(`remove tab`);
     63  await BrowserTestUtils.closeWindow(winPIP);
     64  await tab.close();
     65 });
     66 
     67 /**
     68 * The following is helper function.
     69 */
     70 function pauseMediaAndMediaControlShouldBeStopped(tab, elementId) {
     71  // After pausing media, the stop timer would be triggered and stop the media
     72  // control.
     73  return Promise.all([
     74    new Promise(r => (tab.controller.ondeactivated = r)),
     75    SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => {
     76      content.document.getElementById(Id).pause();
     77    }),
     78  ]);
     79 }