Bug 1255261 - Read current paragraph after pressing stop. r?jaws draft
authorEitan Isaacson <eitan@monotonous.org>
Wed, 03 Aug 2016 10:34:06 -0700
changeset 398707 4c30cda8d1a9a8a6c2deff370a4c84fe28191bed
parent 398460 5a153f865cb1f9ed33b54f1905e3d69d5d4e8123
child 527725 9150ab2c345e31325e4b3a450453bbac7f333f5c
push id25604
push userbmo:eitan@monotonous.org
push dateTue, 09 Aug 2016 16:43:35 +0000
reviewersjaws
bugs1255261
milestone51.0a1
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
toolkit/components/narrate/Narrator.jsm
toolkit/components/narrate/test/browser_narrate.js
--- 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");