Commit 6557ceeb authored by alwu's avatar alwu
Browse files

Bug 1617033 - part9 : add test. r=chunmin

Differential Revision: https://phabricator.services.mozilla.com/D63929

--HG--
extra : moz-landing-system : lando
parent 7dc5318e
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -3,9 +3,14 @@ support-files =
  file_autoplay.html
  file_muted_autoplay.html
  file_non_autoplay.html
  file_non_eligible_media.html
  head.js
  ../../test/gizmo.mp4
  ../../test/gizmo-noaudio.webm
  ../../test/gizmo-short.mp4
  ../../../../toolkit/content/tests/browser/silentAudioTrack.webm

[browser_audio_focus_management.js]
[browser_media_control_metadata.js]
[browser_media_control_keys_event.js]
[browser_media_control_non_eligible_media.js]
+127 −0
Original line number Diff line number Diff line
/* eslint-disable no-undef */
const PAGE_NON_ELIGIBLE_MEDIA =
  "https://example.com/browser/dom/media/mediacontrol/tests/file_non_eligible_media.html";

// This array contains the elements' id in `file_non_eligible_media.html`.
const gNonEligibleElementIds = [
  "muted",
  "volume-0",
  "silent-audio-track",
  "no-audio-track",
  "short-duration",
];

/**
 * This test is used to test couples of things about what kinds of media is
 * eligible for being controlled by media control keys.
 * (1) If media is inaudible all the time, then we would not control it.
 * (2) If media starts inaudibly, we would not try to control it. But once it
 * becomes audible later, we would keep controlling it until it's detroyed.
 * (3) If media's duration is too short (<3s), then we would not control it.
 */
add_task(async function setupTestingPref() {
  await SpecialPowers.pushPrefEnv({
    set: [["media.mediacontrol.testingevents.enabled", true]],
  });
});

add_task(async function testPlayPauseAndStop() {
  for (const elementId of gNonEligibleElementIds) {
    info(`- open new tab and start non eligible media ${elementId} -`);
    const tab = await createTabAndLoad(PAGE_NON_ELIGIBLE_MEDIA);
    await startNonEligibleMedia(tab, elementId);

    // Generate media control event should be postponed for a while to ensure
    // that we didn't create any controller.
    info(`- let media play for a while -`);
    await checkIfMediaIsStillPlaying(tab, elementId);

    info(`- simulate pressing 'pause' media control key -`);
    ChromeUtils.generateMediaControlKeysTestEvent("pause");

    info(`- non eligible media won't be controlled by media control -`);
    await checkIfMediaIsStillPlaying(tab, elementId);

    if (couldElementBecomeEligible(elementId)) {
      info(`- make element ${elementId} audible -`);
      await makeElementEligible(tab, elementId);

      info(`- simulate pressing 'pause' media control key -`);
      ChromeUtils.generateMediaControlKeysTestEvent("pause");

      info(`- audible media should be controlled by media control -`);
      await waitUntilMediaPaused(tab, elementId);
    }

    info(`remove tab`);
    await BrowserTestUtils.removeTab(tab);
  }
});

/**
 * The following are helper functions.
 */
function startNonEligibleMedia(tab, elementId) {
  return SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => {
    const video = content.document.getElementById(Id);
    if (!video) {
      ok(false, `can't get the media element!`);
    }
    if (Id == "volume-0") {
      video.volume = 0.0;
    }
    return video.play();
  });
}

function checkIfMediaIsStillPlaying(tab, elementId) {
  return SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => {
    const video = content.document.getElementById(Id);
    if (!video) {
      ok(false, `can't get the media element!`);
    }
    return new Promise(r => {
      // In order to test "media isn't affected by media control", we would not
      // only check `mPaused`, we would also oberve "timeupdate" event multiple
      // times to ensure that video is still playing continually.
      let timeUpdateCount = 0;
      ok(!video.paused);
      video.ontimeupdate = () => {
        if (++timeUpdateCount == 3) {
          video.ontimeupdate = null;
          r();
        }
      };
    });
  });
}

function couldElementBecomeEligible(elementId) {
  return elementId == "muted" || elementId == "volume-0";
}

function makeElementEligible(tab, elementId) {
  return SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => {
    const video = content.document.getElementById(Id);
    if (!video) {
      ok(false, `can't get the media element!`);
    }
    // to turn inaudible media become audible in order to be controlled.
    video.volume = 1.0;
    video.muted = false;
  });
}

function waitUntilMediaPaused(tab, elementId) {
  return SpecialPowers.spawn(tab.linkedBrowser, [elementId], Id => {
    const video = content.document.getElementById(Id);
    if (!video) {
      ok(false, `can't get the media element!`);
    }
    if (video.paused) {
      ok(true, "media has been paused");
      return Promise.resolve();
    }
    return new Promise(r => (video.onpaused = r));
  });
}
+13 −0
Original line number Diff line number Diff line
<!DOCTYPE html>
<html>
<head>
<title>Media are not eligible to be controlled</title>
</head>
<body>
<video id="muted" src="gizmo.mp4" controls muted loop></video>
<video id="volume-0" src="gizmo.mp4" controls loop></video>
<video id="no-audio-track" src="gizmo-noaudio.webm"  controls loop></video>
<video id="silent-audio-track" src="silentAudioTrack.webm" controls loop></video>
<video id="short-duration" src="gizmo-short.mp4" controls loop></video>
</body>
</html>
+1 −0
Original line number Diff line number Diff line
@@ -356,6 +356,7 @@ LOCAL_INCLUDES += [
    '/layout/xul',
    '/media/libyuv/libyuv/include',
    '/netwerk/base',
    '/toolkit/content/tests/browser/',
]

if CONFIG['MOZ_WEBRTC']: