summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_context_menu_autocomplete_interaction.js
blob: 8ffe07a673228fd40ae9f66f75231250543257a8 (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
109
110
111
112
113
114
115
116
117
118
119
120
/* eslint-disable mozilla/no-arbitrary-setTimeout */
/*
 * Test the password manager context menu interaction with autocomplete.
 */

"use strict";

const TEST_ORIGIN = "https://example.com";
const BASIC_FORM_PAGE_PATH = DIRECTORY_PATH + "form_basic.html";

/**
 * Initialize logins needed for the tests and disable autofill
 * for login forms for easier testing of manual fill.
 */
add_task(async function test_initialize() {
  let autocompletePopup = document.getElementById("PopupAutoComplete");
  Services.prefs.setBoolPref("signon.autofillForms", false);
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("signon.autofillForms");
    autocompletePopup.removeEventListener(
      "popupshowing",
      autocompleteUnexpectedPopupShowing
    );
  });
  await Services.logins.addLogins(loginList());
  autocompletePopup.addEventListener(
    "popupshowing",
    autocompleteUnexpectedPopupShowing
  );
});

add_task(async function test_context_menu_username() {
  let formFilled = listenForTestNotification("FormProcessed");

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + BASIC_FORM_PAGE_PATH,
    },
    async function (browser) {
      await formFilled;
      await openContextMenu(browser, "#form-basic-username");

      let contextMenu = document.getElementById("contentAreaContextMenu");
      Assert.equal(contextMenu.state, "open", "Context menu opened");
      contextMenu.hidePopup();
    }
  );
});

add_task(async function test_context_menu_password() {
  let formFilled = listenForTestNotification("FormProcessed");

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + BASIC_FORM_PAGE_PATH,
    },
    async function (browser) {
      await formFilled;
      await openContextMenu(browser, "#form-basic-password");

      let contextMenu = document.getElementById("contentAreaContextMenu");
      Assert.equal(contextMenu.state, "open", "Context menu opened");
      contextMenu.hidePopup();
    }
  );
});

function autocompleteUnexpectedPopupShowing(event) {
  Assert.ok(false, "Autocomplete shouldn't appear");
  event.target.hidePopup();
}

/**
 * Synthesize mouse clicks to open the context menu popup
 * for a target login input element.
 */
async function openContextMenu(browser, loginInput) {
  // First synthesize a mousedown. We need this to get the focus event with the "contextmenu" event.
  let eventDetails1 = { type: "mousedown", button: 2 };
  await BrowserTestUtils.synthesizeMouseAtCenter(
    loginInput,
    eventDetails1,
    browser
  );

  // Then synthesize the contextmenu click over the input element.
  let contextMenuShownPromise = BrowserTestUtils.waitForEvent(
    window,
    "popupshown"
  );
  let eventDetails = { type: "contextmenu", button: 2 };
  await BrowserTestUtils.synthesizeMouseAtCenter(
    loginInput,
    eventDetails,
    browser
  );
  await contextMenuShownPromise;

  // Wait to see which popups are shown.
  await new Promise(resolve => setTimeout(resolve, 1000));
}

function loginList() {
  return [
    LoginTestUtils.testData.formLogin({
      origin: "https://example.com",
      formActionOrigin: "https://example.com",
      username: "username",
      password: "password",
    }),
    LoginTestUtils.testData.formLogin({
      origin: "https://example.com",
      formActionOrigin: "https://example.com",
      username: "username2",
      password: "password2",
    }),
  ];
}