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"
41 * Fetches the notification box for the specified window.
43 function getNotificationBox(aWindow) {
45 var chromeWin = aWindow
46 .QueryInterface(Ci.nsIInterfaceRequestor)
47 .getInterface(Ci.nsIWebNavigation)
48 .QueryInterface(Ci.nsIDocShellTreeItem)
50 .QueryInterface(Ci.nsIInterfaceRequestor)
51 .getInterface(Ci.nsIDOMWindow);
52 // Don't need .wrappedJSObject here, unlike when chrome does this.
53 var browserWin = chromeWin.browserDOMWindow;
54 var notifyBox = browserWin.getNotificationBox(aWindow);
57 // Find the
<browser> which contains aWindow, by looking
58 // through all the open windows and all the
<browsers> in each.
59 var wm = Cc[
"@mozilla.org/appshell/window-mediator;1"].
60 getService(Ci.nsIWindowMediator);
61 var enumerator = wm.getEnumerator(
"navigator:browser");
62 var tabbrowser = null;
63 var foundBrowser = null;
65 while (!foundBrowser && enumerator.hasMoreElements()) {
66 var win = enumerator.getNext();
67 tabbrowser = win.getBrowser();
68 foundBrowser = tabbrowser.getBrowserForDocument(aWindow.document);
71 // Return the notificationBox associated with the browser.
72 return tabbrowser.getNotificationBox(foundBrowser);
80 function getNotificationBar(aBox, aKind) {
81 ok(true,
"Looking for " + aKind +
" notification bar");
82 return aBox.getNotificationWithValue(aKind);
87 * clickNotificationButton
89 * Clicks the specified notification button.
91 function clickNotificationButton(aBar, aButtonName) {
92 // This is a bit of a hack. The notification doesn't have an API to
93 // trigger buttons, so we dive down into the implementation and twiddle
94 // the buttons directly.
95 var buttons = aBar.getElementsByTagName(
"button");
97 for (var i =
0; i < buttons.length; i++) {
98 if (buttons[i].label == aButtonName) {
105 ok(clicked,
"Clicked \"" + aButtonName + "\
" button");
109 var ignoreLoad = false;
110 function handleLoad(aEvent) {
111 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
113 // ignore every other load event ... We get one for loading the subtest (which
114 // we want to ignore), and another when the subtest's form submits itself
115 // (which we want to handle, to start the next test).
116 ignoreLoad = !ignoreLoad;
118 ok(true,
"Ignoring load of subtest #" + testNum);
121 ok(true,
"Processing submission of subtest #" + testNum);
128 // Remove any notification bar that might be lingering from a failed test.
129 notifyBox.removeAllNotifications(true);
131 if (testNum <= subtests.length) {
132 ok(true,
"Starting test #" + testNum);
133 iframe.src = subtests[testNum-
1];
135 ok(true,
"notification tests finished.");
141 // Remember, Never for This Site, Not Now
142 function checkTest() {
145 // The document generated from formsubmit.sjs contains the user/pass it
146 // received inside
<span id=
"blah">value
</span>
147 var gotUser = iframe.contentDocument.getElementById(
"user").textContent;
148 var gotPass = iframe.contentDocument.getElementById(
"pass").textContent;
153 /* Basic Yes/No/Never tests... */
156 is(gotUser,
"notifyu1",
"Checking submitted username");
157 is(gotPass,
"notifyp1",
"Checking submitted password");
158 bar = getNotificationBar(notifyBox,
"password-save");
159 ok(bar,
"got notification bar");
160 clickNotificationButton(bar,
"Not Now");
164 // Same subtest, this time click Never
165 is(gotUser,
"notifyu1",
"Checking submitted username");
166 is(gotPass,
"notifyp1",
"Checking submitted password");
167 bar = getNotificationBar(notifyBox,
"password-save");
168 ok(bar,
"got notification bar");
169 is(true, pwmgr.getLoginSavingEnabled(
"http://localhost:8888"),
170 "Checking for login saving enabled");
171 clickNotificationButton(bar,
"Never for This Site");
175 // Same subtest, make sure Never took effect
176 is(gotUser,
"notifyu1",
"Checking submitted username");
177 is(gotPass,
"notifyp1",
"Checking submitted password");
178 bar = getNotificationBar(notifyBox,
"password-save");
179 ok(!bar,
"checking for no notification bar");
180 is(false, pwmgr.getLoginSavingEnabled(
"http://localhost:8888"),
181 "Checking for login saving disabled");
182 // reenable login saving.
183 pwmgr.setLoginSavingEnabled(
"http://localhost:8888", true);
187 // Same subtest, this time click Remember
188 is(gotUser,
"notifyu1",
"Checking submitted username");
189 is(gotPass,
"notifyp1",
"Checking submitted password");
190 bar = getNotificationBar(notifyBox,
"password-save");
191 ok(bar,
"got notification bar");
192 clickNotificationButton(bar,
"Remember");
196 // Same subtest, make sure we didn't prompt for an existing login.
197 is(gotUser,
"notifyu1",
"Checking submitted username");
198 is(gotPass,
"notifyp1",
"Checking submitted password");
199 bar = getNotificationBar(notifyBox,
"password-save");
200 ok(!bar,
"checking for no notification bar");
202 var login = Cc[
"@mozilla.org/login-manager/loginInfo;1"].
203 createInstance(Ci.nsILoginInfo);
204 login.init(
"http://localhost:8888",
"http://localhost:8888", null,
205 "notifyu1",
"notifyp1",
"user",
"pass");
206 pwmgr.removeLogin(login);
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");
278 ok(false,
"Unexpected call to checkTest for test #" + testNum);
283 // * existing login test, when only one of form/login has a username --> no prompt
284 // * existing login test, form has different password --> change password, no save prompt
288 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
290 const Ci = Components.interfaces;
291 const Cc = Components.classes;
292 ok(Ci != null,
"Access Ci");
293 ok(Cc != null,
"Access Cc");
295 var pwmgr = Cc[
"@mozilla.org/login-manager;1"].
296 getService(Ci.nsILoginManager);
297 ok(pwmgr != null,
"Access pwmgr");
299 var prefs = Cc[
"@mozilla.org/preferences-service;1"].
300 getService(Ci.nsIPrefService);
301 ok(prefs != null,
"Access prefs");
302 prefs = prefs.getBranch(
"signon.");
303 ok(prefs != null,
"Access pref branch");
305 var iframe = document.getElementById(
"iframe");
306 iframe.onload = handleLoad;
308 // The notification box (not *bar*) is a constant, per-tab container. So, we
309 // only need to fetch it once.
310 var notifyBox = getNotificationBox(window.top);
311 ok(notifyBox,
"Got notification box");
313 // Remove any notification bars that might be left over from other tests.
314 notifyBox.removeAllNotifications(true);
317 ok(true,
"Starting test #" + testNum);
318 iframe.src = subtests[testNum-
1];
320 SimpleTest.waitForExplicitFinish();