gecko-dev/testing/web-platform/tests/speech-api/idlharness.window.js
Philip Jägenstedt 3009bdad70 Bug 1491592 [wpt PR 13017] - Use the voiceschanged event to wait for SpeechSynthesisVoice instance, a=testonly
Automatic update from web-platform-testsUse the voiceschanged event to wait for SpeechSynthesisVoice instance (#13017)

In https://github.com/web-platform-tests/wpt/pull/12317 I noticed that
speechSynthesis.getVoices() could return an empty list and is allowed to
do so by spec, but not that there's also an event that's fired when the
voices are updated:
https://w3c.github.io/speech-api/#eventdef-speechsynthesis-voiceschanged

Using this improves coverage somewhat.
--

wpt-commits: 2d49b1ea2fb2c46765ef5bdb01e287497a7406a9
wpt-pr: 13017
2018-10-09 10:43:13 +00:00

50 lines
1.6 KiB
JavaScript

// META: script=/resources/WebIDLParser.js
// META: script=/resources/idlharness.js
'use strict';
// https://w3c.github.io/speech-api/#dom-speechsynthesis-getvoices can
// return an empty list and a voiceschanged event is fired if the list of
// voices is determined asynchronously.
function getVoices() {
return new Promise(resolve => {
const voices = speechSynthesis.getVoices();
if (voices.length) {
resolve(voices);
} else {
// wait for voiceschanged event
speechSynthesis.addEventListener('voiceschanged', () => {
resolve(speechSynthesis.getVoices());
}, { once: true });
}
});
}
idl_test(
['speech-api'],
['dom', 'html'],
(idl_array, t) => {
idl_array.add_objects({
SpeechGrammar: ['new SpeechGrammar()'],
SpeechGrammarList: ['new SpeechGrammarList()'],
SpeechRecognition: ['new SpeechRecognition()'],
// TODO: SpeechRecognitionAlternative
// TODO: SpeechRecognitionErrorEvent
// TODO: SpeechRecognitionEvent
// TODO: SpeechRecognitionResult
// TODO: SpeechRecognitionResultList
SpeechSynthesis: ['speechSynthesis'],
// TODO: SpeechSynthesisErrorEvent
// TODO: SpeechSynthesisEvent
SpeechSynthesisUtterance: ['new SpeechSynthesisUtterance()'],
SpeechSynthesisVoice: ['voice'],
Window: ['self'],
});
const awaitVoice = getVoices().then(voices => self.voice = voices[0]);
const timeout = new Promise((_, reject) => {
t.step_timeout(() => reject('Timed out waiting for voice'), 3000);
});
return Promise.race([awaitVoice, timeout]);
}
);