summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_doorhanger_window_open.js
blob: 2375a3c53a66906920548c1b4064fa234c606659 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Test capture popup notifications in content opened by window.open
 */

let nsLoginInfo = new Components.Constructor(
  "@mozilla.org/login-manager/loginInfo;1",
  Ci.nsILoginInfo,
  "init"
);
let login1 = new nsLoginInfo(
  "http://mochi.test:8888",
  "http://mochi.test:8888",
  null,
  "notifyu1",
  "notifyp1",
  "user",
  "pass"
);
let login2 = new nsLoginInfo(
  "http://mochi.test:8888",
  "http://mochi.test:8888",
  null,
  "notifyu2",
  "notifyp2",
  "user",
  "pass"
);

function withTestTabUntilStorageChange(aPageFile, aTaskFn) {
  function storageChangedObserved(subject, data) {
    // Watch for actions triggered from a doorhanger (not cleanup tasks with removeLogin)
    if (data == "removeLogin") {
      return false;
    }
    return true;
  }

  let storageChangedPromised = TestUtils.topicObserved(
    "passwordmgr-storage-changed",
    storageChangedObserved
  );
  return BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "http://mochi.test:8888" + DIRECTORY_PATH + aPageFile,
    },
    async function (browser) {
      Assert.ok(true, "loaded " + aPageFile);
      info("running test case task");
      await aTaskFn();
      info("waiting for storage change");
      await storageChangedPromised;
    }
  );
}

add_setup(async function () {
  await SimpleTest.promiseFocus(window);
});

add_task(async function test_saveChromeHiddenAutoClose() {
  let notifShownPromise = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  // query arguments are: username, password, features, auto-close (delimited by '|')
  let url =
    "subtst_notifications_11.html?notifyu1|notifyp1|" +
    "menubar=no,toolbar=no,location=no|autoclose";
  await withTestTabUntilStorageChange(url, async function () {
    info("waiting for popupshown");
    await notifShownPromise;
    // the popup closes and the doorhanger should appear in the opener
    let popup = await getCaptureDoorhangerThatMayOpen("password-save");
    Assert.ok(popup, "got notification popup");
    await checkDoorhangerUsernamePassword("notifyu1", "notifyp1");
    // Sanity check, no logins should exist yet.
    let logins = Services.logins.getAllLogins();
    Assert.equal(logins.length, 0, "Should not have any logins yet");

    clickDoorhangerButton(popup, REMEMBER_BUTTON);
  });
  // Check result of clicking Remember
  let logins = Services.logins.getAllLogins();
  Assert.equal(logins.length, 1, "Should only have 1 login");
  let login = logins[0].QueryInterface(Ci.nsILoginMetaInfo);
  Assert.equal(login.timesUsed, 1, "Check times used on new entry");
  Assert.equal(
    login.username,
    "notifyu1",
    "Check the username used on the new entry"
  );
  Assert.equal(
    login.password,
    "notifyp1",
    "Check the password used on the new entry"
  );
});

add_task(async function test_changeChromeHiddenAutoClose() {
  let notifShownPromise = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  let url =
    "subtst_notifications_11.html?notifyu1|pass2|menubar=no,toolbar=no,location=no|autoclose";
  await withTestTabUntilStorageChange(url, async function () {
    info("waiting for popupshown");
    await notifShownPromise;
    let popup = await getCaptureDoorhangerThatMayOpen("password-change");
    Assert.ok(popup, "got notification popup");
    await checkDoorhangerUsernamePassword("notifyu1", "pass2");
    clickDoorhangerButton(popup, CHANGE_BUTTON);
  });

  // Check to make sure we updated the password, timestamps and use count for
  // the login being changed with this form.
  let logins = Services.logins.getAllLogins();
  Assert.equal(logins.length, 1, "Should only have 1 login");
  let login = logins[0].QueryInterface(Ci.nsILoginMetaInfo);
  Assert.equal(login.username, "notifyu1", "Check the username");
  Assert.equal(login.password, "pass2", "Check password changed");
  Assert.equal(login.timesUsed, 2, "check .timesUsed incremented on change");
  Assert.ok(login.timeCreated < login.timeLastUsed, "timeLastUsed bumped");
  Assert.ok(
    login.timeLastUsed == login.timePasswordChanged,
    "timeUsed == timeChanged"
  );

  login1.password = "pass2";
  Services.logins.removeLogin(login1);
  login1.password = "notifyp1";
});

add_task(async function test_saveChromeVisibleSameWindow() {
  // This test actually opens a new tab in the same window with default browser settings.
  let url = "subtst_notifications_11.html?notifyu2|notifyp2||";
  let notifShownPromise = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  await withTestTabUntilStorageChange(url, async function () {
    await notifShownPromise;
    let popup = await getCaptureDoorhangerThatMayOpen("password-save");
    Assert.ok(popup, "got notification popup");
    await checkDoorhangerUsernamePassword("notifyu2", "notifyp2");
    clickDoorhangerButton(popup, REMEMBER_BUTTON);
    BrowserTestUtils.removeTab(gBrowser.selectedTab);
  });

  // Check result of clicking Remember
  let logins = Services.logins.getAllLogins();
  Assert.equal(logins.length, 1, "Should only have 1 login now");
  let login = logins[0].QueryInterface(Ci.nsILoginMetaInfo);
  Assert.equal(
    login.username,
    "notifyu2",
    "Check the username used on the new entry"
  );
  Assert.equal(
    login.password,
    "notifyp2",
    "Check the password used on the new entry"
  );
  Assert.equal(login.timesUsed, 1, "Check times used on new entry");
});

add_task(async function test_changeChromeVisibleSameWindow() {
  let url = "subtst_notifications_11.html?notifyu2|pass2||";
  let notifShownPromise = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  await withTestTabUntilStorageChange(url, async function () {
    await notifShownPromise;
    let popup = await getCaptureDoorhangerThatMayOpen("password-change");
    Assert.ok(popup, "got notification popup");
    await checkDoorhangerUsernamePassword("notifyu2", "pass2");
    clickDoorhangerButton(popup, CHANGE_BUTTON);
    BrowserTestUtils.removeTab(gBrowser.selectedTab);
  });

  // Check to make sure we updated the password, timestamps and use count for
  // the login being changed with this form.
  let logins = Services.logins.getAllLogins();
  Assert.equal(logins.length, 1, "Should have 1 login");
  let login = logins[0].QueryInterface(Ci.nsILoginMetaInfo);
  Assert.equal(login.username, "notifyu2", "Check the username");
  Assert.equal(login.password, "pass2", "Check password changed");
  Assert.equal(login.timesUsed, 2, "check .timesUsed incremented on change");
  Assert.ok(login.timeCreated < login.timeLastUsed, "timeLastUsed bumped");
  Assert.ok(
    login.timeLastUsed == login.timePasswordChanged,
    "timeUsed == timeChanged"
  );

  // cleanup
  login2.password = "pass2";
  Services.logins.removeLogin(login2);
  login2.password = "notifyp2";
});