summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/tests/chrome/test_login_filter.html
blob: 00e0a96a511b19e2b268207576c6fb80026cb7a6 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<!DOCTYPE HTML>
<html>
<!--
Test the login-filter component
-->
<head>
  <meta charset="utf-8">
  <title>Test the login-filter component</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <script type="module" src="chrome://browser/content/aboutlogins/components/login-filter.mjs"></script>
  <script type="module" src="chrome://browser/content/aboutlogins/components/login-list.mjs"></script>
  <script src="aboutlogins_common.js"></script>

  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body>
  <p id="display">
  </p>
<div id="content" style="display: none">
  <iframe id="templateFrame" src="chrome://browser/content/aboutlogins/aboutLogins.html"
          sandbox="allow-same-origin"></iframe>
</div>
<pre id="test">
</pre>
<script>
/** Test the login-filter component **/

let gLoginFilter;
let gLoginList;
add_setup(async () => {
  let templateFrame = document.getElementById("templateFrame");
  let displayEl = document.getElementById("display");
  importDependencies(templateFrame, displayEl);

  gLoginFilter = document.createElement("login-filter");
  displayEl.appendChild(gLoginFilter);

  gLoginList = document.createElement("login-list");
  displayEl.appendChild(gLoginList);
});

add_task(async function test_empty_filter() {
  ok(gLoginFilter, "loginFilter exists");
  is(gLoginFilter.shadowRoot.querySelector("input").value, "", "Initially empty");
});

add_task(async function test_input_events() {
  let filterEvent = null;
  window.addEventListener("AboutLoginsFilterLogins", event => filterEvent = event);
  let input = SpecialPowers.wrap(gLoginFilter.shadowRoot.querySelector("input"));
  input.setUserInput("test");
  ok(filterEvent, "Filter event received");
  is(filterEvent.detail, "test", "Event includes input value");
});

add_task(async function test_list_filtered() {
  const LOGINS = [{
    guid: "123456789",
    origin: "https://example.com",
    username: "user1",
    password: "pass1",
  }, {
    guid: "987654321",
    origin: "https://example.com",
    username: "user2",
    password: "pass2",
  }];
  gLoginList.setLogins(LOGINS);

  let tests = [
    ["", 2],
    [LOGINS[0].username, 1],
    [LOGINS[0].username + "-notfound", 0],
    [LOGINS[0].username.substr(2, 3), 1],
    ["", 2],
    // The password is also used for search when MP is disabled.
    [LOGINS[0].password, 1],
    [LOGINS[0].password + "-notfound", 0],
    [LOGINS[0].password.substr(2, 3), 1],
    ["", 2],
    [LOGINS[0].origin, 2],
    [LOGINS[0].origin + "-notfound", 0],
    [LOGINS[0].origin.substr(2, 3), 2],
    ["", 2],
    // The guid is not used for search.
    [LOGINS[0].guid, 0],
    [LOGINS[0].guid + "-notfound", 0],
    [LOGINS[0].guid.substr(0, 2), 0],
    ["", 2],
  ];

  let loginFilterInput = gLoginFilter.shadowRoot.querySelector("input");
  loginFilterInput.focus();

  for (let i = 0; i < tests.length; i++) {
    info("Testcase: " + i);

    let testObj = {
      testCase: i,
      query: tests[i][0],
      resultExpectedCount: tests[i][1],
    };

    let filterLength = loginFilterInput.value.length;
    while (filterLength-- > 0) {
      sendKey("BACK_SPACE");
    }
    sendString(testObj.query);

    await SimpleTest.promiseWaitForCondition(() => {
      let countElement = gLoginList.shadowRoot.querySelector(".count");
      return countElement.hasAttribute("data-l10n-args") &&
             JSON.parse(countElement.getAttribute("data-l10n-args")).count == testObj.resultExpectedCount;
    }, `Waiting for the search result count to update to ${testObj.resultExpectedCount} (tc#${testObj.testCase})`);
  }
});

  add_task(async function test_keys_in_filter() {
    const LOGINS = [{
      guid: "123456789",
      origin: "https://example.com",
      username: "user1",
      password: "pass1",
    }, {
      guid: "987654321",
      origin: "https://example.com",
      username: "user2",
      password: "pass2",
    }, {
      guid: "333333333",
      origin: "https://example.com",
      username: "user3",
      password: "pass3",
    }];
    gLoginList.setLogins(LOGINS);

    const ol = gLoginList.shadowRoot.querySelector("ol");
    const loginFilterInput = gLoginFilter.shadowRoot.querySelector("input");
    loginFilterInput.focus();

    // Up/down keys must select previous/next item in the list
    function pressKeyAndExpectSelection(key, selectedIndex) {
      sendKey(key);
      ok(ol.querySelectorAll(".login-list-item[data-guid]:not([hidden])")[selectedIndex]?.classList?.contains("keyboard-selected"),
        `item ${selectedIndex} should be marked as keyboard-selected`);
      is(ol.querySelector(".selected").dataset.guid, LOGINS[selectedIndex].guid, `item ${selectedIndex} must be selected`);
    }

    pressKeyAndExpectSelection("DOWN", 1);
    pressKeyAndExpectSelection("DOWN", 2);

    // ENTER key in search box must click on selected item in the list
    sendKey("RETURN");
    is(ol.querySelector(".selected").dataset.guid, LOGINS[2].guid, "item 2 must still be selected");

    pressKeyAndExpectSelection("DOWN", 2);
    pressKeyAndExpectSelection("UP", 1);
    pressKeyAndExpectSelection("UP", 0);
    pressKeyAndExpectSelection("UP", 0);

    // ESC must clear search box
    async function expectItemCount(count) {
      await SimpleTest.promiseWaitForCondition(() =>
        JSON.parse(gLoginList.shadowRoot.querySelector(".count").getAttribute("data-l10n-args"))?.count == count,
        `Waiting for the search result count to update to ${count}`);
    }

    sendString("unique string");
    await expectItemCount(0);
    sendKey("Escape");
    ok(!loginFilterInput.value, "ESC must clear filter input");
    await expectItemCount(LOGINS.length);
  });
</script>

</body>
</html>