<!DOCTYPE HTML>
<html>
<head>
  <meta charset='utf-8'>
  <title>WebVTT : position align test</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<div id="content">
</div>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();

var video = document.createElement("video");
var trackElement = document.createElement("track");
var cuesNumber = 22;

function isTrackElemenLoaded() {
  // Re-que isTrackElemenLoaded() at the end of the event loop until the track
  // element has loaded its data.
  if (trackElement.readyState == 1) {
    setTimeout(isTrackElemenLoaded, 0);
    return;
  }

  is(trackElement.readyState, 2, "Track::ReadyState should be set to LOADED.");
  runTest();
}

function runTest() {
  info("--- check cues number ---");
  var cues = trackElement.track.cues;
  is(cues.length, cuesNumber, "Cues number is correct.");

  info("--- check the typedef of TextTrackCue and VTTCue ---");
  isnot(window.TextTrackCue, undefined, "TextTrackCue should be defined.");
  isnot(window.VTTCue, undefined, "VTTCue should be defined.");

  info("--- check the type of first parsed cue ---");
  ok(cues[0] instanceof TextTrackCue, "Cue should be an instanceof TextTrackCue.");
  ok(cues[0] instanceof VTTCue, "Cue should be an instanceof VTTCue.");

  info("--- check the cue's position alignment ---");
  let expectedAlignment = ["auto", "line-left", "center", "line-right", "auto"];
  let idx = 0;
  for (;idx < expectedAlignment.length; idx++) {
    is(cues[idx].positionAlign, expectedAlignment[idx], cues[idx].text);
  }

  info("--- check the cue's computed position alignment ---");
  // The "computedPositionAlign" is the chrome-only attributes, we need to get
  // the chrome privilege for cues.
  let cuesChrome = SpecialPowers.wrap(cues);
  expectedAlignment.push("line-left", "line-right", "center");
  for (;idx < expectedAlignment.length; idx++) {
    is(cuesChrome[idx].computedPositionAlign, expectedAlignment[idx],
       cuesChrome[idx].text);
  }

  info(`test only setting text alignment with "start"`);
  expectedAlignment.push("line-left", "line-right", "line-left", "line-right",
                         "line-left", "line-left", "line-right");
  for (;idx < expectedAlignment.length; idx++) {
    is(cuesChrome[idx].computedPositionAlign, expectedAlignment[idx],
       cuesChrome[idx].text);
  }

  info(`test only setting text alignment with "end"`);
  expectedAlignment.push("line-right", "line-left", "line-right", "line-left",
                         "line-right", "line-right", "line-left");
  for (;idx < expectedAlignment.length; idx++) {
    is(cuesChrome[idx].computedPositionAlign, expectedAlignment[idx],
       cuesChrome[idx].text);
  }
  is(idx, cuesNumber, "finished checking all cues");

  info("--- check the cue's computed position alignment from DOM API ---");
  is(cuesChrome[0].computedPositionAlign, "center", "Cue's computedPositionAlign align is center.");

  cuesChrome[0].positionAlign = "auto";
  is(cuesChrome[0].positionAlign, "auto", "Change cue's position align to \"auto\"");

  cuesChrome[0].align = "left";
  is(cuesChrome[0].align, "left", "Change cue's align to \"left\".");

  is(cuesChrome[0].computedPositionAlign, "line-left",  "Cue's computedPositionAlign becomes to \"line-left\"");

  info("--- finish test ---");
  SimpleTest.finish();
}

function setupTest() {
  info("--- setup test ---");
  video.src = "seek.webm";
  video.preload = "auto";

  trackElement.src = "vttPositionAlign.vtt";
  trackElement.kind = "subtitles";
  trackElement.default = true;

  document.getElementById("content").appendChild(video);
  video.appendChild(trackElement);
  video.addEventListener("loadedmetadata", function() {
    isTrackElemenLoaded();
  }, {once: true});
}

onload = setupTest;
</script>
</body>
</html>
