class GoogleTtsTestSupport {
constructor() {
this.init_();
this.notifyCcFixture_();
}
init_() {
window.testUtterances = [];
window.engine.isTastTest_ = true;
}
* Notifies the SpokenFeedbackIntegrationTest fixture, which waits for the JS
* side to call `chrome.test.sendScriptResult`, that it can continue.
* @private
*/
notifyCcFixture_() {
chrome.test.sendScriptResult('ready');
}
async ensureLoaded() {
const loaded = (voices) => {
for (const voice of voices) {
if (voice.extensionId === 'gjjabgpgjpampikjhjpfhneeoapjbjaf') {
return true;
}
}
return false;
};
await new Promise(resolve => {
const intervalId = setInterval(() => {
chrome.tts.getVoices((voices) => {
if (loaded(voices)) {
clearInterval(intervalId);
resolve();
}
});
}, 300);
});
this.notifyCcFixture_();
}
* Requests speech from the Google TTS engine.
* @param {string} utterance
*/
speak(utterance) {
chrome.tts.speak(utterance, {volume: 0.01});
this.notifyCcFixture_();
}
* Loops through the list of spoken strings to see if the provided utterance
* has been spoken. Discards spoken utterances that don't match the provided
* one. Returns once the utterance has been found in the list of spoken
* strings.
* @param {string} utterance
*/
async consume(utterance) {
const hasSpokenUtterance = () => {
const data = window.testUtterances.shift();
if (!data) {
return false;
}
return utterance === data.utterance;
};
if (hasSpokenUtterance()) {
this.notifyCcFixture_();
return;
}
await new Promise(resolve => {
const intervalId = setInterval(() => {
if (hasSpokenUtterance()) {
clearInterval(intervalId);
resolve();
}
}, 300);
});
this.notifyCcFixture_();
}
}
window.googleTtsTestSupport = new GoogleTtsTestSupport();