Files
sousa-gecko/testing/web-platform/tests/webaudio/the-audio-api/the-audiocontext-interface/audiocontext-sinkid-setsinkid.https.html
T
Mahesh Bharadwaj Kannan cebecfb9b1 Bug 2043696 [wpt PR 60252] - [WebAudio] Fix flaky WPT sinkId tests on headless shell bots, a=testonly
Automatic update from web-platform-tests
[WebAudio] Fix flaky WPT sinkId tests on headless shell bots

This CL refactors the AudioContext sinkId WPT tests to eliminate
timing races and fragile timeouts that cause CQ failures:

1. Constructor tests are converted from legacy async_test to modern
   promise_test, completely removing fragile 100ms step timeouts.
2. State change tests are restructured to register all event
   handlers before initiating resume() transitions, eliminating
   a timing race.
3. setSinkId tests are isolated per test file, and the timing race
   in close rejections is fixed by utilizing a valid silent sink
   option instead of an invalid device ID.
4. Detached iframe tests are updated to manually catch cross-realm
   exceptions, ensuring compatibility with WPT harness validations.

Bug: 514359173, 514384316, 514528222
Change-Id: I4c42b66e43207c9147ecd0c4353241f81281af21
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7883424
Reviewed-by: Michael Wilson <mjwilson@chromium.org>
Commit-Queue: Mahesh Kannan <kmaheshb@google.com>
Cr-Commit-Position: refs/heads/main@{#1638538}

--

wpt-commits: 73f61f58a0fdf5ca2336d9d86c101f2d4286b3ef
wpt-pr: 60252
2026-06-02 10:27:48 +00:00

136 lines
5.3 KiB
HTML

<!DOCTYPE html>
<head>
<title>Test AudioContext.setSinkId() method</title>
</head>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
"use strict";
let outputDeviceList = null;
let firstDeviceId = null;
// Setup: Get permission via getUserMedia() and a list of audio output devices.
promise_setup(async t => {
await navigator.mediaDevices.getUserMedia({ audio: true });
const deviceList = await navigator.mediaDevices.enumerateDevices();
outputDeviceList =
deviceList.filter(({kind}) => kind === 'audiooutput');
if (outputDeviceList.length > 1) {
firstDeviceId = outputDeviceList[1].deviceId;
}
}, 'Get permission via getUserMedia() and a list of audio output devices.');
// 1.2.3. AudioContext.setSinkId() method
// https://webaudio.github.io/web-audio-api/#dom-audiocontext-setsinkid-domstring-or-audiosinkoptions-sinkid
promise_test(async t => {
const audioContext = new AudioContext();
assert_equals(audioContext.sinkId, '');
audioContext.close();
}, 'default AudioContext.sinkId should be the empty string.');
promise_test(async t => {
// At least one non-default audio output device is required to test
// setSinkId().
if (outputDeviceList.length <= 1) {
return;
}
const audioContext = new AudioContext();
// Change to the first non-default device in the list.
await audioContext.setSinkId(firstDeviceId);
assert_equals(typeof audioContext.sinkId, 'string');
assert_equals(audioContext.sinkId, firstDeviceId);
// If both `sinkId` and [[sink ID]] are a type of DOMString, and they are
// equal to each other, resolve the promise immediately.
await audioContext.setSinkId(firstDeviceId);
audioContext.close();
}, 'setSinkId() with a valid device identifier and setting the ' +
'same ID should succeed.');
promise_test(async t => {
// If sinkId is a type of AudioSinkOptions and [[sink ID]] is a type of
// AudioSinkInfo, and type in sinkId and type in [[sink ID]] are equal,
// resolve the promise immediately.
const audioContext = new AudioContext();
await audioContext.setSinkId({type: 'none'});
assert_equals(typeof audioContext.sinkId, 'object');
assert_equals(audioContext.sinkId.type, 'none');
await audioContext.setSinkId({type: 'none'});
audioContext.close();
}, 'setSinkId() with AudioSinkOptions.type of "none" and setting ' +
'the same option should succeed.');
// 1.2.4. Validating sinkId
// https://webaudio.github.io/web-audio-api/#validating-sink-identifier
// Step 3. If document is not allowed to use the feature identified by
// "speaker-selection", return a new DOMException whose name is
// "NotAllowedError".
// TODO: Due to the lack of implementation, this step is not tested.
// The wrong AudioSinkOption.type should cause a TypeError.
promise_test(async t => {
const audioContext = new AudioContext();
await promise_rejects_js(t, TypeError,
audioContext.setSinkId({type: 'something_else'}));
audioContext.close();
}, 'setSinkId() should fail with TypeError on an invalid ' +
'AudioSinkOptions.type value.');
// Step 4. If sinkId is a type of DOMString but it is not equal to the empty
// string or it does not match any audio output device identified by the result
// that would be provided by enumerateDevices(), return a new DOMException whose
// name is "NotFoundError".
promise_test(async t => {
const audioContext = new AudioContext();
await promise_rejects_dom(t, 'NotFoundError',
audioContext.setSinkId('some_random_device_id'));
audioContext.close();
}, 'setSinkId() should fail with NotFoundError on an invalid ' +
'device identifier.');
// setSinkId invoked from closed AudioContext should throw InvalidStateError
// DOMException.
promise_test(async t => {
const audioContext = new AudioContext();
await audioContext.close();
assert_equals(audioContext.state, 'closed');
await promise_rejects_dom(t, 'InvalidStateError',
audioContext.setSinkId('some_random_device_id'));
}, 'setSinkId() should fail with InvalidStateError when calling ' +
'from a stopped AudioContext.');
// setSinkId invoked from detached document should throw InvalidStateError
// DOMException.
promise_test(async t => {
const iframe = document.createElementNS(
"http://www.w3.org/1999/xhtml", "iframe");
document.body.appendChild(iframe);
const iframeAudioContext = new iframe.contentWindow.AudioContext();
document.body.removeChild(iframe);
try {
await iframeAudioContext.setSinkId('some_random_device_id');
assert_unreached('setSinkId should have failed on a detached document');
} catch (error) {
assert_equals(error.name, 'InvalidStateError');
}
}, 'setSinkId() should fail with InvalidStateError when calling ' +
'from a detached document.');
// Pending setSinkId() promises should be rejected with a
// DOMException of InvalidStateError when the context is closed.
// See: crbug.com/1408376
promise_test(async t => {
const audioContext = new AudioContext();
const p = audioContext.setSinkId({type: 'none'});
const rejectPromise = promise_rejects_dom(t, 'InvalidStateError', p);
await audioContext.close();
await rejectPromise;
}, 'pending setSinkId() should be rejected with InvalidStateError ' +
'when AudioContext is closed.');
</script>
</html>