<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<canvas id="canvas_source"></canvas>
<script>
description("Exercises that at least one frame passes on captureStream(0).");

window.jsTestIsAsync = true;

var canvas = document.getElementById('canvas_source');
var stream;
var recorder;

function onError() {
  testFailed('Error in recording media stream.');
};

function onRecorderDataAvailable(e) {
  testPassed('Recorder has new data available');
  shouldBeEqualToString('recorder.state', 'recording');
  testPassed('Recorded data size:' + e.data.size);
  finishJSTest();
};

function drawToCanvas(canvas) {
  testPassed('Drawing to canvas.');
  var ctx = canvas.getContext("2d");
  ctx.strokeStyle="#FF0204";
  ctx.beginPath();
  ctx.moveTo(0,0);
  ctx.lineTo(100, 100);
  ctx.stroke();
};

function recordMediaStream() {
  shouldBe('stream.getVideoTracks().length', '1');
  track = stream.getVideoTracks()[0];
  shouldBeEqualToString('track.readyState', 'live');

  recorder = new MediaRecorder(stream);
  recorder.ondataavailable = onRecorderDataAvailable;
  recorder.onError = onRecorderDataAvailable;
  recorder.start(0);
};

drawToCanvas(canvas);
try {
  stream = canvas.captureStream(0);
  testPassed('Got a stream from canvas.');
} catch (e) {
  testFailed('Exception calling captureStream(): ' + e);
  finishJSTest();
}
if (stream) {
  recordMediaStream();
}

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