Bug 507046 - browser_passwordmgrdlg.js test intermittent failure; r=dolske
[mozilla-central.git] / toolkit / components / passwordmgr / test / browser / browser_passwordmgrdlg.js
blob4f49f82d9348a598c453f25fa3f7e32db4c79c99
1 /* ***** BEGIN LICENSE BLOCK *****
2  *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  * 
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is mozilla.org code.
15  *
16  * The Initial Developer of the Original Code is
17  * Ehsan Akhgari.
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  * 
36  * ***** END LICENSE BLOCK ***** */
38 const Cc = Components.classes;
39 const Ci = Components.interfaces;
41 function test() {
42     waitForExplicitFinish();
44     let pwmgr = Cc["@mozilla.org/login-manager;1"].
45                 getService(Ci.nsILoginManager);
46     pwmgr.removeAllLogins();
48     // Add some initial logins
49     let urls = [
50         "http://example.com/",
51         "http://example.org/",
52         "http://mozilla.com/",
53         "http://mozilla.org/",
54         "http://spreadfirefox.com/",
55         "http://planet.mozilla.org/",
56         "https://developer.mozilla.org/",
57         "http://hg.mozilla.org/",
58         "http://mxr.mozilla.org/",
59         "http://feeds.mozilla.org/",
60     ];
61     let nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
62                                                  Ci.nsILoginInfo, "init");
63     let logins = [
64         new nsLoginInfo(urls[0], urls[0], null, "user", "password", "u1", "p1"),
65         new nsLoginInfo(urls[1], urls[1], null, "username", "password", "u2", "p2"),
66         new nsLoginInfo(urls[2], urls[2], null, "ehsan", "mypass", "u3", "p3"),
67         new nsLoginInfo(urls[3], urls[3], null, "ehsan", "mypass", "u4", "p4"),
68         new nsLoginInfo(urls[4], urls[4], null, "john", "smith", "u5", "p5"),
69         new nsLoginInfo(urls[5], urls[5], null, "what?", "very secret", "u6", "p6"),
70         new nsLoginInfo(urls[6], urls[6], null, "really?", "super secret", "u7", "p7"),
71         new nsLoginInfo(urls[7], urls[7], null, "you sure?", "absolutely", "u8", "p8"),
72         new nsLoginInfo(urls[8], urls[8], null, "my user name", "mozilla", "u9", "p9"),
73         new nsLoginInfo(urls[9], urls[9], null, "my username", "mozilla.com", "u10", "p10"),
74     ];
75     logins.forEach(function (login) pwmgr.addLogin(login));
77     // Detect when the password manager window is opened
78     let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
79              getService(Ci.nsIWindowWatcher);
80     let obs = {
81         observe: function(aSubject, aTopic, aData) {
82             // unregister ourself
83             ww.unregisterNotification(this);
85             let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
86             win.addEventListener("focus", function() {
87                 win.removeEventListener("focus", arguments.callee, true);
88                 setTimeout(doTest, 0);
89             }, true);
90         }
91     };
92     ww.registerNotification(obs);
94     // Open the password manager dialog
95     const PWMGR_DLG = "chrome://passwordmgr/content/passwordManager.xul";
96     let pwmgrdlg = window.openDialog(PWMGR_DLG, "Toolkit:PasswordManager", "");
98     // the meat of the test
99     function doTest() {
100         let doc = pwmgrdlg.document;
101         let win = doc.defaultView;
102         let filter = doc.getElementById("filter");
103         let tree = doc.getElementById("signonsTree");
104         let view = tree.treeBoxObject.view;
106         is(filter.value, "", "Filter box should initially be empty");
107         is(view.rowCount, 10, "There should be 10 passwords initially");
109         // Prepare a set of tests
110         //   filter: the text entered in the filter search box
111         //   count: the number of logins which should match the respective filter
112         //   count2: the number of logins which should match the respective filter
113         //           if the passwords are being shown as well
114         //   Note: if a test doesn't have count2 set, count is used instead.
115         let tests = [
116             {filter: "pass", count: 0, count2: 4},
117             {filter: "", count: 10}, // test clearing the filter
118             {filter: "moz", count: 7},
119             {filter: "mozi", count: 7},
120             {filter: "mozil", count: 7},
121             {filter: "mozill", count: 7},
122             {filter: "mozilla", count: 7},
123             {filter: "mozilla.com", count: 1, count2: 2},
124             {filter: "user", count: 4},
125             {filter: "user ", count: 1},
126             {filter: " user", count: 2},
127             {filter: "http", count: 10},
128             {filter: "https", count: 1},
129             {filter: "secret", count: 0, count2: 2},
130             {filter: "secret!", count: 0},
131         ];
133         let toggleCalls = 0;
134         function toggleShowPasswords(func) {
135             let toggleButton = doc.getElementById("togglePasswords");
136             let showMode = (toggleCalls++ % 2) == 0;
138             // only watch for a confirmation dialog every other time being called
139             if (showMode) {
140                 let obs = {
141                     observe: function(aSubject, aTopic, aData) {
142                         if (aTopic == "domwindowclosed")
143                             ww.unregisterNotification(this);
144                         else if (aTopic == "domwindowopened") {
145                             let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
146                             win.addEventListener("focus", function() {
147                                 win.removeEventListener("focus", arguments.callee, true);
148                                 setTimeout(function() {
149                                     EventUtils.synthesizeKey("VK_RETURN", {}, win)
150                                 }, 0);
151                             }, true);
152                         }
153                     }
154                 };
155                 ww.registerNotification(obs);
156             }
158             let obsSvc = Cc["@mozilla.org/observer-service;1"].
159                          getService(Ci.nsIObserverService);
160             obsSvc.addObserver({
161                 observe: function(aSubject, aTopic, aData) {
162                     if (aTopic == "passwordmgr-password-toggle-complete") {
163                         obsSvc.removeObserver(this, "passwordmgr-password-toggle-complete", false);
164                         func();
165                     }
166                 }
167             }, "passwordmgr-password-toggle-complete", false);
169             EventUtils.synthesizeMouse(toggleButton, 1, 1, {}, win);
170         }
172         function runTests(mode, endFunction) {
173             let testCounter = 0;
175             function setFilter(string) {
176                 filter.value = string;
177                 // dispatch the command event to the filter textbox
178                 let event = doc.createEvent("Events");
179                 event.initEvent("command", true, true);
180                 filter.dispatchEvent(event);
181             }
183             function runOneTest(test) {
184                 function tester() {
185                     is(view.rowCount, expected, expected + " logins should match '" + test.filter + "'");
186                 }
188                 let expected;
189                 switch (mode) {
190                 case 1: // without showing passwords
191                     expected = test.count;
192                     break;
193                 case 2: // showing passwords
194                     expected = ("count2" in test) ? test.count2 : test.count;
195                     break;
196                 case 3: // toggle
197                     expected = test.count;
198                     tester();
199                     toggleShowPasswords(function () {
200                         expected = ("count2" in test) ? test.count2 : test.count;
201                         tester();
202                         toggleShowPasswords(proceed);
203                     });
204                     return;
205                 }
206                 tester();
207                 proceed();
208             }
210             function proceed() {
211                 // run the next test if necessary or proceed with the tests
212                 if (testCounter != tests.length)
213                     runNextTest();
214                 else
215                     endFunction();
216             }
218             function runNextTest() {
219                 let test = tests[testCounter++];
220                 setFilter(test.filter);
221                 setTimeout(runOneTest, 0, test);
222             }
224             runNextTest();
225         }
227         function step1() {
228             runTests(1, step2);
229         }
231         function step2() {
232             toggleShowPasswords(function() {
233                 runTests(2, step3);
234             });
235         }
237         function step3() {
238             toggleShowPasswords(function() {
239                 runTests(3, lastStep);
240             });
241         }
243         function lastStep() {
244             // cleanup
245             pwmgrdlg.close();
246             pwmgr.removeAllLogins();
247             finish();
248         }
250         step1();
251     }