Bug 1187105 - Pause speechsynthesis on empty queue. r=smaug

This commit is contained in:
Eitan Isaacson 2015-07-30 16:04:25 -07:00
parent 345d5ef85c
commit f8946263f5
3 changed files with 34 additions and 12 deletions

View File

@ -72,6 +72,7 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE(SpeechSynthesis)
SpeechSynthesis::SpeechSynthesis(nsPIDOMWindow* aParent)
: mParent(aParent)
, mHoldQueue(false)
{
MOZ_ASSERT(aParent->IsInnerWindow());
}
@ -120,11 +121,8 @@ SpeechSynthesis::Speaking() const
bool
SpeechSynthesis::Paused() const
{
if (mSpeechQueue.IsEmpty()) {
return false;
}
return mSpeechQueue.ElementAt(0)->IsPaused();
return mHoldQueue ||
(!mSpeechQueue.IsEmpty() && mSpeechQueue.ElementAt(0)->IsPaused());
}
void
@ -138,7 +136,7 @@ SpeechSynthesis::Speak(SpeechSynthesisUtterance& aUtterance)
mSpeechQueue.AppendElement(&aUtterance);
aUtterance.mState = SpeechSynthesisUtterance::STATE_PENDING;
if (mSpeechQueue.Length() == 1 && !mCurrentTask) {
if (mSpeechQueue.Length() == 1 && !mCurrentTask && !mHoldQueue) {
AdvanceQueue();
}
}
@ -181,28 +179,43 @@ void
SpeechSynthesis::Cancel()
{
if (mCurrentTask) {
if (mSpeechQueue.Length() > 1) {
if (mSpeechQueue.Length() > 1) {
// Remove all queued utterances except for current one.
mSpeechQueue.RemoveElementsAt(1, mSpeechQueue.Length() - 1);
}
mCurrentTask->Cancel();
mCurrentTask->Cancel();
} else {
mSpeechQueue.Clear();
}
}
void
SpeechSynthesis::Pause()
{
if (mCurrentTask && !Paused() && (Speaking() || Pending())) {
if (Paused()) {
return;
}
if (mCurrentTask &&
mSpeechQueue.ElementAt(0)->GetState() != SpeechSynthesisUtterance::STATE_ENDED) {
mCurrentTask->Pause();
} else {
mHoldQueue = true;
}
}
void
SpeechSynthesis::Resume()
{
if (mCurrentTask && Paused()) {
if (!Paused()) {
return;
}
if (mCurrentTask) {
mCurrentTask->Resume();
} else {
mHoldQueue = false;
AdvanceQueue();
}
}

View File

@ -66,9 +66,10 @@ private:
nsRefPtr<nsSpeechTask> mCurrentTask;
nsRefPtrHashtable<nsStringHashKey, SpeechSynthesisVoice> mVoiceCache;
bool mHoldQueue;
};
} // namespace dom
} // namespace mozilla
#endif

View File

@ -71,6 +71,14 @@ utterance3.addEventListener('end', function(e) {
SimpleTest.finish();
});
// Speak/cancel while paused (Bug 1187105)
speechSynthesis.pause();
speechSynthesis.speak(new SpeechSynthesisUtterance("hello."));
ok(speechSynthesis.pending, "paused speechSynthesis has an utterance queued.");
speechSynthesis.cancel();
ok(!speechSynthesis.pending, "paused speechSynthesis has no utterance queued.");
speechSynthesis.resume();
speechSynthesis.speak(utterance);
ok(!speechSynthesis.speaking, "speechSynthesis is not speaking yet.");
ok(speechSynthesis.pending, "speechSynthesis has an utterance queued.");