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=
"/tests/SimpleTest/EventUtils.js"></script>
8 <script type=
"text/javascript" src=
"pwmgr_common.js"></script>
9 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
12 Login Manager test: multiple login autocomplete
15 <!-- we presumably can't hide the content for this test. -->
18 <!-- form1 tests multiple matching logins -->
19 <form id=
"form1" action=
"http://autocomplete:8888/formtest.js" onsubmit=
"return false;">
20 <input type=
"text" name=
"uname">
21 <input type=
"password" name=
"pword">
22 <button type=
"submit">Submit
</button>
25 <!-- other forms test single logins, with autocomplete=off set -->
26 <form id=
"form2" action=
"http://autocomplete2" onsubmit=
"return false;">
27 <input type=
"text" name=
"uname">
28 <input type=
"password" name=
"pword" autocomplete=
"off">
29 <button type=
"submit">Submit
</button>
32 <form id=
"form3" action=
"http://autocomplete2" onsubmit=
"return false;">
33 <input type=
"text" name=
"uname" autocomplete=
"off">
34 <input type=
"password" name=
"pword">
35 <button type=
"submit">Submit
</button>
38 <form id=
"form4" action=
"http://autocomplete2" onsubmit=
"return false;" autocomplete=
"off">
39 <input type=
"text" name=
"uname">
40 <input type=
"password" name=
"pword">
41 <button type=
"submit">Submit
</button>
44 <form id=
"form5" action=
"http://autocomplete2" onsubmit=
"return false;">
45 <input type=
"text" name=
"uname" autocomplete=
"off">
46 <input type=
"password" name=
"pword" autocomplete=
"off">
47 <button type=
"submit">Submit
</button>
51 <form id=
"form6" action=
"http://autocomplete2" onsubmit=
"return false;">
52 <input type=
"text" name=
"uname">
53 <input type=
"password" name=
"pword">
54 <button type=
"submit">Submit
</button>
57 <!-- This form will be manipulated to insert a different username field. -->
58 <form id=
"form7" action=
"http://autocomplete3" onsubmit=
"return false;">
59 <input type=
"text" name=
"uname">
60 <input type=
"password" name=
"pword">
61 <button type=
"submit">Submit
</button>
66 <script class=
"testbody" type=
"text/javascript">
68 /** Test for Login Manager: multiple login autocomplete. **/
70 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
72 var uname = $_(
1,
"uname");
73 var pword = $_(
1,
"pword");
74 const shiftModifier = Components.interfaces.nsIDOMNSEvent.SHIFT_MASK;
76 // Get the pwmgr service
77 var pwmgr = Components.classes[
"@mozilla.org/login-manager;1"]
78 .getService(Components.interfaces.nsILoginManager);
79 ok(pwmgr != null,
"nsLoginManager service");
81 // Create some logins just for this form, since we'll be deleting them.
83 Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1",
84 Components.interfaces.nsILoginInfo,
"init");
85 ok(nsLoginInfo != null,
"nsLoginInfo constructor");
88 var login1 = new nsLoginInfo(
89 "http://localhost:8888",
"http://autocomplete:8888", null,
90 "tempuser1",
"temppass1",
"uname",
"pword");
92 var login2 = new nsLoginInfo(
93 "http://localhost:8888",
"http://autocomplete:8888", null,
94 "testuser2",
"testpass2",
"uname",
"pword");
96 var login3 = new nsLoginInfo(
97 "http://localhost:8888",
"http://autocomplete:8888", null,
98 "testuser3",
"testpass3",
"uname",
"pword");
100 var login4 = new nsLoginInfo(
101 "http://localhost:8888",
"http://autocomplete:8888", null,
102 "zzzuser4",
"zzzpass4",
"uname",
"pword");
104 // login
5 only used in the single-user forms
105 var login5 = new nsLoginInfo(
106 "http://localhost:8888",
"http://autocomplete2", null,
107 "singleuser5",
"singlepass5",
"uname",
"pword");
109 var login6A = new nsLoginInfo(
110 "http://localhost:8888",
"http://autocomplete3", null,
111 "form7user1",
"form7pass1",
"uname",
"pword");
112 var login6B = new nsLoginInfo(
113 "http://localhost:8888",
"http://autocomplete3", null,
114 "form7user2",
"form7pass2",
"uname",
"pword");
116 // try/catch in case someone runs the tests manually, twice.
118 pwmgr.addLogin(login1);
119 pwmgr.addLogin(login2);
120 pwmgr.addLogin(login3);
121 pwmgr.addLogin(login4);
122 pwmgr.addLogin(login5);
123 pwmgr.addLogin(login6A);
124 pwmgr.addLogin(login6B);
126 ok(false,
"addLogin threw: " + e);
130 // Restore the form to the default state.
131 function restoreForm() {
138 // Check for expected username/password in form.
139 function checkACForm(expectedUsername, expectedPassword) {
140 var formID = uname.parentNode.id;
141 is(uname.value, expectedUsername,
"Checking " + formID +
" username");
142 is(pword.value, expectedPassword,
"Checking " + formID +
" password");
148 * Main section of test...
150 * This is a bit hacky, because the events are either being sent or
151 * processes asynchronously, so we need to interrupt our flow with lots of
152 * setTimeout() calls. The case statements are executed in order, one per
155 function runTest(testNum) {
156 // Seems we need to enable this again, or sendKeyEvent() complaints.
157 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
158 ok(true,
"Starting test #" + testNum);
162 // Make sure initial form is empty.
164 // Trigger autocomplete popup
172 checkACForm(
"",
""); // value shouldn't update
173 doKey(
"return"); // not
"enter"!
174 checkACForm(
"tempuser1",
"temppass1");
176 // Trigger autocomplete popup
182 // Check second entry
185 doKey(
"return"); // not
"enter"!
186 checkACForm(
"testuser2",
"testpass2");
188 // Trigger autocomplete popup
199 checkACForm(
"testuser3",
"testpass3");
201 // Trigger autocomplete popup
207 // Check fourth entry
213 checkACForm(
"zzzuser4",
"zzzpass4");
215 // Trigger autocomplete popup
221 // Check first entry (wraparound)
226 doKey(
"down"); // deselects
229 checkACForm(
"tempuser1",
"temppass1");
231 // Trigger autocomplete popup
237 // Check the last entry via arrow-up
240 checkACForm(
"zzzuser4",
"zzzpass4");
242 // Trigger autocomplete popup
248 // Check the last entry via arrow-up
249 doKey(
"down"); // select first entry
250 doKey(
"up"); // selects nothing!
251 doKey(
"up"); // select last entry
253 checkACForm(
"zzzuser4",
"zzzpass4");
255 // Trigger autocomplete popup
261 // Check the last entry via arrow-up (wraparound)
263 doKey(
"up"); // deselects
264 doKey(
"up"); // last entry
267 doKey(
"up"); // first entry
268 doKey(
"up"); // deselects
269 doKey(
"up"); // last entry
271 checkACForm(
"zzzuser4",
"zzzpass4");
273 // Trigger autocomplete popup
279 // Set first entry w/o triggering autocomplete
282 checkACForm(
"tempuser1",
""); // empty password
284 // Trigger autocomplete popup
290 // Set first entry w/o triggering autocomplete
293 checkACForm(
"tempuser1",
""); // empty password
295 // Trigger autocomplete popup
301 // Check first entry (page up)
306 checkACForm(
"tempuser1",
"temppass1");
308 // Trigger autocomplete popup
314 // Check last entry (page down)
318 checkACForm(
"zzzuser4",
"zzzpass4");
320 // Trigger autocomplete popup
328 // XXX This isn't working in the test suite, seems to fubar any later tests.
332 // checkACForm(
"",
"");
334 // Trigger autocomplete popup
339 // XXX tried sending character
"t" before/during dropdown to test
340 // filtering, but had no luck. Seemed like the character was getting lost.
341 // Setting uname.value didn't seem to work either. This works with a human
342 // driver, so I'm not sure what's up.
346 // Delete the first entry (of
4),
"tempuser1"
349 numLogins = pwmgr.countLogins(
"http://localhost:8888",
"http://autocomplete:8888", null);
350 is(numLogins,
4,
"Correct number of logins before deleting one");
352 // On OS X, shift-backspace and shift-delete work, just delete does not.
353 // On Win/Linux, shift-backspace does not work, delete and shift-delete do.
354 doKey(
"delete", shiftModifier);
357 numLogins = pwmgr.countLogins(
"http://localhost:8888",
"http://autocomplete:8888", null);
358 is(numLogins,
3,
"Correct number of logins after deleting one");
360 checkACForm(
"testuser2",
"testpass2");
362 // Trigger autocomplete popup
368 // Check the new first entry (of
3)
371 checkACForm(
"testuser2",
"testpass2");
373 // Trigger autocomplete popup
379 // Delete the second entry (of
3),
"testuser3"
382 doKey(
"delete", shiftModifier);
384 numLogins = pwmgr.countLogins(
"http://localhost:8888",
"http://autocomplete:8888", null);
385 is(numLogins,
2,
"Correct number of logins after deleting one");
387 checkACForm(
"zzzuser4",
"zzzpass4");
389 // Trigger autocomplete popup
395 // Check the new second entry (of
2)
398 checkACForm(
"testuser2",
"testpass2");
400 // Trigger autocomplete popup
406 // Delete the last entry (of
2),
"zzzuser4"
409 doKey(
"delete", shiftModifier);
411 numLogins = pwmgr.countLogins(
"http://localhost:8888",
"http://autocomplete:8888", null);
412 is(numLogins,
1,
"Correct number of logins after deleting one");
414 checkACForm(
"testuser2",
"testpass2");
416 // Trigger autocomplete popup
422 // Check the new second entry (of
2)
425 checkACForm(
"testuser2",
"testpass2");
427 // Trigger autocomplete popup
433 // Delete the only remaining entry,
"testuser2"
435 doKey(
"delete", shiftModifier);
438 numLogins = pwmgr.countLogins(
"http://localhost:8888",
"http://autocomplete:8888", null);
439 is(numLogins,
0,
"Correct number of logins after deleting one");
444 /* Tests for single-user forms with autocomplete=off */
447 // Turn our attention to form2
448 uname = $_(
2,
"uname");
449 pword = $_(
2,
"pword");
452 // Trigger autocomplete popup
460 checkACForm(
"",
""); // value shouldn't update
461 doKey(
"return"); // not
"enter"!
462 checkACForm(
"singleuser5",
"singlepass5");
466 // Turn our attention to form3
467 uname = $_(
3,
"uname");
468 pword = $_(
3,
"pword");
471 // Trigger autocomplete popup
479 checkACForm(
"",
""); // value shouldn't update
480 doKey(
"return"); // not
"enter"!
481 checkACForm(
"singleuser5",
"singlepass5");
485 // Turn our attention to form4
486 uname = $_(
4,
"uname");
487 pword = $_(
4,
"pword");
490 // Trigger autocomplete popup
498 checkACForm(
"",
""); // value shouldn't update
499 doKey(
"return"); // not
"enter"!
500 checkACForm(
"singleuser5",
"singlepass5");
504 // Turn our attention to form5
505 uname = $_(
5,
"uname");
506 pword = $_(
5,
"pword");
509 // Trigger autocomplete popup
517 checkACForm(
"",
""); // value shouldn't update
518 doKey(
"return"); // not
"enter"!
519 checkACForm(
"singleuser5",
"singlepass5");
523 // Turn our attention to form6
524 // (this is a control, w/o autocomplete=off, to ensure the login
525 // that was being suppressed would have been filled in otherwise)
526 uname = $_(
6,
"uname");
527 pword = $_(
6,
"pword");
528 checkACForm(
"singleuser5",
"singlepass5");
530 pwmgr.removeLogin(login5);
535 // Turn our attention to form7
536 uname = $_(
7,
"uname");
537 pword = $_(
7,
"pword");
540 // Insert a new username field into the form. We'll then make sure
541 // that invoking the autocomplete doesn't try to fill the form.
542 var newField = document.createElement(
"input");
543 newField.setAttribute(
"type",
"text");
544 newField.setAttribute(
"name",
"uname2");
545 pword.parentNode.insertBefore(newField, pword);
546 is($_(
7,
"uname2").value,
"",
"Verifying empty uname2");;
548 // Delete login6B. It was created just to prevent filling in a login
549 // automatically, removing it makes it more likely that we'll catch a
550 // future regression with form filling here.
551 pwmgr.removeLogin(login6B);
553 // Trigger autocomplete popup
561 checkACForm(
"",
""); // value shouldn't update
562 doKey(
"return"); // not
"enter"!
563 // The form changes, so we expect the old username field to get the
564 // selected autocomplete value, but neither the new username field nor
565 // the password field should have any values filled in.
566 checkACForm(
"form7user1",
"");
567 is($_(
7,
"uname2").value,
"",
"Verifying empty uname2");;
569 pwmgr.removeLogin(login6A);
576 ok(false,
"Unexpected invocataion of test #" + testNum);
581 setTimeout(runTest,
50, testNum +
1); // XXX
40ms was too slow, why?
585 function startTest() {
589 window.onload = startTest;
591 SimpleTest.waitForExplicitFinish();