summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/tests/browser/browser_deleteLogin.js
blob: 8aa42013783f65168c1c78f6d2794bddfdfa033a (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
179
180
181
182
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_setup(async function () {
  TEST_LOGIN1 = await addLogin(TEST_LOGIN1);
  TEST_LOGIN2 = await addLogin(TEST_LOGIN2);
  await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: "about:logins",
  });
  registerCleanupFunction(() => {
    BrowserTestUtils.removeTab(gBrowser.selectedTab);
  });
});

add_task(async function test_show_logins() {
  let browser = gBrowser.selectedBrowser;

  await SpecialPowers.spawn(
    browser,
    [[TEST_LOGIN1.guid, TEST_LOGIN2.guid]],
    async loginGuids => {
      let loginList = Cu.waiveXrays(
        content.document.querySelector("login-list")
      );
      let loginFound = await ContentTaskUtils.waitForCondition(() => {
        return (
          loginList._loginGuidsSortedOrder.length == 2 &&
          loginList._loginGuidsSortedOrder.includes(loginGuids[0]) &&
          loginList._loginGuidsSortedOrder.includes(loginGuids[1])
        );
      }, "Waiting for logins to be displayed");
      Assert.ok(
        !content.document.documentElement.classList.contains("no-logins"),
        "Should no longer be in no logins view"
      );
      Assert.ok(
        !loginList.classList.contains("no-logins"),
        "login-list should no longer be in no logins view"
      );
      Assert.ok(loginFound, "Newly added logins should be added to the page");
    }
  );
});

add_task(async function test_login_item() {
  let browser = gBrowser.selectedBrowser;

  function waitForDelete() {
    let numLogins = Services.logins.countLogins("", "", "");
    return TestUtils.waitForCondition(
      () => Services.logins.countLogins("", "", "") < numLogins,
      "Error waiting for login deletion"
    );
  }

  async function deleteFirstLoginAfterEdit() {
    await SpecialPowers.spawn(browser, [], async () => {
      let loginList = content.document.querySelector("login-list");
      let loginListItem = loginList.shadowRoot.querySelector(
        ".login-list-item[data-guid]:not([hidden])"
      );
      info("Clicking on the first login");
      loginListItem.click();

      let loginItem = Cu.waiveXrays(
        content.document.querySelector("login-item")
      );
      let loginItemPopulated = await ContentTaskUtils.waitForCondition(() => {
        return loginItem._login.guid == loginListItem.dataset.guid;
      }, "Waiting for login item to get populated");
      Assert.ok(loginItemPopulated, "The login item should get populated");
    });
    let reauthObserved = OSKeyStoreTestUtils.waitForOSKeyStoreLogin(true);
    await SpecialPowers.spawn(browser, [], async () => {
      let loginItem = Cu.waiveXrays(
        content.document.querySelector("login-item")
      );
      let editButton = loginItem.shadowRoot.querySelector(".edit-button");
      editButton.click();
    });
    await reauthObserved;
    return SpecialPowers.spawn(browser, [], async () => {
      let loginItem = Cu.waiveXrays(
        content.document.querySelector("login-item")
      );
      let usernameInput = loginItem.shadowRoot.querySelector(
        "input[name='username']"
      );
      let passwordInput = loginItem._passwordInput;
      usernameInput.value += "-undone";
      passwordInput.value += "-undone";

      let deleteButton = loginItem.shadowRoot.querySelector(".delete-button");
      deleteButton.click();

      let confirmDeleteDialog = Cu.waiveXrays(
        content.document.querySelector("confirmation-dialog")
      );
      let confirmButton =
        confirmDeleteDialog.shadowRoot.querySelector(".confirm-button");
      confirmButton.click();
    });
  }

  function deleteFirstLogin() {
    return SpecialPowers.spawn(browser, [], async () => {
      let loginList = content.document.querySelector("login-list");
      let loginListItem = loginList.shadowRoot.querySelector(
        ".login-list-item[data-guid]:not([hidden])"
      );
      info("Clicking on the first login");
      loginListItem.click();

      let loginItem = Cu.waiveXrays(
        content.document.querySelector("login-item")
      );
      let loginItemPopulated = await ContentTaskUtils.waitForCondition(() => {
        return loginItem._login.guid == loginListItem.dataset.guid;
      }, "Waiting for login item to get populated");
      Assert.ok(loginItemPopulated, "The login item should get populated");

      let deleteButton = loginItem.shadowRoot.querySelector(".delete-button");
      deleteButton.click();

      let confirmDeleteDialog = Cu.waiveXrays(
        content.document.querySelector("confirmation-dialog")
      );
      let confirmButton =
        confirmDeleteDialog.shadowRoot.querySelector(".confirm-button");
      confirmButton.click();
    });
  }

  let onDeletePromise;
  if (OSKeyStoreTestUtils.canTestOSKeyStoreLogin()) {
    // Can only test Edit mode in official builds
    onDeletePromise = waitForDelete();
    await deleteFirstLoginAfterEdit();
    await onDeletePromise;

    await SpecialPowers.spawn(browser, [], async () => {
      let loginList = content.document.querySelector("login-list");
      Assert.ok(
        !content.document.documentElement.classList.contains("no-logins"),
        "Should not be in no logins view as there is still one login"
      );
      Assert.ok(
        !loginList.classList.contains("no-logins"),
        "Should not be in no logins view as there is still one login"
      );

      let confirmDiscardDialog = Cu.waiveXrays(
        content.document.querySelector("confirmation-dialog")
      );
      Assert.ok(
        confirmDiscardDialog.hidden,
        "Discard confirm dialog should not show up after delete an edited login"
      );
    });
  } else {
    onDeletePromise = waitForDelete();
    await deleteFirstLogin();
    await onDeletePromise;
  }

  onDeletePromise = waitForDelete();
  await deleteFirstLogin();
  await onDeletePromise;

  await SpecialPowers.spawn(browser, [], async () => {
    let loginList = content.document.querySelector("login-list");
    Assert.ok(
      content.document.documentElement.classList.contains("no-logins"),
      "Should be in no logins view as all logins got deleted"
    );
    Assert.ok(
      loginList.classList.contains("no-logins"),
      "login-list should be in no logins view as all logins got deleted"
    );
  });
});