1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/test/test_texttrackcue.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,291 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=833386 1.8 +--> 1.9 +<head> 1.10 + <meta charset='utf-8'> 1.11 + <title>Test for Bug 833386 - HTMLTrackElement</title> 1.12 + <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> 1.13 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.14 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 1.15 +</head> 1.16 +<body> 1.17 +<p id="display"></p> 1.18 +<div id="content"> 1.19 +</div> 1.20 +<pre id="test"> 1.21 +<script class="testbody" type="text/javascript"> 1.22 +SimpleTest.waitForExplicitFinish(); 1.23 +SpecialPowers.pushPrefEnv({"set": [["media.webvtt.enabled", true], 1.24 + ["media.webvtt.regions.enabled", true]]}, 1.25 + function() { 1.26 + var video = document.createElement("video"); 1.27 + video.src = "seek.webm"; 1.28 + video.preload = "auto"; 1.29 + var trackElement = document.createElement("track"); 1.30 + trackElement.src = "basic.vtt"; 1.31 + trackElement.kind = "subtitles"; 1.32 + document.getElementById("content").appendChild(video); 1.33 + video.appendChild(trackElement); 1.34 + video.addEventListener("loadedmetadata", function run_tests() { 1.35 + // Re-que run_tests() at the end of the event loop until the track 1.36 + // element has loaded its data. 1.37 + if (trackElement.readyState == 1) { 1.38 + setTimeout(run_tests, 0); 1.39 + return; 1.40 + } 1.41 + is(trackElement.readyState, 2, "Track::ReadyState should be set to LOADED."); 1.42 + // Set mode to hidden so that the active cue lists are being updated. 1.43 + trackElement.track.mode = "hidden"; 1.44 + 1.45 + var cueList = trackElement.track.cues; 1.46 + is(cueList.length, 6, "Cue list length should be 6."); 1.47 + 1.48 + // Check that the typedef of TextTrackCue works in Gecko. 1.49 + is(window.TextTrackCue, undefined, "TextTrackCue should be undefined."); 1.50 + isnot(window.VTTCue, undefined, "VTTCue should be defined."); 1.51 + 1.52 + // Check if first cue was parsed correctly. 1.53 + var cue = cueList[0]; 1.54 + is(cue.id, "1", "Cue's ID should be 1."); 1.55 + is(cue.startTime, 0.5, "Cue's start time should be 0.5."); 1.56 + is(cue.endTime, 0.7, "Cue's end time should be 0.7."); 1.57 + is(cue.pauseOnExit, false, "Cue's pause on exit flag should be false."); 1.58 + is(cue.text, "This", "Cue's text should be set correctly."); 1.59 + is(cue.track, trackElement.track, "Cue's track should be defined."); 1.60 + 1.61 + cue.track = null; 1.62 + isnot(cue.track, null, "Cue's track should not be able to be set."); 1.63 + 1.64 + // Check that all cue times were not rounded 1.65 + is(cueList[1].startTime, 1.2, "Second cue's start time should be 1.2."); 1.66 + is(cueList[1].endTime, 2.4, "Second cue's end time should be 2.4."); 1.67 + is(cueList[2].startTime, 2, "Third cue's start time should be 2."); 1.68 + is(cueList[2].endTime, 3.5, "Third cue's end time should be 3.5."); 1.69 + is(cueList[3].startTime, 2.71, "Fourth cue's start time should be 2.71."); 1.70 + is(cueList[3].endTime, 2.91, "Fourth cue's end time should be 2.91."); 1.71 + is(cueList[4].startTime, 3.217, "Fifth cue's start time should be 3.217."); 1.72 + is(cueList[4].endTime, 3.989, "Fifth cue's end time should be 3.989."); 1.73 + is(cueList[5].startTime, 3.217, "Sixth cue's start time should be 3.217."); 1.74 + is(cueList[5].endTime, 3.989, "Sixth cue's end time should be 3.989."); 1.75 + 1.76 + // Check that Cue setters are working correctly. 1.77 + cue.id = "Cue 01"; 1.78 + is(cue.id, "Cue 01", "Cue's ID should be 'Cue 01'."); 1.79 + cue.startTime = 0.51; 1.80 + is(cue.startTime, 0.51, "Cue's start time should be 0.51."); 1.81 + cue.endTime = 0.71; 1.82 + is(cue.endTime, 0.71, "Cue's end time should be 0.71."); 1.83 + cue.pauseOnExit = true; 1.84 + is(cue.pauseOnExit, true, "Cue's pause on exit flag should be true."); 1.85 + 1.86 + var exceptionHappened; 1.87 + function checkPercentageValue(prop) { 1.88 + ok(prop in cue, prop + " should be a property on VTTCue."); 1.89 + cue[prop] = 20; 1.90 + is(cue[prop], 20, "Cue's " + prop + " should now be 20."); 1.91 + [ 101, -1 ].forEach(function(val) { 1.92 + exceptionHappened = false; 1.93 + try { 1.94 + cue[prop] = val; 1.95 + } catch(e) { 1.96 + exceptionHappened = true; 1.97 + is(e.name, "IndexSizeError", "Should have thrown IndexSizeError."); 1.98 + } 1.99 + ok(exceptionHappened, "Exception should have happened."); 1.100 + }); 1.101 + } 1.102 + 1.103 + checkPercentageValue("size"); 1.104 + checkPercentageValue("position"); 1.105 + 1.106 + ok(cue.snapToLines, "Cue's snapToLines should be set by set."); 1.107 + cue.snapToLines = false; 1.108 + ok(!cue.snapToLines, "Cue's snapToLines should not be set."); 1.109 + 1.110 + function checkEnumValue(prop, initialVal, acceptedValues) { 1.111 + ok(prop in cue, prop + " should be a property on VTTCue."); 1.112 + is(cue[prop], initialVal, "Cue's " + prop + " should be " + initialVal); 1.113 + cue[prop] = "bogus"; 1.114 + is(cue[prop], initialVal, "Cue's " + prop + " should be " + initialVal); 1.115 + acceptedValues.forEach(function(val) { 1.116 + cue[prop] = val; 1.117 + is(cue[prop], val, "Cue's " + prop + " should be " + val); 1.118 + if (typeof val === "string") { 1.119 + cue[prop] = val.toUpperCase(); 1.120 + is(cue[prop], val, "Cue's " + prop + " should be " + val); 1.121 + } 1.122 + }); 1.123 + } 1.124 + 1.125 + checkEnumValue("align", "middle", [ "start", "left", "middle", "right", "end" ]); 1.126 + checkEnumValue("vertical", "", [ "", "lr", "rl" ]); 1.127 + 1.128 + // Check that cue line align works properly 1.129 + is(cue.lineAlign, "start", "Cue's default line alignment should be start."); 1.130 + 1.131 + exceptionHappened = false; 1.132 + try { 1.133 + cue.lineAlign = "left"; 1.134 + } catch(e) { 1.135 + exceptionHappened = true; 1.136 + is(e.name, "SyntaxError", "Should have thrown SyntaxError."); 1.137 + } 1.138 + ok(exceptionHappened, "Exception should have happened."); 1.139 + 1.140 + exceptionHappened = false; 1.141 + try { 1.142 + cue.lineAlign = "right"; 1.143 + } catch(e) { 1.144 + exceptionHappened = true; 1.145 + is(e.name, "SyntaxError", "Should have thrown SyntaxError."); 1.146 + } 1.147 + ok(exceptionHappened, "Exception should have happened."); 1.148 + 1.149 + cue.lineAlign = "middle"; 1.150 + is(cue.lineAlign, "middle", "Cue's line align should be middle."); 1.151 + cue.lineAlign = "START"; 1.152 + is(cue.lineAlign, "middle", "Cue's line align should be middle."); 1.153 + cue.lineAlign = "end"; 1.154 + is(cue.lineAlign, "end", "Cue's line align should be end."); 1.155 + 1.156 + // Check that cue position align works properly 1.157 + is(cue.positionAlign, "middle", "Cue's default position alignment should be middle."); 1.158 + 1.159 + var exceptionHappened = false; 1.160 + try { 1.161 + cue.positionAlign = "left"; 1.162 + } catch(e) { 1.163 + exceptionHappened = true; 1.164 + is(e.name, "SyntaxError", "Should have thrown SyntaxError."); 1.165 + } 1.166 + ok(exceptionHappened, "Exception should have happened."); 1.167 + 1.168 + exceptionHappened = false; 1.169 + try { 1.170 + cue.positionAlign = "right"; 1.171 + } catch(e) { 1.172 + exceptionHappened = true; 1.173 + is(e.name, "SyntaxError", "Should have thrown SyntaxError."); 1.174 + } 1.175 + ok(exceptionHappened, "Exception should have happened."); 1.176 + 1.177 + cue.positionAlign = "start"; 1.178 + is(cue.positionAlign, "start", "Cue's position align should be start."); 1.179 + cue.positionAlign = "end"; 1.180 + is(cue.positionAlign, "end", "Cue's position align should be end."); 1.181 + 1.182 + // Check cue.line 1.183 + is(cue.line, "auto", "Cue's line value should initially be auto."); 1.184 + cue.line = 12410 1.185 + is(cue.line, 12410, "Cue's line value should now be 12410."); 1.186 + cue.line = "auto"; 1.187 + is(cue.line, "auto", "Cue's line value should now be auto."); 1.188 + 1.189 + // Check that we can create and add new VTTCues 1.190 + var vttCue = new VTTCue(3.999, 4, "foo"); 1.191 + is(vttCue.track, undefined, "Cue's track should be undefined."); 1.192 + trackElement.track.addCue(vttCue); 1.193 + is(cue.track, trackElement.track, "Cue's track should be defined."); 1.194 + is(cueList.length, 7, "Cue list length should now be 7."); 1.195 + 1.196 + // Check that new VTTCue was added correctly 1.197 + cue = cueList[6]; 1.198 + is(cue.startTime, 3.999, "Cue's start time should be 3.999."); 1.199 + is(cue.endTime, 4, "Cue's end time should be 4."); 1.200 + is(cue.text, "foo", "Cue's text should be foo."); 1.201 + 1.202 + // Adding the same cue again should not increase the cue count. 1.203 + trackElement.track.addCue(vttCue); 1.204 + is(cueList.length, 7, "Cue list length should be 7."); 1.205 + 1.206 + // Check that we are able to remove cues. 1.207 + trackElement.track.removeCue(cue); 1.208 + is(cueList.length, 6, "Cue list length should be 6."); 1.209 + 1.210 + exceptionHappened = false; 1.211 + try { 1.212 + // We should not be able to remove a cue that is not in the list. 1.213 + cue = new VTTCue(1, 2, "foo"); 1.214 + trackElement.track.removeCue(cue); 1.215 + } catch (e) { 1.216 + // "NotFoundError" should be thrown when trying to remove a cue that is 1.217 + // not in the list. 1.218 + is(e.name, "NotFoundError", "Should have thrown NotFoundError."); 1.219 + exceptionHappened = true; 1.220 + } 1.221 + // If this is false then we did not throw an error and probably removed a cue 1.222 + // when we shouln't have. 1.223 + ok(exceptionHappened, "Exception should have happened."); 1.224 + 1.225 + is(cueList.length, 6, "Cue list length should be 6."); 1.226 + 1.227 + video.currentTime = 2; 1.228 + isnot(trackElement.track.activeCues, null); 1.229 + 1.230 + trackElement.track.mode = "disabled"; 1.231 + is(trackElement.track.activeCues, null); 1.232 + 1.233 + trackElement.track.mode = "showing"; 1.234 + video.currentTime = 0; 1.235 + 1.236 + var regionInfo = [ 1.237 + { lines: 2, width: 30 }, 1.238 + { lines: 4, width: 20 }, 1.239 + { lines: 2, width: 30 } 1.240 + ]; 1.241 + 1.242 + for (var i = 0; i < regionInfo.length; i++) { 1.243 + var cue = cueList[i]; 1.244 + isnot(cue.region, null, "Cue at " + i + " should have a region."); 1.245 + for (var key in regionInfo[i]) { 1.246 + is(cue.region[key], regionInfo[i][key], "Region should have a " + key + 1.247 + " property with a value of " + regionInfo[i][key]) 1.248 + } 1.249 + } 1.250 + 1.251 + // Test TextTrack::ActiveCues. 1.252 + var cueInfo = [ 1.253 + { startTime: 0.51, endTime: 0.71, ids: ["Cue 01"] }, 1.254 + { startTime: 0.72, endTime: 1.19, ids: [] }, 1.255 + { startTime: 1.2, endTime: 1.9, ids: [2] }, 1.256 + { startTime: 2, endTime: 2.4, ids: [2, 2.5] }, 1.257 + { startTime: 2.41, endTime: 2.70, ids: [2.5] }, 1.258 + { startTime: 2.71, endTime: 2.91, ids: [2.5, 3] }, 1.259 + { startTime: 2.92, endTime: 3.216, ids: [2.5] }, 1.260 + { startTime: 3.217, endTime: 3.5, ids: [2.5, 4, 5] }, 1.261 + { startTime: 3.51, endTime: 3.989, ids: [4, 5] }, 1.262 + { startTime: 3.99, endTime: 4, ids: [] } 1.263 + ]; 1.264 + 1.265 + video.addEventListener("timeupdate", function() { 1.266 + var activeCues = trackElement.track.activeCues, 1.267 + found = false, 1.268 + playbackTime = video.currentTime; 1.269 + 1.270 + for (var i = 0; i < cueInfo.length; i++) { 1.271 + var cue = cueInfo[i]; 1.272 + if (playbackTime >= cue.startTime && playbackTime <= cue.endTime) { 1.273 + is(activeCues.length, cue.ids.length, "There should be " + cue.ids.length + " currently active cue(s)."); 1.274 + for (var j = 0; j < cue.ids.length; j++) { 1.275 + isnot(activeCues.getCueById(cue.ids[j]), undefined, "The cue with ID " + cue.ids[j] + " should be active."); 1.276 + } 1.277 + break; 1.278 + } 1.279 + } 1.280 + }); 1.281 + 1.282 + video.addEventListener("ended", function(){ 1.283 + SimpleTest.finish(); 1.284 + }); 1.285 + 1.286 + video.play(); 1.287 + }); 1.288 + } 1.289 +); 1.290 + 1.291 +</script> 1.292 +</pre> 1.293 +</body> 1.294 +</html>