summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_played.html
blob: 52d3ab45f7cb6de03da8ec041cd72477495babba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!DOCTYPE HTML>
<html>
<head>
<title>Test played member for media elements</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<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='application/javascript;version=1.8'>
//longer timeout for sometimes B2G emulator runs very slowly
if (SpecialPowers.Services.appinfo.name == "B2G") {
  SimpleTest.requestLongerTimeout(3);
}

SimpleTest.requestCompleteLog();
let manager = new MediaTestManager;

function finish_test(element) {
  removeNodeAndSource(element);
  manager.finished(element.token);
}

// Check that a file has been played in its entirety.
function check_full_file_played(element) {
  element.addEventListener('ended', (function(e) {
    let interval_count = e.target.played.length;
    is(interval_count, 1, element.token + ": played.length must be 1");
    is(element.played.start(0), 0, element.token + ": start time shall be 0");
    is(element.played.end(0), e.target.duration, element.token + ": end time shall be duration");
    finish_test(e.target);
  }), false);
}

var tests = [
// Without playing, check that player.played.length == 0.
{
  setup : function(element) {
    element.addEventListener("loadedmetadata", function() {
      is(element.played.length, 0, element.token + ": initial played.length equals zero");
      finish_test(element);
    });
  },
  name: "test1"
},
// Play the file, test the range we have.
{
  setup : function(element) {
    check_full_file_played(element);
    element.play();
  },
  name: "test2"
},

// Play the second half of the file, pause, play
// an check we have only one range.
{
  setup : function (element) {
    element.onended = function (e) {
      var t = e.target;
      t.onended = null;
      check_full_file_played(t);
      t.pause();
      t.currentTime = 0;
      t.play();
    };
    element.addEventListener("loadedmetadata", function() {
      element.currentTime = element.duration / 2;
      element.play();
    }, false);
  },
  name: "test3"
},

// Play the first half of the file, seek back, while
// continuing to play. We shall have only one range.
{
  setup : function (element) {
    let onTimeUpdate = function() {
      if (element.currentTime > element.duration / 2) {
        info(element.token + ": currentTime=" + element.currentTime + ", duration=" + element.duration);
        element.removeEventListener("timeupdate", onTimeUpdate, false);
        element.pause();
        var oldEndRange = element.played.end(0);
        element.currentTime = element.duration / 4;
        is(element.played.end(0), oldEndRange,
           element.token + ": When seeking back, |played| should not be changed");
        element.play();
      }
    }
    element.addEventListener("timeupdate", onTimeUpdate, false);
    check_full_file_played(element);
    element.play();
  },
  name: "test4"
},

// Play and seek to have two ranges, and check that, as well a
// boundaries.
{
  setup : function (element) {
    let seekTarget = 0;
    let onTimeUpdate = function() {
      if (element.currentTime > element.duration / 2) {
        info(element.token + ": currentTime=" + element.currentTime + ", duration=" + element.duration);
        element.removeEventListener("timeupdate", onTimeUpdate, false);
        element.pause();
        // Remember seek target for later comparison since duration may change
        // during playback.
        seekTarget = element.currentTime = element.duration / 10;
        element.currentTime = seekTarget;
        element.play();
      }
    }

    element.addEventListener("loadedmetadata", function() {
      element.addEventListener("timeupdate", onTimeUpdate, false);
    }, false);


    element.addEventListener("ended", (function() {
      if(element.played.length > 1) {
        is(element.played.length, 2, element.token + ": element.played.length == 2");
        is(element.played.start(1), seekTarget, element.token + ": we should have seeked forward by one tenth of the duration");
        is(element.played.end(1), element.duration, element.token + ": end of second range shall be the total duration");
      }
      is(element.played.start(0), 0, element.token + ": start of first range shall be 0");
      finish_test(element);
    }), false);

    element.play();
  },
  name: "test5"
},

// Play to create two ranges, in the reverse order. check that they are sorted.
{
  setup : function (element) {
    function end() {
      element.pause();
      let p = element.played;
      ok(p.length >= 1, element.token + ": There should be at least one range=" + p.length);
      is(p.start(0), seekTarget, element.token + ": Start of first range should be the sixth of the duration");
      ok(p.end(p.length - 1) > 5 * element.duration / 6, element.token + ": End of last range should be greater that five times the sixth of the duration");
      finish_test(element);
    }

    let seekTarget = 0;
    function pauseseekrestart() {
      element.pause();
      // Remember seek target for later comparison since duration may change
      // during playback.
      seekTarget = element.duration / 6;
      element.currentTime = seekTarget;
      element.play();
    }

    function onTimeUpdate_pauseseekrestart() {
      if (element.currentTime > 5 * element.duration / 6) {
        element.removeEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false);
        pauseseekrestart();
        element.addEventListener("timeupdate", onTimeUpdate_end, false);
      }
    }

    function onTimeUpdate_end() {
      if (element.currentTime > 3 * element.duration / 6) {
        element.removeEventListener("timeupdate", onTimeUpdate_end, false);
        end();
      }
    }

    element.addEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false);

    element.addEventListener('loadedmetadata', function() {
      element.currentTime = 4 * element.duration / 6;
      element.play();
    }, false);
  },
  name: "test6"
},
// Seek repeatedly without playing. No range should appear.
{
  setup : function(element) {
    let index = 1;

    element.addEventListener('seeked', function() {
      index++;
      element.currentTime = index * element.duration / 5;
      is(element.played.length, 0, element.token + ": played.length should be 0");
      if (index == 5) {
        finish_test(element);
      }
    }, false);

    element.addEventListener('loadedmetadata', function() {
      element.currentTime = element.duration / 5;
    }, false);
  },
  name: "test7"
}
];

function createTestArray() {
  var A = [];
  for (var i=0; i<tests.length; i++) {
    for (var k=0; k<gPlayedTests.length; k++) {
      var t = new Object();
      t.setup = tests[i].setup;
      t.name = tests[i].name + "-" + gPlayedTests[k].name;
      t.type = gPlayedTests[k].type;
      t.src = gPlayedTests[k].name;
      A.push(t);
    }
  }
  return A;
}

function startTest(test, token) {
  var elemType = getMajorMimeType(test.type);
  var element = document.createElement(elemType);
  element.src = test.src;
  element.token = token;
  element.preload = "metadata";
  test.setup(element);
  manager.started(token);

  // Log events for debugging.
  var events = ["suspend", "play", "canplay", "canplaythrough", "loadstart", "loadedmetadata",
                "loadeddata", "playing", "ended", "error", "stalled", "emptied", "abort",
                "waiting", "pause"];
  function logEvent(e) {
    var v = e.target;
    info(v.token + ": got " + e.type);
  }
  events.forEach(function(e) {
    element.addEventListener(e, logEvent, false);
  });

}


manager.runTests(createTestArray(), startTest);

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