Commit c0276385 authored by Greg Tatum's avatar Greg Tatum
Browse files

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

parent 9e7bde31
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -8,3 +8,7 @@ JAR_MANIFESTS += ["jar.mn"]

with Files("**"):
    BUG_COMPONENT = ("Firefox", "General")

BROWSER_CHROME_MANIFESTS += [
    "tests/browser/browser.ini",
]
+10 −0
Original line number Diff line number Diff line
[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.
+79 −0
Original line number Diff line number Diff line
/* 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("");
  });
});
+68 −0
Original line number Diff line number Diff line
/* 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.");
  });
});
+28 −0
Original line number Diff line number Diff line
/* 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 ?? "";
}
Loading