summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_username_select_dialog.js
blob: 8df89b5105125b861e3a946ec0c3e1a6a9edce6c (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 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.xul loaded.
  var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
           getService(Ci.nsIWindowMediator);
  // var enumerator = wm.getEnumerator("navigator:browser");
  var enumerator = wm.getXULWindowEnumerator(null);

  while (enumerator.hasMoreElements()) {
    var win = enumerator.getNext();
    var windowDocShell = win.QueryInterface(Ci.nsIXULWindow).docShell;

    var containedDocShells = windowDocShell.getDocShellEnumerator(
                                      Ci.nsIDocShellTreeItem.typeChrome,
                                      Ci.nsIDocShell.ENUMERATE_FORWARDS);
    while (containedDocShells.hasMoreElements()) {
        // Get the corresponding document for this docshell
        var childDocShell = containedDocShells.getNext();
        // We don't want it if it's not done loading.
        if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE)
          continue;
        var childDoc = childDocShell.QueryInterface(Ci.nsIDocShell)
                                    .contentViewer
                                    .DOMDocument;

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

  return null;
}

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

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

  yield testSubmittingLoginForm("subtst_notifications_change_p.html", function*(fieldValues) {
    is(fieldValues.username, "null", "Checking submitted username");
    is(fieldValues.password, "pass2", "Checking submitted password");

    yield ContentTaskUtils.waitForCondition(() => {
      return getSelectDialogDoc();
    }, "Wait for selection dialog to be accessible.");

    let doc = getSelectDialogDoc();
    let dialog = doc.getElementsByTagName("dialog")[0];
    let listbox = doc.getElementById("list");

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

    dialog.acceptDialog();

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

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

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

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

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

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

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

  yield testSubmittingLoginForm("subtst_notifications_change_p.html", function*(fieldValues) {
    is(fieldValues.username, "null", "Checking submitted username");
    is(fieldValues.password, "pass2", "Checking submitted password");

    yield ContentTaskUtils.waitForCondition(() => {
      return getSelectDialogDoc();
    }, "Wait for selection dialog to be accessible.");

    let doc = getSelectDialogDoc();
    let dialog = doc.getElementsByTagName("dialog")[0];
    let listbox = doc.getElementById("list");

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

    dialog.cancelDialog();

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

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

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

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

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