Skip to content

Truly independent web browser

LibWeb: Import ScriptProcessorNode WPT tests

Jelle Raaijmakers8a5f4227e654
diff --git a/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/scriptprocessor-rendersizehint.https.txt b/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/scriptprocessor-rendersizehint.https.txt
new file mode 100644
index 00000000000..bb840675f35
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/scriptprocessor-rendersizehint.https.txt
@@ -0,0 +1,10 @@
+Harness status: OK
+
+Found 5 tests
+
+5 Fail
+Fail ScriptProcessor handles block size with renderSizeHint 1 & bufferSize 256
+Fail ScriptProcessor handles block size with renderSizeHint 256 & bufferSize 256
+Fail ScriptProcessor handles block size with renderSizeHint 100 & bufferSize 256
+Fail ScriptProcessor handles block size with renderSizeHint 4096 & bufferSize 4096
+Fail ScriptProcessor handles block size with renderSizeHint 66000 & bufferSize 16384
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/simple-input-output.txt b/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/simple-input-output.txt
new file mode 100644
index 00000000000..5c3cd4acb4f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/simple-input-output.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail ScriptProcessor with stopped input source
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/webaudio/resources/rendersizehint-robustness-helper.js b/Tests/LibWeb/Text/input/wpt-import/webaudio/resources/rendersizehint-robustness-helper.js
new file mode 100644
index 00000000000..04b957d8e2a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webaudio/resources/rendersizehint-robustness-helper.js
@@ -0,0 +1,88 @@
+// Copyright 2026 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+const ROBUSTNESS_TEST_CONFIGS = [
+ {sampleRate: 3000, renderSizeHint: 1, length: 512},
+ {sampleRate: 48000, renderSizeHint: 256, length: 1024},
+ {sampleRate: 48000, renderSizeHint: 100, length: 1024},
+ {sampleRate: 192000, renderSizeHint: 4096, length: 8192},
+ {sampleRate: 11000, renderSizeHint: 66000, length: 132000}
+];
+
+/**
+ * Asserts that the rendered AudioBuffer's first channel does not contain
+ * NaN or Infinities.
+ *
+ * @param {AudioBuffer} renderedBuffer The buffer returned from rendering.
+ */
+const assertNoNaNOrInfinity = (renderedBuffer) => {
+ const data = renderedBuffer.getChannelData(0);
+ assert_false(data.includes(NaN), 'Output contains NaN');
+ assert_false(data.includes(Infinity), 'Output contains Infinity');
+ assert_false(data.includes(-Infinity), 'Output contains -Infinity');
+};
+
+/**
+ * Creates a unit impulse response AudioBuffer of a specified length.
+ *
+ * @param {BaseAudioContext} context The audio context.
+ * @param {number} length The length of the impulse response in frames.
+ * @returns {AudioBuffer} The unit impulse AudioBuffer.
+ */
+const createImpulseResponse = (context, length) => {
+ const buffer = new AudioBuffer({
+ numberOfChannels: 1,
+ length: length,
+ sampleRate: context.sampleRate
+ });
+ const channelData = buffer.getChannelData(0);
+ channelData[0] = 1.0; // Unit impulse at sample 0
+ return buffer;
+};
+
+/**
+ * Runs a robustness/stability test for a given node processor.
+ *
+ * @param {object} config The test configuration containing sampleRate,
+ * renderSizeHint, and length.
+ * @param {function(OfflineAudioContext): AudioNode} createTestSetupFunc A
+ * factory function to create and setup the processor node under test.
+ * @param {string} testName The name of the test case.
+ * @param {function(OfflineAudioContext, AudioBuffer, object): void}
+ * [postRenderAssertFunc] An optional hook to run assertions after
+ * rendering completes, receiving the audioContext, the rendered
+ * AudioBuffer, and the test context.
+ */
+const runQuantumRobustnessTest = (
+ config, createTestSetupFunc, testName, postRenderAssertFunc) => {
+ const {sampleRate, renderSizeHint, length} = config;
+
+ promise_test(async (t) => {
+ const audioContext = new OfflineAudioContext({
+ numberOfChannels: 1,
+ length,
+ sampleRate,
+ renderSizeHint
+ });
+
+ const source = new ConstantSourceNode(audioContext);
+ const node = createTestSetupFunc(audioContext, t);
+ if (!node) {
+ return;
+ }
+
+ source.connect(node).connect(audioContext.destination);
+ source.start();
+
+ const renderedBuffer = await audioContext.startRendering();
+
+ // Verify that the audio output contains no invalid floating-point values
+ // (NaN or Infinities), indicating stability under custom block sizes.
+ assertNoNaNOrInfinity(renderedBuffer);
+
+ if (postRenderAssertFunc) {
+ postRenderAssertFunc(audioContext, renderedBuffer, t);
+ }
+ }, testName);
+};
diff --git a/Tests/LibWeb/Text/input/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/scriptprocessor-rendersizehint.https.html b/Tests/LibWeb/Text/input/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/scriptprocessor-rendersizehint.https.html
new file mode 100644
index 00000000000..0eaaaafbede
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/scriptprocessor-rendersizehint.https.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<title>Test ScriptProcessorNode with renderSizeHint</title>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script src="../../../webaudio/resources/rendersizehint-robustness-helper.js"></script>
+<script>
+for (const config of ROBUSTNESS_TEST_CONFIGS) {
+ const {sampleRate, renderSizeHint} = config;
+
+ // Find the nearest valid ScriptProcessor buffer size (power of two
+ // between 256 and 16384) using log2 and clamping.
+ const nearestPowerOf2 = Math.pow(2, Math.round(Math.log2(renderSizeHint)));
+ const bufferSize = Math.min(16384, Math.max(256, nearestPowerOf2));
+
+ const state = {processCalled: false};
+
+ runQuantumRobustnessTest(
+ config,
+ (audioContext, t) => {
+ const renderQuantumSize = audioContext.renderQuantumSize;
+ const isSupported =
+ renderQuantumSize > 0 && (bufferSize % renderQuantumSize === 0);
+
+ if (!isSupported) {
+ assert_throws_dom('NotSupportedError', () => {
+ audioContext.createScriptProcessor(bufferSize, 1, 1);
+ });
+ return null;
+ }
+
+ const scriptProcessor =
+ audioContext.createScriptProcessor(bufferSize, 1, 1);
+ scriptProcessor.onaudioprocess = t.step_func((e) => {
+ state.processCalled = true;
+ assert_equals(
+ e.inputBuffer.length, scriptProcessor.bufferSize,
+ 'Expected input buffer length to match ' +
+ 'ScriptProcessor buffer size');
+ });
+ return scriptProcessor;
+ },
+ `ScriptProcessor handles block size with renderSizeHint ` +
+ `${renderSizeHint} & bufferSize ${bufferSize}`,
+ () => {
+ assert_true(state.processCalled,
+ 'onaudioprocess should have been called');
+ }
+ );
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/simple-input-output.html b/Tests/LibWeb/Text/input/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/simple-input-output.html
new file mode 100644
index 00000000000..9532e65703c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webaudio/the-audio-api/the-scriptprocessornode-interface/simple-input-output.html
@@ -0,0 +1,84 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Test ScriptProcessorNode</title>
+ <script src="../../../resources/testharness.js"></script>
+ <script src="../../../resources/testharnessreport.js"></script>
+ <script src="../../../webaudio/resources/audit-util.js"></script>
+</head>
+<body>
+ <script>
+ // Arbitrary sample rate
+ const sampleRate = 48000;
+
+ promise_test(async (t) => {
+ // Two channels for testing. Channel 0 is the output of the
+ // scriptProcessor. Channel 1 is the oscillator so we can compare
+ // the outputs.
+ const context = new OfflineAudioContext({
+ numberOfChannels: 2,
+ length: sampleRate,
+ sampleRate: sampleRate});
+
+ const merger = new ChannelMergerNode(
+ context, {numberOfChannels: context.destination.channelCount});
+ merger.connect(context.destination);
+
+ const src = new OscillatorNode(context);
+
+ // Arbitrary buffer size for the ScriptProcessorNode. Don't use 0;
+ // we need to know the actual size to know the latency of the node
+ // (easily).
+ const spnSize = 512;
+ const spn = context.createScriptProcessor(spnSize, 1, 1);
+
+ // Arrange for the ScriptProcessor to add |offset| to the input.
+ const offset = 1;
+ spn.onaudioprocess = (event) => {
+ const input = event.inputBuffer.getChannelData(0);
+ const output = event.outputBuffer.getChannelData(0);
+ for (let k = 0; k < output.length; ++k) {
+ output[k] = input[k] + offset;
+ }
+ };
+
+ src.connect(spn).connect(merger, 0, 0);
+ src.connect(merger, 0, 1);
+
+ // Start and stop the source. The stop time is fairly arbitrary,
+ // but use a render quantum boundary for simplicity.
+ const stopFrame = RENDER_QUANTUM_FRAMES;
+ src.start();
+ src.stop(stopFrame / context.sampleRate);
+
+ const buffer = await context.startRendering();
+ const ch0 = buffer.getChannelData(0);
+ const ch1 = buffer.getChannelData(1);
+
+ const shifted = ch1.slice(0, stopFrame).map((x) => x + offset);
+
+ // SPN has a basic latency of 2*|spnSize| fraems, so the
+ // beginning is silent.
+ assert_array_equals(
+ ch0.slice(0, 2 * spnSize - 1),
+ new Float32Array(2 * spnSize - 1),
+ `ScriptProcessor output[0:${2 * spnSize - 1}]`);
+
+ // For the middle section (after adding latency), the output
+ // should be the source shifted by |offset|.
+ assert_array_equals(
+ ch0.slice(2 * spnSize, 2 * spnSize + stopFrame),
+ new Float32Array(shifted),
+ `ScriptProcessor output[${2 * spnSize}:` +
+ `${2 * spnSize + stopFrame - 1}]`);
+
+ // Output should be constant after the source has stopped.
+ // Include the latency introduced by the node.
+ assert_array_equals(
+ ch0.slice(2 * spnSize + stopFrame),
+ new Float32Array(ch0.length - (2 * spnSize + stopFrame)).fill(offset),
+ `ScriptProcessor output[${2 * spnSize + stopFrame}:]`);
+ }, 'ScriptProcessor with stopped input source');
+ </script>
+</body>
+</html>