Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 8 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /webaudio/the-audio-api/the-audiocontext-interface/audiocontext-sinkid-setsinkid.https.html - WPT Dashboard Interop Dashboard
<!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
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
// 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(
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>