summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/tests/browser/browser_loginListChanges.js
blob: 13df6c1ef600f4e02b835d40ae8143cd2941c34f (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

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

add_task(async function test_login_added() {
  let login = {
    guid: "70",
    username: "jared",
    password: "deraj",
    origin: "https://www.example.com",
  };
  let browser = gBrowser.selectedBrowser;
  browser.browsingContext.currentWindowGlobal
    .getActor("AboutLogins")
    .sendAsyncMessage("AboutLogins:LoginAdded", login);

  await SpecialPowers.spawn(browser, [login], async addedLogin => {
    let loginList = Cu.waiveXrays(content.document.querySelector("login-list"));
    let loginFound = await ContentTaskUtils.waitForCondition(() => {
      return (
        loginList._loginGuidsSortedOrder.length == 1 &&
        loginList._loginGuidsSortedOrder[0] == addedLogin.guid
      );
    }, "Waiting for login to be added");
    Assert.ok(loginFound, "Newly added logins should be added to the page");
  });
});

add_task(async function test_login_modified() {
  let login = {
    guid: "70",
    username: "jared@example.com",
    password: "deraj",
    origin: "https://www.example.com",
  };
  let browser = gBrowser.selectedBrowser;
  browser.browsingContext.currentWindowGlobal
    .getActor("AboutLogins")
    .sendAsyncMessage("AboutLogins:LoginModified", login);

  await SpecialPowers.spawn(browser, [login], async modifiedLogin => {
    let loginList = Cu.waiveXrays(content.document.querySelector("login-list"));
    let loginFound = await ContentTaskUtils.waitForCondition(() => {
      return (
        loginList._loginGuidsSortedOrder.length == 1 &&
        loginList._loginGuidsSortedOrder[0] == modifiedLogin.guid &&
        loginList._logins[loginList._loginGuidsSortedOrder[0]].login.username ==
          modifiedLogin.username
      );
    }, "Waiting for username to get updated");
    Assert.ok(loginFound, "The login should get updated on the page");
  });
});

add_task(async function test_login_removed() {
  let login = {
    guid: "70",
    username: "jared@example.com",
    password: "deraj",
    origin: "https://www.example.com",
  };
  let browser = gBrowser.selectedBrowser;
  browser.browsingContext.currentWindowGlobal
    .getActor("AboutLogins")
    .sendAsyncMessage("AboutLogins:LoginRemoved", login);

  await SpecialPowers.spawn(browser, [login], async removedLogin => {
    let loginList = Cu.waiveXrays(content.document.querySelector("login-list"));
    let loginRemoved = await ContentTaskUtils.waitForCondition(() => {
      return !loginList._loginGuidsSortedOrder.length;
    }, "Waiting for login to get removed");
    Assert.ok(loginRemoved, "The login should be removed from the page");
  });
});

add_task(async function test_all_logins_removed() {
  // Setup the test with 2 logins.
  let logins = [
    {
      guid: "70",
      username: "jared",
      password: "deraj",
      origin: "https://www.example.com",
    },
    {
      guid: "71",
      username: "ntim",
      password: "verysecurepassword",
      origin: "https://www.example.com",
    },
  ];

  let browser = gBrowser.selectedBrowser;
  browser.browsingContext.currentWindowGlobal
    .getActor("AboutLogins")
    .sendAsyncMessage("AboutLogins:AllLogins", logins);

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

  Services.logins.removeAllUserFacingLogins();

  await SpecialPowers.spawn(browser, [], async () => {
    let loginList = Cu.waiveXrays(content.document.querySelector("login-list"));
    let loginFound = await ContentTaskUtils.waitForCondition(() => {
      return !loginList._loginGuidsSortedOrder.length;
    }, "Waiting for logins to be cleared");
    Assert.ok(loginFound, "Logins should be cleared");
    Assert.ok(
      content.document.documentElement.classList.contains("no-logins"),
      "Should be in no logins view after clearing"
    );
    Assert.ok(
      loginList.classList.contains("no-logins"),
      "login-list should be in no logins view after clearing"
    );
  });
});