diff --git a/browser/components/textrecognition/moz.build b/browser/components/textrecognition/moz.build index 08f60c31d3e10ec54d126ed1516bc2ec8426beb9..563f347905f8672ef91dc79f1a83ecfd4a8396c4 100644 --- a/browser/components/textrecognition/moz.build +++ b/browser/components/textrecognition/moz.build @@ -8,3 +8,7 @@ JAR_MANIFESTS += ["jar.mn"] with Files("**"): BUG_COMPONENT = ("Firefox", "General") + +BROWSER_CHROME_MANIFESTS += [ + "tests/browser/browser.ini", +] diff --git a/browser/components/textrecognition/tests/browser/browser.ini b/browser/components/textrecognition/tests/browser/browser.ini new file mode 100644 index 0000000000000000000000000000000000000000..66a00efcc0c50160472cfe3c117d8e40b4e5c385 --- /dev/null +++ b/browser/components/textrecognition/tests/browser/browser.ini @@ -0,0 +1,10 @@ +[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. diff --git a/browser/components/textrecognition/tests/browser/browser_textrecognition.js b/browser/components/textrecognition/tests/browser/browser_textrecognition.js new file mode 100644 index 0000000000000000000000000000000000000000..10651b02e53b4c066acf7a4c7e499caa81b261bc --- /dev/null +++ b/browser/components/textrecognition/tests/browser/browser_textrecognition.js @@ -0,0 +1,79 @@ +/* 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(""); + }); +}); diff --git a/browser/components/textrecognition/tests/browser/browser_textrecognition_no_result.js b/browser/components/textrecognition/tests/browser/browser_textrecognition_no_result.js new file mode 100644 index 0000000000000000000000000000000000000000..fd5cbe6f6ee489a3fd2684fb00cdb83a14eb5589 --- /dev/null +++ b/browser/components/textrecognition/tests/browser/browser_textrecognition_no_result.js @@ -0,0 +1,68 @@ +/* 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."); + }); +}); diff --git a/browser/components/textrecognition/tests/browser/head.js b/browser/components/textrecognition/tests/browser/head.js new file mode 100644 index 0000000000000000000000000000000000000000..08b002f42f7a454841035b386505b08f01d170d1 --- /dev/null +++ b/browser/components/textrecognition/tests/browser/head.js @@ -0,0 +1,28 @@ +/* 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 ?? ""; +} diff --git a/browser/components/textrecognition/tests/browser/image.png b/browser/components/textrecognition/tests/browser/image.png new file mode 100644 index 0000000000000000000000000000000000000000..3faa11b221f2648cb7471af721581a8d1ca5201d Binary files /dev/null and b/browser/components/textrecognition/tests/browser/image.png differ