Skip to content

Commit c027638

Browse files
committed
Bug 1782578 - Add tests for the text recognition modal r=nordzilla
Differential Revision: https://phabricator.services.mozilla.com/D153397
1 parent 9e7bde3 commit c027638

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

browser/components/textrecognition/moz.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ JAR_MANIFESTS += ["jar.mn"]
88

99
with Files("**"):
1010
BUG_COMPONENT = ("Firefox", "General")
11+
12+
BROWSER_CHROME_MANIFESTS += [
13+
"tests/browser/browser.ini",
14+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[DEFAULT]
2+
support-files =
3+
head.js
4+
image.png
5+
!/toolkit/content/tests/browser/doggy.png
6+
7+
[browser_textrecognition.js]
8+
run-if = os == "mac" # Mac only feature.
9+
[browser_textrecognition_no_result.js]
10+
run-if = os == "mac" # Mac only feature.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Any copyright is dedicated to the Public Domain.
2+
http://creativecommons.org/publicdomain/zero/1.0/ */
3+
"use strict";
4+
5+
add_task(async function() {
6+
const URL_IMG =
7+
"http://mochi.test:8888/browser/browser/components/textrecognition/tests/browser/image.png";
8+
9+
await SpecialPowers.pushPrefEnv({
10+
set: [["dom.text-recognition.enabled", true]],
11+
});
12+
13+
await BrowserTestUtils.withNewTab(URL_IMG, async function(browser) {
14+
setClipboardText("");
15+
is(getTextFromClipboard(), "", "The copied text is empty.");
16+
17+
info("Right click image to show context menu.");
18+
let popupShownPromise = BrowserTestUtils.waitForEvent(
19+
document,
20+
"popupshown"
21+
);
22+
await BrowserTestUtils.synthesizeMouseAtCenter(
23+
"img",
24+
{ type: "contextmenu", button: 2 },
25+
browser
26+
);
27+
await popupShownPromise;
28+
29+
info("Click context menu to copy the image text.");
30+
document.getElementById("context-imagetext").doCommand();
31+
32+
info("Close the context menu.");
33+
let contextMenu = document.getElementById("contentAreaContextMenu");
34+
let popupHiddenPromise = BrowserTestUtils.waitForEvent(
35+
contextMenu,
36+
"popuphidden"
37+
);
38+
contextMenu.hidePopup();
39+
await popupHiddenPromise;
40+
41+
info("Waiting for the dialog browser to be shown.");
42+
const { contentDocument } = await BrowserTestUtils.waitForCondition(() =>
43+
document.querySelector(".textRecognitionDialogFrame")
44+
);
45+
46+
info("Waiting for text results.");
47+
const resultsHeader = contentDocument.querySelector(
48+
"#text-recognition-header-results"
49+
);
50+
await BrowserTestUtils.waitForCondition(() => {
51+
return resultsHeader.style.display !== "none";
52+
});
53+
54+
info("Checking the text results.");
55+
const text = contentDocument.querySelector(".textRecognitionText");
56+
is(text.children.length, 2, "Two piece of text were found");
57+
const [p1, p2] = text.children;
58+
is(p1.tagName, "P", "The children are paragraph tags.");
59+
is(p2.tagName, "P", "The children are paragraph tags.");
60+
is(p1.innerText, "Mozilla\n", "The first piece of text matches.");
61+
is(p2.innerText, "Firefox\n", "The second piece of text matches.");
62+
63+
info("Close the dialog box.");
64+
const close = contentDocument.querySelector("#text-recognition-close");
65+
close.click();
66+
67+
is(
68+
getTextFromClipboard(),
69+
"Mozilla\nFirefox\n",
70+
"The copied text matches."
71+
);
72+
73+
info("Waiting for the dialog frame to close.");
74+
await BrowserTestUtils.waitForCondition(
75+
() => !document.querySelector(".textRecognitionDialogFrame")
76+
);
77+
setClipboardText("");
78+
});
79+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Any copyright is dedicated to the Public Domain.
2+
http://creativecommons.org/publicdomain/zero/1.0/ */
3+
"use strict";
4+
5+
add_task(async function() {
6+
const url =
7+
"http://mochi.test:8888/browser/toolkit/content/tests/browser/doggy.png";
8+
9+
await SpecialPowers.pushPrefEnv({
10+
set: [["dom.text-recognition.enabled", true]],
11+
});
12+
13+
await BrowserTestUtils.withNewTab(url, async function(browser) {
14+
setClipboardText("");
15+
is(getTextFromClipboard(), "", "The copied text is empty.");
16+
17+
info("Right click image to show context menu.");
18+
let popupShownPromise = BrowserTestUtils.waitForEvent(
19+
document,
20+
"popupshown"
21+
);
22+
await BrowserTestUtils.synthesizeMouseAtCenter(
23+
"img",
24+
{ type: "contextmenu", button: 2 },
25+
browser
26+
);
27+
await popupShownPromise;
28+
29+
info("Click context menu to copy the image text.");
30+
document.getElementById("context-imagetext").doCommand();
31+
32+
info("Close the context menu.");
33+
let contextMenu = document.getElementById("contentAreaContextMenu");
34+
let popupHiddenPromise = BrowserTestUtils.waitForEvent(
35+
contextMenu,
36+
"popuphidden"
37+
);
38+
contextMenu.hidePopup();
39+
await popupHiddenPromise;
40+
41+
info("Waiting for the dialog browser to be shown.");
42+
const { contentDocument } = await BrowserTestUtils.waitForCondition(() =>
43+
document.querySelector(".textRecognitionDialogFrame")
44+
);
45+
46+
info("Waiting for no results message.");
47+
const noResultsHeader = contentDocument.querySelector(
48+
"#text-recognition-header-no-results"
49+
);
50+
await BrowserTestUtils.waitForCondition(() => {
51+
return noResultsHeader.style.display !== "none";
52+
});
53+
54+
const text = contentDocument.querySelector(".textRecognitionText");
55+
is(text.children.length, 0, "No results are listed.");
56+
57+
info("Close the dialog box.");
58+
const close = contentDocument.querySelector("#text-recognition-close");
59+
close.click();
60+
61+
info("Waiting for the dialog frame to close.");
62+
await BrowserTestUtils.waitForCondition(
63+
() => !document.querySelector(".textRecognitionDialogFrame")
64+
);
65+
66+
is(getTextFromClipboard(), "", "The copied text is still empty.");
67+
});
68+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Any copyright is dedicated to the Public Domain.
2+
http://creativecommons.org/publicdomain/zero/1.0/ */
3+
4+
/**
5+
* @param {string} text
6+
*/
7+
function setClipboardText(text) {
8+
const ClipboardHelper = Cc[
9+
"@mozilla.org/widget/clipboardhelper;1"
10+
].getService(Ci.nsIClipboardHelper);
11+
ClipboardHelper.copyString(text);
12+
}
13+
14+
/**
15+
* @returns {string}
16+
*/
17+
function getTextFromClipboard() {
18+
const transferable = Cc["@mozilla.org/widget/transferable;1"].createInstance(
19+
Ci.nsITransferable
20+
);
21+
transferable.init(window.docShell.QueryInterface(Ci.nsILoadContext));
22+
transferable.addDataFlavor("text/unicode");
23+
Services.clipboard.getData(transferable, Services.clipboard.kGlobalClipboard);
24+
25+
const results = {};
26+
transferable.getTransferData("text/unicode", results);
27+
return results.value.QueryInterface(Ci.nsISupportsString)?.data ?? "";
28+
}
Loading

0 commit comments

Comments
 (0)