<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 951008 Test MediaRecorder Record has start event</title>
  <script 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>

<script class="testbody" type="text/javascript">

var manager = new MediaTestManager;

function startTest(test, token) {

  var element = document.createElement('audio');
  var hasonstart = false;
  var hasondataavailable = false;
  var mMediaRecorder;

  element.token = token;
  manager.started(token);
  element.src = test.name;
  element.test = test;
  element.stream = element.mozCaptureStream();

  mMediaRecorder = new MediaRecorder(element.stream);
  is(mMediaRecorder.mimeType, '', 'Expected MediaRecorder mimetype');
  mMediaRecorder.onwarning = function() {
    ok(false, 'onwarning unexpectedly fired');
  };

  mMediaRecorder.onerror = function() {
    ok(false, 'onerror unexpectedly fired');
  };

  mMediaRecorder.onstart = function() {
    info('onstart fired successfully');
    hasonstart = true;
    is(mMediaRecorder.mimeType, 'audio/ogg; codecs=opus',
      "MediaRecorder mimetype as expected");
    mMediaRecorder.requestData();
  };

  mMediaRecorder.onstop = function() {
    info('onstop fired successfully');
    ok(hasondataavailable, "should have ondataavailable before onstop");
    is(mMediaRecorder.state, 'inactive', 'check recording status is inactive');
    SimpleTest.finish();
  };

  mMediaRecorder.ondataavailable = function (e) {
    info('ondataavailable fired successfully');
    if (mMediaRecorder.state == 'recording') {
      hasondataavailable = true;
      ok(hasonstart, "should have had start event first");
      is(e.data.type, mMediaRecorder.mimeType,
        "blob's mimeType matches the recorder's");
      mMediaRecorder.stop();
    }
  };

  // Start recording once metadata are parsed.
  element.onloadedmetadata = function() {
    element.oncanplaythrough = null;
    mMediaRecorder.start(0);
    is(mMediaRecorder.state, 'recording', 'Media recorder should be recording');
    is(mMediaRecorder.stream, element.stream,
       'Media recorder stream = element stream at the start of recording');
    };

  element.play();
}

manager.runTests(gMediaRecorderTests, startTest);

</script>
</pre>
</body>
</html>
