summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_username_select_dialog.js
blob: 03ac74e9efa604d2b22e6dd6e86b2fb7bce92241 (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
/*
 * Test username selection dialog, on password update from a p-only form,
 * when there are multiple saved logins on the domain.
 */

// Copied from prompt_common.js. TODO: share the code.
function getSelectDialogDoc() {
  // Trudge through all the open windows, until we find the one
  // that has selectDialog.xhtml loaded.
  // var enumerator = Services.wm.getEnumerator("navigator:browser");
  for (let { docShell } of Services.wm.getEnumerator(null)) {
    var containedDocShells = docShell.getAllDocShellsInSubtree(
      docShell.typeChrome,
      docShell.ENUMERATE_FORWARDS
    );
    for (let childDocShell of containedDocShells) {
      // We don't want it if it's not done loading.
      if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE) {
        continue;
      }
      var childDoc = childDocShell.contentViewer.DOMDocument;

      if (
        childDoc.location.href == "chrome://global/content/selectDialog.xhtml"
      ) {
        return childDoc;
      }
    }
  }

  return null;
}

let nsLoginInfo = new Components.Constructor(
  "@mozilla.org/login-manager/loginInfo;1",
  Ci.nsILoginInfo,
  "init"
);
let login1 = new nsLoginInfo(
  "https://example.com",
  "https://example.com",
  null,
  "notifyu1",
  "notifyp1",
  "user",
  "pass"
);
let login1B = new nsLoginInfo(
  "https://example.com",
  "https://example.com",
  null,
  "notifyu1B",
  "notifyp1B",
  "user",
  "pass"
);

add_task(async function test_changeUPLoginOnPUpdateForm_accept() {
  info(
    "Select an u+p login from multiple logins, on password update form, and accept."
  );
  await Services.logins.addLogins([login1, login1B]);

  let selectDialogPromise = TestUtils.topicObserved("select-dialog-loaded");

  await testSubmittingLoginForm(
    "subtst_notifications_change_p.html",
    async function (fieldValues) {
      Assert.equal(fieldValues.username, "null", "Checking submitted username");
      Assert.equal(
        fieldValues.password,
        "pass2",
        "Checking submitted password"
      );

      info("Waiting for select dialog to appear.");
      let doc = (await selectDialogPromise)[0].document;
      let dialog = doc.getElementsByTagName("dialog")[0];
      let listbox = doc.getElementById("list");

      Assert.equal(listbox.selectedIndex, 0, "Checking selected index");
      Assert.equal(listbox.itemCount, 2, "Checking selected length");
      ["notifyu1", "notifyu1B"].forEach((username, i) => {
        Assert.equal(
          listbox.getItemAtIndex(i).label,
          username,
          "Check username selection on dialog"
        );
      });

      dialog.acceptDialog();

      await TestUtils.waitForCondition(() => {
        return !getSelectDialogDoc();
      }, "Wait for selection dialog to disappear.");
    }
  );

  let logins = Services.logins.getAllLogins();
  Assert.equal(logins.length, 2, "Should have 2 logins");

  let login = SpecialPowers.wrap(logins[0]).QueryInterface(Ci.nsILoginMetaInfo);
  Assert.equal(login.username, "notifyu1", "Check the username unchanged");
  Assert.equal(login.password, "pass2", "Check the password changed");
  Assert.equal(login.timesUsed, 2, "Check times used");

  login = SpecialPowers.wrap(logins[1]).QueryInterface(Ci.nsILoginMetaInfo);
  Assert.equal(login.username, "notifyu1B", "Check the username unchanged");
  Assert.equal(login.password, "notifyp1B", "Check the password unchanged");
  Assert.equal(login.timesUsed, 1, "Check times used");

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

  Services.logins.removeLogin(login1B);
});

add_task(async function test_changeUPLoginOnPUpdateForm_cancel() {
  info(
    "Select an u+p login from multiple logins, on password update form, and cancel."
  );
  await Services.logins.addLogins([login1, login1B]);

  let selectDialogPromise = TestUtils.topicObserved("select-dialog-loaded");

  await testSubmittingLoginForm(
    "subtst_notifications_change_p.html",
    async function (fieldValues) {
      Assert.equal(fieldValues.username, "null", "Checking submitted username");
      Assert.equal(
        fieldValues.password,
        "pass2",
        "Checking submitted password"
      );

      info("Waiting for select dialog to appear.");
      let doc = (await selectDialogPromise)[0].document;
      let dialog = doc.getElementsByTagName("dialog")[0];
      let listbox = doc.getElementById("list");

      Assert.equal(listbox.selectedIndex, 0, "Checking selected index");
      Assert.equal(listbox.itemCount, 2, "Checking selected length");
      ["notifyu1", "notifyu1B"].forEach((username, i) => {
        Assert.equal(
          listbox.getItemAtIndex(i).label,
          username,
          "Check username selection on dialog"
        );
      });

      dialog.cancelDialog();

      await TestUtils.waitForCondition(() => {
        return !getSelectDialogDoc();
      }, "Wait for selection dialog to disappear.");
    }
  );

  let logins = Services.logins.getAllLogins();
  Assert.equal(logins.length, 2, "Should have 2 logins");

  let login = SpecialPowers.wrap(logins[0]).QueryInterface(Ci.nsILoginMetaInfo);
  Assert.equal(login.username, "notifyu1", "Check the username unchanged");
  Assert.equal(login.password, "notifyp1", "Check the password unchanged");
  Assert.equal(login.timesUsed, 1, "Check times used");

  login = SpecialPowers.wrap(logins[1]).QueryInterface(Ci.nsILoginMetaInfo);
  Assert.equal(login.username, "notifyu1B", "Check the username unchanged");
  Assert.equal(login.password, "notifyp1B", "Check the password unchanged");
  Assert.equal(login.timesUsed, 1, "Check times used");

  // cleanup
  Services.logins.removeLogin(login1);
  Services.logins.removeLogin(login1B);
});