Bug 1166365 - Add tests for narrate. r=Gijs draft
authorEitan Isaacson <eitan@monotonous.org>
Wed, 24 Feb 2016 11:56:52 -0800
changeset 337023 8e12afa45ec88098fc2e086d2f56162f90024d0a
parent 337022 02c1c2ddd25f4fb4779c26bbe29d4ecabf5d25d0
child 515563 dea4afb025ef0f10de5f28beb1b110d8e7110bef
push id12250
push userbmo:eitan@monotonous.org
push dateFri, 04 Mar 2016 19:07:54 +0000
reviewersGijs
bugs1166365
milestone47.0a1
Bug 1166365 - Add tests for narrate. r=Gijs MozReview-Commit-ID: 84zRbvMimLV
toolkit/components/narrate/moz.build
toolkit/components/narrate/test/.eslintrc
toolkit/components/narrate/test/NarrateTestUtils.jsm
toolkit/components/narrate/test/browser.ini
toolkit/components/narrate/test/browser_narrate.js
toolkit/components/narrate/test/browser_narrate_disable.js
toolkit/components/narrate/test/browser_voiceselect.js
toolkit/components/narrate/test/head.js
toolkit/components/reader/AboutReader.jsm
--- a/toolkit/components/narrate/moz.build
+++ b/toolkit/components/narrate/moz.build
@@ -4,8 +4,10 @@
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 EXTRA_JS_MODULES.narrate = [
   'NarrateControls.jsm',
   'Narrator.jsm',
   'VoiceSelect.jsm'
 ]
+
+BROWSER_CHROME_MANIFESTS += ['test/browser.ini']
new file mode 100644
--- /dev/null
+++ b/toolkit/components/narrate/test/.eslintrc
@@ -0,0 +1,21 @@
+{
+  "extends": [
+    "../.eslintrc"
+  ],
+
+  "globals": {
+    "is": true,
+    "isnot": true,
+    "ok": true,
+    "NarrateTestUtils": true,
+    "content": true,
+    "ContentTaskUtils": true,
+    "ContentTask": true,
+    "BrowserTestUtils": true,
+    "gBrowser": true,
+  },
+
+  "rules": {
+    "mozilla/import-headjs-globals": 1
+  }
+}
new file mode 100644
--- /dev/null
+++ b/toolkit/components/narrate/test/NarrateTestUtils.jsm
@@ -0,0 +1,114 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+Components.utils.import("resource://gre/modules/Services.jsm");
+
+this.EXPORTED_SYMBOLS = [ "NarrateTestUtils" ];
+
+this.NarrateTestUtils = {
+  TOGGLE: "#narrate-toggle",
+  POPUP: "#narrate-dropdown .dropdown-popup",
+  VOICE_SELECT: "#narrate-voices .select-toggle",
+  VOICE_OPTIONS: "#narrate-voices .options",
+  VOICE_SELECTED: "#narrate-voices .options .option.selected",
+  VOICE_SELECT_LABEL: "#narrate-voices .select-toggle .current-voice",
+  RATE: "#narrate-rate-input",
+  START: "#narrate-start-stop:not(.speaking)",
+  STOP: "#narrate-start-stop.speaking",
+  BACK: "#narrate-skip-previous",
+  FORWARD: "#narrate-skip-next",
+
+  isVisible: function(element) {
+    let style = element.ownerDocument.defaultView.getComputedStyle(element, "");
+    if (style.display == "none") {
+      return false;
+    } else if (style.visibility != "visible") {
+      return false;
+    } else if (style.display == "-moz-popup" && element.state != "open") {
+      return false;
+    }
+
+    // Hiding a parent element will hide all its children
+    if (element.parentNode != element.ownerDocument) {
+      return this.isVisible(element.parentNode);
+    }
+
+    return true;
+  },
+
+  isStoppedState: function(window, ok) {
+    let $ = window.document.querySelector.bind(window.document);
+    ok($(this.BACK).disabled, "back button is disabled");
+    ok($(this.FORWARD).disabled, "forward button is disabled");
+    ok(!!$(this.START), "start button is showing");
+    ok(!$(this.STOP), "stop button is hidden");
+  },
+
+  isStartedState: function(window, ok) {
+    let $ = window.document.querySelector.bind(window.document);
+    ok(!$(this.BACK).disabled, "back button is enabled");
+    ok(!$(this.FORWARD).disabled, "forward button is enabled");
+    ok(!$(this.START), "start button is hidden");
+    ok(!!$(this.STOP), "stop button is showing");
+  },
+
+  selectVoice: function(window, voiceUri) {
+    if (!this.isVisible(window.document.querySelector(this.VOICE_OPTIONS))) {
+      window.document.querySelector(this.VOICE_SELECT).click();
+    }
+
+    let voiceOption = window.document.querySelector(
+      `#narrate-voices .option[data-value="${voiceUri}"]`);
+
+    voiceOption.focus();
+    voiceOption.click();
+
+    return voiceOption.classList.contains("selected");
+  },
+
+  getEventUtils: function(window) {
+    let eventUtils = {
+      "_EU_Ci": Components.interfaces,
+      "_EU_Cc": Components.classes,
+      window: window,
+      parent: window,
+      navigator: window.navigator,
+      KeyboardEvent: window.KeyboardEvent,
+      KeyEvent: window.KeyEvent
+    };
+    Services.scriptloader.loadSubScript(
+      "chrome://mochikit/content/tests/SimpleTest/EventUtils.js", eventUtils);
+    return eventUtils;
+  },
+
+  getReaderReadyPromise: function(window) {
+    return new Promise(resolve => {
+      function observeReady(subject, topic) {
+        if (subject == window) {
+          Services.obs.removeObserver(observeReady, topic);
+          resolve();
+        }
+      }
+
+      if (window.document.body.classList.contains("loaded")) {
+        resolve();
+      } else {
+        Services.obs.addObserver(observeReady, "AboutReader:Ready", false);
+      }
+    });
+  },
+
+  waitForPrefChange: function(pref) {
+    return new Promise(resolve => {
+      function observeChange() {
+        Services.prefs.removeObserver(pref, observeChange);
+        resolve();
+      }
+
+      Services.prefs.addObserver(pref, observeChange, false);
+    });
+  }
+};
new file mode 100644
--- /dev/null
+++ b/toolkit/components/narrate/test/browser.ini
@@ -0,0 +1,8 @@
+[DEFAULT]
+support-files =
+  head.js
+  NarrateTestUtils.jsm
+
+[browser_narrate.js]
+[browser_narrate_disable.js]
+[browser_voiceselect.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/narrate/test/browser_narrate.js
@@ -0,0 +1,101 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* globals is, isnot, registerCleanupFunction, add_task */
+
+"use strict";
+
+registerCleanupFunction(teardown);
+
+add_task(function* testNarrate() {
+  setup();
+
+  yield spawnInNewReaderTab(TEST_ARTICLE, function* () {
+    let TEST_VOICE = "urn:moz-tts:fake-indirect:teresa";
+    let $ = content.document.querySelector.bind(content.document);
+
+    let popup = $(NarrateTestUtils.POPUP);
+    ok(!NarrateTestUtils.isVisible(popup), "popup is initially hidden");
+
+    let toggle = $(NarrateTestUtils.TOGGLE);
+    toggle.click();
+
+    ok(NarrateTestUtils.isVisible(popup), "popup toggled");
+
+    let voiceOptions = $(NarrateTestUtils.VOICE_OPTIONS);
+    ok(!NarrateTestUtils.isVisible(voiceOptions),
+      "voice options are initially hidden");
+
+    $(NarrateTestUtils.VOICE_SELECT).click();
+    ok(NarrateTestUtils.isVisible(voiceOptions), "voice options pop up");
+
+    let prefChanged = NarrateTestUtils.waitForPrefChange("narrate.voice");
+    ok(NarrateTestUtils.selectVoice(content, TEST_VOICE),
+      "test voice selected");
+    yield prefChanged;
+
+    ok(!NarrateTestUtils.isVisible(voiceOptions), "voice options hidden again");
+
+    NarrateTestUtils.isStoppedState(content, ok);
+
+    let promiseEvent = ContentTaskUtils.waitForEvent(content, "paragraphstart");
+    $(NarrateTestUtils.START).click();
+    let speechinfo = (yield promiseEvent).detail;
+    is(speechinfo.voice, TEST_VOICE, "correct voice is being used");
+    is(speechinfo.paragraph, 0, "first paragraph is being spoken");
+
+    NarrateTestUtils.isStartedState(content, ok);
+
+    promiseEvent = ContentTaskUtils.waitForEvent(content, "paragraphstart");
+    $(NarrateTestUtils.FORWARD).click();
+    speechinfo = (yield promiseEvent).detail;
+    is(speechinfo.voice, TEST_VOICE, "same voice is used");
+    is(speechinfo.paragraph, 1, "second paragraph is being spoken");
+
+    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");
+    isnot(newspeechinfo.rate, speechinfo.rate, "rate changed");
+    yield prefChanged;
+
+    promiseEvent = ContentTaskUtils.waitForEvent(content, "paragraphend");
+    $(NarrateTestUtils.STOP).click();
+    yield promiseEvent;
+
+    yield ContentTaskUtils.waitForCondition(
+      () => !$(NarrateTestUtils.STOP), "transitioned to stopped state");
+    NarrateTestUtils.isStoppedState(content, ok);
+
+    promiseEvent = ContentTaskUtils.waitForEvent(content, "scroll");
+    content.scrollBy(0, 10);
+    yield promiseEvent;
+    ok(!NarrateTestUtils.isVisible(popup), "popup is hidden after scroll");
+
+    toggle.click();
+    ok(NarrateTestUtils.isVisible(popup), "popup is toggled again");
+
+    promiseEvent = ContentTaskUtils.waitForEvent(content, "paragraphstart");
+    $(NarrateTestUtils.START).click();
+    yield promiseEvent;
+    NarrateTestUtils.isStartedState(content, ok);
+
+    promiseEvent = ContentTaskUtils.waitForEvent(content, "scroll");
+    content.scrollBy(0, -10);
+    yield promiseEvent;
+    ok(NarrateTestUtils.isVisible(popup), "popup stays visible after scroll");
+
+    promiseEvent = ContentTaskUtils.waitForEvent(content, "paragraphend");
+    toggle.click();
+    yield promiseEvent;
+    ok(!NarrateTestUtils.isVisible(popup), "popup is dismissed while speaking");
+    ok(true, "speech stopped when popup is dismissed");
+  });
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/narrate/test/browser_narrate_disable.js
@@ -0,0 +1,37 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* globals registerCleanupFunction, add_task */
+
+"use strict";
+
+const ENABLE_PREF = "narrate.enabled";
+
+registerCleanupFunction(() => {
+  clearUserPref(ENABLE_PREF);
+  teardown();
+});
+
+add_task(function* testNarratePref() {
+  setup();
+
+  yield spawnInNewReaderTab(TEST_ARTICLE, function() {
+    is(content.document.querySelectorAll(NarrateTestUtils.TOGGLE).length, 1,
+      "narrate is inserted by default");
+  });
+
+  setBoolPref(ENABLE_PREF, false);
+
+  yield spawnInNewReaderTab(TEST_ARTICLE, function() {
+    ok(!content.document.querySelector(NarrateTestUtils.TOGGLE),
+      "narrate is disabled and is not in reader mode");
+  });
+
+  setBoolPref(ENABLE_PREF, true);
+
+  yield spawnInNewReaderTab(TEST_ARTICLE, function() {
+    is(content.document.querySelectorAll(NarrateTestUtils.TOGGLE).length, 1,
+      "narrate is re-enabled and appears only once");
+  });
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/narrate/test/browser_voiceselect.js
@@ -0,0 +1,106 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* globals registerCleanupFunction, add_task, is, isnot */
+
+"use strict";
+
+registerCleanupFunction(teardown);
+
+add_task(function* testVoiceselectDropdownAutoclose() {
+  setup();
+
+  yield spawnInNewReaderTab(TEST_ARTICLE, function* () {
+    let $ = content.document.querySelector.bind(content.document);
+
+    $(NarrateTestUtils.TOGGLE).click();
+    ok(NarrateTestUtils.isVisible($(NarrateTestUtils.POPUP)),
+      "popup is toggled");
+
+    ok(!NarrateTestUtils.isVisible($(NarrateTestUtils.VOICE_OPTIONS)),
+      "voice options are initially hidden");
+
+    $(NarrateTestUtils.VOICE_SELECT).click();
+    ok(NarrateTestUtils.isVisible($(NarrateTestUtils.VOICE_OPTIONS)),
+      "voice options are toggled");
+
+    $(NarrateTestUtils.TOGGLE).click();
+    // A focus will follow a real click.
+    $(NarrateTestUtils.TOGGLE).focus();
+    ok(!NarrateTestUtils.isVisible($(NarrateTestUtils.POPUP)),
+      "narrate popup is dismissed");
+
+    $(NarrateTestUtils.TOGGLE).click();
+    // A focus will follow a real click.
+    $(NarrateTestUtils.TOGGLE).focus();
+    ok(NarrateTestUtils.isVisible($(NarrateTestUtils.POPUP)),
+      "narrate popup is showing again");
+    ok(!NarrateTestUtils.isVisible($(NarrateTestUtils.VOICE_OPTIONS)),
+      "voice options are hidden after popup comes back");
+  });
+});
+
+add_task(function* testVoiceselectLabelChange() {
+  setup();
+
+  yield spawnInNewReaderTab(TEST_ARTICLE, function* () {
+    let $ = content.document.querySelector.bind(content.document);
+
+    $(NarrateTestUtils.TOGGLE).click();
+    ok(NarrateTestUtils.isVisible($(NarrateTestUtils.POPUP)),
+      "popup is toggled");
+
+    ok(NarrateTestUtils.selectVoice(content, "urn:moz-tts:fake-direct:lenny"),
+      "voice selected");
+
+    let selectedOption = $(NarrateTestUtils.VOICE_SELECTED);
+    let selectLabel = $(NarrateTestUtils.VOICE_SELECT_LABEL);
+
+    is(selectedOption.textContent, selectLabel.textContent,
+      "new label matches selected voice");
+  });
+});
+
+add_task(function* testVoiceselectKeyboard() {
+  setup();
+
+  yield spawnInNewReaderTab(TEST_ARTICLE, function* () {
+    let $ = content.document.querySelector.bind(content.document);
+
+    $(NarrateTestUtils.TOGGLE).click();
+    ok(NarrateTestUtils.isVisible($(NarrateTestUtils.POPUP)),
+      "popup is toggled");
+
+    let eventUtils = NarrateTestUtils.getEventUtils(content);
+
+    let firstValue = $(NarrateTestUtils.VOICE_SELECTED).dataset.value;
+
+    ok(!NarrateTestUtils.isVisible($(NarrateTestUtils.VOICE_OPTIONS)),
+      "voice options initially are hidden");
+
+    $(NarrateTestUtils.VOICE_SELECT).focus();
+
+    eventUtils.sendKey("DOWN", content);
+
+    yield ContentTaskUtils.waitForCondition(
+      () => $(NarrateTestUtils.VOICE_SELECTED).dataset.value != firstValue,
+      "value changed after pressing DOWN key");
+
+    eventUtils.sendKey("RETURN", content);
+
+    ok(NarrateTestUtils.isVisible($(NarrateTestUtils.VOICE_OPTIONS)),
+      "voice options showing after pressing RETURN");
+
+    eventUtils.sendKey("UP", content);
+
+    eventUtils.sendKey("RETURN", content);
+
+    ok(!NarrateTestUtils.isVisible($(NarrateTestUtils.VOICE_OPTIONS)),
+      "voice options hidden after pressing RETURN");
+
+    yield ContentTaskUtils.waitForCondition(
+      () => $(NarrateTestUtils.VOICE_SELECTED).dataset.value == firstValue,
+      "value changed back to original after pressing RETURN");
+  });
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/narrate/test/head.js
@@ -0,0 +1,67 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* exported teardown, setup, toggleExtension,
+   spawnInNewReaderTab, TEST_ARTICLE  */
+
+"use strict";
+
+const TEST_ARTICLE = "http://example.com/browser/browser/base/content/test/" +
+  "general/readerModeArticle.html";
+
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "Promise",
+  "resource://gre/modules/Promise.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Services",
+  "resource://gre/modules/Services.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
+  "resource://gre/modules/AddonManager.jsm");
+
+const TEST_PREFS = [
+  ["reader.parse-on-load.enabled", true],
+  ["media.webspeech.synth.enabled", true],
+  ["media.webspeech.synth.test", true],
+  ["narrate.enabled", true],
+  ["narrate.test", true]
+];
+
+function setup() {
+  // Set required test prefs.
+  TEST_PREFS.forEach(([name, value]) => {
+    setBoolPref(name, value);
+  });
+}
+
+function teardown() {
+  // Reset test prefs.
+  TEST_PREFS.forEach(pref => {
+    clearUserPref(pref[0]);
+  });
+}
+
+function spawnInNewReaderTab(url, func) {
+  return BrowserTestUtils.withNewTab(
+    { gBrowser,
+      url: `about:reader?url=${encodeURIComponent(url)}` },
+      function* (browser) {
+        yield ContentTask.spawn(browser, null, function* () {
+          Components.utils.import("chrome://mochitests/content/browser/" +
+            "toolkit/components/narrate/test/NarrateTestUtils.jsm");
+
+          yield NarrateTestUtils.getReaderReadyPromise(content);
+        });
+
+        yield ContentTask.spawn(browser, null, func);
+      });
+}
+
+function setBoolPref(name, value) {
+  Services.prefs.setBoolPref(name, value);
+}
+
+function clearUserPref(name) {
+  Services.prefs.clearUserPref(name);
+}
+
--- a/toolkit/components/reader/AboutReader.jsm
+++ b/toolkit/components/reader/AboutReader.jsm
@@ -609,17 +609,17 @@ AboutReader.prototype = {
     this._maybeSetTextDirection(article);
 
     this._contentElement.style.display = "block";
     this._updateImageMargins();
 
     this._requestFavicon();
     this._doc.body.classList.add("loaded");
 
-    Services.obs.notifyObservers(null, "AboutReader:Ready", "");
+    Services.obs.notifyObservers(this._win, "AboutReader:Ready", "");
   },
 
   _hideContent: function() {
     this._headerElement.style.display = "none";
     this._contentElement.style.display = "none";
   },
 
   _showProgressDelayed: function() {