<!DOCTYPE HTML>
<html>
<head>
  <title>Test track interfaces when consuming media resources</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;

function startTest(test, token) {
  var elemType = getMajorMimeType(test.type);
  var element = document.createElement(elemType);

  var audioOnchange = 0;
  var audioOnaddtrack = 0;
  var audioOnremovetrack = 0;
  var videoOnchange = 0;
  var videoOnaddtrack = 0;
  var videoOnremovetrack = 0;
  var isPlaying = false;

  isnot(element.audioTracks, undefined,
        'HTMLMediaElement::AudioTracks() property should be available.');
  isnot(element.videoTracks, undefined,
        'HTMLMediaElement::VideoTracks() property should be available.');

  element.audioTracks.onaddtrack = function(e) {
    audioOnaddtrack++;
  }

  element.audioTracks.onremovetrack = function(e) {
    audioOnremovetrack++;
  }

  element.audioTracks.onchange = function(e) {
    audioOnchange++;
  }

  element.videoTracks.onaddtrack = function(e) {
    videoOnaddtrack++;
  }

  element.videoTracks.onremovetrack = function(e) {
    videoOnremovetrack++;
  }

  element.videoTracks.onchange = function(e) {
    videoOnchange++;
  }

  function checkTrackRemoved() {
    if (isPlaying) {
      if (test.hasAudio) {
        is(audioOnremovetrack, 1, 'Calls of onremovetrack on audioTracks should be 1.');
        is(element.audioTracks.length, 0, 'The length of audioTracks should be 0.');
      }
      if (test.hasVideo) {
        is(videoOnremovetrack, 1, 'Calls of onremovetrack on videoTracks should be 1.');
        is(element.videoTracks.length, 0, 'The length of videoTracks should be 0.');
      }
    }
  }

  function onended() {
    ok(true, 'Event ended is expected to be fired on element.');
    checkTrackRemoved();
    element.onended = null;
    element.onplaying = null;
    element.onpause = null;
    manager.finished(element.token);
  }

  function checkTrackAdded() {
    isPlaying = true;
    if (test.hasAudio) {
      is(audioOnaddtrack, 1, 'Calls of onaddtrack on audioTracks should be 1.');
      is(element.audioTracks.length, 1, 'The length of audioTracks should be 1.');
      ok(element.audioTracks[0].enabled, 'Audio track should be enabled as default.');
    }
    if (test.hasVideo) {
      is(videoOnaddtrack, 1, 'Calls of onaddtrack on videoTracks should be 1.');
      is(element.videoTracks.length, 1, 'The length of videoTracks should be 1.');
      is(element.videoTracks.selectedIndex, 0,
         'The first video track is set selected as default.');
    }
  }

  function setTrackEnabled(enabled) {
    if (test.hasAudio) {
      element.audioTracks[0].enabled = enabled;
    }
    if (test.hasVideo) {
      element.videoTracks[0].selected = enabled;
    }
  }

  function checkTrackChanged(calls, enabled) {
    if (test.hasAudio) {
      is(audioOnchange, calls, 'Calls of onchange on audioTracks should be '+calls);
      is(element.audioTracks[0].enabled, enabled,
         'Enabled value of the audio track should be ' +enabled);
    }
    if (test.hasVideo) {
      is(videoOnchange, calls, 'Calls of onchange on videoTracks should be '+calls);
      is(element.videoTracks[0].selected, enabled,
         'Selected value of the video track should be ' +enabled);
      var index = enabled ? 0 : -1;
      is(element.videoTracks.selectedIndex, index,
         'SelectedIndex of video tracks should be ' +index);
    }
  }

  function onpause() {
    element.onpause = null;
    if (element.ended) {
      return;
    }
    if (steps == 1) {
      setTrackEnabled(false);
      element.onplaying = onplaying;
      element.play();
      steps++;
    } else if (steps == 2) {
      setTrackEnabled(true);
      element.onplaying = onplaying;
      element.play();
      steps++;
    }
  }

  function onplaying() {
    element.onplaying = null;
    if (element.ended) {
      return;
    }
    if (steps == 1) {
      element.onpause = onpause;
      element.pause();
      checkTrackAdded();
    } else if (steps == 2) {
      element.onpause = onpause;
      element.pause();
      checkTrackChanged(1, false);
    } else if (steps == 3) {
      checkTrackChanged(2, true);
    }
  }

  var steps = 0;

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

  element.src = test.name;
  element.test = test;
  element.onplaying = onplaying;
  element.onended = onended;
  element.play();
  steps++;
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv(
  {
    "set": [
      ["media.track.enabled", true]
    ]
  },
  function() {
    manager.runTests(gTrackTests, startTest);
  });

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