content/media/test/test_mediarecorder_record_no_timeslice.html
author Randy Lin <rlin@mozilla.com>
Wed, 07 May 2014 23:50:57 +0800
changeset 192228 e532119b92ab33dfb9d401bc825637547739e88a
parent 185540 ff1cf4f665ff53cf70939f9ea2bbc2c2c6b83f9a
child 210503 9b0718a01b13ef28b27c2bad52bfb3a59dbc94d4
permissions -rw-r--r--
Bug 969372 - Move mediaRecorder in global scope to avoid test timeout. r=jsmith, a=test-only

<!DOCTYPE HTML>
<html>
<head>
  <title>Test MediaRecorder Record No Timeslice</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>
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
var manager = new MediaTestManager;
// Need to clarify why declare mediaRecorder in startTest cause timeout.;
var mediaRecorder;

/**
 * Starts a test on every media recorder file included to check that a
 * stream derived from the file can be recorded with no time slice provided.
 */
function startTest(test, token) {
  var element = document.createElement('audio');
  var expectedMimeType = test.type.substring(0, test.type.indexOf(';'));

  element.token = token;
  manager.started(token);

  element.src = test.name;
  element.test = test;
  element.stream = element.mozCaptureStreamUntilEnded();

  mediaRecorder = new MediaRecorder(element.stream);
  var onStopFired = false;
  var onDataAvailableFired = false;

  mediaRecorder.onerror = function () {
    ok(false, 'Unexpected onerror callback fired');
  };

  mediaRecorder.onwarning = function () {
    ok(false, 'Unexpected onwarning callback fired');
  };

  // This handler verifies that only a single onstop event handler is fired.
  mediaRecorder.onstop = function () {
    if (onStopFired) {
      ok(false, 'onstop unexpectedly fired more than once');
    } else {
      onStopFired = true;

      // ondataavailable should always fire before onstop
      if (onDataAvailableFired) {
        ok(true, 'onstop fired after ondataavailable');
        manager.finished(token);
      } else {
        ok(false, 'onstop fired without an ondataavailable event first');
      }
    }
  };

  // This handler verifies that only a single ondataavailable event handler
  // is fired with the blob generated having greater than zero size
  // and a correct mime type.
  mediaRecorder.ondataavailable = function (evt) {
    if (onDataAvailableFired) {
      ok(false, 'ondataavailable unexpectedly fired more than once');
    } else {
      onDataAvailableFired = true;

      ok(evt instanceof BlobEvent,
         'Events fired from ondataavailable should be BlobEvent');
      is(evt.type, 'dataavailable',
         'Event type should dataavailable');
      ok(evt.data.size > 0,
         'Blob data received should be greater than zero');
      is(evt.data.type, expectedMimeType,
         'Blob data received should have type = ' + expectedMimeType);

      is(mediaRecorder.mimeType, expectedMimeType,
         'Mime type in ondataavailable = ' + expectedMimeType);

      // onstop should not have fired before ondataavailable
      if (onStopFired) {
        ok(false, 'ondataavailable unexpectedly fired later than onstop');
        manager.finished(token);
      }
    }
  };

  element.oncanplaythrough = function () {
    element.oncanplaythrough = null;
    // If content has ended, skip the test
    if (element.ended) {
      ok(true, 'ended fired before canplaythrough, skipping test');
      manager.finished(token);
    } else {
      // If content hasn't ended, start recording
      mediaRecorder.start();
      is(mediaRecorder.state, 'recording',
         'Media recorder should be recording');
      is(mediaRecorder.stream, element.stream,
         'Media recorder stream = element stream at the start of recording');
      // Recording will automatically stop when the stream ends.
    }
  }

  element.play();
}

manager.runTests(gMediaRecorderTests, startTest);
</script>
</pre>
</body>
</html>