Bug 1255261 - Read current paragraph after pressing stop. r?jaws
We always had to make sure we were one paragraph back from where we wanted
to start. That seems broken. I made it so that we start from the current
paragraph.
MozReview-Commit-ID: 4HMTdXcF644
--- a/toolkit/components/narrate/Narrator.jsm
+++ b/toolkit/components/narrate/Narrator.jsm
@@ -138,17 +138,17 @@ Narrator.prototype = {
let win = this._win;
win.dispatchEvent(new win.CustomEvent(eventType,
{ detail: Cu.cloneInto(detail, win.document) }));
},
_speakInner: function() {
this._win.speechSynthesis.cancel();
let tw = this._treeWalker;
- let paragraph = tw.nextNode();
+ let paragraph = tw.currentNode;
if (!paragraph) {
tw.currentNode = tw.root;
return Promise.resolve();
}
let utterance = new this._win.SpeechSynthesisUtterance(
paragraph.textContent);
utterance.rate = this._speechOptions.rate;
@@ -188,16 +188,17 @@ Narrator.prototype = {
if (this._inTest) {
this._sendTestEvent("paragraphend", {});
}
if (this._stopped) {
// User pressed stopped.
resolve();
} else {
+ tw.nextNode();
this._speakInner().then(resolve, reject);
}
});
utterance.addEventListener("error", () => {
reject("speech synthesis failed");
});
@@ -211,24 +212,20 @@ Narrator.prototype = {
voice: this._getVoice(speechOptions.voice)
};
this._stopped = false;
return this._detectLanguage().then(() => {
let tw = this._treeWalker;
if (!this._isParagraphInView(tw.currentNode)) {
tw.currentNode = tw.root;
- while (tw.nextNode()) {
- if (this._isParagraphInView(tw.currentNode)) {
- break;
- }
- }
- // _speakInner will advance to the next node for us, so we need
- // to have it one paragraph back from the first visible one.
- tw.previousNode();
+ while (tw.nextNode() && !this._isParagraphInView(tw.currentNode)) {}
+ }
+ if (tw.currentNode == tw.root) {
+ tw.nextNode();
}
return this._speakInner();
});
},
stop: function() {
this._stopped = true;
--- a/toolkit/components/narrate/test/browser_narrate.js
+++ b/toolkit/components/narrate/test/browser_narrate.js
@@ -52,16 +52,29 @@ add_task(function* testNarrate() {
promiseEvent = ContentTaskUtils.waitForEvent(content, "paragraphstart");
$(NarrateTestUtils.FORWARD).click();
speechinfo = (yield promiseEvent).detail;
is(speechinfo.voice, TEST_VOICE, "same voice is used");
isnot(speechinfo.paragraph, paragraph, "next paragraph is being spoken");
NarrateTestUtils.isStartedState(content, ok);
+ paragraph = speechinfo.paragraph;
+ $(NarrateTestUtils.STOP).click();
+ yield ContentTaskUtils.waitForCondition(
+ () => !$(NarrateTestUtils.STOP), "transitioned to stopped state");
+ NarrateTestUtils.isStoppedState(content, ok);
+
+ promiseEvent = ContentTaskUtils.waitForEvent(content, "paragraphstart");
+ $(NarrateTestUtils.START).click();
+ speechinfo = (yield promiseEvent).detail;
+ is(speechinfo.paragraph, paragraph, "read same paragraph again");
+
+ NarrateTestUtils.isStartedState(content, ok);
+
let eventUtils = NarrateTestUtils.getEventUtils(content);
promiseEvent = ContentTaskUtils.waitForEvent(content, "paragraphstart");
prefChanged = NarrateTestUtils.waitForPrefChange("narrate.rate");
$(NarrateTestUtils.RATE).focus();
eventUtils.sendKey("PAGE_UP", content);
let newspeechinfo = (yield promiseEvent).detail;
is(newspeechinfo.paragraph, speechinfo.paragraph, "same paragraph");