1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is mozilla.org code.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2009
19 * the Initial Developer. All Rights Reserved.
22 * Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
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.
36 * ***** END LICENSE BLOCK ***** */
38 const Cc = Components.classes;
39 const Ci = Components.interfaces;
42 waitForExplicitFinish();
44 let pwmgr = Cc["@mozilla.org/login-manager;1"].
45 getService(Ci.nsILoginManager);
46 pwmgr.removeAllLogins();
48 // Add some initial logins
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/",
61 let nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
62 Ci.nsILoginInfo, "init");
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"),
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);
81 observe: function(aSubject, aTopic, aData) {
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);
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
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.
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},
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
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)
155 ww.registerNotification(obs);
158 let obsSvc = Cc["@mozilla.org/observer-service;1"].
159 getService(Ci.nsIObserverService);
161 observe: function(aSubject, aTopic, aData) {
162 if (aTopic == "passwordmgr-password-toggle-complete") {
163 obsSvc.removeObserver(this, "passwordmgr-password-toggle-complete", false);
167 }, "passwordmgr-password-toggle-complete", false);
169 EventUtils.synthesizeMouse(toggleButton, 1, 1, {}, win);
172 function runTests(mode, endFunction) {
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);
183 function runOneTest(test) {
185 is(view.rowCount, expected, expected + " logins should match '" + test.filter + "'");
190 case 1: // without showing passwords
191 expected = test.count;
193 case 2: // showing passwords
194 expected = ("count2" in test) ? test.count2 : test.count;
197 expected = test.count;
199 toggleShowPasswords(function () {
200 expected = ("count2" in test) ? test.count2 : test.count;
202 toggleShowPasswords(proceed);
211 // run the next test if necessary or proceed with the tests
212 if (testCounter != tests.length)
218 function runNextTest() {
219 let test = tests[testCounter++];
220 setFilter(test.filter);
221 setTimeout(runOneTest, 0, test);
232 toggleShowPasswords(function() {
238 toggleShowPasswords(function() {
239 runTests(3, lastStep);
243 function lastStep() {
246 pwmgr.removeAllLogins();