ctor-dynamicscompressor.html (5003B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>DynamicsCompressorNode Constructor</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 </head> 8 <body> 9 <script> 10 let context; 11 12 setup(() => { 13 context = new AudioContext(); 14 }); 15 16 test(t => { 17 assert_throws_js( 18 TypeError, 19 () => new DynamicsCompressorNode(), 20 'new DynamicsCompressorNode without context must throw TypeError'); 21 22 assert_throws_js( 23 TypeError, 24 () => new DynamicsCompressorNode(1), 25 'new DynamicsCompressorNode(1) must throw TypeError'); 26 27 assert_throws_js( 28 TypeError, 29 () => new DynamicsCompressorNode(context, 42), 30 'new DynamicsCompressorNode(context, 42) must throw TypeError'); 31 }, 'DynamicsCompressorNode: invalid arguments throw TypeError'); 32 33 test(t => { 34 const node = new DynamicsCompressorNode(context); 35 36 assert_true(node instanceof DynamicsCompressorNode); 37 38 // Check node structure 39 assert_equals(node.numberOfInputs, 1); 40 assert_equals(node.numberOfOutputs, 1); 41 assert_equals(node.channelCount, 2); 42 assert_equals(node.channelCountMode, 'clamped-max'); 43 assert_equals(node.channelInterpretation, 'speakers'); 44 45 // Check default attributes 46 assert_equals(node.threshold.value, -24); 47 assert_equals(node.knee.value, 30); 48 assert_equals(node.ratio.value, 12); 49 assert_equals(node.reduction, 0); 50 assert_approx_equals(node.attack.value, 0.003, 1e-6); 51 assert_equals(node.release.value, 0.25); 52 }, 'DynamicsCompressorNode: default constructor and attributes'); 53 54 test(t => { 55 // Can't use testAudioNodeOptions because the constraints for this node 56 // are not supported there. 57 58 // Array of test options to be run. Each entry is a dictionary where 59 // |testAttribute| is the name of the attribute to be tested, 60 // |testValue| is the value to be used, and |expectedErrorType| is the 61 // error type if the test is expected to throw an error. 62 // |expectedErrorType| should be set only if the test does throw. 63 const testOptions = [ 64 {attr: 'channelCount', value: 1}, 65 {attr: 'channelCount', value: 2}, 66 {attr: 'channelCount', value: 0, error: 'NotSupportedError'}, 67 {attr: 'channelCount', value: 3, error: 'NotSupportedError'}, 68 {attr: 'channelCount', value: 99, error: 'NotSupportedError'}, 69 {attr: 'channelCountMode', value: 'clamped-max'}, 70 {attr: 'channelCountMode', value: 'explicit'}, 71 { 72 attr: 'channelCountMode', 73 value: 'max', 74 error: 'NotSupportedError' 75 }, 76 {attr: 'channelCountMode', value: 'foobar', error: TypeError}, 77 {attr: 'channelInterpretation', value: 'speakers'}, 78 {attr: 'channelInterpretation', value: 'discrete'}, 79 { 80 attr: 'channelInterpretation', 81 value: 'foobar', 82 error: TypeError 83 } 84 ]; 85 86 for (const opt of testOptions) { 87 const options = {[opt.attr]: opt.value}; 88 const desc = 89 `new DynamicsCompressorNode(context, { ${opt.attr}: ` + 90 `${JSON.stringify(opt.value)} })`; 91 92 const createNode = () => new DynamicsCompressorNode(context, options); 93 94 if (opt.error) { 95 const shouldThrowDom = typeof opt.error === 'string'; 96 if (shouldThrowDom) { 97 assert_throws_dom(opt.error, createNode, desc); 98 } else { 99 assert_throws_js(opt.error, createNode, desc); 100 } 101 } else { 102 const node = createNode(); 103 assert_equals( 104 node[opt.attr], opt.value, 105 `node.${opt.attr} == ${opt.value}`); 106 } 107 } 108 109 }, 'DynamicsCompressorNode: constructor with various AudioNodeOptions'); 110 111 test(t => { 112 const options = { 113 threshold: -33, 114 knee: 15, 115 ratio: 7, 116 attack: 0.625, 117 release: 0.125 118 }; 119 120 const node = new DynamicsCompressorNode(context, options); 121 122 assert_true(node instanceof DynamicsCompressorNode); 123 124 assert_equals(node.threshold.value, options.threshold); 125 assert_equals(node.knee.value, options.knee); 126 assert_equals(node.ratio.value, options.ratio); 127 assert_equals(node.attack.value, options.attack); 128 assert_equals(node.release.value, options.release); 129 130 // Confirm default AudioNodeOptions still applied 131 assert_equals(node.channelCount, 2); 132 assert_equals(node.channelCountMode, 'clamped-max'); 133 assert_equals(node.channelInterpretation, 'speakers'); 134 }, 'DynamicsCompressorNode: constructor with parameter options'); 135 </script> 136 </body> 137 </html>