summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_capture_doorhanger_window_open.js
blob: 1bcfec5eb169633673c804f285c9c028417ff194 (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
/*
 * 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,
  }, function*(browser) {
    ok(true, "loaded " + aPageFile);
    info("running test case task");
    yield* aTaskFn();
    info("waiting for storage change");
    yield storageChangedPromised;
  });
}

add_task(function* setup() {
  yield SimpleTest.promiseFocus(window);
});

add_task(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";
  yield withTestTabUntilStorageChange(url, function*() {
    info("waiting for popupshown");
    yield notifShownPromise;
    // the popup closes and the doorhanger should appear in the opener
    let popup = getCaptureDoorhanger("password-save");
    ok(popup, "got notification popup");
    yield* checkDoorhangerUsernamePassword("notifyu1", "notifyp1");
    // Sanity check, no logins should exist yet.
    let logins = Services.logins.getAllLogins();
    is(logins.length, 0, "Should not have any logins yet");

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

add_task(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";
  yield withTestTabUntilStorageChange(url, function*() {
    info("waiting for popupshown");
    yield notifShownPromise;
    let popup = getCaptureDoorhanger("password-change");
    ok(popup, "got notification popup");
    yield* 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();
  is(logins.length, 1, "Should only have 1 login");
  let login = logins[0].QueryInterface(Ci.nsILoginMetaInfo);
  is(login.username, "notifyu1", "Check the username");
  is(login.password, "pass2", "Check password changed");
  is(login.timesUsed, 2, "check .timesUsed incremented on change");
  ok(login.timeCreated < login.timeLastUsed, "timeLastUsed bumped");
  ok(login.timeLastUsed == login.timePasswordChanged, "timeUsed == timeChanged");

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

add_task(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");
  yield withTestTabUntilStorageChange(url, function*() {
    yield notifShownPromise;
    let popup = getCaptureDoorhanger("password-save");
    ok(popup, "got notification popup");
    yield* checkDoorhangerUsernamePassword("notifyu2", "notifyp2");
    clickDoorhangerButton(popup, REMEMBER_BUTTON);
    yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
  });

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

add_task(function* test_changeChromeVisibleSameWindow() {
  let url = "subtst_notifications_11.html?notifyu2|pass2||";
  let notifShownPromise = BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
  yield withTestTabUntilStorageChange(url, function*() {
    yield notifShownPromise;
    let popup = getCaptureDoorhanger("password-change");
    ok(popup, "got notification popup");
    yield* checkDoorhangerUsernamePassword("notifyu2", "pass2");
    clickDoorhangerButton(popup, CHANGE_BUTTON);
    yield 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();
  is(logins.length, 1, "Should have 1 login");
  let login = logins[0].QueryInterface(Ci.nsILoginMetaInfo);
  is(login.username, "notifyu2", "Check the username");
  is(login.password, "pass2", "Check password changed");
  is(login.timesUsed, 2, "check .timesUsed incremented on change");
  ok(login.timeCreated < login.timeLastUsed, "timeLastUsed bumped");
  ok(login.timeLastUsed == login.timePasswordChanged, "timeUsed == timeChanged");

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