summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pdfjs/test/browser_pdfjs_fill_login.js
blob: b31b9b607c65a6649d551bae01c446208d653084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;

// This is a modified version from browser_contextmenuFillLogins.js.
async function openContextMenuForSelector(browser, selector) {
  const doc = browser.ownerDocument;
  const CONTEXT_MENU = doc.getElementById("contentAreaContextMenu");

  let contextMenuShownPromise = BrowserTestUtils.waitForEvent(
    CONTEXT_MENU,
    "popupshown"
  );

  let inputCoords = await SpecialPowers.spawn(
    browser,
    [selector],
    async selector => {
      let input = content.document.querySelector(selector);
      input.focus();
      let inputRect = input.getBoundingClientRect();

      // listen for the contextmenu event so we can assert on receiving it
      // and examine the target
      content.contextmenuPromise = new Promise(resolve => {
        content.document.body.addEventListener(
          "contextmenu",
          event => {
            info(
              `Received event on target: ${event.target.nodeName}, type: ${event.target.type}`
            );
            content.console.log("got contextmenu event: ", event);
            resolve(event);
          },
          { once: true }
        );
      });

      let coords = {
        x: inputRect.x + inputRect.width / 2,
        y: inputRect.y + inputRect.height / 2,
      };
      return coords;
    }
  );

  // add the offsets of the <browser> in the chrome window
  let browserOffsets = browser.getBoundingClientRect();
  let offsetX = browserOffsets.x + inputCoords.x;
  let offsetY = browserOffsets.y + inputCoords.y;

  // Synthesize a right mouse click over the input element, we have to trigger
  // both events because formfill code relies on this event happening before the contextmenu
  // (which it does for real user input) in order to not show the password autocomplete.
  let eventDetails = { type: "mousedown", button: 2 };
  await EventUtils.synthesizeMouseAtPoint(offsetX, offsetY, eventDetails);

  // Synthesize a contextmenu event to actually open the context menu.
  eventDetails = { type: "contextmenu", button: 2 };
  await EventUtils.synthesizeMouseAtPoint(offsetX, offsetY, eventDetails);

  await SpecialPowers.spawn(browser, [], async () => {
    await content.contextmenuPromise;
  });

  info("waiting for contextMenuShownPromise");

  await contextMenuShownPromise;
  return CONTEXT_MENU;
}

/**
 * Ensure the fill login context menu is not shown for PDF.js forms.
 */
add_task(async function test_filllogin() {
  await SpecialPowers.pushPrefEnv({
    set: [["pdfjs.renderInteractiveForms", true]],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:blank" },
    async function (browser) {
      await waitForPdfJSAnnotationLayer(
        browser,
        TESTROOT + "file_pdfjs_form.pdf"
      );

      let contextMenu = await openContextMenuForSelector(
        browser,
        "#viewerContainer input"
      );
      let fillItem = contextMenu.querySelector("#fill-login");
      ok(fillItem, "fill menu item exists");
      ok(fillItem && EventUtils.isHidden(fillItem), "fill menu item is hidden");

      let promiseHidden = BrowserTestUtils.waitForEvent(
        contextMenu,
        "popuphidden"
      );
      info("Calling hidePopup on contextMenu");
      contextMenu.hidePopup();
      info("waiting for promiseHidden");
      await promiseHidden;
    }
  );
});