| OLD | NEW |
| 1 | |
| 2 var expectedStartTimes = new Array(); | |
| 3 var expectedEndTimes = new Array(); | |
| 4 var timeRangeCount = 0; | 1 var timeRangeCount = 0; |
| 5 var currentTimeRange = 0; | 2 var currentTimeRange = 0; |
| 6 var currentTest = 0; | 3 var playDuration = 0; |
| 7 var willPauseInExistingRange = false; | 4 var startTimeOfPlay = 0; |
| 8 var willExtendAnExistingRange = false; | 5 var startTime = 0; |
| 9 | 6 |
| 10 var testStartTime = 0; | 7 function testRanges(expectedStartTimes, expectedEndTimes) { |
| 11 var logTestTiming = false; | 8 assert_equals(video.played.length, timeRangeCount); |
| 12 | 9 |
| 13 //@@@@@ Uncomment the following line to log the time each "video-played" sub-tes
t takes in test output | |
| 14 //@@@@@ logTestTiming = true; | |
| 15 | |
| 16 function logRanges() | |
| 17 { | |
| 18 consoleWrite(""); | |
| 19 for (i = 0; i < timeRangeCount; i++) { | 10 for (i = 0; i < timeRangeCount; i++) { |
| 20 consoleWrite("**** range " + i + " ( " + video.played.start(i).toFixed(
2) + ".." + video.played.end(i).toFixed(2) + ")"); | 11 assert_equals(video.played.start(i).toFixed(2), expectedStartTimes[i]); |
| 12 assert_equals(video.played.end(i).toFixed(2), expectedEndTimes[i]); |
| 21 } | 13 } |
| 22 } | 14 } |
| 23 | 15 |
| 24 function testRanges() | 16 function waitForPauseAndContinue(t, nextFunc, extendsRange, expectedStartTimes,
expectedEndTimes) { |
| 25 { | 17 video.onpause = t.step_func(function() { |
| 26 if (testStartTime) { | 18 var currentTime = video.currentTime.toFixed(2); |
| 27 logRanges(); | 19 if (extendsRange) { |
| 28 | 20 if(expectedEndTimes[currentTimeRange] < currentTime |
| 29 var duration = (window.performance.now() - testStartTime) / 1000; | 21 || expectedEndTimes[currentTimeRange] == undefined) { |
| 30 consoleWrite("**** Test " + currentTest + " took " + duration.toFixed(2)
+ " seconds"); | 22 expectedEndTimes[currentTimeRange] = currentTime; |
| 31 } | 23 } |
| 32 | 24 } else { |
| 33 testExpected("video.played.length", timeRangeCount); | 25 expectedEndTimes.splice(currentTimeRange, 0, currentTime); |
| 34 | 26 } |
| 35 for (i = 0; i < timeRangeCount; i++) { | 27 testRanges(expectedStartTimes, expectedEndTimes); |
| 36 testExpected("video.played.start(" + (i) + ").toFixed(2)", expectedStart
Times[i]); | 28 if (nextFunc) |
| 37 testExpected("video.played.end(" + (i) + ").toFixed(2)", expectedEndTi
mes[i]); | 29 nextFunc(); |
| 38 } | 30 else |
| 31 t.done(); |
| 32 }); |
| 39 } | 33 } |
| 40 | 34 |
| 41 function nextTest() | 35 function willCreateNewRange(expectedStartTimes) { |
| 42 { | |
| 43 if (logTestTiming) | |
| 44 testStartTime = window.performance.now(); | |
| 45 | |
| 46 if (currentTest >= testFunctions.length) | |
| 47 endTest(); | |
| 48 else | |
| 49 (testFunctions[currentTest])(); | |
| 50 currentTest++; | |
| 51 } | |
| 52 | |
| 53 function pause(evt) | |
| 54 { | |
| 55 currentTime = video.currentTime.toFixed(2); | |
| 56 | |
| 57 if (!willExtendAnExistingRange) | |
| 58 expectedEndTimes.splice(currentTimeRange, 0, currentTime) | |
| 59 else if(expectedEndTimes[currentTimeRange] < currentTime || expectedEndT
imes[currentTimeRange] == undefined) | |
| 60 expectedEndTimes[currentTimeRange] = currentTime; | |
| 61 | |
| 62 testRanges(); | |
| 63 nextTest(); | |
| 64 } | |
| 65 | |
| 66 function canplay(event) | |
| 67 { | |
| 68 testRanges(); | |
| 69 nextTest(); | |
| 70 } | |
| 71 | |
| 72 function willCreateNewRange() | |
| 73 { | |
| 74 expectedStartTimes.splice(currentTimeRange, 0, video.currentTime.toFixed(2)) | 36 expectedStartTimes.splice(currentTimeRange, 0, video.currentTime.toFixed(2)) |
| 75 ++timeRangeCount; | 37 ++timeRangeCount; |
| 76 } | 38 } |
| 77 | 39 |
| 78 function startPlayingInNewRange() | 40 function callPauseIfTimeIsReached() { |
| 79 { | 41 var playedTime = video.currentTime - startTimeOfPlay; |
| 80 willCreateNewRange(); | 42 if (playedTime < 0) { |
| 81 startPlaying(); | 43 // Deal with "loop" attribute. This allows only one loop, hence the firs
t warning |
| 44 // at the begining of platForDuration(). |
| 45 playedTime = video.duration - startTimeOfPlay + video.currentTime; |
| 46 } |
| 47 |
| 48 var elapsed = (performance.now() / 1000) - startTime; |
| 49 assert_less_than_equal(elapsed, 3.0); |
| 50 if (playedTime >= playDuration || video.currentTime == video.duration) |
| 51 video.pause(); |
| 52 else { |
| 53 var delta = (playDuration - playedTime) * 1000; |
| 54 setTimeout(this.step_func(callPauseIfTimeIsReached), delta); |
| 55 } |
| 82 } | 56 } |
| 83 | 57 |
| 84 function startPlaying() | 58 function playForDuration(duration, t) { |
| 85 { | 59 playDuration = duration; |
| 86 playForMillisecs(100); // Triggers pause() | 60 assert_less_than_equal(duration, video.duration); |
| 61 |
| 62 // A 2 second timeout was sometimes insufficient to play 1.25 seconds of mov
ie, |
| 63 // though more than 1 second of movie typically had played prior to those fa
ilures. |
| 64 // Use a larger value than 2 here. |
| 65 var timeoutThreshold = 3.0; |
| 66 assert_greater_than_equal(video.duration, timeoutThreshold); |
| 67 assert_less_than_equal(duration, timeoutThreshold - 1.5); |
| 68 video.play(); |
| 69 startTime = performance.now() / 1000; |
| 70 startTimeOfPlay = video.currentTime; |
| 71 |
| 72 // Add a small amount to the timer because it will take a non-zero |
| 73 // amount of time for the video to start playing. |
| 74 setTimeout(t.step_func(callPauseIfTimeIsReached), (duration * 1000) + 100); |
| 87 } | 75 } |
| 88 | 76 |
| 89 function secToMilli(seconds) | 77 function startPlayingInNewRange(t, expectedStartTimes) { |
| 90 { | 78 willCreateNewRange(expectedStartTimes); |
| 91 return seconds * 1000.; | 79 playForDuration(0.1, t); |
| 92 } | 80 } |
| 93 | |
| 94 function milliToSecs(milliseconds) | |
| 95 { | |
| 96 return milliseconds / 1000; | |
| 97 } | |
| 98 | |
| 99 function nowInSecs() | |
| 100 { | |
| 101 return milliToSecs(window.performance.now()); | |
| 102 } | |
| 103 | |
| 104 function playForMillisecs(milliseconds) | |
| 105 { | |
| 106 var playDuration = milliToSecs(milliseconds); | |
| 107 if (playDuration > video.duration) { | |
| 108 failTest("WARNING: playForMillisecs() does not support range (" + playDu
ration + ") bigger than video duration (" + video.duration + ") (yet)"); | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 // A 2 second timeout was sometimes insufficient to play 1.25 seconds of mov
ie, though more | |
| 113 // than 1 second of movie typically had played prior to those failures. Use
a larger value | |
| 114 // than 2 here. | |
| 115 var timeoutThreshold = 3.; | |
| 116 | |
| 117 if (video.duration < timeoutThreshold) { | |
| 118 failTest("WARNING: playForMillisecs() does not support video duration("
+ video.duration + ") smaller than timeout threshold (" + timeoutThreshold + ")"
); | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 if (playDuration > timeoutThreshold - 1.5) { | |
| 123 failTest("WARNING: playForMillisecs() does not support range (" + playDu
ration + ") within 1.5 seconds of timeout threshold (" + timeoutThreshold + ")")
; | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 run("video.play()"); | |
| 128 | |
| 129 var startTime = nowInSecs(); | |
| 130 var playedFromTime = video.currentTime; | |
| 131 var callPauseIfTimeIsReached = function () | |
| 132 { | |
| 133 var playedTime = video.currentTime - playedFromTime; | |
| 134 | |
| 135 if (playedTime < 0) { | |
| 136 // Deal with 'loop' attribute. This allows only one loop, hence the
first warning | |
| 137 // at the begining of playForMillisecs(). | |
| 138 playedTime = video.duration - playedFromTime + video.currentTime; | |
| 139 } | |
| 140 | |
| 141 var elapsed = nowInSecs() - startTime; | |
| 142 if (elapsed > timeoutThreshold) { | |
| 143 // Just in case something goes wrong. | |
| 144 failTest("ERROR: test stalled, waited " + elapsed + " seconds for mo
vie to play " + playedTime + " seconds"); | |
| 145 return; | |
| 146 } | |
| 147 | |
| 148 if (playedTime >= playDuration || video.currentTime == video.duration) | |
| 149 run("video.pause()"); | |
| 150 else { | |
| 151 var delta = milliseconds - playedTime * 1000; | |
| 152 setTimeout(callPauseIfTimeIsReached, delta); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 // Add a small amount to the timer because it will take a non-zero amount of
time for the | |
| 157 // video to start playing. | |
| 158 setTimeout(callPauseIfTimeIsReached, milliseconds + 100); | |
| 159 } | |
| 160 | |
| 161 function videoPlayedMain() | |
| 162 { | |
| 163 findMediaElement(); | |
| 164 | |
| 165 video.src = findMediaFile("video", "content/test"); | |
| 166 | |
| 167 waitForEvent("error"); | |
| 168 waitForEvent("loadstart"); | |
| 169 waitForEvent("ratechange"); | |
| 170 waitForEvent("loadedmetadata"); | |
| 171 waitForEventOnce("canplay", canplay); // Will trigger nextTest() which launc
hes the tests. | |
| 172 waitForEvent("pause", pause); | |
| 173 | |
| 174 video.load(); | |
| 175 } | |
| OLD | NEW |