| OLD | NEW |
| 1 <!doctype html> | 1 <!doctype html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <title>Test Constructor: AudioBuffer</title> | 4 <title>Test Constructor: AudioBuffer</title> |
| 5 <script src="../../resources/testharness.js"></script> | 5 <script src="../../resources/testharness.js"></script> |
| 6 <script src="../../resources/testharnessreport.js"></script> | 6 <script src="../../resources/testharnessreport.js"></script> |
| 7 <script src="../resources/audit-util.js"></script> | 7 <script src="../resources/audit-util.js"></script> |
| 8 <script src="../resources/audio-testing.js"></script> | 8 <script src="../resources/audit.js"></script> |
| 9 <script src="new-audionodeoptions.js"></script> |
| 9 </head> | 10 </head> |
| 10 | 11 |
| 11 <body> | 12 <body> |
| 12 <script> | 13 <script> |
| 13 var context; | 14 let context; |
| 14 | 15 |
| 15 var audit = Audit.createTaskRunner(); | 16 let audit = Audit.createTaskRunner(); |
| 16 | 17 |
| 17 audit.defineTask("initialize", function (taskDone) { | 18 audit.define('initialize', (task, should) => { |
| 18 Should("context = new OfflineAudioContext(...)", function () { | 19 context = initializeContext(should); |
| 19 context = new OfflineAudioContext(1, 1, 48000); | 20 task.done(); |
| 20 }).notThrow(); | |
| 21 | |
| 22 taskDone(); | |
| 23 }); | 21 }); |
| 24 | 22 |
| 25 audit.defineTask("invalid constructor", function (taskDone) { | 23 audit.define('invalid constructor', (task, should) => { |
| 26 var node; | 24 should(() => { |
| 27 var args; | 25 new AudioBuffer(); |
| 28 var success = true; | 26 }, 'new AudioBuffer()').throw('TypeError'); |
| 27 should(() => { |
| 28 new AudioBuffer(1); |
| 29 }, 'new AudioBuffer(1)').throw('TypeError'); |
| 30 should(() => { |
| 31 new AudioBuffer(Date, 42); |
| 32 }, 'new AudioBuffer(Date, 42)').throw('TypeError'); |
| 29 | 33 |
| 30 args = []; | 34 task.done(); |
| 31 success = Should("new AudioBuffer()", function () { | |
| 32 node = new AudioBuffer(); | |
| 33 }).throw("TypeError"); | |
| 34 success = Should("new AudioBuffer(1)", function () { | |
| 35 node = new AudioBuffer(1) && success; | |
| 36 }).throw("TypeError"); | |
| 37 success = Should("new AudioBuffer(Date, 42)", function () { | |
| 38 node = new AudioBuffer(Date, 42) && success; | |
| 39 }).throw("TypeError"); | |
| 40 | |
| 41 Should("Invalid constructors", success) | |
| 42 .summarize( | |
| 43 "correctly threw errors", | |
| 44 "did not throw errors in all cases"); | |
| 45 | |
| 46 taskDone(); | |
| 47 }); | 35 }); |
| 48 | 36 |
| 49 audit.defineTask("required options", function (taskDone) { | 37 audit.define('required options', (task, should) => { |
| 50 var success = true; | 38 let buffer; |
| 51 | |
| 52 var buffer; | |
| 53 | 39 |
| 54 // The length and sampleRate attributes are required; all others are | 40 // The length and sampleRate attributes are required; all others are |
| 55 // optional. | 41 // optional. |
| 56 success = Should("buffer = new AudioBuffer({})", function () { | 42 should(() => { |
| 57 var buffer = new AudioBuffer({}); | 43 new AudioBuffer({}); |
| 58 }).throw("TypeError"); | 44 }, 'buffer = new AudioBuffer({})').throw('TypeError'); |
| 59 | 45 |
| 60 success = Should("buffer = new AudioBuffer({length: 1})", function () { | 46 should(() => { |
| 61 new AudioBuffer({ | 47 new AudioBuffer({length: 1}); |
| 62 length: 1 | 48 }, 'buffer = new AudioBuffer({length: 1})').throw('TypeError'); |
| 63 }); | |
| 64 }).throw("TypeError") && success; | |
| 65 | 49 |
| 66 success = Should("buffer = new AudioBuffer({sampleRate: 48000})", | 50 should(() => { |
| 67 function () { | 51 new AudioBuffer({sampleRate: 48000}); |
| 68 new AudioBuffer({ | 52 }, 'buffer = new AudioBuffer({sampleRate: 48000})').throw('TypeError'); |
| 69 sampleRate: 48000 | |
| 70 }); | |
| 71 }).throw("TypeError") && success; | |
| 72 | 53 |
| 73 success = Should("buffer = new AudioBuffer({numberOfChannels: 1}", | 54 should(() => { |
| 74 function () { | 55 buffer = new AudioBuffer({numberOfChannels: 1}); |
| 75 buffer = new AudioBuffer({ | 56 }, 'buffer = new AudioBuffer({numberOfChannels: 1}').throw('TypeError'); |
| 76 numberOfChannels: 1 | |
| 77 }); | |
| 78 }).throw("TypeError") && success; | |
| 79 | 57 |
| 80 // Length and sampleRate are required, but others are optional. | 58 // Length and sampleRate are required, but others are optional. |
| 81 success = Should( | 59 should( |
| 82 "buffer = new AudioBuffer({length: 21, sampleRate: 48000}", | 60 () => { |
| 83 function () { | 61 buffer = |
| 84 buffer = new AudioBuffer({ | 62 new AudioBuffer({length: 21, sampleRate: context.sampleRate}); |
| 85 length: 21, | 63 }, |
| 86 sampleRate: context.sampleRate | 64 'buffer0 = new AudioBuffer({length: 21, sampleRate: ' + |
| 87 }); | 65 context.sampleRate + '}') |
| 88 }).notThrow() && success; | 66 .notThrow(); |
| 89 // Verify the buffer has the correct values. | 67 // Verify the buffer has the correct values. |
| 90 success = Should("buffer.numberOfChannels", buffer.numberOfChannels) | 68 should(buffer.numberOfChannels, 'buffer0.numberOfChannels') |
| 91 .beEqualTo(1) && success; | 69 .beEqualTo(1); |
| 92 success = Should("buffer.length", buffer.length) | 70 should(buffer.length, 'buffer0.length').beEqualTo(21); |
| 93 .beEqualTo(21) && success; | 71 should(buffer.sampleRate, 'buffer0.sampleRate') |
| 94 success = Should("buffer.sampleRate", buffer.sampleRate) | 72 .beEqualTo(context.sampleRate); |
| 95 .beEqualTo(context.sampleRate) && success; | |
| 96 | 73 |
| 97 success = Should( | 74 should( |
| 98 "buffer = new AudioBuffer({numberOfChannels: 1, length: 1, sampleRate:
48000}", | 75 () => { |
| 99 function () { | 76 buffer = new AudioBuffer( |
| 100 buffer = new AudioBuffer({ | 77 {numberOfChannels: 3, length: 1, sampleRate: 48000}); |
| 101 numberOfChannels: 1, | 78 }, |
| 102 length: 1, | 79 'buffer1 = new AudioBuffer(' + |
| 103 sampleRate: 48000 | 80 '{numberOfChannels: 3, length: 1, sampleRate: 48000})') |
| 104 }); | 81 .notThrow(); |
| 105 }).notThrow() && success; | 82 // Verify the buffer has the correct values. |
| 83 should(buffer.numberOfChannels, 'buffer1.numberOfChannels') |
| 84 .beEqualTo(3); |
| 85 should(buffer.length, 'buffer1.length').beEqualTo(1); |
| 86 should(buffer.sampleRate, 'buffer1.sampleRate').beEqualTo(48000); |
| 106 | 87 |
| 107 Should("Missing option values handled", success) | 88 task.done(); |
| 108 .summarize("correctly", "incorrectly"); | |
| 109 | |
| 110 taskDone(); | |
| 111 }); | 89 }); |
| 112 | 90 |
| 113 audit.defineTask("invalid option values", function (taskDone) { | 91 audit.define('invalid option values', (task, should) => { |
| 114 var success = true; | 92 let options = {numberOfChannels: 0, length: 1, sampleRate: 16000}; |
| 93 should( |
| 94 () => { |
| 95 let buffer = new AudioBuffer(options); |
| 96 }, |
| 97 'new AudioBuffer(' + JSON.stringify(options) + ')') |
| 98 .throw('NotSupportedError'); |
| 115 | 99 |
| 116 var options = { | 100 options = {numberOfChannels: 99, length: 0, sampleRate: 16000}; |
| 117 numberOfChannels: 0, | 101 should( |
| 118 length: 1, | 102 () => { |
| 119 sampleRate: 16000 | 103 let buffer = new AudioBuffer(options); |
| 120 }; | 104 }, |
| 121 success = Should("new AudioBuffer(" + JSON.stringify(options) + ")", fun
ction () { | 105 'new AudioBuffer(' + JSON.stringify(options) + ')') |
| 122 var buffer = new AudioBuffer(options); | 106 .throw('NotSupportedError'); |
| 123 }).throw("NotSupportedError"); | |
| 124 | 107 |
| 125 options = { | 108 options = {numberOfChannels: 1, length: 0, sampleRate: 16000}; |
| 126 numberOfChannels: 99, | 109 should( |
| 127 length: 0, | 110 () => { |
| 128 sampleRate: 16000 | 111 let buffer = new AudioBuffer(options); |
| 129 }; | 112 }, |
| 130 success = Should("new AudioBuffer(" + JSON.stringify(options) + ")", fun
ction () { | 113 'new AudioBuffer(' + JSON.stringify(options) + ')') |
| 131 var buffer = new AudioBuffer(options); | 114 .throw('NotSupportedError'); |
| 132 }).throw("NotSupportedError") && success; | |
| 133 | 115 |
| 134 options = { | 116 options = {numberOfChannels: 1, length: 1, sampleRate: 100}; |
| 135 numberOfChannels: 1, | 117 should( |
| 136 length: 0, | 118 () => { |
| 137 sampleRate: 16000 | 119 let buffer = new AudioBuffer(options); |
| 138 }; | 120 }, |
| 139 success = Should("new AudioBuffer(" + JSON.stringify(options) + ")", fun
ction () { | 121 'new AudioBuffer(' + JSON.stringify(options) + ')') |
| 140 var buffer = new AudioBuffer(options); | 122 .throw('NotSupportedError'); |
| 141 }).throw("NotSupportedError") && success; | |
| 142 | 123 |
| 143 options = { | 124 task.done(); |
| 144 numberOfChannels: 1, | |
| 145 length: 1, | |
| 146 sampleRate: 100 | |
| 147 }; | |
| 148 success = Should("new AudioBuffer(" + JSON.stringify(options) + ")", fun
ction () { | |
| 149 var buffer = new AudioBuffer(options); | |
| 150 }).throw("NotSupportedError") && success; | |
| 151 | |
| 152 Should("Invalid option values handled", success) | |
| 153 .summarize("correctly", "incorrectly"); | |
| 154 | |
| 155 taskDone(); | |
| 156 }); | 125 }); |
| 157 | 126 |
| 158 audit.defineTask("default constructor", function (taskDone) { | 127 audit.define('default constructor', (task, should) => { |
| 159 var buffer; | 128 let buffer; |
| 160 var success = true; | |
| 161 | 129 |
| 162 var options = { | 130 let options = {numberOfChannels: 5, length: 17, sampleRate: 16000}; |
| 163 numberOfChannels: 5, | 131 should( |
| 164 length: 17, | 132 () => { |
| 165 sampleRate: 16000 | 133 buffer = new AudioBuffer(options); |
| 166 }; | 134 }, |
| 167 success = Should( | 135 'buffer = new AudioBuffer(' + JSON.stringify(options) + ')') |
| 168 "buffer = new AudioBuffer(" + JSON.stringify(options) + ")", | 136 .notThrow(); |
| 169 function () { | |
| 170 buffer = new AudioBuffer(options); | |
| 171 }).notThrow(); | |
| 172 | 137 |
| 173 success = Should("buffer.numberOfChannels", buffer.numberOfChannels) | 138 should(buffer.numberOfChannels, 'buffer.numberOfChannels') |
| 174 .beEqualTo(options.numberOfChannels) && success; | 139 .beEqualTo(options.numberOfChannels); |
| 175 success = Should("buffer.length", buffer.length) | 140 should(buffer.length, 'buffer.length').beEqualTo(options.length); |
| 176 .beEqualTo(options.length) && success; | 141 should(buffer.sampleRate, 'buffer.sampleRate').beEqualTo(16000); |
| 177 success = Should("buffer.sampleRate", buffer.sampleRate) | |
| 178 .beEqualTo(16000) && success; | |
| 179 | 142 |
| 180 Should("Default constructor values set", success) | 143 task.done(); |
| 181 .summarize("correctly", "incorrectly"); | |
| 182 | |
| 183 taskDone(); | |
| 184 }); | 144 }); |
| 185 | 145 |
| 186 audit.defineTask("valid constructor", function (taskDone) { | 146 audit.define('valid constructor', (task, should) => { |
| 187 var buffer; | 147 let buffer; |
| 188 var success = true; | |
| 189 | 148 |
| 190 var options = { | 149 let options = {numberOfChannels: 3, length: 42, sampleRate: 54321}; |
| 191 numberOfChannels: 3, | |
| 192 length: 42, | |
| 193 sampleRate: 54321 | |
| 194 }; | |
| 195 | 150 |
| 196 var message = "new AudioBuffer(" + JSON.stringify(options) + ")"; | 151 let message = 'new AudioBuffer(' + JSON.stringify(options) + ')'; |
| 197 success = Should(message, function () { | 152 should(() => { |
| 198 buffer = new AudioBuffer(options); | 153 buffer = new AudioBuffer(options); |
| 199 }).notThrow(); | 154 }, message).notThrow(); |
| 200 | 155 |
| 201 success = Should("buffer.numberOfChannels", buffer.numberOfChannels) | 156 should(buffer.numberOfChannels, 'buffer.numberOfChannels') |
| 202 .beEqualTo(options.numberOfChannels) && success; | 157 .beEqualTo(options.numberOfChannels); |
| 203 | 158 |
| 204 success = Should("buffer.length", buffer.length) | 159 should(buffer.length, 'buffer.length').beEqualTo(options.length); |
| 205 .beEqualTo(options.length) && success; | |
| 206 | 160 |
| 207 success = Should("buffer.sampleRate", buffer.sampleRate) | 161 should(buffer.sampleRate, 'buffer.sampleRate') |
| 208 .beEqualTo(options.sampleRate) && success; | 162 .beEqualTo(options.sampleRate); |
| 209 | 163 |
| 210 // Verify that we actually got the right number of channels | 164 // Verify that we actually got the right number of channels |
| 211 for (var k = 0; k < options.numberOfChannels; ++k) { | 165 for (let k = 0; k < options.numberOfChannels; ++k) { |
| 212 var data; | 166 let data; |
| 213 var message = "buffer.getChannelData(" + k + ")"; | 167 let message = 'buffer.getChannelData(' + k + ')'; |
| 214 success = Should(message, function () { | 168 should(() => { |
| 215 data = buffer.getChannelData(k); | 169 data = buffer.getChannelData(k); |
| 216 }).notThrow() && success; | 170 }, message).notThrow(); |
| 217 | 171 |
| 218 success = Should(message + " length", data.length) | 172 should(data.length, message + ' length').beEqualTo(options.length); |
| 219 .beEqualTo(options.length) && success; | |
| 220 } | 173 } |
| 221 | 174 |
| 222 Should("buffer.getChannelData(" + options.numberOfChannels + ")", | 175 should( |
| 223 function () { | 176 () => { |
| 224 buffer.getChannelData(options.numberOfChannels); | 177 buffer.getChannelData(options.numberOfChannels); |
| 225 }).throw("IndexSizeError") && success; | 178 }, |
| 179 'buffer.getChannelData(' + options.numberOfChannels + ')') |
| 180 .throw('IndexSizeError'); |
| 226 | 181 |
| 227 Should("AudioBuffer constructed", success) | 182 task.done(); |
| 228 .summarize("correctly", "incorrectly"); | |
| 229 | |
| 230 taskDone(); | |
| 231 }); | 183 }); |
| 232 | 184 |
| 233 audit.defineTask("multiple contexts", function (taskDone) { | 185 audit.define('multiple contexts', (task, should) => { |
| 234 // Test that an AudioBuffer can be used for different contexts. | 186 // Test that an AudioBuffer can be used for different contexts. |
| 235 var buffer = new AudioBuffer({ | 187 let buffer = |
| 236 length: 128, | 188 new AudioBuffer({length: 128, sampleRate: context.sampleRate}); |
| 237 sampleRate: context.sampleRate | |
| 238 }); | |
| 239 | 189 |
| 240 var data = buffer.getChannelData(0); | 190 let data = buffer.getChannelData(0); |
| 241 for (var k = 0; k < data.length; ++k) | 191 for (let k = 0; k < data.length; ++k) |
| 242 data[k] = 1 + k; | 192 data[k] = 1 + k; |
| 243 | 193 |
| 244 var c1 = new OfflineAudioContext(1, 128, context.sampleRate); | 194 let c1 = new OfflineAudioContext(1, 128, context.sampleRate); |
| 245 var c2 = new OfflineAudioContext(1, 128, context.sampleRate); | 195 let c2 = new OfflineAudioContext(1, 128, context.sampleRate); |
| 246 | 196 |
| 247 | 197 let s1 = new AudioBufferSourceNode(c1, {buffer: buffer}); |
| 248 var s1 = new AudioBufferSourceNode(c1, { | 198 let s2 = new AudioBufferSourceNode(c2, {buffer: buffer}); |
| 249 buffer: buffer | |
| 250 }); | |
| 251 var s2 = new AudioBufferSourceNode(c2, { | |
| 252 buffer: buffer | |
| 253 }); | |
| 254 | 199 |
| 255 s1.connect(c1.destination); | 200 s1.connect(c1.destination); |
| 256 s2.connect(c2.destination); | 201 s2.connect(c2.destination); |
| 257 | 202 |
| 258 s1.start(); | 203 s1.start(); |
| 259 s2.start(); | 204 s2.start(); |
| 260 | 205 |
| 261 var c1Success = false; | 206 Promise |
| 262 var c2Success = false; | 207 .all([ |
| 263 | 208 c1.startRendering().then(function(resultBuffer) { |
| 264 Promise.all([ | 209 return should(resultBuffer.getChannelData(0), 'c1 result') |
| 265 c1.startRendering().then(function (resultBuffer) { | 210 .beEqualToArray(data); |
| 266 c1Success = Should("c1 result", resultBuffer.getChannelData(0)) | 211 }), |
| 267 .beEqualToArray(data); | 212 c2.startRendering().then(function(resultBuffer) { |
| 268 }), | 213 return should(resultBuffer.getChannelData(0), 'c2 result') |
| 269 c2.startRendering().then(function (resultBuffer) { | 214 .beEqualToArray(data); |
| 270 c2Success = Should("c2 result", resultBuffer.getChannelData(0)) | 215 }), |
| 271 .beEqualToArray(data); | 216 ]) |
| 272 }), | 217 .then(returnValues => { |
| 273 ]) | 218 should( |
| 274 .then(function () { | 219 returnValues[0] && returnValues[1], |
| 275 Should("AudioBuffer shared between two different contexts", | 220 'AudioBuffer shared between two different contexts') |
| 276 c1Success && c2Success) | 221 .message('correctly', 'incorrectly'); |
| 277 .summarize("correctly", "incorrectly"); | 222 task.done(); |
| 278 taskDone(); | 223 }); |
| 279 }); | |
| 280 }); | 224 }); |
| 281 | 225 |
| 282 audit.runTasks(); | 226 audit.run(); |
| 283 </script> | 227 </script> |
| 284 </body> | 228 </body> |
| 285 </html> | 229 </html> |
| OLD | NEW |