OLD | NEW |
1 | 1 // TODO(srirama.m): Remove these globals with the help of expected arrays. |
2 var expectedStartTimes = new Array(); | |
3 var expectedEndTimes = new Array(); | |
4 var timeRangeCount = 0; | 2 var timeRangeCount = 0; |
5 var currentTimeRange = 0; | 3 var currentTimeRange = 0; |
6 var currentTest = 0; | 4 var playDuration = 0; |
7 var willPauseInExistingRange = false; | 5 var startTimeOfPlay = 0; |
8 var willExtendAnExistingRange = false; | 6 var startTime = 0; |
9 | 7 |
10 var testStartTime = 0; | 8 function testRanges(expectedStartTimes, expectedEndTimes) { |
11 var logTestTiming = false; | 9 assert_equals(video.played.length, timeRangeCount); |
12 | 10 |
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++) { | 11 for (i = 0; i < timeRangeCount; i++) { |
20 consoleWrite("**** range " + i + " ( " + video.played.start(i).toFixed(
2) + ".." + video.played.end(i).toFixed(2) + ")"); | 12 assert_equals(video.played.start(i).toFixed(2), expectedStartTimes[i]); |
| 13 assert_equals(video.played.end(i).toFixed(2), expectedEndTimes[i]); |
21 } | 14 } |
22 } | 15 } |
23 | 16 |
24 function testRanges() | 17 function waitForPauseAndContinue(t, nextFunc, extendsRange, expectedStartTimes,
expectedEndTimes) { |
25 { | 18 video.onpause = t.step_func(function() { |
26 if (testStartTime) { | 19 var currentTime = video.currentTime.toFixed(2); |
27 logRanges(); | 20 if (extendsRange) { |
28 | 21 if(expectedEndTimes[currentTimeRange] < currentTime |
29 var duration = (window.performance.now() - testStartTime) / 1000; | 22 || expectedEndTimes[currentTimeRange] == undefined) { |
30 consoleWrite("**** Test " + currentTest + " took " + duration.toFixed(2)
+ " seconds"); | 23 expectedEndTimes[currentTimeRange] = currentTime; |
31 } | 24 } |
32 | 25 } else { |
33 testExpected("video.played.length", timeRangeCount); | 26 expectedEndTimes.splice(currentTimeRange, 0, currentTime); |
34 | 27 } |
35 for (i = 0; i < timeRangeCount; i++) { | 28 testRanges(expectedStartTimes, expectedEndTimes); |
36 testExpected("video.played.start(" + (i) + ").toFixed(2)", expectedStart
Times[i]); | 29 if (nextFunc) |
37 testExpected("video.played.end(" + (i) + ").toFixed(2)", expectedEndTi
mes[i]); | 30 nextFunc(); |
38 } | 31 else |
| 32 t.done(); |
| 33 }); |
39 } | 34 } |
40 | 35 |
41 function nextTest() | 36 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)) | 37 expectedStartTimes.splice(currentTimeRange, 0, video.currentTime.toFixed(2)) |
75 ++timeRangeCount; | 38 ++timeRangeCount; |
76 } | 39 } |
77 | 40 |
78 function startPlayingInNewRange() | 41 function callPauseIfTimeIsReached() { |
79 { | 42 var playedTime = video.currentTime - startTimeOfPlay; |
80 willCreateNewRange(); | 43 if (playedTime < 0) { |
81 startPlaying(); | 44 // Deal with "loop" attribute. This allows only one loop, hence the firs
t warning |
| 45 // at the begining of platForDuration(). |
| 46 playedTime = video.duration - startTimeOfPlay + video.currentTime; |
| 47 } |
| 48 |
| 49 var elapsed = (performance.now() / 1000) - startTime; |
| 50 assert_less_than_equal(elapsed, 3.0); |
| 51 if (playedTime >= playDuration || video.currentTime == video.duration) |
| 52 video.pause(); |
| 53 else { |
| 54 var delta = (playDuration - playedTime) * 1000; |
| 55 setTimeout(this.step_func(callPauseIfTimeIsReached), delta); |
| 56 } |
82 } | 57 } |
83 | 58 |
84 function startPlaying() | 59 function playForDuration(duration, t) { |
85 { | 60 playDuration = duration; |
86 playForMillisecs(100); // Triggers pause() | 61 assert_less_than_equal(duration, video.duration); |
| 62 |
| 63 // A 2 second timeout was sometimes insufficient to play 1.25 seconds of mov
ie, |
| 64 // though more than 1 second of movie typically had played prior to those fa
ilures. |
| 65 // Use a larger value than 2 here. |
| 66 var timeoutThreshold = 3.0; |
| 67 assert_greater_than_equal(video.duration, timeoutThreshold); |
| 68 assert_less_than_equal(duration, timeoutThreshold - 1.5); |
| 69 video.play(); |
| 70 startTime = performance.now() / 1000; |
| 71 startTimeOfPlay = video.currentTime; |
| 72 |
| 73 // Add a small amount to the timer because it will take a non-zero |
| 74 // amount of time for the video to start playing. |
| 75 setTimeout(t.step_func(callPauseIfTimeIsReached), (duration * 1000) + 100); |
87 } | 76 } |
88 | 77 |
89 function secToMilli(seconds) | 78 function startPlayingInNewRange(t, expectedStartTimes) { |
90 { | 79 willCreateNewRange(expectedStartTimes); |
91 return seconds * 1000.; | 80 playForDuration(0.1, t); |
92 } | 81 } |
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 |