summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/processors/process-getter-test-instance-processor.js
blob: b1434f54ba587fcb5daf9c0e95a011b725788904 (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
/**
 * @class ProcessGetterTestInstanceProcessor
 * @extends AudioWorkletProcessor
 *
 * This processor class tests that a 'process' getter on an
 * AudioWorkletProcessorConstructor instance is called at the right times.
 */

class ProcessGetterTestInstanceProcessor extends AudioWorkletProcessor {
  constructor() {
    super();
    this.getterCallCount = 0;
    this.totalProcessCallCount = 0;
    Object.defineProperty(this, 'process', { get: function() {
      if (!(this instanceof ProcessGetterTestInstanceProcessor)) {
        throw new Error('`process` getter called with bad `this`.');
      }
      ++this.getterCallCount;
      let functionCallCount = 0;
      return () => {
        if (++functionCallCount > 1) {
          const message = 'Closure of function returned from `process` getter' +
              ' should be used for only one call.'
          this.port.postMessage({message: message});
          throw new Error(message);
        }
        if (++this.totalProcessCallCount < 2) {
          return true; // Expect another getter call.
        }
        if (this.totalProcessCallCount != this.getterCallCount) {
          const message =
              'Getter should be called only once for each process() call.'
          this.port.postMessage({message: message});
          throw new Error(message);
        }
        this.port.postMessage({message: 'done'});
        return false; // No more calls required.
      };
    }});
  }
}

registerProcessor('process-getter-test-instance',
                  ProcessGetterTestInstanceProcessor);