Skip to content
Snippets Groups Projects
Commit 57b038ea authored by Greg Tatum's avatar Greg Tatum
Browse files

Bug 1782578 - Add tests for the text recognition modal r=nordzilla

parent b218d4bc
No related branches found
No related tags found
No related merge requests found
......@@ -8,3 +8,7 @@ JAR_MANIFESTS += ["jar.mn"]
with Files("**"):
BUG_COMPONENT = ("Firefox", "General")
BROWSER_CHROME_MANIFESTS += [
"tests/browser/browser.ini",
]
[DEFAULT]
support-files =
head.js
image.png
!/toolkit/content/tests/browser/doggy.png
[browser_textrecognition.js]
run-if = os == "mac" # Mac only feature.
[browser_textrecognition_no_result.js]
run-if = os == "mac" # Mac only feature.
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
add_task(async function() {
const URL_IMG =
"http://mochi.test:8888/browser/browser/components/textrecognition/tests/browser/image.png";
await SpecialPowers.pushPrefEnv({
set: [["dom.text-recognition.enabled", true]],
});
await BrowserTestUtils.withNewTab(URL_IMG, async function(browser) {
setClipboardText("");
is(getTextFromClipboard(), "", "The copied text is empty.");
info("Right click image to show context menu.");
let popupShownPromise = BrowserTestUtils.waitForEvent(
document,
"popupshown"
);
await BrowserTestUtils.synthesizeMouseAtCenter(
"img",
{ type: "contextmenu", button: 2 },
browser
);
await popupShownPromise;
info("Click context menu to copy the image text.");
document.getElementById("context-imagetext").doCommand();
info("Close the context menu.");
let contextMenu = document.getElementById("contentAreaContextMenu");
let popupHiddenPromise = BrowserTestUtils.waitForEvent(
contextMenu,
"popuphidden"
);
contextMenu.hidePopup();
await popupHiddenPromise;
info("Waiting for the dialog browser to be shown.");
const { contentDocument } = await BrowserTestUtils.waitForCondition(() =>
document.querySelector(".textRecognitionDialogFrame")
);
info("Waiting for text results.");
const resultsHeader = contentDocument.querySelector(
"#text-recognition-header-results"
);
await BrowserTestUtils.waitForCondition(() => {
return resultsHeader.style.display !== "none";
});
info("Checking the text results.");
const text = contentDocument.querySelector(".textRecognitionText");
is(text.children.length, 2, "Two piece of text were found");
const [p1, p2] = text.children;
is(p1.tagName, "P", "The children are paragraph tags.");
is(p2.tagName, "P", "The children are paragraph tags.");
is(p1.innerText, "Mozilla\n", "The first piece of text matches.");
is(p2.innerText, "Firefox\n", "The second piece of text matches.");
info("Close the dialog box.");
const close = contentDocument.querySelector("#text-recognition-close");
close.click();
is(
getTextFromClipboard(),
"Mozilla\nFirefox\n",
"The copied text matches."
);
info("Waiting for the dialog frame to close.");
await BrowserTestUtils.waitForCondition(
() => !document.querySelector(".textRecognitionDialogFrame")
);
setClipboardText("");
});
});
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
add_task(async function() {
const url =
"http://mochi.test:8888/browser/toolkit/content/tests/browser/doggy.png";
await SpecialPowers.pushPrefEnv({
set: [["dom.text-recognition.enabled", true]],
});
await BrowserTestUtils.withNewTab(url, async function(browser) {
setClipboardText("");
is(getTextFromClipboard(), "", "The copied text is empty.");
info("Right click image to show context menu.");
let popupShownPromise = BrowserTestUtils.waitForEvent(
document,
"popupshown"
);
await BrowserTestUtils.synthesizeMouseAtCenter(
"img",
{ type: "contextmenu", button: 2 },
browser
);
await popupShownPromise;
info("Click context menu to copy the image text.");
document.getElementById("context-imagetext").doCommand();
info("Close the context menu.");
let contextMenu = document.getElementById("contentAreaContextMenu");
let popupHiddenPromise = BrowserTestUtils.waitForEvent(
contextMenu,
"popuphidden"
);
contextMenu.hidePopup();
await popupHiddenPromise;
info("Waiting for the dialog browser to be shown.");
const { contentDocument } = await BrowserTestUtils.waitForCondition(() =>
document.querySelector(".textRecognitionDialogFrame")
);
info("Waiting for no results message.");
const noResultsHeader = contentDocument.querySelector(
"#text-recognition-header-no-results"
);
await BrowserTestUtils.waitForCondition(() => {
return noResultsHeader.style.display !== "none";
});
const text = contentDocument.querySelector(".textRecognitionText");
is(text.children.length, 0, "No results are listed.");
info("Close the dialog box.");
const close = contentDocument.querySelector("#text-recognition-close");
close.click();
info("Waiting for the dialog frame to close.");
await BrowserTestUtils.waitForCondition(
() => !document.querySelector(".textRecognitionDialogFrame")
);
is(getTextFromClipboard(), "", "The copied text is still empty.");
});
});
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* @param {string} text
*/
function setClipboardText(text) {
const ClipboardHelper = Cc[
"@mozilla.org/widget/clipboardhelper;1"
].getService(Ci.nsIClipboardHelper);
ClipboardHelper.copyString(text);
}
/**
* @returns {string}
*/
function getTextFromClipboard() {
const transferable = Cc["@mozilla.org/widget/transferable;1"].createInstance(
Ci.nsITransferable
);
transferable.init(window.docShell.QueryInterface(Ci.nsILoadContext));
transferable.addDataFlavor("text/unicode");
Services.clipboard.getData(transferable, Services.clipboard.kGlobalClipboard);
const results = {};
transferable.getTransferData("text/unicode", results);
return results.value.QueryInterface(Ci.nsISupportsString)?.data ?? "";
}
browser/components/textrecognition/tests/browser/image.png

6.9 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment