test_mediarecorder_state_transition.html (6062B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test MediaRecorder State Transition</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 <script type="text/javascript" src="manifest.js"></script> 8 </head> 9 <body> 10 <pre id="test"> 11 <script class="testbody" type="text/javascript"> 12 var manager = new MediaTestManager; 13 14 // List of operation tests for media recorder objects to verify if running 15 // these operations should result in an exception or not 16 var operationTests = [ 17 { 18 operations: ['stop'], 19 isValid: true 20 }, 21 { 22 operations: ['requestData'], 23 isValid: false 24 }, 25 { 26 operations: ['pause'], 27 isValid: false 28 }, 29 { 30 operations: ['resume'], 31 isValid: false 32 }, 33 { 34 operations: ['start'], 35 isValid: true 36 }, 37 { 38 operations: ['start'], 39 isValid: true, 40 timeSlice: 200 41 }, 42 { 43 operations: ['start', 'pause'], 44 isValid: true 45 }, 46 { 47 operations: ['start', 'pause'], 48 isValid: true, 49 timeSlice: 200 50 }, 51 { 52 operations: ['start', 'start'], 53 isValid: false 54 }, 55 { 56 operations: ['start', 'resume'], 57 isValid: true 58 }, 59 { 60 operations: ['pause', 'start'], 61 isValid: false 62 }, 63 { 64 operations: ['resume', 'start'], 65 isValid: false 66 }, 67 { 68 operations: ['requestData', 'start'], 69 isValid: false 70 }, 71 { 72 operations: ['stop', 'start'], 73 isValid: true 74 }, 75 { 76 operations: ['start', 'stop'], 77 isValid: true 78 }, 79 { 80 operations: ['start', 'stop'], 81 isValid: true, 82 timeSlice: 200 83 }, 84 { 85 operations: ['start', 'requestData'], 86 isValid: true 87 }, 88 { 89 operations: ['start', 'requestData'], 90 isValid: true, 91 timeSlice: 200 92 }, 93 { 94 operations: ['start', 'pause', 'stop'], 95 isValid: true 96 }, 97 { 98 operations: ['start', 'pause', 'start'], 99 isValid: false 100 }, 101 { 102 operations: ['start', 'pause', 'pause'], 103 isValid: true 104 }, 105 { 106 operations: ['start', 'pause', 'requestData'], 107 isValid: true 108 }, 109 { 110 operations: ['start', 'pause', 'resume'], 111 isValid: true 112 }, 113 { 114 operations: ['start', 'pause', 'resume'], 115 isValid: true, 116 timeSlice: 200 117 }, 118 { 119 operations: ['start', 'resume', 'resume'], 120 isValid: true 121 }, 122 { 123 operations: ['start', 'resume', 'resume'], 124 isValid: true, 125 timeSlice: 200 126 }, 127 { 128 operations: ['start', 'resume', 'stop'], 129 isValid: true 130 }, 131 { 132 operations: ['start', 'resume', 'stop'], 133 isValid: true, 134 timeSlice: 200 135 }, 136 { 137 operations: ['start', 'stop', 'start'], 138 isValid: true 139 }, 140 { 141 operations: ['start', 'stop', 'start'], 142 isValid: true, 143 timeSlice: 200 144 }, 145 { 146 operations: ['start', 'stop', 'pause'], 147 isValid: false 148 }, 149 { 150 operations: ['start', 'stop', 'resume'], 151 isValid: false 152 }, 153 { 154 operations: ['start', 'stop', 'requestData'], 155 isValid: false 156 }, 157 { 158 operations: ['start', 'stop', 'stop'], 159 isValid: true 160 }, 161 { 162 operations: ['start', 'pause', 'resume', 'resume'], 163 isValid: true 164 }, 165 { 166 operations: ['start', 'pause', 'resume', 'resume'], 167 isValid: true, 168 timeSlice: 200 169 }, 170 { 171 operations: ['start', 'pause', 'pause', 'resume'], 172 isValid: true 173 }, 174 { 175 operations: ['start', 'pause', 'pause', 'resume'], 176 isValid: true, 177 timeSlice: 200 178 }, 179 { 180 operations: ['start', 'stop', 'start', 'stop'], 181 isValid: true 182 }, 183 { 184 operations: ['start', 'stop', 'start', 'stop'], 185 isValid: true, 186 timeSlice: 200 187 }, 188 { 189 operations: ['start', 'stop', 'start', 'pause'], 190 isValid: true 191 }, 192 { 193 operations: ['start', 'stop', 'start', 'pause'], 194 isValid: true, 195 timeSlice: 200 196 }, 197 { 198 operations: ['start', 'stop', 'start', 'resume'], 199 isValid: true 200 }, 201 { 202 operations: ['start', 'stop', 'start', 'resume'], 203 isValid: true, 204 timeSlice: 200 205 }, 206 { 207 operations: ['start', 'stop', 'start', 'requestData'], 208 isValid: true 209 }, 210 { 211 operations: ['start', 'stop', 'start', 'requestData'], 212 isValid: true, 213 timeSlice: 200 214 }, 215 ]; 216 217 /** 218 * Runs through each available state transition test by running all 219 * available operations on a media recorder object. Then, we report 220 * back if the test was expected through an exception or not. 221 * 222 * @param {MediaStream} testStream the media stream used for media recorder 223 * operation tests 224 */ 225 function runStateTransitionTests(testStream) { 226 for (const operationTest of operationTests) { 227 var mediaRecorder = new MediaRecorder(testStream); 228 var operationsString = operationTest.operations.toString(); 229 230 try { 231 for (const operation of operationTest.operations) { 232 if (operationTest.timeSlice && operation === 'start') { 233 operationsString += ' with timeslice ' + operationTest.timeSlice; 234 mediaRecorder[operation](operationTest.timeSlice); 235 } else { 236 mediaRecorder[operation](); 237 } 238 } 239 240 ok(operationTest.isValid, `${operationsString} should succeed`); 241 } catch (err) { 242 if (operationTest.isValid) { 243 ok(false, `${operationsString} failed unexpectedly with ${err.name}`); 244 } else { 245 is(err.name, "InvalidStateError", 246 `${operationsString} expected to fail with InvalidStateError`); 247 } 248 } 249 } 250 } 251 252 /** 253 * Starts a test on every media recorder file included to check that various 254 * state transition flows that can happen in the media recorder object throw 255 * exceptions when they are expected to and vice versa. 256 */ 257 function startTest(test, token) { 258 var element = document.createElement('audio'); 259 260 element.token = token; 261 manager.started(token); 262 263 element.src = test.name; 264 element.test = test; 265 element.stream = element.mozCaptureStream(); 266 267 element.oncanplaythrough = function () { 268 element.oncanplaythrough = null; 269 runStateTransitionTests(element.stream); 270 manager.finished(token); 271 }; 272 273 element.play(); 274 } 275 276 manager.runTests(gMediaRecorderTests, startTest); 277 </script> 278 </pre> 279 </body> 280 </html>