summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-dynamicscompressornode-interface/ctor-dynamicscompressor.html
blob: c2460dfa1ddd26a5c2e199873c0b28189275ff83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<html>
  <head>
    <title>
      Test Constructor: DynamicsCompressor
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/resources/audit-util.js"></script>
    <script src="/webaudio/resources/audit.js"></script>
    <script src="/webaudio/resources/audionodeoptions.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let context;

      let audit = Audit.createTaskRunner();

      audit.define('initialize', (task, should) => {
        context = initializeContext(should);
        task.done();
      });

      audit.define('invalid constructor', (task, should) => {
        testInvalidConstructor(should, 'DynamicsCompressorNode', context);
        task.done();
      });

      audit.define('default constructor', (task, should) => {
        let prefix = 'node0';
        let node =
            testDefaultConstructor(should, 'DynamicsCompressorNode', context, {
              prefix: prefix,
              numberOfInputs: 1,
              numberOfOutputs: 1,
              channelCount: 2,
              channelCountMode: 'clamped-max',
              channelInterpretation: 'speakers'
            });

        testDefaultAttributes(should, node, prefix, [
          {name: 'threshold', value: -24}, {name: 'knee', value: 30},
          {name: 'ratio', value: 12}, {name: 'reduction', value: 0},
          {name: 'attack', value: Math.fround(0.003)},
          {name: 'release', value: 0.25}
        ]);

        task.done();
      });

      audit.define('test AudioNodeOptions', (task, should) => {
        // Can't use testAudioNodeOptions because the constraints for this node
        // are not supported there.

        // Array of test options to be run.  Each entry is a dictionary where
        // |testAttribute| is the name of the attribute to be tested,
        // |testValue| is the value to be used, and |expectedErrorType| is the
        // error type if the test is expected to throw an error.
        // |expectedErrorType| should be set only if the test does throw.
        let testOptions = [
          // Test channel count
          {
            testAttribute: 'channelCount',
            testValue: 1,
          },
          {
            testAttribute: 'channelCount',
            testValue: 2,
          },
          {
            testAttribute: 'channelCount',
            testValue: 0,
            expectedErrorType: 'NotSupportedError'
          },
          {
            testAttribute: 'channelCount',
            testValue: 3,
            expectedErrorType: 'NotSupportedError'
          },
          {
            testAttribute: 'channelCount',
            testValue: 99,
            expectedErrorType: 'NotSupportedError'
          },
          // Test channel count mode
          {
            testAttribute: 'channelCountMode',
            testValue: 'clamped-max',
          },
          {
            testAttribute: 'channelCountMode',
            testValue: 'explicit',
          },
          {
            testAttribute: 'channelCountMode',
            testValue: 'max',
            expectedErrorType: 'NotSupportedError'
          },
          {
            testAttribute: 'channelCountMode',
            testValue: 'foobar',
            expectedErrorType: TypeError
          },
          // Test channel interpretation
          {
            testAttribute: 'channelInterpretation',
            testValue: 'speakers',
          },
          {
            testAttribute: 'channelInterpretation',
            testValue: 'discrete',
          },
          {
            testAttribute: 'channelInterpretation',
            testValue: 'foobar',
            expectedErrorType: TypeError
          }
        ];

        testOptions.forEach((option) => {
          let nodeOptions = {};
          nodeOptions[option.testAttribute] = option.testValue;

          testNode(should, context, {
            nodeOptions: nodeOptions,
            testAttribute: option.testAttribute,
            expectedValue: option.testValue,
            expectedErrorType: option.expectedErrorType
          });
        });

        task.done();
      });

      audit.define('constructor with options', (task, should) => {
        let node;
        let options =
            {threshold: -33, knee: 15, ratio: 7, attack: 0.625, release: 0.125};

        should(
            () => {
              node = new DynamicsCompressorNode(context, options);
            },
            'node1 = new DynamicsCompressorNode(c, ' + JSON.stringify(options) +
                ')')
            .notThrow();
        should(
            node instanceof DynamicsCompressorNode,
            'node1 instanceof DynamicsCompressorNode')
            .beEqualTo(true);

        should(node.threshold.value, 'node1.threshold.value')
            .beEqualTo(options.threshold);
        should(node.knee.value, 'node1.knee.value').beEqualTo(options.knee);
        should(node.ratio.value, 'node1.ratio.value').beEqualTo(options.ratio);
        should(node.attack.value, 'node1.attack.value')
            .beEqualTo(options.attack);
        should(node.release.value, 'node1.release.value')
            .beEqualTo(options.release);

        should(node.channelCount, 'node1.channelCount').beEqualTo(2);
        should(node.channelCountMode, 'node1.channelCountMode')
            .beEqualTo('clamped-max');
        should(node.channelInterpretation, 'node1.channelInterpretation')
            .beEqualTo('speakers');

        task.done();
      });

      audit.run();

      // Test possible options for DynamicsCompressor constructor.
      function testNode(should, context, options) {
        // Node to be tested
        let node;

        let createNodeFunction = () => {
          return () => node =
                     new DynamicsCompressorNode(context, options.nodeOptions);
        };

        let message = 'new DynamicsCompressorNode(c, ' +
            JSON.stringify(options.nodeOptions) + ')';

        if (options.expectedErrorType === TypeError) {
          should(createNodeFunction(), message)
              .throw(options.expectedErrorType);
        } else if (options.expectedErrorType === 'NotSupportedError') {
          should(createNodeFunction(), message)
              .throw(DOMException, 'NotSupportedError');
        } else {
          should(createNodeFunction(), message).notThrow();
          should(node[options.testAttribute], 'node.' + options.testAttribute)
              .beEqualTo(options.expectedValue);
        }
      }
    </script>
  </body>
</html>