Bug 384458 - part 5: add unit test coverage for the findbar modal highlight feature. r=jaws

This commit is contained in:
Mike de Boer 2016-06-09 13:30:51 +02:00
parent 2cdbcb870d
commit e42260141c
7 changed files with 251 additions and 29 deletions

View File

@ -37,7 +37,7 @@ function runTest() {
msg_name: "findchange",
active: true,
searchString: 'foo',
searchLimit: 100,
searchLimit: 1000,
activeMatchOrdinal: 1,
numberOfMatches: 5,
}), `test ${testCount++}`);
@ -48,7 +48,7 @@ function runTest() {
msg_name: "findchange",
active: true,
searchString: 'foo',
searchLimit: 100,
searchLimit: 1000,
activeMatchOrdinal: 2,
numberOfMatches: 5,
}), `test ${testCount++}`);
@ -59,7 +59,7 @@ function runTest() {
msg_name: "findchange",
active: true,
searchString: 'foo',
searchLimit: 100,
searchLimit: 1000,
activeMatchOrdinal: 1,
numberOfMatches: 5,
}), `test ${testCount++}`);
@ -70,7 +70,7 @@ function runTest() {
msg_name: "findchange",
active: true,
searchString: 'xxx',
searchLimit: 100,
searchLimit: 1000,
activeMatchOrdinal: 0,
numberOfMatches: 0,
}), `test ${testCount++}`);
@ -81,7 +81,7 @@ function runTest() {
msg_name: "findchange",
active: true,
searchString: 'bar',
searchLimit: 100,
searchLimit: 1000,
activeMatchOrdinal: 1,
numberOfMatches: 4,
}), `test ${testCount++}`);
@ -92,7 +92,7 @@ function runTest() {
msg_name: "findchange",
active: true,
searchString: 'bar',
searchLimit: 100,
searchLimit: 1000,
activeMatchOrdinal: 2,
numberOfMatches: 4,
}), `test ${testCount++}`);
@ -103,7 +103,7 @@ function runTest() {
msg_name: "findchange",
active: true,
searchString: 'bar',
searchLimit: 100,
searchLimit: 1000,
activeMatchOrdinal: 3,
numberOfMatches: 4,
}), `test ${testCount++}`);
@ -114,7 +114,7 @@ function runTest() {
msg_name: "findchange",
active: true,
searchString: 'bar',
searchLimit: 100,
searchLimit: 1000,
activeMatchOrdinal: 4,
numberOfMatches: 4,
}), `test ${testCount++}`);
@ -125,7 +125,7 @@ function runTest() {
msg_name: "findchange",
active: true,
searchString: 'bar',
searchLimit: 100,
searchLimit: 1000,
activeMatchOrdinal: 1,
numberOfMatches: 4,
}), `test ${testCount++}`);

View File

@ -23,6 +23,8 @@ user_pref("browser.shell.checkDefaultBrowser", false);
user_pref("shell.checkDefaultClient", false);
user_pref("browser.warnOnQuit", false);
user_pref("accessibility.typeaheadfind.autostart", false);
user_pref("findbar.highlightAll", false);
user_pref("findbar.modalHighlight", false);
user_pref("javascript.options.showInConsole", true);
user_pref("devtools.browsertoolbox.panel", "jsdebugger");
user_pref("devtools.debugger.remote-port", 6023);

View File

@ -11,6 +11,7 @@ support-files =
[browser_autoscroll_disabled.js]
[browser_bug295977_autoscroll_overflow.js]
[browser_bug451286.js]
skip-if = !e10s
[browser_bug594509.js]
[browser_bug982298.js]
[browser_bug1198465.js]

View File

@ -20,6 +20,7 @@ add_task(function*() {
yield openFindBarAndWait();
gFindBar._findField.value = SEARCH_TEXT;
yield findAgainAndWait();
var matchCase = gFindBar.getElement("find-case-sensitive");
if (matchCase.checked)
matchCase.doCommand();
@ -118,6 +119,21 @@ function toggleHighlightAndWait(shouldHighlight) {
});
}
function findAgainAndWait() {
return new Promise(resolve => {
let listener = {
onFindResult() {
gFindBar.browser.finder.removeResultListener(listener);
resolve();
},
onHighlightFinished() {},
onMatchesCountResult() {}
};
gFindBar.browser.finder.addResultListener(listener);
gFindBar.onFindAgainCommand();
});
}
function* openFindBarAndWait() {
let awaitTransitionEnd = BrowserTestUtils.waitForEvent(gFindBar, "transitionend");
gFindBar.open();

View File

@ -64,7 +64,7 @@
// Make sure the findfield is correctly focused on open
var searchStr = "text inside an input element";
yield* enterStringIntoFindField(searchStr);
yield promiseEnterStringIntoFindField(searchStr);
is(document.commandDispatcher.focusedElement,
gFindBar._findField.inputField, "Find field isn't focused");
@ -72,12 +72,6 @@
// when the find bar is closed.
gFindBar.close();
gFindBar.onFindAgainCommand(false);
// For remote browsers, the content document DOM tree is not accessible, thus
// the focused element should fall back to the browser element.
if (gBrowser.hasAttribute("remote")) {
is(document.commandDispatcher.focusedElement, gBrowser,
"Browser element isn't focused");
}
yield ContentTask.spawn(gBrowser, null, function* () {
Assert.equal(content.document.activeElement,
content.document.getElementById("input"), "Input Element isn't focused");
@ -93,23 +87,30 @@
"Focus was stolen from a chrome element");
}
function* enterStringIntoFindField(aString) {
for (let i = 0; i < aString.length; i++) {
let event = document.createEvent("KeyEvents");
let promise = new Promise(resolve => {
let listener = {
onFindResult: function() {
gFindBar.browser.finder.removeResultListener(listener);
resolve();
function promiseFindResult(str = null) {
return new Promise(resolve => {
let listener = {
onFindResult: function({ searchString }) {
if (str !== null && str != searchString) {
return;
}
};
gFindBar.browser.finder.addResultListener(listener);
});
gFindBar.browser.finder.removeResultListener(listener);
resolve();
}
};
gFindBar.browser.finder.addResultListener(listener);
});
}
function promiseEnterStringIntoFindField(str) {
let promise = promiseFindResult(str);
for (let i = 0; i < str.length; i++) {
let event = document.createEvent("KeyEvents");
event.initKeyEvent("keypress", true, true, null, false, false,
false, false, 0, aString.charCodeAt(i));
false, false, 0, str.charCodeAt(i));
gFindBar._findField.inputField.dispatchEvent(event);
yield promise;
}
return promise;
}
]]></script>
<textbox id="textbox"/>

View File

@ -26,6 +26,8 @@ support-files =
[browser_Deprecated.js]
[browser_Finder.js]
[browser_Finder_hidden_textarea.js]
[browser_FinderHighlighter.js]
skip-if = debug
[browser_Geometry.js]
[browser_InlineSpellChecker.js]
[browser_WebNavigation.js]

View File

@ -0,0 +1,200 @@
"use strict";
Cu.import("resource://testing-common/BrowserTestUtils.jsm", this);
Cu.import("resource://testing-common/ContentTask.jsm", this);
Cu.import("resource://gre/modules/Promise.jsm", this);
Cu.import("resource://gre/modules/Task.jsm", this);
Cu.import("resource://gre/modules/AppConstants.jsm");
const kPrefModalHighlight = "findbar.modalHighlight";
function promiseOpenFindbar(findbar) {
findbar.onFindCommand()
return gFindBar._startFindDeferred && gFindBar._startFindDeferred.promise;
}
function promiseFindResult(findbar, str = null) {
let highlightFinished = false;
let findFinished = false;
return new Promise(resolve => {
let listener = {
onFindResult({ searchString }) {
if (str !== null && str != searchString) {
return;
}
findFinished = true;
if (highlightFinished) {
findbar.browser.finder.removeResultListener(listener);
resolve();
}
},
onHighlightFinished() {
highlightFinished = true;
if (findFinished) {
findbar.browser.finder.removeResultListener(listener);
resolve();
}
}
};
findbar.browser.finder.addResultListener(listener);
});
}
function promiseEnterStringIntoFindField(findbar, str) {
let promise = promiseFindResult(findbar, str);
for (let i = 0; i < str.length; i++) {
let event = document.createEvent("KeyEvents");
event.initKeyEvent("keypress", true, true, null, false, false,
false, false, 0, str.charCodeAt(i));
findbar._findField.inputField.dispatchEvent(event);
}
return promise;
}
function promiseTestHighlighterOutput(browser, word, expectedResult) {
return ContentTask.spawn(browser, { word, expectedResult }, function* ({ word, expectedResult }) {
let document = content.document;
return new Promise((resolve, reject) => {
let stubbed = [document.insertAnonymousContent,
document.removeAnonymousContent];
let callCounts = {
insertCalls: [],
removeCalls: []
};
// Amount of milliseconds to wait after the last time one of our stubs
// was called.
const kTimeoutMs = 1000;
// The initial timeout may wait for a while for results to come in.
let timeout = content.setTimeout(finish, kTimeoutMs * 4);
function finish(ok = true, message) {
// Restore the functions we stubbed out.
document.insertAnonymousContent = stubbed[0];
document.removeAnonymousContent = stubbed[1];
content.clearTimeout(timeout);
Assert.equal(callCounts.insertCalls.length, expectedResult.insertCalls,
`Insert calls should match for '${word}'.`);
Assert.equal(callCounts.removeCalls.length, expectedResult.removeCalls,
`Remove calls should match for '${word}'.`);
// We reached the amount of calls we expected, so now we can check
// the amount of rects.
let lastSVGNode = callCounts.insertCalls.pop();
if (!lastSVGNode && expectedResult.rectCount !== 0) {
Assert.ok(false, `No SVG node found, but expected ${expectedResult.rectCount} rects.`);
}
if (lastSVGNode) {
Assert.equal(lastSVGNode.getElementsByTagName("mask")[0]
.getElementsByTagName("rect").length, expectedResult.rectCount,
`Amount of inserted rects should match for '${word}'.`);
}
resolve();
}
// Create a function that will stub the original version and collects
// the arguments so we can check the results later.
function stub(which) {
let prop = which + "Calls";
return function(node) {
callCounts[prop].push(node);
content.clearTimeout(timeout);
timeout = content.setTimeout(finish, kTimeoutMs);
return node;
};
}
document.insertAnonymousContent = stub("insert");
document.removeAnonymousContent = stub("remove");
});
});
}
add_task(function* setup() {
yield SpecialPowers.pushPrefEnv({ set: [
["findbar.highlightAll", true],
["findbar.modalHighlight", true]
]});
});
// Test the results of modal highlighting, which is on by default.
add_task(function* testModalResults() {
let tests = new Map([
["mo", {
rectCount: 5,
insertCalls: 2,
removeCalls: AppConstants.platform == "linux" ? 1 : 2
}],
["m", {
rectCount: 9,
insertCalls: 1,
removeCalls: 1
}],
["new", {
rectCount: 2,
insertCalls: 1,
removeCalls: 1
}],
["o", {
rectCount: 1218,
insertCalls: 1,
removeCalls: 1
}]
]);
yield BrowserTestUtils.withNewTab("about:mozilla", function* (browser) {
// We're inserting 1200 additional o's at the end of the document.
yield ContentTask.spawn(browser, null, function* () {
let document = content.document;
document.getElementsByTagName("section")[0].innerHTML += "<p>" +
(new Array(1200).join(" o ")) + "</p>";
});
let findbar = gBrowser.getFindBar();
for (let [word, expectedResult] of tests) {
yield promiseOpenFindbar(findbar);
Assert.ok(!findbar.hidden, "Findbar should be open now.");
let promise = promiseTestHighlighterOutput(browser, word, expectedResult);
yield promiseEnterStringIntoFindField(findbar, word);
yield promise;
findbar.close();
}
});
});
// Test if runtime switching of highlight modes between modal and non-modal works
// as expected.
add_task(function* testModalSwitching() {
yield BrowserTestUtils.withNewTab("about:mozilla", function* (browser) {
let findbar = gBrowser.getFindBar();
yield promiseOpenFindbar(findbar);
Assert.ok(!findbar.hidden, "Findbar should be open now.");
let word = "mo";
let expectedResult = {
rectCount: 5,
insertCalls: 2,
removeCalls: AppConstants.platform == "linux" ? 1 : 2
};
let promise = promiseTestHighlighterOutput(browser, word, expectedResult);
yield promiseEnterStringIntoFindField(findbar, word);
yield promise;
yield SpecialPowers.pushPrefEnv({ "set": [[ kPrefModalHighlight, false ]] });
expectedResult = {
rectCount: 0,
insertCalls: 0,
removeCalls: 0
};
promise = promiseTestHighlighterOutput(browser, word, expectedResult);
findbar.clear();
yield promiseEnterStringIntoFindField(findbar, word);
yield promise;
});
});