Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=833386
5 -->
6 <head>
7 <meta charset='utf-8'>
8 <title>Test for Bug 833386 - HTMLTrackElement</title>
9 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
10 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
12 </head>
13 <body>
14 <p id="display"></p>
15 <div id="content">
16 </div>
17 <pre id="test">
18 <script class="testbody" type="text/javascript">
19 SimpleTest.waitForExplicitFinish();
20 SpecialPowers.pushPrefEnv({"set": [["media.webvtt.enabled", true],
21 ["media.webvtt.regions.enabled", true]]},
22 function() {
23 var video = document.createElement("video");
24 video.src = "seek.webm";
25 video.preload = "auto";
26 var trackElement = document.createElement("track");
27 trackElement.src = "basic.vtt";
28 trackElement.kind = "subtitles";
29 document.getElementById("content").appendChild(video);
30 video.appendChild(trackElement);
31 video.addEventListener("loadedmetadata", function run_tests() {
32 // Re-que run_tests() at the end of the event loop until the track
33 // element has loaded its data.
34 if (trackElement.readyState == 1) {
35 setTimeout(run_tests, 0);
36 return;
37 }
38 is(trackElement.readyState, 2, "Track::ReadyState should be set to LOADED.");
39 // Set mode to hidden so that the active cue lists are being updated.
40 trackElement.track.mode = "hidden";
42 var cueList = trackElement.track.cues;
43 is(cueList.length, 6, "Cue list length should be 6.");
45 // Check that the typedef of TextTrackCue works in Gecko.
46 is(window.TextTrackCue, undefined, "TextTrackCue should be undefined.");
47 isnot(window.VTTCue, undefined, "VTTCue should be defined.");
49 // Check if first cue was parsed correctly.
50 var cue = cueList[0];
51 is(cue.id, "1", "Cue's ID should be 1.");
52 is(cue.startTime, 0.5, "Cue's start time should be 0.5.");
53 is(cue.endTime, 0.7, "Cue's end time should be 0.7.");
54 is(cue.pauseOnExit, false, "Cue's pause on exit flag should be false.");
55 is(cue.text, "This", "Cue's text should be set correctly.");
56 is(cue.track, trackElement.track, "Cue's track should be defined.");
58 cue.track = null;
59 isnot(cue.track, null, "Cue's track should not be able to be set.");
61 // Check that all cue times were not rounded
62 is(cueList[1].startTime, 1.2, "Second cue's start time should be 1.2.");
63 is(cueList[1].endTime, 2.4, "Second cue's end time should be 2.4.");
64 is(cueList[2].startTime, 2, "Third cue's start time should be 2.");
65 is(cueList[2].endTime, 3.5, "Third cue's end time should be 3.5.");
66 is(cueList[3].startTime, 2.71, "Fourth cue's start time should be 2.71.");
67 is(cueList[3].endTime, 2.91, "Fourth cue's end time should be 2.91.");
68 is(cueList[4].startTime, 3.217, "Fifth cue's start time should be 3.217.");
69 is(cueList[4].endTime, 3.989, "Fifth cue's end time should be 3.989.");
70 is(cueList[5].startTime, 3.217, "Sixth cue's start time should be 3.217.");
71 is(cueList[5].endTime, 3.989, "Sixth cue's end time should be 3.989.");
73 // Check that Cue setters are working correctly.
74 cue.id = "Cue 01";
75 is(cue.id, "Cue 01", "Cue's ID should be 'Cue 01'.");
76 cue.startTime = 0.51;
77 is(cue.startTime, 0.51, "Cue's start time should be 0.51.");
78 cue.endTime = 0.71;
79 is(cue.endTime, 0.71, "Cue's end time should be 0.71.");
80 cue.pauseOnExit = true;
81 is(cue.pauseOnExit, true, "Cue's pause on exit flag should be true.");
83 var exceptionHappened;
84 function checkPercentageValue(prop) {
85 ok(prop in cue, prop + " should be a property on VTTCue.");
86 cue[prop] = 20;
87 is(cue[prop], 20, "Cue's " + prop + " should now be 20.");
88 [ 101, -1 ].forEach(function(val) {
89 exceptionHappened = false;
90 try {
91 cue[prop] = val;
92 } catch(e) {
93 exceptionHappened = true;
94 is(e.name, "IndexSizeError", "Should have thrown IndexSizeError.");
95 }
96 ok(exceptionHappened, "Exception should have happened.");
97 });
98 }
100 checkPercentageValue("size");
101 checkPercentageValue("position");
103 ok(cue.snapToLines, "Cue's snapToLines should be set by set.");
104 cue.snapToLines = false;
105 ok(!cue.snapToLines, "Cue's snapToLines should not be set.");
107 function checkEnumValue(prop, initialVal, acceptedValues) {
108 ok(prop in cue, prop + " should be a property on VTTCue.");
109 is(cue[prop], initialVal, "Cue's " + prop + " should be " + initialVal);
110 cue[prop] = "bogus";
111 is(cue[prop], initialVal, "Cue's " + prop + " should be " + initialVal);
112 acceptedValues.forEach(function(val) {
113 cue[prop] = val;
114 is(cue[prop], val, "Cue's " + prop + " should be " + val);
115 if (typeof val === "string") {
116 cue[prop] = val.toUpperCase();
117 is(cue[prop], val, "Cue's " + prop + " should be " + val);
118 }
119 });
120 }
122 checkEnumValue("align", "middle", [ "start", "left", "middle", "right", "end" ]);
123 checkEnumValue("vertical", "", [ "", "lr", "rl" ]);
125 // Check that cue line align works properly
126 is(cue.lineAlign, "start", "Cue's default line alignment should be start.");
128 exceptionHappened = false;
129 try {
130 cue.lineAlign = "left";
131 } catch(e) {
132 exceptionHappened = true;
133 is(e.name, "SyntaxError", "Should have thrown SyntaxError.");
134 }
135 ok(exceptionHappened, "Exception should have happened.");
137 exceptionHappened = false;
138 try {
139 cue.lineAlign = "right";
140 } catch(e) {
141 exceptionHappened = true;
142 is(e.name, "SyntaxError", "Should have thrown SyntaxError.");
143 }
144 ok(exceptionHappened, "Exception should have happened.");
146 cue.lineAlign = "middle";
147 is(cue.lineAlign, "middle", "Cue's line align should be middle.");
148 cue.lineAlign = "START";
149 is(cue.lineAlign, "middle", "Cue's line align should be middle.");
150 cue.lineAlign = "end";
151 is(cue.lineAlign, "end", "Cue's line align should be end.");
153 // Check that cue position align works properly
154 is(cue.positionAlign, "middle", "Cue's default position alignment should be middle.");
156 var exceptionHappened = false;
157 try {
158 cue.positionAlign = "left";
159 } catch(e) {
160 exceptionHappened = true;
161 is(e.name, "SyntaxError", "Should have thrown SyntaxError.");
162 }
163 ok(exceptionHappened, "Exception should have happened.");
165 exceptionHappened = false;
166 try {
167 cue.positionAlign = "right";
168 } catch(e) {
169 exceptionHappened = true;
170 is(e.name, "SyntaxError", "Should have thrown SyntaxError.");
171 }
172 ok(exceptionHappened, "Exception should have happened.");
174 cue.positionAlign = "start";
175 is(cue.positionAlign, "start", "Cue's position align should be start.");
176 cue.positionAlign = "end";
177 is(cue.positionAlign, "end", "Cue's position align should be end.");
179 // Check cue.line
180 is(cue.line, "auto", "Cue's line value should initially be auto.");
181 cue.line = 12410
182 is(cue.line, 12410, "Cue's line value should now be 12410.");
183 cue.line = "auto";
184 is(cue.line, "auto", "Cue's line value should now be auto.");
186 // Check that we can create and add new VTTCues
187 var vttCue = new VTTCue(3.999, 4, "foo");
188 is(vttCue.track, undefined, "Cue's track should be undefined.");
189 trackElement.track.addCue(vttCue);
190 is(cue.track, trackElement.track, "Cue's track should be defined.");
191 is(cueList.length, 7, "Cue list length should now be 7.");
193 // Check that new VTTCue was added correctly
194 cue = cueList[6];
195 is(cue.startTime, 3.999, "Cue's start time should be 3.999.");
196 is(cue.endTime, 4, "Cue's end time should be 4.");
197 is(cue.text, "foo", "Cue's text should be foo.");
199 // Adding the same cue again should not increase the cue count.
200 trackElement.track.addCue(vttCue);
201 is(cueList.length, 7, "Cue list length should be 7.");
203 // Check that we are able to remove cues.
204 trackElement.track.removeCue(cue);
205 is(cueList.length, 6, "Cue list length should be 6.");
207 exceptionHappened = false;
208 try {
209 // We should not be able to remove a cue that is not in the list.
210 cue = new VTTCue(1, 2, "foo");
211 trackElement.track.removeCue(cue);
212 } catch (e) {
213 // "NotFoundError" should be thrown when trying to remove a cue that is
214 // not in the list.
215 is(e.name, "NotFoundError", "Should have thrown NotFoundError.");
216 exceptionHappened = true;
217 }
218 // If this is false then we did not throw an error and probably removed a cue
219 // when we shouln't have.
220 ok(exceptionHappened, "Exception should have happened.");
222 is(cueList.length, 6, "Cue list length should be 6.");
224 video.currentTime = 2;
225 isnot(trackElement.track.activeCues, null);
227 trackElement.track.mode = "disabled";
228 is(trackElement.track.activeCues, null);
230 trackElement.track.mode = "showing";
231 video.currentTime = 0;
233 var regionInfo = [
234 { lines: 2, width: 30 },
235 { lines: 4, width: 20 },
236 { lines: 2, width: 30 }
237 ];
239 for (var i = 0; i < regionInfo.length; i++) {
240 var cue = cueList[i];
241 isnot(cue.region, null, "Cue at " + i + " should have a region.");
242 for (var key in regionInfo[i]) {
243 is(cue.region[key], regionInfo[i][key], "Region should have a " + key +
244 " property with a value of " + regionInfo[i][key])
245 }
246 }
248 // Test TextTrack::ActiveCues.
249 var cueInfo = [
250 { startTime: 0.51, endTime: 0.71, ids: ["Cue 01"] },
251 { startTime: 0.72, endTime: 1.19, ids: [] },
252 { startTime: 1.2, endTime: 1.9, ids: [2] },
253 { startTime: 2, endTime: 2.4, ids: [2, 2.5] },
254 { startTime: 2.41, endTime: 2.70, ids: [2.5] },
255 { startTime: 2.71, endTime: 2.91, ids: [2.5, 3] },
256 { startTime: 2.92, endTime: 3.216, ids: [2.5] },
257 { startTime: 3.217, endTime: 3.5, ids: [2.5, 4, 5] },
258 { startTime: 3.51, endTime: 3.989, ids: [4, 5] },
259 { startTime: 3.99, endTime: 4, ids: [] }
260 ];
262 video.addEventListener("timeupdate", function() {
263 var activeCues = trackElement.track.activeCues,
264 found = false,
265 playbackTime = video.currentTime;
267 for (var i = 0; i < cueInfo.length; i++) {
268 var cue = cueInfo[i];
269 if (playbackTime >= cue.startTime && playbackTime <= cue.endTime) {
270 is(activeCues.length, cue.ids.length, "There should be " + cue.ids.length + " currently active cue(s).");
271 for (var j = 0; j < cue.ids.length; j++) {
272 isnot(activeCues.getCueById(cue.ids[j]), undefined, "The cue with ID " + cue.ids[j] + " should be active.");
273 }
274 break;
275 }
276 }
277 });
279 video.addEventListener("ended", function(){
280 SimpleTest.finish();
281 });
283 video.play();
284 });
285 }
286 );
288 </script>
289 </pre>
290 </body>
291 </html>