Bug 1255261 - Read current paragraph after pressing stop. r=jaws, a=ritu
authorEitan Isaacson <eitan@monotonous.org>
Thu, 18 Aug 2016 10:15:00 -0400
changeset 347741 84b11aee0b433f8ca33c870804965f5f9e952261
parent 347740 18102c4b22762693f42bc71e12180a32090731fd
child 347742 a55ede08a8bad53f6fbecf32aea2df6e28980e7f
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws, ritu
bugs1255261
milestone50.0a2
Bug 1255261 - Read current paragraph after pressing stop. r=jaws, a=ritu 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,20 +212,24 @@ 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() && !this._isParagraphInView(tw.currentNode)) {}
-        // _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()) {
+          if (this._isParagraphInView(tw.currentNode)) {
+            break;
+          }
+        }
+      }
+      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");