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
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <title>Test played member for media elements</title> |
michael@0 | 5 | <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> |
michael@0 | 6 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 7 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 8 | <script type="text/javascript" src="manifest.js"></script> |
michael@0 | 9 | </head> |
michael@0 | 10 | <body> |
michael@0 | 11 | <pre id='test'> |
michael@0 | 12 | <script class="testbody" type='application/javascript;version=1.8'> |
michael@0 | 13 | |
michael@0 | 14 | let manager = new MediaTestManager; |
michael@0 | 15 | |
michael@0 | 16 | SimpleTest.expectAssertions(0, 2); |
michael@0 | 17 | |
michael@0 | 18 | function finish_test(element) { |
michael@0 | 19 | if (element.parentNode) |
michael@0 | 20 | element.parentNode.removeChild(element); |
michael@0 | 21 | element.src=""; |
michael@0 | 22 | manager.finished(element.token); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | // Check that a file has been played in its entirety. |
michael@0 | 26 | function check_full_file_played(element) { |
michael@0 | 27 | element.addEventListener('ended', (function(e) { |
michael@0 | 28 | let interval_count = e.target.played.length; |
michael@0 | 29 | is(interval_count, 1, "normal play : a.played.length must be 1"); |
michael@0 | 30 | is(element.played.start(0), 0, "start time shall be 0"); |
michael@0 | 31 | is(element.played.end(0), e.target.duration, "end time shall be duration"); |
michael@0 | 32 | finish_test(e.target); |
michael@0 | 33 | }), false); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | var tests = [ |
michael@0 | 37 | // Without playing, check that player.played.length == 0. |
michael@0 | 38 | { |
michael@0 | 39 | setup : function(element) { |
michael@0 | 40 | element.addEventListener("loadedmetadata", function() { |
michael@0 | 41 | is(element.played.length, 0, "audio : initial played.length equals zero"); |
michael@0 | 42 | finish_test(element); |
michael@0 | 43 | }); |
michael@0 | 44 | } |
michael@0 | 45 | }, |
michael@0 | 46 | // Play the file, test the range we have. |
michael@0 | 47 | { |
michael@0 | 48 | setup : function(element) { |
michael@0 | 49 | check_full_file_played(element); |
michael@0 | 50 | element.play(); |
michael@0 | 51 | } |
michael@0 | 52 | }, |
michael@0 | 53 | |
michael@0 | 54 | // Play the second half of the file, pause, play |
michael@0 | 55 | // an check we have only one range. |
michael@0 | 56 | { |
michael@0 | 57 | setup : function (element) { |
michael@0 | 58 | element.addEventListener("ended", function (e) { |
michael@0 | 59 | var t = e.target; |
michael@0 | 60 | check_full_file_played(t); |
michael@0 | 61 | t.pause(); |
michael@0 | 62 | t.currentTime = 0; |
michael@0 | 63 | t.play(); |
michael@0 | 64 | }, false); |
michael@0 | 65 | element.addEventListener("loadedmetadata", function() { |
michael@0 | 66 | element.currentTime = element.duration / 2; |
michael@0 | 67 | element.play(); |
michael@0 | 68 | }, false); |
michael@0 | 69 | } |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | // Play the first half of the file, seek back, while |
michael@0 | 73 | // continuing to play. We shall have only one range. |
michael@0 | 74 | { |
michael@0 | 75 | setup : function (element) { |
michael@0 | 76 | let onTimeUpdate = function() { |
michael@0 | 77 | if (element.currentTime > element.duration/2) { |
michael@0 | 78 | element.removeEventListener("timeupdate", onTimeUpdate, false); |
michael@0 | 79 | element.pause(); |
michael@0 | 80 | var oldEndRange = element.played.end(0); |
michael@0 | 81 | element.currentTime = element.duration / 4; |
michael@0 | 82 | is(element.played.end(0), oldEndRange, |
michael@0 | 83 | "When seeking back, |played| should not be changed"); |
michael@0 | 84 | element.play(); |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | element.addEventListener("timeupdate", onTimeUpdate, false); |
michael@0 | 88 | check_full_file_played(element); |
michael@0 | 89 | element.play(); |
michael@0 | 90 | } |
michael@0 | 91 | }, |
michael@0 | 92 | |
michael@0 | 93 | // Play and seek to have two ranges, and check that, as well a |
michael@0 | 94 | // boundaries. |
michael@0 | 95 | { |
michael@0 | 96 | setup : function (element) { |
michael@0 | 97 | let onTimeUpdate = function() { |
michael@0 | 98 | if (element.currentTime > element.duration / 2) { |
michael@0 | 99 | element.removeEventListener("timeupdate", onTimeUpdate, false); |
michael@0 | 100 | element.pause(); |
michael@0 | 101 | element.currentTime += element.duration/10; |
michael@0 | 102 | element.play(); |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | element.addEventListener("loadedmetadata", function() { |
michael@0 | 107 | element.addEventListener("timeupdate", onTimeUpdate, false); |
michael@0 | 108 | }, false); |
michael@0 | 109 | |
michael@0 | 110 | |
michael@0 | 111 | element.addEventListener("ended", (function() { |
michael@0 | 112 | if(element.played.length > 1) { |
michael@0 | 113 | is(element.played.length, 2, "element.played.length == 2"); |
michael@0 | 114 | var guess = element.played.end(0) + element.duration/10.0; |
michael@0 | 115 | ok(rangeCheck(element.played.start(1), guess), "we should have seeked forward by one tenth of the duration"); |
michael@0 | 116 | is(element.played.end(1), element.duration, "end of second range shall be the total duration"); |
michael@0 | 117 | } |
michael@0 | 118 | is(element.played.start(0), 0, "start of first range shall be 0"); |
michael@0 | 119 | finish_test(element); |
michael@0 | 120 | }), false); |
michael@0 | 121 | |
michael@0 | 122 | element.play(); |
michael@0 | 123 | } |
michael@0 | 124 | }, |
michael@0 | 125 | |
michael@0 | 126 | // Play to create two ranges, in the reverse order. check that they are sorted. |
michael@0 | 127 | { |
michael@0 | 128 | setup : function (element) { |
michael@0 | 129 | function end() { |
michael@0 | 130 | element.pause(); |
michael@0 | 131 | let p = element.played; |
michael@0 | 132 | ok(p.length >= 1, "There should be at least one range"); |
michael@0 | 133 | is(p.start(0), element.duration/6, "Start of first range should be the sixth of the duration"); |
michael@0 | 134 | ok(p.end(p.length - 1) > 5*element.duration/6, "End of last range should be greater that five times the sixth of the duration"); |
michael@0 | 135 | finish_test(element); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | function pauseseekrestart() { |
michael@0 | 139 | element.pause(); |
michael@0 | 140 | element.currentTime = element.duration/6; |
michael@0 | 141 | element.play(); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | function onTimeUpdate_pauseseekrestart() { |
michael@0 | 145 | if (element.currentTime > 5*element.duration/6) { |
michael@0 | 146 | element.removeEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false); |
michael@0 | 147 | pauseseekrestart(); |
michael@0 | 148 | element.addEventListener("timeupdate", onTimeUpdate_end, false); |
michael@0 | 149 | } |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | function onTimeUpdate_end() { |
michael@0 | 153 | if (element.currentTime > 3 * element.duration/6) { |
michael@0 | 154 | element.removeEventListener("timeupdate", onTimeUpdate_end, false); |
michael@0 | 155 | end(); |
michael@0 | 156 | } |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | element.addEventListener("timeupdate", onTimeUpdate_pauseseekrestart, false); |
michael@0 | 160 | |
michael@0 | 161 | element.addEventListener('loadedmetadata', function() { |
michael@0 | 162 | element.currentTime = 4 * element.duration/6; |
michael@0 | 163 | element.play(); |
michael@0 | 164 | }, false); |
michael@0 | 165 | } |
michael@0 | 166 | }, |
michael@0 | 167 | // Seek repeatedly without playing. No range should appear. |
michael@0 | 168 | { |
michael@0 | 169 | setup : function(element) { |
michael@0 | 170 | let index = 1; |
michael@0 | 171 | |
michael@0 | 172 | element.addEventListener('seeked', function() { |
michael@0 | 173 | index++; |
michael@0 | 174 | element.currentTime = index * element.duration / 5; |
michael@0 | 175 | is(element.played.length, 0, "element.played.length should be 0"); |
michael@0 | 176 | if (index == 5) { |
michael@0 | 177 | finish_test(element); |
michael@0 | 178 | } |
michael@0 | 179 | }, false); |
michael@0 | 180 | |
michael@0 | 181 | element.addEventListener('loadedmetadata', function() { |
michael@0 | 182 | element.currentTime = element.duration / 5; |
michael@0 | 183 | }, false); |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | ]; |
michael@0 | 187 | |
michael@0 | 188 | function rangeCheck(n1, n2) { |
michael@0 | 189 | var THRESHOLD = 0.35; |
michael@0 | 190 | var diff = Math.abs(n1 - n2); |
michael@0 | 191 | if (diff < THRESHOLD) { |
michael@0 | 192 | return true; |
michael@0 | 193 | } |
michael@0 | 194 | return false; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | function createTestArray() { |
michael@0 | 198 | var A = []; |
michael@0 | 199 | for (var i=0; i<tests.length; i++) { |
michael@0 | 200 | for (var k=0; k<gPlayedTests.length; k++) { |
michael@0 | 201 | var t = new Object(); |
michael@0 | 202 | t.setup = tests[i].setup; |
michael@0 | 203 | t.name = gPlayedTests[k].name; |
michael@0 | 204 | t.type = gPlayedTests[k].type; |
michael@0 | 205 | A.push(t); |
michael@0 | 206 | } |
michael@0 | 207 | } |
michael@0 | 208 | return A; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | function startTest(test, token) { |
michael@0 | 212 | var elemType = getMajorMimeType(test.type); |
michael@0 | 213 | var element = document.createElement(elemType); |
michael@0 | 214 | element.src = test.name; |
michael@0 | 215 | element.token = token; |
michael@0 | 216 | element.volume = 0; |
michael@0 | 217 | test.setup(element); |
michael@0 | 218 | manager.started(token); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | |
michael@0 | 222 | manager.runTests(createTestArray(), startTest); |
michael@0 | 223 | |
michael@0 | 224 | </script> |
michael@0 | 225 | </pre> |
michael@0 | 226 | </body> |
michael@0 | 227 | </html> |