tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ctor-convolver.html (5397B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4    <title>ConvolverNode Constructor</title>
      5    <script src="/resources/testharness.js"></script>
      6    <script src="/resources/testharnessreport.js"></script>
      7  </head>
      8  <body>
      9    <script>
     10      const context = new AudioContext();
     11 
     12      test(() => {
     13        assert_throws_js(
     14            TypeError,
     15            () => new ConvolverNode(),
     16            'new ConvolverNode() without context must throw TypeError');
     17 
     18        assert_throws_js(
     19            TypeError,
     20            () => new DynamicsCompressorNode(1),
     21            'new DynamicsCompressorNode(1) must throw TypeError');
     22 
     23        assert_throws_js(
     24            TypeError,
     25            () => new DynamicsCompressorNode(context, 42),
     26            'new DynamicsCompressorNode(context, 42) must throw TypeError');
     27      }, 'ConvolverNode: invalid constructor throws TypeError');
     28 
     29      test(() => {
     30        const prefix = 'node0';
     31        const node = new ConvolverNode(context);
     32 
     33        assert_equals(node.numberOfInputs, 1, `${prefix}.numberOfInputs`);
     34        assert_equals(node.numberOfOutputs, 1, `${prefix}.numberOfOutputs`);
     35        assert_equals(node.channelCount, 2, `${prefix}.channelCount`);
     36        assert_equals(
     37            node.channelCountMode, 'clamped-max', `${prefix}.channelCountMode`);
     38        assert_equals(
     39            node.channelInterpretation, 'speakers',
     40            `${prefix}.channelInterpretation`);
     41 
     42        assert_equals(node.normalize, true, `${prefix}.normalize`);
     43        assert_equals(node.buffer, null, `${prefix}.buffer`);
     44      }, 'ConvolverNode: default constructor and attributes');
     45 
     46      test(() => {
     47        const attributeTests = [
     48          {
     49            attribute: 'channelCount',
     50            testOptions: [
     51              {value: 1},
     52              {value: 2},
     53              {value: 0, error: 'NotSupportedError'},
     54              {value: 3, error: 'NotSupportedError'},
     55              {value: 99, error: 'NotSupportedError'}
     56            ]
     57          },
     58          {
     59            attribute: 'channelCountMode',
     60            testOptions: [
     61              {value: 'clamped-max'},
     62              {value: 'explicit'},
     63              {value: 'max', error: 'NotSupportedError'},
     64              {value: 'foobar', error: TypeError}
     65            ]
     66          },
     67          {
     68            attribute: 'channelInterpretation',
     69            testOptions: [
     70              {value: 'speakers'},
     71              {value: 'discrete'},
     72              {value: 'foobar', error: TypeError}
     73            ]
     74          }
     75        ];
     76 
     77        for (const attributeTest of attributeTests) {
     78          for (const testOption of attributeTest.testOptions) {
     79            const options = {};
     80            options[attributeTest.attribute] = testOption.value;
     81 
     82            const desc =
     83                `new ConvolverNode(context, ${JSON.stringify(options)})`;
     84            const createNode = () => new ConvolverNode(context, options);
     85 
     86            if (testOption.error) {
     87              if (typeof testOption.error === 'string') {
     88                assert_throws_dom(testOption.error, createNode, desc);
     89              } else {
     90                assert_throws_js(testOption.error, createNode, desc);
     91              }
     92            } else {
     93              const node = createNode();
     94              assert_equals(
     95                  node[attributeTest.attribute], testOption.value,
     96                  `node.${attributeTest.attribute} == ${testOption.value}`);
     97            }
     98          }
     99        }
    100      }, 'ConvolverNode constructor: AudioNodeOptions are correctly handled');
    101 
    102      test(() => {
    103        const options = {buffer: null};
    104        const node = new ConvolverNode(context, options);
    105        assert_equals(node.buffer, null, 'node1.buffer');
    106      }, 'ConvolverNode: nullable buffer');
    107 
    108      test(() => {
    109        const invalidSampleRate = context.sampleRate / 2;
    110        const buffer = context.createBuffer(1, 1, invalidSampleRate);
    111        const options = {buffer};
    112 
    113        assert_throws_dom(
    114            'NotSupportedError', () => {
    115              new ConvolverNode(context, options);
    116            },
    117            `new ConvolverNode(context, { buffer: <invalid sample rate> })`);
    118      }, 'ConvolverNode: illegal sample rate buffer throws NotSupportedError');
    119 
    120      test(() => {
    121        const buf = context.createBuffer(1, 1, context.sampleRate);
    122        const options = {buffer: buf, disableNormalization: false};
    123 
    124        const node1 = new ConvolverNode(context, options);
    125 
    126        assert_true(node1 instanceof ConvolverNode,
    127            'node1 instanceOf ConvolverNode');
    128        assert_equals(node1.buffer, options.buffer, 'node1.buffer === <buf>');
    129        assert_equals(
    130            node1.normalize, !options.disableNormalization, 'node1.normalize');
    131 
    132        options.buffer = null;
    133        options.disableNormalization = true;
    134        const node2 = new ConvolverNode(context, options);
    135        assert_equals(node2.buffer, null, 'node2.buffer');
    136        assert_equals(
    137            node2.normalize, !options.disableNormalization, 'node2.normalize');
    138 
    139        options.disableNormalization = false;
    140        const node3 = new ConvolverNode(context, options);
    141        assert_equals(node3.buffer, null, 'node3.buffer');
    142        assert_equals(
    143            node3.normalize, !options.disableNormalization, 'node3.normalize');
    144      }, 'ConvolverNode: construct with buffer and normalization options');
    145    </script>
    146  </body>
    147 </html>