Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Helpers for Web Audio tests |
michael@0 | 2 | |
michael@0 | 3 | function expectException(func, exceptionCode) { |
michael@0 | 4 | var threw = false; |
michael@0 | 5 | try { |
michael@0 | 6 | func(); |
michael@0 | 7 | } catch (ex) { |
michael@0 | 8 | threw = true; |
michael@0 | 9 | ok(ex instanceof DOMException, "Expect a DOM exception"); |
michael@0 | 10 | is(ex.code, exceptionCode, "Expect the correct exception code"); |
michael@0 | 11 | } |
michael@0 | 12 | ok(threw, "The exception was thrown"); |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | function expectNoException(func) { |
michael@0 | 16 | var threw = false; |
michael@0 | 17 | try { |
michael@0 | 18 | func(); |
michael@0 | 19 | } catch (ex) { |
michael@0 | 20 | threw = true; |
michael@0 | 21 | } |
michael@0 | 22 | ok(!threw, "An exception was not thrown"); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | function expectTypeError(func) { |
michael@0 | 26 | var threw = false; |
michael@0 | 27 | try { |
michael@0 | 28 | func(); |
michael@0 | 29 | } catch (ex) { |
michael@0 | 30 | threw = true; |
michael@0 | 31 | ok(ex instanceof TypeError, "Expect a TypeError"); |
michael@0 | 32 | } |
michael@0 | 33 | ok(threw, "The exception was thrown"); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | function fuzzyCompare(a, b) { |
michael@0 | 37 | return Math.abs(a - b) < 9e-3; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | function compareChannels(buf1, buf2, |
michael@0 | 41 | /*optional*/ length, |
michael@0 | 42 | /*optional*/ sourceOffset, |
michael@0 | 43 | /*optional*/ destOffset, |
michael@0 | 44 | /*optional*/ skipLengthCheck) { |
michael@0 | 45 | if (!skipLengthCheck) { |
michael@0 | 46 | is(buf1.length, buf2.length, "Channels must have the same length"); |
michael@0 | 47 | } |
michael@0 | 48 | sourceOffset = sourceOffset || 0; |
michael@0 | 49 | destOffset = destOffset || 0; |
michael@0 | 50 | if (length == undefined) { |
michael@0 | 51 | length = buf1.length - sourceOffset; |
michael@0 | 52 | } |
michael@0 | 53 | var difference = 0; |
michael@0 | 54 | var maxDifference = 0; |
michael@0 | 55 | var firstBadIndex = -1; |
michael@0 | 56 | for (var i = 0; i < length; ++i) { |
michael@0 | 57 | if (!fuzzyCompare(buf1[i + sourceOffset], buf2[i + destOffset])) { |
michael@0 | 58 | difference++; |
michael@0 | 59 | maxDifference = Math.max(maxDifference, Math.abs(buf1[i + sourceOffset] - buf2[i + destOffset])); |
michael@0 | 60 | if (firstBadIndex == -1) { |
michael@0 | 61 | firstBadIndex = i; |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | }; |
michael@0 | 65 | |
michael@0 | 66 | is(difference, 0, "maxDifference: " + maxDifference + |
michael@0 | 67 | ", first bad index: " + firstBadIndex + |
michael@0 | 68 | " with test-data offset " + sourceOffset + " and expected-data offset " + |
michael@0 | 69 | destOffset + "; corresponding values " + buf1[firstBadIndex + sourceOffset] + |
michael@0 | 70 | " and " + buf2[firstBadIndex + destOffset] + " --- differences"); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | function compareBuffers(got, expected) { |
michael@0 | 74 | if (got.numberOfChannels != expected.numberOfChannels) { |
michael@0 | 75 | is(got.numberOfChannels, expected.numberOfChannels, |
michael@0 | 76 | "Correct number of buffer channels"); |
michael@0 | 77 | return; |
michael@0 | 78 | } |
michael@0 | 79 | if (got.length != expected.length) { |
michael@0 | 80 | is(got.length, expected.length, |
michael@0 | 81 | "Correct buffer length"); |
michael@0 | 82 | return; |
michael@0 | 83 | } |
michael@0 | 84 | if (got.sampleRate != expected.sampleRate) { |
michael@0 | 85 | is(got.sampleRate, expected.sampleRate, |
michael@0 | 86 | "Correct sample rate"); |
michael@0 | 87 | return; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | for (var i = 0; i < got.numberOfChannels; ++i) { |
michael@0 | 91 | compareChannels(got.getChannelData(i), expected.getChannelData(i), |
michael@0 | 92 | got.length, 0, 0, true); |
michael@0 | 93 | } |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | function getEmptyBuffer(context, length) { |
michael@0 | 97 | return context.createBuffer(gTest.numberOfChannels, length, context.sampleRate); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | /** |
michael@0 | 101 | * This function assumes that the test file defines a single gTest variable with |
michael@0 | 102 | * the following properties and methods: |
michael@0 | 103 | * |
michael@0 | 104 | * + numberOfChannels: optional property which specifies the number of channels |
michael@0 | 105 | * in the output. The default value is 2. |
michael@0 | 106 | * + createGraph: mandatory method which takes a context object and does |
michael@0 | 107 | * everything needed in order to set up the Web Audio graph. |
michael@0 | 108 | * This function returns the node to be inspected. |
michael@0 | 109 | * + createGraphAsync: async version of createGraph. This function takes |
michael@0 | 110 | * a callback which should be called with an argument |
michael@0 | 111 | * set to the node to be inspected when the callee is |
michael@0 | 112 | * ready to proceed with the test. Either this function |
michael@0 | 113 | * or createGraph must be provided. |
michael@0 | 114 | * + createExpectedBuffers: optional method which takes a context object and |
michael@0 | 115 | * returns either one expected buffer or an array of |
michael@0 | 116 | * them, designating what is expected to be observed |
michael@0 | 117 | * in the output. If omitted, the output is expected |
michael@0 | 118 | * to be silence. All buffers must have the same |
michael@0 | 119 | * length, which must be a bufferSize supported by |
michael@0 | 120 | * ScriptProcessorNode. This function is guaranteed |
michael@0 | 121 | * to be called before createGraph. |
michael@0 | 122 | * + length: property equal to the total number of frames which we are waiting |
michael@0 | 123 | * to see in the output, mandatory if createExpectedBuffers is not |
michael@0 | 124 | * provided, in which case it must be a bufferSize supported by |
michael@0 | 125 | * ScriptProcessorNode (256, 512, 1024, 2048, 4096, 8192, or 16384). |
michael@0 | 126 | * If createExpectedBuffers is provided then this must be equal to |
michael@0 | 127 | * the number of expected buffers * the expected buffer length. |
michael@0 | 128 | * |
michael@0 | 129 | * + skipOfflineContextTests: optional. when true, skips running tests on an offline |
michael@0 | 130 | * context by circumventing testOnOfflineContext. |
michael@0 | 131 | */ |
michael@0 | 132 | function runTest() |
michael@0 | 133 | { |
michael@0 | 134 | function done() { |
michael@0 | 135 | SimpleTest.finish(); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 139 | function runTestFunction () { |
michael@0 | 140 | if (!gTest.numberOfChannels) { |
michael@0 | 141 | gTest.numberOfChannels = 2; // default |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | var testLength; |
michael@0 | 145 | |
michael@0 | 146 | function runTestOnContext(context, callback, testOutput) { |
michael@0 | 147 | if (!gTest.createExpectedBuffers) { |
michael@0 | 148 | // Assume that the output is silence |
michael@0 | 149 | var expectedBuffers = getEmptyBuffer(context, gTest.length); |
michael@0 | 150 | } else { |
michael@0 | 151 | var expectedBuffers = gTest.createExpectedBuffers(context); |
michael@0 | 152 | } |
michael@0 | 153 | if (!(expectedBuffers instanceof Array)) { |
michael@0 | 154 | expectedBuffers = [expectedBuffers]; |
michael@0 | 155 | } |
michael@0 | 156 | var expectedFrames = 0; |
michael@0 | 157 | for (var i = 0; i < expectedBuffers.length; ++i) { |
michael@0 | 158 | is(expectedBuffers[i].numberOfChannels, gTest.numberOfChannels, |
michael@0 | 159 | "Correct number of channels for expected buffer " + i); |
michael@0 | 160 | expectedFrames += expectedBuffers[i].length; |
michael@0 | 161 | } |
michael@0 | 162 | if (gTest.length && gTest.createExpectedBuffers) { |
michael@0 | 163 | is(expectedFrames, gTest.length, "Correct number of expected frames"); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | if (gTest.createGraphAsync) { |
michael@0 | 167 | gTest.createGraphAsync(context, function(nodeToInspect) { |
michael@0 | 168 | testOutput(nodeToInspect, expectedBuffers, callback); |
michael@0 | 169 | }); |
michael@0 | 170 | } else { |
michael@0 | 171 | testOutput(gTest.createGraph(context), expectedBuffers, callback); |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | function testOnNormalContext(callback) { |
michael@0 | 176 | function testOutput(nodeToInspect, expectedBuffers, callback) { |
michael@0 | 177 | testLength = 0; |
michael@0 | 178 | var sp = context.createScriptProcessor(expectedBuffers[0].length, gTest.numberOfChannels); |
michael@0 | 179 | nodeToInspect.connect(sp); |
michael@0 | 180 | sp.connect(context.destination); |
michael@0 | 181 | sp.onaudioprocess = function(e) { |
michael@0 | 182 | var expectedBuffer = expectedBuffers.shift(); |
michael@0 | 183 | testLength += expectedBuffer.length; |
michael@0 | 184 | compareBuffers(e.inputBuffer, expectedBuffer); |
michael@0 | 185 | if (expectedBuffers.length == 0) { |
michael@0 | 186 | sp.onaudioprocess = null; |
michael@0 | 187 | callback(); |
michael@0 | 188 | } |
michael@0 | 189 | }; |
michael@0 | 190 | } |
michael@0 | 191 | var context = new AudioContext(); |
michael@0 | 192 | runTestOnContext(context, callback, testOutput); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | function testOnOfflineContext(callback, sampleRate) { |
michael@0 | 196 | function testOutput(nodeToInspect, expectedBuffers, callback) { |
michael@0 | 197 | nodeToInspect.connect(context.destination); |
michael@0 | 198 | context.oncomplete = function(e) { |
michael@0 | 199 | var samplesSeen = 0; |
michael@0 | 200 | while (expectedBuffers.length) { |
michael@0 | 201 | var expectedBuffer = expectedBuffers.shift(); |
michael@0 | 202 | is(e.renderedBuffer.numberOfChannels, expectedBuffer.numberOfChannels, |
michael@0 | 203 | "Correct number of input buffer channels"); |
michael@0 | 204 | for (var i = 0; i < e.renderedBuffer.numberOfChannels; ++i) { |
michael@0 | 205 | compareChannels(e.renderedBuffer.getChannelData(i), |
michael@0 | 206 | expectedBuffer.getChannelData(i), |
michael@0 | 207 | expectedBuffer.length, |
michael@0 | 208 | samplesSeen, |
michael@0 | 209 | undefined, |
michael@0 | 210 | true); |
michael@0 | 211 | } |
michael@0 | 212 | samplesSeen += expectedBuffer.length; |
michael@0 | 213 | } |
michael@0 | 214 | callback(); |
michael@0 | 215 | }; |
michael@0 | 216 | context.startRendering(); |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | var context = new OfflineAudioContext(gTest.numberOfChannels, testLength, sampleRate); |
michael@0 | 220 | runTestOnContext(context, callback, testOutput); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | testOnNormalContext(function() { |
michael@0 | 224 | if (!gTest.skipOfflineContextTests) { |
michael@0 | 225 | testOnOfflineContext(function() { |
michael@0 | 226 | testOnOfflineContext(done, 44100); |
michael@0 | 227 | }, 48000); |
michael@0 | 228 | } else { |
michael@0 | 229 | done(); |
michael@0 | 230 | } |
michael@0 | 231 | }); |
michael@0 | 232 | }; |
michael@0 | 233 | |
michael@0 | 234 | if (document.readyState !== 'complete') { |
michael@0 | 235 | addLoadEvent(runTestFunction); |
michael@0 | 236 | } else { |
michael@0 | 237 | runTestFunction(); |
michael@0 | 238 | } |
michael@0 | 239 | } |