Bug 394611 - Always prompt the user before changing a stored password. r=gavin
[wine-gecko.git] / toolkit / components / passwordmgr / test / test_notifications.html
blob73edc600ed8c4521190a7c337d6aca44cd1f723e
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 <script type="text/javascript" src="notification_common.js"></script>
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
10 </head>
11 <body>
12 Login Manager test: notifications
13 <p id="display"></p>
15 <div id="content" style="display: none">
16 <iframe id="iframe"></iframe>
17 </div>
19 <pre id="test">
20 <script class="testbody" type="text/javascript">
22 /** Test for Login Manager: notifications. **/
24 var subtests = [
25 "subtst_notifications_1.html", // 1
26 "subtst_notifications_1.html", // 2
27 "subtst_notifications_1.html", // 3
28 "subtst_notifications_1.html", // 4
29 "subtst_notifications_1.html", // 5
30 "subtst_notifications_1.html", // 6
31 "subtst_notifications_1.html", // 7
32 "subtst_notifications_1.html", // 8
33 "subtst_notifications_2.html", // 9
34 "subtst_notifications_3.html", // 10
35 "subtst_notifications_4.html", // 11
36 "subtst_notifications_5.html", // 12
37 "subtst_notifications_1.html", // 13
38 "subtst_notifications_6.html", // 14
39 "subtst_notifications_1.html", // 15
40 "subtst_notifications_6.html", // 16
41 "subtst_notifications_8.html", // 17
42 "subtst_notifications_8.html", // 18
43 "subtst_notifications_9.html", // 19
44 "subtst_notifications_10.html" // 20
48 var ignoreLoad = false;
49 function handleLoad(aEvent) {
50 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
52 // ignore every other load event ... We get one for loading the subtest (which
53 // we want to ignore), and another when the subtest's form submits itself
54 // (which we want to handle, to start the next test).
55 ignoreLoad = !ignoreLoad;
56 if (ignoreLoad) {
57 ok(true, "Ignoring load of subtest #" + testNum);
58 return;
60 ok(true, "Processing submission of subtest #" + testNum);
62 checkTest();
64 testNum++;
67 // Remove any notification bar that might be lingering from a failed test.
68 notifyBox.removeAllNotifications(true);
70 if (testNum <= subtests.length) {
71 ok(true, "Starting test #" + testNum);
72 iframe.src = subtests[testNum-1];
73 } else {
74 ok(true, "notification tests finished.");
75 SimpleTest.finish();
80 // Remember, Never for This Site, Not Now
81 function checkTest() {
82 var bar;
84 // The document generated from formsubmit.sjs contains the user/pass it
85 // received inside <span id="blah">value</span>
86 var gotUser = iframe.contentDocument.getElementById("user").textContent;
87 var gotPass = iframe.contentDocument.getElementById("pass").textContent;
90 switch(testNum) {
92 /* Basic Yes/No/Never tests... */
94 case 1:
95 is(gotUser, "notifyu1", "Checking submitted username");
96 is(gotPass, "notifyp1", "Checking submitted password");
97 bar = getNotificationBar(notifyBox, "password-save");
98 ok(bar, "got notification bar");
99 clickNotificationButton(bar, "Not Now");
100 break;
102 case 2:
103 // Same subtest, this time click Never
104 is(gotUser, "notifyu1", "Checking submitted username");
105 is(gotPass, "notifyp1", "Checking submitted password");
106 bar = getNotificationBar(notifyBox, "password-save");
107 ok(bar, "got notification bar");
108 is(true, pwmgr.getLoginSavingEnabled("http://localhost:8888"),
109 "Checking for login saving enabled");
110 clickNotificationButton(bar, "Never for This Site");
111 break;
113 case 3:
114 // Same subtest, make sure Never took effect
115 is(gotUser, "notifyu1", "Checking submitted username");
116 is(gotPass, "notifyp1", "Checking submitted password");
117 bar = getNotificationBar(notifyBox, "password-save");
118 ok(!bar, "checking for no notification bar");
119 is(false, pwmgr.getLoginSavingEnabled("http://localhost:8888"),
120 "Checking for login saving disabled");
121 // reenable login saving.
122 pwmgr.setLoginSavingEnabled("http://localhost:8888", true);
123 break;
125 case 4:
126 // Same subtest, this time click Remember
127 is(gotUser, "notifyu1", "Checking submitted username");
128 is(gotPass, "notifyp1", "Checking submitted password");
129 bar = getNotificationBar(notifyBox, "password-save");
130 ok(bar, "got notification bar");
131 clickNotificationButton(bar, "Remember");
132 break;
134 case 5:
135 // Same subtest, make sure we didn't prompt for an existing login.
136 is(gotUser, "notifyu1", "Checking submitted username");
137 is(gotPass, "notifyp1", "Checking submitted password");
138 bar = getNotificationBar(notifyBox, "password-save");
139 ok(!bar, "checking for no notification bar");
140 // remove that login
141 pwmgr.removeLogin(login1);
142 break;
144 /* signons.rememberSignons pref tests... */
146 case 6:
147 // Same subtest, make sure we're getting the bar again.
148 is(gotUser, "notifyu1", "Checking submitted username");
149 is(gotPass, "notifyp1", "Checking submitted password");
150 bar = getNotificationBar(notifyBox, "password-save");
151 ok(bar, "got notification bar");
152 clickNotificationButton(bar, "Not Now");
153 // Change prefs to no longer remember signons
154 prefs.setBoolPref("rememberSignons", false);
155 break;
157 case 7:
158 // Same subtest, make sure we're not prompting.
159 is(gotUser, "notifyu1", "Checking submitted username");
160 is(gotPass, "notifyp1", "Checking submitted password");
161 bar = getNotificationBar(notifyBox, "password-save");
162 ok(!bar, "checking for no notification bar");
163 // Change prefs to remember signons again
164 prefs.setBoolPref("rememberSignons", true);
165 break;
167 case 8:
168 // Same subtest, make sure we're getting the bar again.
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 clickNotificationButton(bar, "Not Now");
174 break;
176 /* autocomplete=off tests... */
178 case 9:
179 // Check for no notification bar when autocomplete=off present
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 break;
186 case 10:
187 // Check for no notification bar when autocomplete=off present
188 is(gotUser, "notifyu1", "Checking submitted username");
189 is(gotPass, "notifyp1", "Checking submitted password");
190 bar = getNotificationBar(notifyBox, "password-save");
191 ok(!bar, "checking for no notification bar");
192 break;
194 case 11:
195 // Check for no notification bar when autocomplete=off present
196 is(gotUser, "notifyu1", "Checking submitted username");
197 is(gotPass, "notifyp1", "Checking submitted password");
198 bar = getNotificationBar(notifyBox, "password-save");
199 ok(!bar, "checking for no notification bar");
200 break;
202 /* no password field test... */
204 case 12:
205 // Check for no notification bar when no password field present
206 is(gotUser, "notifyu1", "Checking submitted username");
207 is(gotPass, "null", "Checking submitted password");
208 bar = getNotificationBar(notifyBox, "password-save");
209 ok(!bar, "checking for no notification bar");
211 // Add login for the next test.
212 pwmgr.addLogin(login2);
213 break;
215 case 13:
216 // Check for no notification bar when existing pw-only login matches form.
217 is(gotUser, "notifyu1", "Checking submitted username");
218 is(gotPass, "notifyp1", "Checking submitted password");
219 bar = getNotificationBar(notifyBox, "password-save");
220 ok(!bar, "checking for no notification bar");
221 pwmgr.removeLogin(login2);
223 // Add login for the next test
224 pwmgr.addLogin(login1);
225 break;
227 case 14:
228 // Check for no notification bar when pw-only form matches existing login.
229 is(gotUser, "null", "Checking submitted username");
230 is(gotPass, "notifyp1", "Checking submitted password");
231 bar = getNotificationBar(notifyBox, "password-save");
232 ok(!bar, "checking for no notification bar");
233 pwmgr.removeLogin(login1);
235 // Add login for the next test
236 pwmgr.addLogin(login2B);
237 break;
239 case 15:
240 // Check for notification bar when existing pw-only login doesn't match form.
241 is(gotUser, "notifyu1", "Checking submitted username");
242 is(gotPass, "notifyp1", "Checking submitted password");
243 bar = getNotificationBar(notifyBox, "password-save");
244 ok(bar, "got notification bar");
245 clickNotificationButton(bar, "Not Now");
246 pwmgr.removeLogin(login2B);
248 // Add login for the next test
249 pwmgr.addLogin(login1B);
250 break;
252 case 16:
253 // Check for notification bar when pw-only form doesn't match existing login.
254 is(gotUser, "null", "Checking submitted username");
255 is(gotPass, "notifyp1", "Checking submitted password");
256 bar = getNotificationBar(notifyBox, "password-save");
257 ok(bar, "got notification bar");
258 clickNotificationButton(bar, "Not Now");
259 pwmgr.removeLogin(login1B);
261 // Add login for the next tests
262 pwmgr.addLogin(login1);
263 break;
265 case 17:
266 // Check for change-password bar, u+p login on u+p form. (not changed)
267 is(gotUser, "notifyu1", "Checking submitted username");
268 is(gotPass, "pass2", "Checking submitted password");
269 bar = getNotificationBar(notifyBox, "password-change");
270 ok(bar, "got notification bar");
271 clickNotificationButton(bar, "Don't Change");
272 break;
274 case 18:
275 // Check for change-password bar, u+p login on u+p form.
276 is(gotUser, "notifyu1", "Checking submitted username");
277 is(gotPass, "pass2", "Checking submitted password");
278 bar = getNotificationBar(notifyBox, "password-change");
279 ok(bar, "got notification bar");
280 clickNotificationButton(bar, "Change");
282 // cleanup
283 login1.password = "pass2";
284 pwmgr.removeLogin(login1);
285 login1.password = "notifyp1";
287 // Add login for the next test
288 pwmgr.addLogin(login2);
289 break;
291 // ...can't change a u+p login on a p-only form...
293 case 19:
294 // Check for change-password bar, p-only login on u+p form.
295 // (needed a different subtest for this because the login created in
296 // test_0init was interfering)
297 is(gotUser, "", "Checking submitted username");
298 is(gotPass, "pass2", "Checking submitted password");
299 bar = getNotificationBar(notifyBox, "password-change");
300 ok(bar, "got notification bar");
301 clickNotificationButton(bar, "Change");
302 break;
304 case 20:
305 // Check for change-password bar, p-only login on p-only form.
306 is(gotUser, "null", "Checking submitted username");
307 is(gotPass, "notifyp1", "Checking submitted password");
308 bar = getNotificationBar(notifyBox, "password-change");
309 ok(bar, "got notification bar");
310 clickNotificationButton(bar, "Change");
312 pwmgr.removeLogin(login2);
313 break;
315 default:
316 ok(false, "Unexpected call to checkTest for test #" + testNum);
320 // TODO:
321 // * existing login test, form has different password --> change password, no save prompt
325 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
327 const Ci = Components.interfaces;
328 const Cc = Components.classes;
329 ok(Ci != null, "Access Ci");
330 ok(Cc != null, "Access Cc");
332 var pwmgr = Cc["@mozilla.org/login-manager;1"].
333 getService(Ci.nsILoginManager);
334 ok(pwmgr != null, "Access pwmgr");
336 var prefs = Cc["@mozilla.org/preferences-service;1"].
337 getService(Ci.nsIPrefService);
338 ok(prefs != null, "Access prefs");
339 prefs = prefs.getBranch("signon.");
340 ok(prefs != null, "Access pref branch");
342 var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
343 Ci.nsILoginInfo, "init");
344 var login1 = new nsLoginInfo("http://localhost:8888", "http://localhost:8888", null,
345 "notifyu1", "notifyp1", "user", "pass");
346 var login2 = new nsLoginInfo("http://localhost:8888", "http://localhost:8888", null,
347 "", "notifyp1", "", "pass");
348 var login1B = new nsLoginInfo("http://localhost:8888", "http://localhost:8888", null,
349 "notifyu1B", "notifyp1B", "user", "pass");
350 var login2B = new nsLoginInfo("http://localhost:8888", "http://localhost:8888", null,
351 "", "notifyp1B", "", "pass");
353 var iframe = document.getElementById("iframe");
354 iframe.onload = handleLoad;
356 // The notification box (not *bar*) is a constant, per-tab container. So, we
357 // only need to fetch it once.
358 var notifyBox = getNotificationBox(window.top);
359 ok(notifyBox, "Got notification box");
361 // Remove any notification bars that might be left over from other tests.
362 notifyBox.removeAllNotifications(true);
364 var testNum = 1;
365 ok(true, "Starting test #" + testNum);
366 iframe.src = subtests[testNum-1];
368 SimpleTest.waitForExplicitFinish();
369 </script>
370 </pre>
371 </body>
372 </html>