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

EXPECTED_BREACH = {
  AddedDate: "2018-12-20T23:56:26Z",
  BreachDate: "2018-12-16",
  Domain: "breached.example.com",
  Name: "Breached",
  PwnCount: 1643100,
  DataClasses: ["Email addresses", "Usernames", "Passwords", "IP addresses"],
  _status: "synced",
  id: "047940fe-d2fd-4314-b636-b4a952ee0043",
  last_modified: "1541615610052",
  schema: "1541615609018",
};

const SORT_PREF_NAME = "signon.management.page.sort";

add_setup(async function () {
  TEST_LOGIN3.QueryInterface(Ci.nsILoginMetaInfo).timePasswordChanged = 1;
  TEST_LOGIN1 = await addLogin(TEST_LOGIN1);
  info(`TEST_LOGIN1 added with guid=${TEST_LOGIN1.guid}`);
  TEST_LOGIN3 = await addLogin(TEST_LOGIN3);
  info(`TEST_LOGIN3 added with guid=${TEST_LOGIN3.guid}`);
  registerCleanupFunction(() => {
    Services.logins.removeAllUserFacingLogins();
    Services.prefs.clearUserPref(SORT_PREF_NAME);
  });
});

add_task(async function test_sort_order_persisted() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "about:logins",
    },
    async function (browser) {
      await ContentTask.spawn(
        browser,
        [TEST_LOGIN1.guid, TEST_LOGIN3.guid],
        async function ([testLogin1Guid, testLogin3Guid]) {
          let loginList = Cu.waiveXrays(
            content.document.querySelector("login-list")
          );
          await ContentTaskUtils.waitForCondition(
            () => loginList._sortSelect.value == "alerts",
            "Waiting for login-list sort to get changed to 'alerts'. Current value is: " +
              loginList._sortSelect.value
          );
          Assert.equal(
            loginList._sortSelect.value,
            "alerts",
            "selected sort should be 'alerts' since there is a breached login"
          );
          Assert.equal(
            loginList._list.querySelector(
              ".login-list-item[data-guid]:not([hidden])"
            ).dataset.guid,
            testLogin3Guid,
            "the first login should be TEST_LOGIN3 since they are sorted by alerts"
          );

          loginList._sortSelect.value = "last-changed";
          loginList._sortSelect.dispatchEvent(
            new content.Event("change", { bubbles: true })
          );
          Assert.equal(
            loginList._list.querySelector(
              ".login-list-item[data-guid]:not([hidden])"
            ).dataset.guid,
            testLogin1Guid,
            "the first login should be TEST_LOGIN1 since it has the most recent timePasswordChanged value"
          );
        }
      );
    }
  );

  Assert.equal(
    Services.prefs.getCharPref(SORT_PREF_NAME),
    "last-changed",
    "'last-changed' should be stored in the pref"
  );

  // Set the pref to the value used in Fx70-76 to confirm our
  // backwards-compat support that "breached" is changed to "alerts"
  Services.prefs.setCharPref(SORT_PREF_NAME, "breached");
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "about:logins",
    },
    async function (browser) {
      await ContentTask.spawn(
        browser,
        TEST_LOGIN3.guid,
        async function (testLogin3Guid) {
          let loginList = Cu.waiveXrays(
            content.document.querySelector("login-list")
          );
          await ContentTaskUtils.waitForCondition(
            () => loginList._sortSelect.value == "alerts",
            "Waiting for login-list sort to get changed to 'alerts'. Current value is: " +
              loginList._sortSelect.value
          );
          Assert.equal(
            loginList._sortSelect.value,
            "alerts",
            "selected sort should be restored to 'alerts' since 'breached' was in prefs"
          );
          Assert.equal(
            loginList._list.querySelector(
              ".login-list-item[data-guid]:not([hidden])"
            ).dataset.guid,
            testLogin3Guid,
            "the first login should be TEST_LOGIN3 since they are sorted by alerts"
          );
        }
      );
    }
  );

  let storageChangedPromised = TestUtils.topicObserved(
    "passwordmgr-storage-changed",
    (_, data) => data == "removeLogin"
  );
  Services.logins.removeLogin(TEST_LOGIN3);
  await storageChangedPromised;
  TEST_LOGIN2 = await addLogin(TEST_LOGIN2);

  Assert.equal(
    Services.prefs.getCharPref(SORT_PREF_NAME),
    "breached",
    "confirm that the stored sort is still 'breached' and as such shouldn't apply when the page loads"
  );
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "about:logins",
    },
    async function (browser) {
      await ContentTask.spawn(
        browser,
        TEST_LOGIN2.guid,
        async function (testLogin2Guid) {
          let loginList = Cu.waiveXrays(
            content.document.querySelector("login-list")
          );
          await ContentTaskUtils.waitForCondition(
            () =>
              loginList._list.querySelector(
                ".login-list-item[data-guid]:not([hidden])"
              ),
            "wait for a visible loging to get populated"
          );
          Assert.equal(
            loginList._sortSelect.value,
            "name",
            "selected sort should be name since 'alerts' no longer applies with no breached or vulnerable logins"
          );
          Assert.equal(
            loginList._list.querySelector(
              ".login-list-item[data-guid]:not([hidden])"
            ).dataset.guid,
            testLogin2Guid,
            "the first login should be TEST_LOGIN2 since it is sorted first by 'name'"
          );
        }
      );
    }
  );
});