dom/media/test/test_streams_individual_pause.html
author Andreas Pehrson <apehrson@mozilla.com>
Mon, 26 Nov 2018 16:54:13 +0000
changeset 507218 759ec08eebaaa4418a3a96b20d5942d55a3cafcc
parent 475237 1076e77e7b5c6f5e443da46820d7571c291bb399
child 531525 ba6f655fd68963530c866d0d4a48c3db3d307777
permissions -rw-r--r--
Bug 1306999 - Change from "playing" to "loadeddata" before assuming there's a video frame. r=jib Differential Revision: https://phabricator.services.mozilla.com/D12929

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for bug 1073406. Pausing a video element should not pause another playing the same stream.</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="manifest.js"></script>
  <script type="text/javascript" src="gUM_support.js"></script>
</head>
<body>
<video id="video1" autoplay></video>
<video id="video2" autoplay></video>
<script class="testbody" type="text/javascript">
async function startTest() {
  function getVideoImagePixelData(v) {
    let canvas = document.createElement("canvas");
    let ctx = canvas.getContext("2d");
    ctx.drawImage(v, 0, 0);
    let imgData = ctx.getImageData(canvas.width/2, canvas.height/2, 1, 1).data;
    return "r" + imgData[0] +
           "g" + imgData[1] +
           "b" + imgData[2] +
           "a" + imgData[3];
  }

  try {
    // This test expects fake devices so that the video color will change
    // over time, explicitly request fakes.
    await pushGetUserMediaTestPrefs({fakeAudio: true, fakeVideo: true});
    let stream = await navigator.mediaDevices.getUserMedia({video: true});
    let video1 = document.getElementById('video1');
    let video2 = document.getElementById('video2');

    video1.srcObject = stream;
    video2.srcObject = stream;

    video1.onloadeddata = () => video1.pause();

    let v1PausedImageData;
    let v2PausedImageData;

    video1.onpause = function() {
      v1PausedImageData = getVideoImagePixelData(video1);
      v2PausedImageData = getVideoImagePixelData(video2);
      v2TimesToTest = 3;
      video2.ontimeupdate = function() {
        if (getVideoImagePixelData(video2) === v2PausedImageData) {
          // Wait until video2 has progressed it's video.
          // If it doesn't, we'll time out and fail.
          info("video2 has not progressed. Waiting.");
          return;
        }

        if (--v2TimesToTest > 0) {
          // Wait for a while to be sure video1 would have gotten a frame
          // if it is playing.
          info("video2 progressed OK");
          return;
        }

        video2.ontimeupdate = null;
        ok(true, "video2 is playing");
        isnot(video1.currentTime, video2.currentTime,
              "v1 and v2 should not be at the same currentTime");
        is(v1PausedImageData, getVideoImagePixelData(video1),
           "video1 video frame should not have updated since video1 paused");
        SimpleTest.finish();
      };
    };
  } catch (error) {
    ok(false, "getUserMedia should not fail, got " + error.name);
    SimpleTest.finish();
  }
}

SimpleTest.waitForExplicitFinish();
startTest();
</script>
</body>
</html>