Bug 444959 ? Need tests for login manager notification bars. r=gavin
[wine-gecko.git] / toolkit / components / passwordmgr / test / test_notifications.html
blob614fd61a35e6341ebeb17ff4f3879b5c3788610d
1 <!DOCTYPE HTML>
2 <html>
3 <head>
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" />
9 </head>
10 <body>
11 Login Manager test: notifications
12 <p id="display"></p>
14 <div id="content" style="display: none">
15 <iframe id="iframe"></iframe>
16 </div>
18 <pre id="test">
19 <script class="testbody" type="text/javascript">
21 /** Test for Login Manager: notifications. **/
23 var subtests = [
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"
39 * getNotificationBox
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)
49 .rootTreeItem
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);
55 return notifyBox;
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);
77 * getNotificationBar
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");
96 var clicked = false;
97 for (var i = 0; i < buttons.length; i++) {
98 if (buttons[i].label == aButtonName) {
99 buttons[i].click();
100 clicked = true;
101 break;
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;
117 if (ignoreLoad) {
118 ok(true, "Ignoring load of subtest #" + testNum);
119 return;
121 ok(true, "Processing submission of subtest #" + testNum);
123 checkTest();
125 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];
134 } else {
135 ok(true, "notification tests finished.");
136 SimpleTest.finish();
141 // Remember, Never for This Site, Not Now
142 function checkTest() {
143 var bar;
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;
151 switch(testNum) {
153 /* Basic Yes/No/Never tests... */
155 case 1:
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");
161 break;
163 case 2:
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");
172 break;
174 case 3:
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);
184 break;
186 case 4:
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");
193 break;
195 case 5:
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");
201 // remove that login
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);
207 break;
209 /* signons.rememberSignons pref tests... */
211 case 6:
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);
220 break;
222 case 7:
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);
230 break;
232 case 8:
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");
239 break;
241 /* autocomplete=off tests... */
243 case 9:
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");
249 break;
251 case 10:
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");
257 break;
259 case 11:
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");
265 break;
267 /* no password field test... */
269 case 12:
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");
275 break;
277 default:
278 ok(false, "Unexpected call to checkTest for test #" + testNum);
282 // TODO:
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);
316 var testNum = 1;
317 ok(true, "Starting test #" + testNum);
318 iframe.src = subtests[testNum-1];
320 SimpleTest.waitForExplicitFinish();
321 </script>
322 </pre>
323 </body>
324 </html>