summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_focus_before_first_DOMContentLoaded.js
blob: 783f8ea3d874433f432f52f2799c7fadf3a0afdf (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
/**
 * Test that autocomplete is properly attached to a username field which gets
 * focused before DOMContentLoaded in a new browser and process.
 */

"use strict";

add_setup(async () => {
  let nsLoginInfo = Components.Constructor(
    "@mozilla.org/login-manager/loginInfo;1",
    Ci.nsILoginInfo,
    "init"
  );
  Assert.ok(nsLoginInfo != null, "nsLoginInfo constructor");

  info("Adding two logins to get autocomplete instead of autofill");
  let login1 = new nsLoginInfo(
    "https://example.com",
    "https://autocomplete:8888",
    null,
    "tempuser1",
    "temppass1"
  );

  let login2 = new nsLoginInfo(
    "https://example.com",
    "https://autocomplete:8888",
    null,
    "testuser2",
    "testpass2"
  );

  await Services.logins.addLogins([login1, login2]);
});

add_task(async function test_autocompleteFromUsername() {
  let autocompletePopup = document.getElementById("PopupAutoComplete");
  let autocompletePopupShown = BrowserTestUtils.waitForEvent(
    autocompletePopup,
    "popupshown"
  );

  const URL = `https://example.com${DIRECTORY_PATH}file_focus_before_DOMContentLoaded.sjs`;

  let newTab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: URL,
    forceNewProcess: true,
  });

  await SpecialPowers.spawn(
    newTab.linkedBrowser,
    [],
    function checkInitialValues() {
      let doc = content.document;
      let uname = doc.querySelector("#uname");
      let pword = doc.querySelector("#pword");

      Assert.ok(uname, "Username field found");
      Assert.ok(pword, "Password field found");

      Assert.equal(
        doc.activeElement,
        uname,
        "#uname element should be focused"
      );
      Assert.equal(uname.value, "", "Checking username is empty");
      Assert.equal(pword.value, "", "Checking password is empty");
    }
  );

  await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, newTab.linkedBrowser);
  await autocompletePopupShown;

  let richlistbox = autocompletePopup.richlistbox;
  Assert.equal(
    richlistbox.localName,
    "richlistbox",
    "The richlistbox should be the first anonymous node"
  );
  for (let i = 0; i < autocompletePopup.view.matchCount; i++) {
    if (
      richlistbox.selectedItem &&
      richlistbox.selectedItem.textContent.includes("tempuser1")
    ) {
      break;
    }
    await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, newTab.linkedBrowser);
  }

  await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, newTab.linkedBrowser);

  await SpecialPowers.spawn(newTab.linkedBrowser, [], function checkFill() {
    let doc = content.document;
    let uname = doc.querySelector("#uname");
    let pword = doc.querySelector("#pword");

    Assert.equal(uname.value, "tempuser1", "Checking username is filled");
    Assert.equal(pword.value, "temppass1", "Checking password is filled");
  });

  BrowserTestUtils.removeTab(newTab);
});