tor-browser

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

browser_resume_latest_paused_media.js (5827B)


      1 const PAGE_URL =
      2  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_multiple_audible_media.html";
      3 
      4 add_task(async function setupTestingPref() {
      5  await SpecialPowers.pushPrefEnv({
      6    set: [["media.mediacontrol.testingevents.enabled", true]],
      7  });
      8 });
      9 
     10 /**
     11 * This test is used to check when resuming media, we would only resume latest
     12 * paused media, not all media in the page.
     13 */
     14 add_task(async function testResumingLatestPausedMedias() {
     15  info(`open media page and play all media`);
     16  const tab = await createLoadedTabWrapper(PAGE_URL);
     17  await playAllMedia(tab);
     18 
     19  /**
     20   * Pressing `pause` key would pause video1, video2, video3
     21   * So resuming from media control key would affect those three media
     22   */
     23  info(`pressing 'pause' should pause all media`);
     24  await generateMediaControlKeyEvent("pause");
     25  await checkMediaPausedState(tab, {
     26    shouldVideo1BePaused: true,
     27    shouldVideo2BePaused: true,
     28    shouldVideo3BePaused: true,
     29  });
     30 
     31  info(`all media are latest paused, pressing 'play' should resume all`);
     32  await generateMediaControlKeyEvent("play");
     33  await checkMediaPausedState(tab, {
     34    shouldVideo1BePaused: false,
     35    shouldVideo2BePaused: false,
     36    shouldVideo3BePaused: false,
     37  });
     38 
     39  info(`pause only one playing video by calling its webidl method`);
     40  await pauseMedia(tab, "video3");
     41  await checkMediaPausedState(tab, {
     42    shouldVideo1BePaused: false,
     43    shouldVideo2BePaused: false,
     44    shouldVideo3BePaused: true,
     45  });
     46 
     47  /**
     48   * Pressing `pause` key would pause video1, video2
     49   * So resuming from media control key would affect those two media
     50   */
     51  info(`pressing 'pause' should pause two playing media`);
     52  await generateMediaControlKeyEvent("pause");
     53  await checkMediaPausedState(tab, {
     54    shouldVideo1BePaused: true,
     55    shouldVideo2BePaused: true,
     56    shouldVideo3BePaused: true,
     57  });
     58 
     59  info(`two media are latest paused, pressing 'play' should only affect them`);
     60  await generateMediaControlKeyEvent("play");
     61  await checkMediaPausedState(tab, {
     62    shouldVideo1BePaused: false,
     63    shouldVideo2BePaused: false,
     64    shouldVideo3BePaused: true,
     65  });
     66 
     67  info(`pause only one playing video by calling its webidl method`);
     68  await pauseMedia(tab, "video2");
     69  await checkMediaPausedState(tab, {
     70    shouldVideo1BePaused: false,
     71    shouldVideo2BePaused: true,
     72    shouldVideo3BePaused: true,
     73  });
     74 
     75  /**
     76   * Pressing `pause` key would pause video1
     77   * So resuming from media control key would only affect one media
     78   */
     79  info(`pressing 'pause' should pause one playing media`);
     80  await generateMediaControlKeyEvent("pause");
     81  await checkMediaPausedState(tab, {
     82    shouldVideo1BePaused: true,
     83    shouldVideo2BePaused: true,
     84    shouldVideo3BePaused: true,
     85  });
     86 
     87  info(`one media is latest paused, pressing 'play' should only affect it`);
     88  await generateMediaControlKeyEvent("play");
     89  await checkMediaPausedState(tab, {
     90    shouldVideo1BePaused: false,
     91    shouldVideo2BePaused: true,
     92    shouldVideo3BePaused: true,
     93  });
     94 
     95  /**
     96   * Only one media is playing, so pausing it should not stop controlling media.
     97   * We should still be able to resume it later.
     98   */
     99  info(`pause only playing video by calling its webidl method`);
    100  await pauseMedia(tab, "video1");
    101  await checkMediaPausedState(tab, {
    102    shouldVideo1BePaused: true,
    103    shouldVideo2BePaused: true,
    104    shouldVideo3BePaused: true,
    105  });
    106 
    107  info(`pressing 'pause' for already paused media, nothing would happen`);
    108  // All media are already paused, so no need to wait for playback state change,
    109  // call the method directly.
    110  MediaControlService.generateMediaControlKey("pause");
    111 
    112  info(`pressing 'play' would still affect on latest paused media`);
    113  await generateMediaControlKeyEvent("play");
    114  await checkMediaPausedState(tab, {
    115    shouldVideo1BePaused: false,
    116    shouldVideo2BePaused: true,
    117    shouldVideo3BePaused: true,
    118  });
    119 
    120  info(`remove tab`);
    121  await tab.close();
    122 });
    123 
    124 /**
    125 * The following are helper functions.
    126 */
    127 async function playAllMedia(tab) {
    128  const playbackStateChangedPromise = waitUntilDisplayedPlaybackChanged();
    129  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    130    return new Promise(r => {
    131      const videos = content.document.getElementsByTagName("video");
    132      let mediaCount = 0;
    133      docShell.chromeEventHandler.addEventListener(
    134        "MozStartMediaControl",
    135        () => {
    136          if (++mediaCount == videos.length) {
    137            info(`all media have started media control`);
    138            r();
    139          }
    140        }
    141      );
    142      for (let video of videos) {
    143        info(`play ${video.id} video`);
    144        video.play();
    145      }
    146    });
    147  });
    148  await playbackStateChangedPromise;
    149 }
    150 
    151 async function pauseMedia(tab, videoId) {
    152  await SpecialPowers.spawn(tab.linkedBrowser, [videoId], videoId => {
    153    const video = content.document.getElementById(videoId);
    154    if (!video) {
    155      ok(false, `can not find ${videoId}!`);
    156    }
    157    video.pause();
    158  });
    159 }
    160 
    161 function checkMediaPausedState(
    162  tab,
    163  { shouldVideo1BePaused, shouldVideo2BePaused, shouldVideo3BePaused }
    164 ) {
    165  return SpecialPowers.spawn(
    166    tab.linkedBrowser,
    167    [shouldVideo1BePaused, shouldVideo2BePaused, shouldVideo3BePaused],
    168    (shouldVideo1BePaused, shouldVideo2BePaused, shouldVideo3BePaused) => {
    169      const video1 = content.document.getElementById("video1");
    170      const video2 = content.document.getElementById("video2");
    171      const video3 = content.document.getElementById("video3");
    172      is(
    173        video1.paused,
    174        shouldVideo1BePaused,
    175        "Correct paused state for video1"
    176      );
    177      is(
    178        video2.paused,
    179        shouldVideo2BePaused,
    180        "Correct paused state for video2"
    181      );
    182      is(
    183        video3.paused,
    184        shouldVideo3BePaused,
    185        "Correct paused state for video3"
    186      );
    187    }
    188  );
    189 }