start-stop-exceptions.js (2881B)
1 // Test that exceptions are throw for invalid values for start and 2 // stop. 3 function testStartStop(should, node, options) { 4 // Test non-finite values for start. These should all throw a TypeError 5 const nonFiniteValues = [NaN, Infinity, -Infinity]; 6 7 nonFiniteValues.forEach(time => { 8 should(() => { 9 node.start(time); 10 }, `start(${time})`) 11 .throw(TypeError); 12 }); 13 14 should(() => { 15 node.stop(); 16 }, 'Calling stop() before start()').throw(DOMException, 'InvalidStateError'); 17 18 should(() => { 19 node.start(-1); 20 }, 'start(-1)').throw(RangeError); 21 22 if (options) { 23 options.forEach(test => { 24 should(() => {node.start(...test.args)}, 25 'start(' + test.args + ')').throw(test.errorType); 26 }); 27 } 28 29 node.start(); 30 should(() => { 31 node.start(); 32 }, 'Calling start() twice').throw(DOMException, 'InvalidStateError'); 33 should(() => { 34 node.stop(-1); 35 }, 'stop(-1)').throw(RangeError); 36 37 // Test non-finite stop times 38 nonFiniteValues.forEach(time => { 39 should(() => { 40 node.stop(time); 41 }, `stop(${time})`) 42 .throw(TypeError); 43 }); 44 } 45 46 /** 47 * @function 48 * @param {AudioScheduledSourceNode} node - The AudioScheduledSourceNode (e.g., 49 * ConstantSourceNode, AudioBufferSourceNode) to test. 50 * @param {Array<Object>} [options] - Optional: An array of test objects for 51 * additional start() exceptions. Each object should have: 52 * - `errorType`: The expected error constructor(e.g., TypeError, 53 * RangeError). 54 * - `args`: An array of arguments to pass to the `node.start()` method. 55 * @description Tests that AudioScheduledSourceNode's `start()` and `stop()` 56 * methods throw the correct exceptions for invalid input values and states, 57 * according to the Web Audio API specification. This function uses 58 * `testharness.js` assertions. 59 */ 60 const testStartStop_W3CTH = (node, options) => { 61 // Test non-finite values for start. These should all throw a TypeError 62 const nonFiniteValues = [NaN, Infinity, -Infinity]; 63 64 nonFiniteValues.forEach((time) => { 65 assert_throws_js(TypeError, () => { 66 node.start(time); 67 }, `start(${time})`); 68 }); 69 70 assert_throws_dom('InvalidStateError', () => { 71 node.stop(); 72 }, 'Calling stop() before start()'); 73 74 assert_throws_js(RangeError, () => { 75 node.start(-1); 76 }, 'start(-1)'); 77 78 if (options) { 79 options.forEach((test) => { 80 assert_throws_js(test.errorType, () => { 81 node.start(...test.args); 82 }, `start(${test.args})`); 83 }); 84 } 85 86 node.start(); 87 assert_throws_dom('InvalidStateError', () => { 88 node.start(); 89 }, 'Calling start() twice'); 90 assert_throws_js(RangeError, () => { 91 node.stop(-1); 92 }, 'stop(-1)'); 93 94 // Test non-finite stop times 95 nonFiniteValues.forEach((time) => { 96 assert_throws_js(TypeError, () => { 97 node.stop(time); 98 }, `stop(${time})`); 99 }); 100 }