Bug 1166365 - Add tests for narrate. r=Gijs

MozReview-Commit-ID: 84zRbvMimLV
This commit is contained in:
Eitan Isaacson 2016-02-24 11:56:52 -08:00
parent 318429c8a8
commit e628c99c32
9 changed files with 457 additions and 1 deletions

View File

@ -9,3 +9,5 @@ EXTRA_JS_MODULES.narrate = [
'Narrator.jsm',
'VoiceSelect.jsm'
]
BROWSER_CHROME_MANIFESTS += ['test/browser.ini']

View File

@ -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
}
}

View File

@ -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);
});
}
};

View File

@ -0,0 +1,8 @@
[DEFAULT]
support-files =
head.js
NarrateTestUtils.jsm
[browser_narrate.js]
[browser_narrate_disable.js]
[browser_voiceselect.js]

View File

@ -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");
});
});

View File

@ -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");
});
});

View File

@ -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");
});
});

View File

@ -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);
}

View File

@ -616,7 +616,7 @@ AboutReader.prototype = {
this._requestFavicon();
this._doc.body.classList.add("loaded");
Services.obs.notifyObservers(null, "AboutReader:Ready", "");
Services.obs.notifyObservers(this._win, "AboutReader:Ready", "");
},
_hideContent: function() {