Source code
Revision control
Copy as Markdown
Other Tools
<?xml version="1.0"?>
<!-- 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
<window id="451540test"
width="600"
height="600"
title="451540 test">
<script type="application/javascript"><![CDATA[
const {BrowserTestUtils} = ChromeUtils.importESModule(
);
const SEARCH_TEXT = "minefield";
let gFindBar = null;
let gBrowser;
var SimpleTest = window.arguments[0].SimpleTest;
var ok = window.arguments[0].ok;
var is = window.arguments[0].is;
var info = window.arguments[0].info;
SimpleTest.requestLongerTimeout(2);
function startTest() {
gFindBar = document.getElementById("FindToolbar");
gBrowser = document.getElementById("content");
gBrowser.addEventListener("pageshow", onPageShow, { once: true });
let data = `data:text/html,<input id="inp" type="text" />
<textarea id="tarea"/>`;
BrowserTestUtils.startLoadingURIString(gBrowser, data);
}
function promiseHighlightFinished() {
return new Promise(resolve => {
let listener = {
onHighlightFinished() {
gFindBar.browser.finder.removeResultListener(listener);
resolve();
}
};
gFindBar.browser.finder.addResultListener(listener);
});
}
async function resetForNextTest(elementId, aText) {
if (!aText)
aText = SEARCH_TEXT;
// Turn off highlighting
let highlightButton = gFindBar.getElement("highlight");
if (highlightButton.checked) {
highlightButton.click();
}
// Initialise input
info(`setting element value to ${aText}`);
await SpecialPowers.spawn(gBrowser, [{elementId, aText}], async function(args) {
let {elementId, aText} = args;
let doc = content.document;
let element = doc.getElementById(elementId);
element.value = aText;
element.focus();
});
info(`just set element value to ${aText}`);
gFindBar._findField.value = SEARCH_TEXT;
// Perform search and turn on highlighting
gFindBar._find();
highlightButton.click();
await promiseHighlightFinished();
// Move caret to start of element
info(`focusing element`);
await SpecialPowers.spawn(gBrowser, [elementId], async function(elementId) {
let doc = content.document;
let element = doc.getElementById(elementId);
element.focus();
});
info(`focused element`);
if (navigator.platform.includes("Mac")) {
await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", { metaKey: true }, gBrowser);
} else {
await BrowserTestUtils.synthesizeKey("KEY_Home", {}, gBrowser);
}
}
async function testSelection(elementId, expectedRangeCount, message) {
await SpecialPowers.spawn(gBrowser, [{elementId, expectedRangeCount, message}], async function(args) {
let {elementId, expectedRangeCount, message} = args;
let doc = content.document;
let element = doc.getElementById(elementId);
let controller = element.editor.selectionController;
let selection = controller.getSelection(controller.SELECTION_FIND);
Assert.equal(selection.rangeCount, expectedRangeCount, message);
});
}
async function testInput(elementId, testTypeText) {
let isEditableElement = await SpecialPowers.spawn(gBrowser, [elementId], async function(elementId) {
let doc = content.document;
let element = doc.getElementById(elementId);
let elementClass = ChromeUtils.getClassName(element);
return elementClass === "HTMLInputElement" ||
elementClass === "HTMLTextAreaElement";
});
if (!isEditableElement) {
return;
}
let moveCaretToNextWordBoundary = async (aBrowser) => {
if (!navigator.platform.includes("Mac")) {
return BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { accelKey: true }, aBrowser);
}
// macOS does not have default shortcut key to move caret per word.
return SpecialPowers.spawn(aBrowser, [], async () => {
content.docShell.doCommand("cmd_wordNext");
});
};
// Initialize the findbar
let matchCase = gFindBar.getElement("find-case-sensitive");
if (matchCase.checked) {
matchCase.doCommand();
}
// First check match has been correctly highlighted
await resetForNextTest(elementId);
await testSelection(elementId, 1, testTypeText + " correctly highlighted match");
// Test 2: check highlight removed when text added within the highlight
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);
await testSelection(elementId, 0, testTypeText + " correctly removed highlight on text insertion");
// Test 3: check highlighting remains when text added before highlight
await resetForNextTest(elementId);
await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);
await testSelection(elementId, 1, testTypeText + " highlight correctly remained on text insertion at start");
// Test 4: check highlighting remains when text added after highlight
await resetForNextTest(elementId);
for (let x = 0; x < SEARCH_TEXT.length; x++) {
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
}
await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);
await testSelection(elementId, 1, testTypeText + " highlight correctly remained on text insertion at end");
// Test 5: deleting text within the highlight
await resetForNextTest(elementId);
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
await testSelection(elementId, 0, testTypeText + " correctly removed highlight on text deletion");
// Test 6: deleting text at end of highlight
await resetForNextTest(elementId, SEARCH_TEXT + "A");
for (let x = 0; x < (SEARCH_TEXT + "A").length; x++) {
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
}
await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
await testSelection(elementId, 1, testTypeText + " highlight correctly remained on text deletion at end");
// Test 7: deleting text at start of highlight
await resetForNextTest(elementId, "A" + SEARCH_TEXT);
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
await testSelection(elementId, 1, testTypeText + " highlight correctly remained on text deletion at start");
// Test 8: deleting selection
await resetForNextTest(elementId);
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, gBrowser);
await BrowserTestUtils.synthesizeKey("x", { accelKey: true }, gBrowser);
await testSelection(elementId, 0, testTypeText + " correctly removed highlight on selection deletion");
// Test 9: Multiple matches within one editor (part 1)
// Check second match remains highlighted after inserting text into
// first match, and that its highlighting gets removed when the
// second match is edited
await resetForNextTest(elementId, SEARCH_TEXT + " " + SEARCH_TEXT);
await testSelection(elementId, 2, testTypeText + " correctly highlighted both matches");
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);
await testSelection(elementId, 1, testTypeText + " correctly removed only the first highlight on text insertion");
await moveCaretToNextWordBoundary(gBrowser);
await moveCaretToNextWordBoundary(gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", {}, gBrowser);
await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);
await testSelection(elementId, 0, testTypeText + " correctly removed second highlight on text insertion");
// Test 10: Multiple matches within one editor (part 2)
// Check second match remains highlighted after deleting text in
// first match, and that its highlighting gets removed when the
// second match is edited
await resetForNextTest(elementId, SEARCH_TEXT + " " + SEARCH_TEXT);
await testSelection(elementId, 2, testTypeText + " correctly highlighted both matches");
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
await testSelection(elementId, 1, testTypeText + " correctly removed only the first highlight on text deletion");
await moveCaretToNextWordBoundary(gBrowser);
await moveCaretToNextWordBoundary(gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", {}, gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
await testSelection(elementId, 0, testTypeText + " correctly removed second highlight on text deletion");
// Test 11: Multiple matches within one editor (part 3)
// Check second match remains highlighted after deleting selection
// in first match, and that second match highlighting gets correctly
// removed when it has a selection deleted from it
await resetForNextTest(elementId, SEARCH_TEXT + " " + SEARCH_TEXT);
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, gBrowser);
await BrowserTestUtils.synthesizeKey("x", { accelKey: true }, gBrowser);
await testSelection(elementId, 1, testTypeText + " correctly removed only first highlight on selection deletion");
await moveCaretToNextWordBoundary(gBrowser);
await moveCaretToNextWordBoundary(gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, gBrowser);
await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, gBrowser);
await BrowserTestUtils.synthesizeKey("x", { accelKey: true }, gBrowser);
await testSelection(elementId, 0, testTypeText + " correctly removed second highlight on selection deletion");
// Turn off highlighting
let highlightButton = gFindBar.getElement("highlight");
if (highlightButton.checked) {
highlightButton.click();
}
}
function onPageShow() {
(async function() {
gFindBar.open();
await testInput("inp", "Input:");
await testInput("tarea", "Textarea:");
})().then(() => {
window.close();
SimpleTest.finish();
});
}
SimpleTest.waitForFocus(startTest, window);
]]></script>
<browser type="content" primary="true" flex="1" id="content" messagemanagergroup="test" src="about:blank"/>
<browser type="content" primary="true" flex="1" id="content-remote" remote="true" messagemanagergroup="test" src="about:blank"/>
<findbar id="FindToolbar" browserid="content"/>
</window>