Bug 1372325 - Allow to reuse SpeechSynthesisUtterance. r?eeejay
Google translate in search page reuses SpeechSynthesisUtterance. So after output utterance, it no longer work until page is reloaded. Edge, Safari and Blink can reuse SpeechSynthesisUtterance, so we should allow to reuse it.
MozReview-Commit-ID: JONakA2TTSC
--- a/dom/media/webspeech/synth/SpeechSynthesis.cpp
+++ b/dom/media/webspeech/synth/SpeechSynthesis.cpp
@@ -133,17 +133,18 @@ bool SpeechSynthesis::HasVoices() const
}
return voiceCount != 0;
}
void
SpeechSynthesis::Speak(SpeechSynthesisUtterance& aUtterance)
{
- if (aUtterance.mState != SpeechSynthesisUtterance::STATE_NONE) {
+ if (aUtterance.mState != SpeechSynthesisUtterance::STATE_NONE &&
+ aUtterance.mState != SpeechSynthesisUtterance::STATE_ENDED) {
// XXX: Should probably raise an error
return;
}
mSpeechQueue.AppendElement(&aUtterance);
aUtterance.mState = SpeechSynthesisUtterance::STATE_PENDING;
// If we only have one item in the queue, we aren't pre-paused, and
new file mode 100644
--- /dev/null
+++ b/dom/media/webspeech/synth/test/file_speech_reuse.html
@@ -0,0 +1,51 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1372325
+-->
+<head>
+ <meta charset="utf-8">
+ <title>Test for Bug 1372325</title>
+ <script type="application/javascript">
+ window.SimpleTest = parent.SimpleTest;
+ window.info = parent.info;
+ window.is = parent.is;
+ window.isnot = parent.isnot;
+ window.ok = parent.ok;
+ </script>
+ <script type="application/javascript" src="common.js"></script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1372325">Mozilla Bug 1372325</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+/** Test for Bug 1372325 **/
+
+function testFunc() {
+ var utterance = new SpeechSynthesisUtterance();
+
+ function reuseUtterance() {
+ utterance.removeEventListener("end", reuseUtterance);
+ utterance.addEventListener("end", () => {
+ ok(true, "Reusing uttrance works well.")
+ SimpleTest.finish();
+ });
+ speechSynthesis.speak(utterance);
+ }
+
+ utterance.addEventListener("end", reuseUtterance);
+ speechSynthesis.speak(utterance);
+}
+
+// Run test with no global queue, and then run it with a global queue.
+testFunc();
+
+</script>
+</pre>
+</body>
+</html>
--- a/dom/media/webspeech/synth/test/mochitest.ini
+++ b/dom/media/webspeech/synth/test/mochitest.ini
@@ -5,23 +5,25 @@ support-files =
common.js
file_bfcache_frame.html
file_bfcache_frame2.html
file_setup.html
file_speech_queue.html
file_speech_simple.html
file_speech_cancel.html
file_speech_error.html
+ file_speech_reuse.html
file_indirect_service_events.html
file_global_queue.html
file_global_queue_cancel.html
file_global_queue_pause.html
[test_setup.html]
[test_speech_queue.html]
[test_speech_simple.html]
[test_speech_cancel.html]
[test_speech_error.html]
+[test_speech_reuse.html]
[test_indirect_service_events.html]
[test_global_queue.html]
[test_global_queue_cancel.html]
[test_global_queue_pause.html]
[test_bfcache.html]
new file mode 100644
--- /dev/null
+++ b/dom/media/webspeech/synth/test/test_speech_reuse.html
@@ -0,0 +1,35 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1372325
+-->
+<head>
+ <meta charset="utf-8">
+ <title>Test for Bug 1372325</title>
+ <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="application/javascript" src="common.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1372325">Mozilla Bug 1372325</a>
+<p id="display"></p>
+<iframe id="testFrame"></iframe>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+/** Test for Bug 1372325 **/
+
+SimpleTest.waitForExplicitFinish();
+
+SpecialPowers.pushPrefEnv(
+ { set: [['media.webspeech.synth.enabled', true],
+ ['media.webspeech.synth.force_global_queue', false]] },
+ function() { loadSpeechTest("file_speech_reuse.html"); });
+
+</script>
+</pre>
+</body>
+</html>