4 <title>Test for Login Manager
</title>
5 <script type=
"text/javascript" src=
"/MochiKit/MochiKit.js"></script>
6 <script type=
"text/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <script type=
"text/javascript" src=
"pwmgr_common.js"></script>
8 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
11 Login Manager test: notifications
14 <div id=
"content" style=
"display: none">
15 <iframe id=
"iframe"></iframe>
19 <script class=
"testbody" type=
"text/javascript">
21 /** Test for Login Manager: notifications. **/
24 "subtst_notifications_1.html", //
1
25 "subtst_notifications_1.html", //
2
26 "subtst_notifications_1.html", //
3
27 "subtst_notifications_1.html", //
4
28 "subtst_notifications_1.html", //
5
29 "subtst_notifications_1.html", //
6
30 "subtst_notifications_1.html", //
7
31 "subtst_notifications_1.html", //
8
32 "subtst_notifications_2.html", //
9
33 "subtst_notifications_3.html", //
10
34 "subtst_notifications_4.html", //
11
35 "subtst_notifications_5.html", //
12
36 "subtst_notifications_1.html", //
13
37 "subtst_notifications_6.html", //
14
38 "subtst_notifications_1.html", //
15
39 "subtst_notifications_6.html"
45 * Fetches the notification box for the specified window.
47 function getNotificationBox(aWindow) {
49 var chromeWin = aWindow
50 .QueryInterface(Ci.nsIInterfaceRequestor)
51 .getInterface(Ci.nsIWebNavigation)
52 .QueryInterface(Ci.nsIDocShellTreeItem)
54 .QueryInterface(Ci.nsIInterfaceRequestor)
55 .getInterface(Ci.nsIDOMWindow);
56 // Don't need .wrappedJSObject here, unlike when chrome does this.
57 var browserWin = chromeWin.browserDOMWindow;
58 var notifyBox = browserWin.getNotificationBox(aWindow);
61 // Find the
<browser> which contains aWindow, by looking
62 // through all the open windows and all the
<browsers> in each.
63 var wm = Cc[
"@mozilla.org/appshell/window-mediator;1"].
64 getService(Ci.nsIWindowMediator);
65 var enumerator = wm.getEnumerator(
"navigator:browser");
66 var tabbrowser = null;
67 var foundBrowser = null;
69 while (!foundBrowser && enumerator.hasMoreElements()) {
70 var win = enumerator.getNext();
71 tabbrowser = win.getBrowser();
72 foundBrowser = tabbrowser.getBrowserForDocument(aWindow.document);
75 // Return the notificationBox associated with the browser.
76 return tabbrowser.getNotificationBox(foundBrowser);
84 function getNotificationBar(aBox, aKind) {
85 ok(true,
"Looking for " + aKind +
" notification bar");
86 return aBox.getNotificationWithValue(aKind);
91 * clickNotificationButton
93 * Clicks the specified notification button.
95 function clickNotificationButton(aBar, aButtonName) {
96 // This is a bit of a hack. The notification doesn't have an API to
97 // trigger buttons, so we dive down into the implementation and twiddle
98 // the buttons directly.
99 var buttons = aBar.getElementsByTagName(
"button");
101 for (var i =
0; i < buttons.length; i++) {
102 if (buttons[i].label == aButtonName) {
109 ok(clicked,
"Clicked \"" + aButtonName + "\
" button");
113 var ignoreLoad = false;
114 function handleLoad(aEvent) {
115 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
117 // ignore every other load event ... We get one for loading the subtest (which
118 // we want to ignore), and another when the subtest's form submits itself
119 // (which we want to handle, to start the next test).
120 ignoreLoad = !ignoreLoad;
122 ok(true,
"Ignoring load of subtest #" + testNum);
125 ok(true,
"Processing submission of subtest #" + testNum);
132 // Remove any notification bar that might be lingering from a failed test.
133 notifyBox.removeAllNotifications(true);
135 if (testNum <= subtests.length) {
136 ok(true,
"Starting test #" + testNum);
137 iframe.src = subtests[testNum-
1];
139 ok(true,
"notification tests finished.");
145 // Remember, Never for This Site, Not Now
146 function checkTest() {
149 // The document generated from formsubmit.sjs contains the user/pass it
150 // received inside
<span id=
"blah">value
</span>
151 var gotUser = iframe.contentDocument.getElementById(
"user").textContent;
152 var gotPass = iframe.contentDocument.getElementById(
"pass").textContent;
157 /* Basic Yes/No/Never tests... */
160 is(gotUser,
"notifyu1",
"Checking submitted username");
161 is(gotPass,
"notifyp1",
"Checking submitted password");
162 bar = getNotificationBar(notifyBox,
"password-save");
163 ok(bar,
"got notification bar");
164 clickNotificationButton(bar,
"Not Now");
168 // Same subtest, this time click Never
169 is(gotUser,
"notifyu1",
"Checking submitted username");
170 is(gotPass,
"notifyp1",
"Checking submitted password");
171 bar = getNotificationBar(notifyBox,
"password-save");
172 ok(bar,
"got notification bar");
173 is(true, pwmgr.getLoginSavingEnabled(
"http://localhost:8888"),
174 "Checking for login saving enabled");
175 clickNotificationButton(bar,
"Never for This Site");
179 // Same subtest, make sure Never took effect
180 is(gotUser,
"notifyu1",
"Checking submitted username");
181 is(gotPass,
"notifyp1",
"Checking submitted password");
182 bar = getNotificationBar(notifyBox,
"password-save");
183 ok(!bar,
"checking for no notification bar");
184 is(false, pwmgr.getLoginSavingEnabled(
"http://localhost:8888"),
185 "Checking for login saving disabled");
186 // reenable login saving.
187 pwmgr.setLoginSavingEnabled(
"http://localhost:8888", true);
191 // Same subtest, this time click Remember
192 is(gotUser,
"notifyu1",
"Checking submitted username");
193 is(gotPass,
"notifyp1",
"Checking submitted password");
194 bar = getNotificationBar(notifyBox,
"password-save");
195 ok(bar,
"got notification bar");
196 clickNotificationButton(bar,
"Remember");
200 // Same subtest, make sure we didn't prompt for an existing login.
201 is(gotUser,
"notifyu1",
"Checking submitted username");
202 is(gotPass,
"notifyp1",
"Checking submitted password");
203 bar = getNotificationBar(notifyBox,
"password-save");
204 ok(!bar,
"checking for no notification bar");
206 pwmgr.removeLogin(login1);
209 /* signons.rememberSignons pref tests... */
212 // Same subtest, make sure we're getting the bar again.
213 is(gotUser,
"notifyu1",
"Checking submitted username");
214 is(gotPass,
"notifyp1",
"Checking submitted password");
215 bar = getNotificationBar(notifyBox,
"password-save");
216 ok(bar,
"got notification bar");
217 clickNotificationButton(bar,
"Not Now");
218 // Change prefs to no longer remember signons
219 prefs.setBoolPref(
"rememberSignons", false);
223 // Same subtest, make sure we're not prompting.
224 is(gotUser,
"notifyu1",
"Checking submitted username");
225 is(gotPass,
"notifyp1",
"Checking submitted password");
226 bar = getNotificationBar(notifyBox,
"password-save");
227 ok(!bar,
"checking for no notification bar");
228 // Change prefs to remember signons again
229 prefs.setBoolPref(
"rememberSignons", true);
233 // Same subtest, make sure we're getting the bar again.
234 is(gotUser,
"notifyu1",
"Checking submitted username");
235 is(gotPass,
"notifyp1",
"Checking submitted password");
236 bar = getNotificationBar(notifyBox,
"password-save");
237 ok(bar,
"got notification bar");
238 clickNotificationButton(bar,
"Not Now");
241 /* autocomplete=off tests... */
244 // Check for no notification bar when autocomplete=off present
245 is(gotUser,
"notifyu1",
"Checking submitted username");
246 is(gotPass,
"notifyp1",
"Checking submitted password");
247 bar = getNotificationBar(notifyBox,
"password-save");
248 ok(!bar,
"checking for no notification bar");
252 // Check for no notification bar when autocomplete=off present
253 is(gotUser,
"notifyu1",
"Checking submitted username");
254 is(gotPass,
"notifyp1",
"Checking submitted password");
255 bar = getNotificationBar(notifyBox,
"password-save");
256 ok(!bar,
"checking for no notification bar");
260 // Check for no notification bar when autocomplete=off present
261 is(gotUser,
"notifyu1",
"Checking submitted username");
262 is(gotPass,
"notifyp1",
"Checking submitted password");
263 bar = getNotificationBar(notifyBox,
"password-save");
264 ok(!bar,
"checking for no notification bar");
267 /* no password field test... */
270 // Check for no notification bar when no password field present
271 is(gotUser,
"notifyu1",
"Checking submitted username");
272 is(gotPass,
"null",
"Checking submitted password");
273 bar = getNotificationBar(notifyBox,
"password-save");
274 ok(!bar,
"checking for no notification bar");
276 // Add login for the next test.
277 pwmgr.addLogin(login2);
281 // Check for no notification bar when existing pw-only login matches form.
282 is(gotUser,
"notifyu1",
"Checking submitted username");
283 is(gotPass,
"notifyp1",
"Checking submitted password");
284 bar = getNotificationBar(notifyBox,
"password-save");
285 ok(!bar,
"checking for no notification bar");
286 pwmgr.removeLogin(login2);
288 // Add login for the next test
289 pwmgr.addLogin(login1);
293 // Check for no notification bar when pw-only form matches existing login.
294 is(gotUser,
"null",
"Checking submitted username");
295 is(gotPass,
"notifyp1",
"Checking submitted password");
296 bar = getNotificationBar(notifyBox,
"password-save");
297 ok(!bar,
"checking for no notification bar");
298 pwmgr.removeLogin(login1);
300 // Add login for the next test
301 pwmgr.addLogin(login2B);
305 // Check for notification bar when existing pw-only login doesn't match form.
306 is(gotUser,
"notifyu1",
"Checking submitted username");
307 is(gotPass,
"notifyp1",
"Checking submitted password");
308 bar = getNotificationBar(notifyBox,
"password-save");
309 ok(bar,
"got notification bar");
310 clickNotificationButton(bar,
"Not Now");
311 pwmgr.removeLogin(login2B);
313 // Add login for the next test
314 pwmgr.addLogin(login1B);
318 // Check for notification bar when pw-only form doesn't match existing login.
319 is(gotUser,
"null",
"Checking submitted username");
320 is(gotPass,
"notifyp1",
"Checking submitted password");
321 bar = getNotificationBar(notifyBox,
"password-save");
322 ok(bar,
"got notification bar");
323 clickNotificationButton(bar,
"Not Now");
324 pwmgr.removeLogin(login1B);
328 ok(false,
"Unexpected call to checkTest for test #" + testNum);
333 // * existing login test, form has different password --> change password, no save prompt
337 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
339 const Ci = Components.interfaces;
340 const Cc = Components.classes;
341 ok(Ci != null,
"Access Ci");
342 ok(Cc != null,
"Access Cc");
344 var pwmgr = Cc[
"@mozilla.org/login-manager;1"].
345 getService(Ci.nsILoginManager);
346 ok(pwmgr != null,
"Access pwmgr");
348 var prefs = Cc[
"@mozilla.org/preferences-service;1"].
349 getService(Ci.nsIPrefService);
350 ok(prefs != null,
"Access prefs");
351 prefs = prefs.getBranch(
"signon.");
352 ok(prefs != null,
"Access pref branch");
354 var nsLoginInfo = new Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1",
355 Ci.nsILoginInfo,
"init");
356 var login1 = new nsLoginInfo(
"http://localhost:8888",
"http://localhost:8888", null,
357 "notifyu1",
"notifyp1",
"user",
"pass");
358 var login2 = new nsLoginInfo(
"http://localhost:8888",
"http://localhost:8888", null,
359 "",
"notifyp1",
"",
"pass");
360 var login1B = new nsLoginInfo(
"http://localhost:8888",
"http://localhost:8888", null,
361 "notifyu1B",
"notifyp1B",
"user",
"pass");
362 var login2B = new nsLoginInfo(
"http://localhost:8888",
"http://localhost:8888", null,
363 "",
"notifyp1B",
"",
"pass");
365 var iframe = document.getElementById(
"iframe");
366 iframe.onload = handleLoad;
368 // The notification box (not *bar*) is a constant, per-tab container. So, we
369 // only need to fetch it once.
370 var notifyBox = getNotificationBox(window.top);
371 ok(notifyBox,
"Got notification box");
373 // Remove any notification bars that might be left over from other tests.
374 notifyBox.removeAllNotifications(true);
377 ok(true,
"Starting test #" + testNum);
378 iframe.src = subtests[testNum-
1];
380 SimpleTest.waitForExplicitFinish();