<!doctype html>
<title>TextTrack.cues</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=log></div>
<script>
test(function(){
    var video = document.createElement('video');
    var t1 = video.addTextTrack('subtitles');
    assert_equals(t1.cues, t1.cues, 't1.cues should return same object');
    assert_not_equals(t1.cues, null, 't1.cues should not be null');
    assert_true(t1.cues instanceof TextTrackCueList, 't1.cues instanceof TextTrackCueList');
    assert_equals(t1.cues.length, 0, 't1.cues.length');
}, document.title+', empty list');

function addCue(texttrack, start, end, text, id) {
  var c = new VTTCue(start, end, text);
  c.id = id;
  texttrack.addCue(c);
  return c;
}

test(function(){
    var video = document.createElement('video');
    var t1 = video.addTextTrack('subtitles');
    var t1_cues = t1.cues;
    var c = addCue(t1, 0, 1, 'text', 'id');
    assert_equals(t1.cues, t1_cues, "t1.cues should return same object");
    assert_equals(t1.cues.length, 1, "t1.cues.length");
    var c2 = addCue(t1, 1, 2, 'text2', 'id2');
    assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after adding a second cue");
    assert_equals(t1.cues.length, 2, "t1.cues.length after adding a second cue");
    assert_equals(t1.cues[0].id, "id");
    assert_equals(t1.cues[1].id, "id2");
}, document.title+', after addCue()');

test(function(){
    var video = document.createElement('video');
    var t1 = video.addTextTrack('subtitles');
    var t1_cues = t1.cues;
    var c = addCue(t1, 0, 1, 'text', 'id');
    var c2 = addCue(t1, 1, 2, 'text2', 'id2');
    t1.mode = 'showing';
    assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after setting mode to 'showing'");
    t1.mode = 'hidden';
    assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after setting mode to 'hidden'");
    t1.mode = 'disabled';
    assert_equals(t1.cues, null, "t1.cues should be null when mode is 'disabled'");
    assert_equals(t1_cues.length, 2, "t1_cues should still be intact after setting mode to 'disabled'");
    assert_equals(t1_cues[0].id, "id", "t1_cues first cue should still be intact after setting mode to 'disabled'");
    assert_equals(t1_cues[1].id, "id2", "t1_cues second cue should still be intact after setting mode to 'disabled'");
    t1.mode = 'hidden';
    assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after setting mode to 'disabled' and then 'hidden'");
    t1.mode = 'disabled';
    assert_equals(t1.cues, null, "t1.cues should be null when mode is set to 'disabled' again");
    assert_equals(t1_cues.length, 2, "t1_cues should still be intact after setting mode to 'disabled' again");
    assert_equals(t1_cues[0].id, "id", "t1_cues first cue should still be intact after setting mode to 'disabled' again");
    assert_equals(t1_cues[1].id, "id2", "t1_cues second cue should still be intact after setting mode to 'disabled' again");
    t1.mode = 'showing';
    assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after setting mode to 'disabled' and then 'showing'");
}, document.title+', different modes');

test(function(){
    var video = document.createElement('video');
    var t1 = video.addTextTrack('subtitles');
    var t1_cues = t1.cues;
    var c = addCue(t1, 0, 1, 'text', 'id');
    var c2 = addCue(t1, 1, 2, 'text2', 'id2');
    t1.mode = 'showing';
    t1.cues[1].startTime = 0; // this should change the text track cue order
    assert_equals(t1.cues[0].id, 'id2');
    assert_equals(t1.cues[1].id, 'id');
    t1.cues[0].startTime = 0.5; // this should change it back
    assert_equals(t1.cues[0].id, 'id');
    assert_equals(t1.cues[1].id, 'id2');
}, document.title+', changing order');

async_test(function(){
    var video = document.createElement('video');
    var t1 = video.addTextTrack('subtitles');
    var t1_cues = t1.cues;
    t1.mode = 'hidden';
    var track = document.createElement('track');
    track['default'] = true;
    video.appendChild(track); // queues a task to "honor user preferences...", media element event task source
    var t2 = track.track;
    assert_equals(t2.cues, null, 't2.cues should be null');
    // We need to wait until the "honor user preferences..." steps have run so we invoke play()
    // which queues an event with the same task source.
    video.onplay = this.step_func(function(){
        assert_equals(t2.cues, t2.cues, 't2.cues should return same object');
        assert_not_equals(t1.cues, t2.cues, 't1.cues and t2.cues should be different objects');
        assert_not_equals(t2.cues, null, 't2.cues should not be null');
        assert_true(t2.cues instanceof TextTrackCueList, 't2.cues instanceof TextTrackCueList');
        assert_equals(t2.cues.length, 0, 't2.cues should have length 0');
        this.done();
    });
    video.play(); // queues a task to fire 'play', media element event task source
}, document.title+', default attribute');
</script>
