| OLD | NEW |
| 1 // Test that constructor for the node with name |nodeName| handles the various | 1 // Test that constructor for the node with name |nodeName| handles the |
| 2 // possible values for channelCount, channelCountMode, and | 2 // various possible values for channelCount, channelCountMode, and |
| 3 // channelInterpretation. | 3 // channelInterpretation. |
| 4 function testAudioNodeOptions(context, nodeName, nodeOptions) { | 4 |
| 5 if (nodeOptions === undefined) | 5 // The |should| parameter is the test function from new |Audit|. |
| 6 nodeOptions = {}; | 6 function testAudioNodeOptions(should, context, nodeName, expectedNodeOptions) { |
| 7 var node; | 7 if (expectedNodeOptions === undefined) |
| 8 var success = true; | 8 expectedNodeOptions = {}; |
| 9 | 9 let node; |
| 10 // Test that we can set channelCount and that errors are thrown for invalid
values | 10 |
| 11 var testCount = 17; | 11 // Test that we can set channelCount and that errors are thrown for |
| 12 if (nodeOptions.expectedChannelCount) { | 12 // invalid values |
| 13 testCount = nodeOptions.expectedChannelCount.value; | 13 let testChannelCount = 17; |
| 14 if (expectedNodeOptions.channelCount) { |
| 15 testChannelCount = expectedNodeOptions.channelCount.value; |
| 16 } |
| 17 should( |
| 18 () => { |
| 19 node = new window[nodeName]( |
| 20 context, Object.assign({}, expectedNodeOptions.additionalOptions, { |
| 21 channelCount: testChannelCount |
| 22 })); |
| 23 }, |
| 24 'new ' + nodeName + '(c, {channelCount: ' + testChannelCount + '}}') |
| 25 .notThrow(); |
| 26 should(node.channelCount, 'node.channelCount').beEqualTo(testChannelCount); |
| 27 |
| 28 if (expectedNodeOptions.channelCount && |
| 29 expectedNodeOptions.channelCount.isFixed) { |
| 30 // The channel count is fixed. Verify that we throw an error if |
| 31 // we try to change it. Arbitrarily set the count to be one more |
| 32 // than the expected value. |
| 33 testChannelCount = expectedNodeOptions.channelCount.value + 1; |
| 34 should( |
| 35 () => { |
| 36 node = new window[nodeName]( |
| 37 context, |
| 38 Object.assign( |
| 39 {}, expectedNodeOptions.additionalOptions, |
| 40 {channelCount: testChannelCount})); |
| 41 }, |
| 42 'new ' + nodeName + '(c, {channelCount: ' + testChannelCount + '}}') |
| 43 .throw(expectedNodeOptions.channelCount.errorType || 'TypeError'); |
| 44 } else { |
| 45 // The channel count is not fixed. Try to set the count to invalid |
| 46 // values and make sure an error is thrown. |
| 47 let errorType = 'NotSupportedError'; |
| 48 |
| 49 should(() => { |
| 50 node = new window[nodeName]( |
| 51 context, |
| 52 Object.assign( |
| 53 {}, expectedNodeOptions.additionalOptions, {channelCount: 0})); |
| 54 }, 'new ' + nodeName + '(c, {channelCount: 0}}', ).throw(errorType); |
| 55 |
| 56 should(() => { |
| 57 node = new window[nodeName]( |
| 58 context, |
| 59 Object.assign( |
| 60 {}, expectedNodeOptions.additionalOptions, {channelCount: 99})); |
| 61 }, 'new ' + nodeName + '(c, {channelCount: 99}}').throw(errorType); |
| 62 } |
| 63 |
| 64 // Test channelCountMode |
| 65 let testChannelCountMode = 'max'; |
| 66 if (expectedNodeOptions.channelCountMode) { |
| 67 testChannelCountMode = expectedNodeOptions.channelCountMode.value; |
| 68 } |
| 69 should( |
| 70 () => { |
| 71 node = new window[nodeName]( |
| 72 context, Object.assign({}, expectedNodeOptions.additionalOptions, { |
| 73 channelCountMode: testChannelCountMode |
| 74 })); |
| 75 }, |
| 76 'new ' + nodeName + '(c, {channelCountMode: "' + testChannelCountMode + '"
}') |
| 77 .notThrow(); |
| 78 should(node.channelCountMode, 'node.channelCountMode').beEqualTo(testChannelCo
untMode); |
| 79 |
| 80 if (expectedNodeOptions.channelCountMode && |
| 81 expectedNodeOptions.channelCountMode.isFixed) { |
| 82 // Channel count mode is fixed. Test setting to something else throws. |
| 83 let testChannelCountModeMap = { |
| 84 'max': 'clamped-max', |
| 85 'clamped-max': 'explicit', |
| 86 'explicit': 'max' |
| 87 }; |
| 88 testChannelCountMode = testChannelCountModeMap[expectedNodeOptions.channelCo
untMode.value]; |
| 89 should( |
| 90 () => { |
| 91 node = new window[nodeName]( |
| 92 context, |
| 93 Object.assign( |
| 94 {}, expectedNodeOptions.additionalOptions, |
| 95 {channelCountMode: testChannelCountMode})); |
| 96 }, |
| 97 'new ' + nodeName + '(c, {channelCountMode: "' + testChannelCountMode +
'"}') |
| 98 .throw(expectedNodeOptions.channelCountMode.errorType); |
| 99 } else { |
| 100 // Mode is not fixed. Verify that we can set the mode to all valid |
| 101 // values, and that we throw for invalid values. |
| 102 |
| 103 should(() => { |
| 104 node = new window[nodeName]( |
| 105 context, Object.assign({}, expectedNodeOptions.additionalOptions, { |
| 106 channelCountMode: 'clamped-max' |
| 107 })); |
| 108 }, 'new ' + nodeName + '(c, {channelCountMode: "clamped-max"}').notThrow(); |
| 109 should(node.channelCountMode, 'node.channelCountMode after invalid setter') |
| 110 .beEqualTo('clamped-max'); |
| 111 |
| 112 should(() => { |
| 113 node = new window[nodeName]( |
| 114 context, Object.assign({}, expectedNodeOptions.additionalOptions, { |
| 115 channelCountMode: 'explicit' |
| 116 })); |
| 117 }, 'new ' + nodeName + '(c, {channelCountMode: "explicit"}').notThrow(); |
| 118 should(node.channelCountMode, 'node.channelCountMode') |
| 119 .beEqualTo('explicit'); |
| 120 |
| 121 should( |
| 122 () => { |
| 123 node = new window[nodeName]( |
| 124 context, |
| 125 Object.assign( |
| 126 {}, expectedNodeOptions.additionalOptions, |
| 127 {channelCountMode: 'foobar'})); |
| 128 }, |
| 129 'new ' + nodeName + '(c, {channelCountMode: "foobar"}') |
| 130 .throw('TypeError'); |
| 131 should(node.channelCountMode, 'node.channelCountMode after invalid setter') |
| 132 .beEqualTo('explicit'); |
| 133 } |
| 134 |
| 135 // Test channelInterpretation |
| 136 should(() => { |
| 137 node = new window[nodeName]( |
| 138 context, Object.assign({}, expectedNodeOptions.additionalOptions, { |
| 139 channelInterpretation: 'speakers' |
| 140 })); |
| 141 }, 'new ' + nodeName + '(c, {channelInterpretation: "speakers"})').notThrow(); |
| 142 should(node.channelInterpretation, 'node.channelInterpretation') |
| 143 .beEqualTo('speakers'); |
| 144 |
| 145 should(() => { |
| 146 node = new window[nodeName]( |
| 147 context, Object.assign({}, expectedNodeOptions.additionalOptions, { |
| 148 channelInterpretation: 'discrete' |
| 149 })); |
| 150 }, 'new ' + nodeName + '(c, {channelInterpretation: "discrete"})').notThrow(); |
| 151 should(node.channelInterpretation, 'node.channelInterpretation') |
| 152 .beEqualTo('discrete'); |
| 153 |
| 154 should( |
| 155 () => { |
| 156 node = new window[nodeName]( |
| 157 context, Object.assign({}, expectedNodeOptions.additionalOptions, { |
| 158 channelInterpretation: 'foobar' |
| 159 })); |
| 160 }, |
| 161 'new ' + nodeName + '(c, {channelInterpretation: "foobar"})') |
| 162 .throw('TypeError'); |
| 163 should( |
| 164 node.channelInterpretation, |
| 165 'node.channelInterpretation after invalid setter') |
| 166 .beEqualTo('discrete'); |
| 167 } |
| 168 |
| 169 function initializeContext(should) { |
| 170 let c; |
| 171 should(() => { |
| 172 c = new OfflineAudioContext(1, 1, 48000); |
| 173 }, 'context = new OfflineAudioContext(...)').notThrow(); |
| 174 |
| 175 return c; |
| 176 } |
| 177 |
| 178 function testInvalidConstructor(should, name, context) { |
| 179 should(() => { |
| 180 new window[name](); |
| 181 }, 'new ' + name + '()').throw('TypeError'); |
| 182 should(() => { |
| 183 new window[name](1); |
| 184 }, 'new ' + name + '(1)').throw('TypeError'); |
| 185 should(() => { |
| 186 new window[name](context, 42); |
| 187 }, 'new ' + name + '(context, 42)').throw('TypeError'); |
| 188 } |
| 189 |
| 190 function testDefaultConstructor(should, name, context, options) { |
| 191 let node; |
| 192 |
| 193 let message = options.prefix + ' = new ' + name + '(context'; |
| 194 if (options.constructorOptions) |
| 195 message += ', ' + JSON.stringify(options.constructorOptions); |
| 196 message += ')' |
| 197 |
| 198 should(() => { |
| 199 node = new window[name](context, options.constructorOptions); |
| 200 }, message).notThrow(); |
| 201 |
| 202 should(node instanceof window[name], options.prefix + ' instanceof ' + name) |
| 203 .beEqualTo(true); |
| 204 should(node.numberOfInputs, options.prefix + '.numberOfInputs') |
| 205 .beEqualTo(options.numberOfInputs); |
| 206 should(node.numberOfOutputs, options.prefix + '.numberOfOutputs') |
| 207 .beEqualTo(options.numberOfOutputs); |
| 208 should(node.channelCount, options.prefix + '.channelCount') |
| 209 .beEqualTo(options.channelCount); |
| 210 should(node.channelCountMode, options.prefix + '.channelCountMode') |
| 211 .beEqualTo(options.channelCountMode); |
| 212 should(node.channelInterpretation, options.prefix + '.channelInterpretation') |
| 213 .beEqualTo(options.channelInterpretation); |
| 214 |
| 215 return node; |
| 216 } |
| 217 |
| 218 function testDefaultAttributes(should, node, prefix, items) { |
| 219 items.forEach((item) => { |
| 220 let attr = node[item.name]; |
| 221 if (attr instanceof AudioParam) { |
| 222 should(attr.value, prefix + '.' + item.name + '.value') |
| 223 .beEqualTo(item.value); |
| 224 } else { |
| 225 should(attr, prefix + '.' + item.name).beEqualTo(item.value); |
| 14 } | 226 } |
| 15 success = Should("new " + nodeName + "(c, {channelCount: " + testCount + "}}
", | 227 }); |
| 16 function () { | 228 } |
| 17 node = new window[nodeName]( | |
| 18 context, | |
| 19 Object.assign({}, | |
| 20 nodeOptions.additionalOptions, { | |
| 21 channelCount: testCount | |
| 22 })); | |
| 23 }).notThrow(); | |
| 24 success = Should("node.channelCount", node.channelCount) | |
| 25 .beEqualTo(testCount) && success; | |
| 26 | |
| 27 if (nodeOptions.expectedChannelCount && nodeOptions.expectedChannelCount.isF
ixed) { | |
| 28 // The channel count is fixed. Verify that we throw an error if we try
to | |
| 29 // change it. Arbitrarily set the count to be one more than the expected | |
| 30 // value. | |
| 31 testCount = nodeOptions.expectedChannelCount.value + 1; | |
| 32 success = Should("new " + nodeName + "(c, {channelCount: " + testCount +
"}}", | |
| 33 function () { | |
| 34 node = new window[nodeName]( | |
| 35 context, | |
| 36 Object.assign({}, | |
| 37 nodeOptions.additionalOptions, { | |
| 38 channelCount: testCount | |
| 39 })); | |
| 40 }).throw(nodeOptions.expectedChannelCount.errorType || "TypeError")
&& success; | |
| 41 } else { | |
| 42 // The channel count is not fixed. Try to set the count to invalid | |
| 43 // values and make sure an error is thrown. | |
| 44 var errorType = "NotSupportedError"; | |
| 45 | |
| 46 success = Should("new " + nodeName + "(c, {channelCount: 0}}", | |
| 47 function () { | |
| 48 node = new window[nodeName]( | |
| 49 context, | |
| 50 Object.assign({}, | |
| 51 nodeOptions.additionalOptions, { | |
| 52 channelCount: 0 | |
| 53 })); | |
| 54 }).throw(errorType) && success; | |
| 55 | |
| 56 success = Should("new " + nodeName + "(c, {channelCount: 99}}", | |
| 57 function () { | |
| 58 node = new window[nodeName]( | |
| 59 context, | |
| 60 Object.assign({}, | |
| 61 nodeOptions.additionalOptions, { | |
| 62 channelCount: 99 | |
| 63 })); | |
| 64 }).throw(errorType) && success; | |
| 65 } | |
| 66 | |
| 67 // Test channelCountMode | |
| 68 var testMode = "max"; | |
| 69 if (nodeOptions && nodeOptions.expectedChannelCountMode) { | |
| 70 testMode = nodeOptions.expectedChannelCountMode.value; | |
| 71 } | |
| 72 success = Should("new " + nodeName + '(c, {channelCountMode: "' + testMode +
'"}', | |
| 73 function () { | |
| 74 node = new window[nodeName]( | |
| 75 context, | |
| 76 Object.assign({}, | |
| 77 nodeOptions.additionalOptions, { | |
| 78 channelCountMode: testMode | |
| 79 })); | |
| 80 }).notThrow() && success; | |
| 81 success = Should("node.channelCountMode", node.channelCountMode) | |
| 82 .beEqualTo(testMode) && success; | |
| 83 | |
| 84 if (nodeOptions.expectedChannelCountMode && nodeOptions.expectedChannelCount
Mode.isFixed) { | |
| 85 // Channel count mode is fixed. Test setting to something else throws. | |
| 86 var testModeMap = { | |
| 87 "max": "clamped-max", | |
| 88 "clamped-max": "explicit", | |
| 89 "explicit": "max" | |
| 90 }; | |
| 91 testMode = testModeMap[nodeOptions.expectedChannelCountMode.value]; | |
| 92 success = Should("new " + nodeName + '(c, {channelCountMode: "' + testMo
de + '"}', | |
| 93 function () { | |
| 94 node = new window[nodeName]( | |
| 95 context, | |
| 96 Object.assign({}, | |
| 97 nodeOptions.additionalOptions, { | |
| 98 channelCountMode: testMode | |
| 99 })); | |
| 100 }).throw(nodeOptions.expectedChannelCountMode.errorType) && success; | |
| 101 } else { | |
| 102 // Mode is not fixed. Verify that we can set the mode to all valid | |
| 103 // values, and that we throw for invalid values. | |
| 104 | |
| 105 success = Should("new " + nodeName + '(c, {channelCountMode: "clamped-ma
x"}', | |
| 106 function () { | |
| 107 node = new window[nodeName]( | |
| 108 context, | |
| 109 Object.assign({}, | |
| 110 nodeOptions.additionalOptions, { | |
| 111 channelCountMode: "clamped-max" | |
| 112 })); | |
| 113 }).notThrow() && success; | |
| 114 success = Should("node.channelCountMode after invalid setter", node.chan
nelCountMode) | |
| 115 .beEqualTo("clamped-max") && success; | |
| 116 | |
| 117 success = Should("new " + nodeName + '(c, {channelCountMode: "explicit"}
', | |
| 118 function () { | |
| 119 node = new window[nodeName]( | |
| 120 context, | |
| 121 Object.assign({}, | |
| 122 nodeOptions.additionalOptions, { | |
| 123 channelCountMode: "explicit" | |
| 124 })); | |
| 125 }).notThrow() && success; | |
| 126 success = Should("node.channelCountMode", node.channelCountMode) | |
| 127 .beEqualTo("explicit") && success; | |
| 128 | |
| 129 success = Should("new " + nodeName + '(c, {channelCountMode: "foobar"}', | |
| 130 function () { | |
| 131 node = new window[nodeName]( | |
| 132 context, | |
| 133 Object.assign({}, | |
| 134 nodeOptions.additionalOptions, { | |
| 135 channelCountMode: "foobar" | |
| 136 })); | |
| 137 }).throw("TypeError") && success; | |
| 138 success = Should("node.channelCountMode after invalid setter", node.chan
nelCountMode) | |
| 139 .beEqualTo("explicit") && success; | |
| 140 } | |
| 141 | |
| 142 // Test channelInterpretation | |
| 143 success = Should("new " + nodeName + '(c, {channelInterpretation: "speakers"
})', | |
| 144 function () { | |
| 145 node = new window[nodeName]( | |
| 146 context, | |
| 147 Object.assign({}, | |
| 148 nodeOptions.additionalOptions, { | |
| 149 channelInterpretation: "speakers" | |
| 150 })); | |
| 151 }).notThrow() && success; | |
| 152 success = Should("node.channelInterpretation", node.channelInterpretation) | |
| 153 .beEqualTo("speakers") && success; | |
| 154 | |
| 155 success = Should("new " + nodeName + '(c, {channelInterpretation: "discrete"
})', | |
| 156 function () { | |
| 157 node = new window[nodeName]( | |
| 158 context, | |
| 159 Object.assign({}, | |
| 160 nodeOptions.additionalOptions, { | |
| 161 channelInterpretation: "discrete" | |
| 162 })); | |
| 163 }).notThrow() && success; | |
| 164 success = Should("node.channelInterpretation", node.channelInterpretation) | |
| 165 .beEqualTo("discrete") && success; | |
| 166 | |
| 167 success = Should("new " + nodeName + '(c, {channelInterpretation: "foobar"})
', | |
| 168 function () { | |
| 169 node = new window[nodeName]( | |
| 170 context, | |
| 171 Object.assign({}, | |
| 172 nodeOptions.additionalOptions, { | |
| 173 channelInterpretation: "foobar" | |
| 174 })); | |
| 175 }).throw("TypeError") && success; | |
| 176 success = Should("node.channelInterpretation after invalid setter", node.cha
nnelInterpretation) | |
| 177 .beEqualTo("discrete") && success; | |
| 178 | |
| 179 | |
| 180 Should("AudioNodeOptions for " + nodeName, success) | |
| 181 .summarize( | |
| 182 "were correctly handled", | |
| 183 "were not correctly handled"); | |
| 184 } | |
| OLD | NEW |