1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/passwordmgr/test/browser/browser_passwordmgrdlg.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,192 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function test() { 1.9 + waitForExplicitFinish(); 1.10 + 1.11 + let pwmgr = Cc["@mozilla.org/login-manager;1"]. 1.12 + getService(Ci.nsILoginManager); 1.13 + pwmgr.removeAllLogins(); 1.14 + 1.15 + // Add some initial logins 1.16 + let urls = [ 1.17 + "http://example.com/", 1.18 + "http://example.org/", 1.19 + "http://mozilla.com/", 1.20 + "http://mozilla.org/", 1.21 + "http://spreadfirefox.com/", 1.22 + "http://planet.mozilla.org/", 1.23 + "https://developer.mozilla.org/", 1.24 + "http://hg.mozilla.org/", 1.25 + "http://mxr.mozilla.org/", 1.26 + "http://feeds.mozilla.org/", 1.27 + ]; 1.28 + let nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", 1.29 + Ci.nsILoginInfo, "init"); 1.30 + let logins = [ 1.31 + new nsLoginInfo(urls[0], urls[0], null, "user", "password", "u1", "p1"), 1.32 + new nsLoginInfo(urls[1], urls[1], null, "username", "password", "u2", "p2"), 1.33 + new nsLoginInfo(urls[2], urls[2], null, "ehsan", "mypass", "u3", "p3"), 1.34 + new nsLoginInfo(urls[3], urls[3], null, "ehsan", "mypass", "u4", "p4"), 1.35 + new nsLoginInfo(urls[4], urls[4], null, "john", "smith", "u5", "p5"), 1.36 + new nsLoginInfo(urls[5], urls[5], null, "what?", "very secret", "u6", "p6"), 1.37 + new nsLoginInfo(urls[6], urls[6], null, "really?", "super secret", "u7", "p7"), 1.38 + new nsLoginInfo(urls[7], urls[7], null, "you sure?", "absolutely", "u8", "p8"), 1.39 + new nsLoginInfo(urls[8], urls[8], null, "my user name", "mozilla", "u9", "p9"), 1.40 + new nsLoginInfo(urls[9], urls[9], null, "my username", "mozilla.com", "u10", "p10"), 1.41 + ]; 1.42 + logins.forEach(function (login) pwmgr.addLogin(login)); 1.43 + 1.44 + // Open the password manager dialog 1.45 + const PWMGR_DLG = "chrome://passwordmgr/content/passwordManager.xul"; 1.46 + let pwmgrdlg = window.openDialog(PWMGR_DLG, "Toolkit:PasswordManager", ""); 1.47 + SimpleTest.waitForFocus(doTest, pwmgrdlg); 1.48 + 1.49 + // the meat of the test 1.50 + function doTest() { 1.51 + let doc = pwmgrdlg.document; 1.52 + let win = doc.defaultView; 1.53 + let filter = doc.getElementById("filter"); 1.54 + let tree = doc.getElementById("signonsTree"); 1.55 + let view = tree.treeBoxObject.view; 1.56 + 1.57 + is(filter.value, "", "Filter box should initially be empty"); 1.58 + is(view.rowCount, 10, "There should be 10 passwords initially"); 1.59 + 1.60 + // Prepare a set of tests 1.61 + // filter: the text entered in the filter search box 1.62 + // count: the number of logins which should match the respective filter 1.63 + // count2: the number of logins which should match the respective filter 1.64 + // if the passwords are being shown as well 1.65 + // Note: if a test doesn't have count2 set, count is used instead. 1.66 + let tests = [ 1.67 + {filter: "pass", count: 0, count2: 4}, 1.68 + {filter: "", count: 10}, // test clearing the filter 1.69 + {filter: "moz", count: 7}, 1.70 + {filter: "mozi", count: 7}, 1.71 + {filter: "mozil", count: 7}, 1.72 + {filter: "mozill", count: 7}, 1.73 + {filter: "mozilla", count: 7}, 1.74 + {filter: "mozilla.com", count: 1, count2: 2}, 1.75 + {filter: "user", count: 4}, 1.76 + {filter: "user ", count: 1}, 1.77 + {filter: " user", count: 2}, 1.78 + {filter: "http", count: 10}, 1.79 + {filter: "https", count: 1}, 1.80 + {filter: "secret", count: 0, count2: 2}, 1.81 + {filter: "secret!", count: 0}, 1.82 + ]; 1.83 + 1.84 + let toggleCalls = 0; 1.85 + function toggleShowPasswords(func) { 1.86 + let toggleButton = doc.getElementById("togglePasswords"); 1.87 + let showMode = (toggleCalls++ % 2) == 0; 1.88 + 1.89 + // only watch for a confirmation dialog every other time being called 1.90 + if (showMode) { 1.91 + Services.ww.registerNotification(function (aSubject, aTopic, aData) { 1.92 + if (aTopic == "domwindowclosed") 1.93 + Services.ww.unregisterNotification(arguments.callee); 1.94 + else if (aTopic == "domwindowopened") { 1.95 + let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget); 1.96 + SimpleTest.waitForFocus(function() { 1.97 + EventUtils.sendKey("RETURN", win); 1.98 + }, win); 1.99 + } 1.100 + }); 1.101 + } 1.102 + 1.103 + Services.obs.addObserver(function (aSubject, aTopic, aData) { 1.104 + if (aTopic == "passwordmgr-password-toggle-complete") { 1.105 + Services.obs.removeObserver(arguments.callee, aTopic); 1.106 + func(); 1.107 + } 1.108 + }, "passwordmgr-password-toggle-complete", false); 1.109 + 1.110 + EventUtils.synthesizeMouse(toggleButton, 1, 1, {}, win); 1.111 + } 1.112 + 1.113 + function runTests(mode, endFunction) { 1.114 + let testCounter = 0; 1.115 + 1.116 + function setFilter(string) { 1.117 + filter.value = string; 1.118 + filter.doCommand(); 1.119 + } 1.120 + 1.121 + function runOneTest(test) { 1.122 + function tester() { 1.123 + is(view.rowCount, expected, expected + " logins should match '" + test.filter + "'"); 1.124 + } 1.125 + 1.126 + let expected; 1.127 + switch (mode) { 1.128 + case 1: // without showing passwords 1.129 + expected = test.count; 1.130 + break; 1.131 + case 2: // showing passwords 1.132 + expected = ("count2" in test) ? test.count2 : test.count; 1.133 + break; 1.134 + case 3: // toggle 1.135 + expected = test.count; 1.136 + tester(); 1.137 + toggleShowPasswords(function () { 1.138 + expected = ("count2" in test) ? test.count2 : test.count; 1.139 + tester(); 1.140 + toggleShowPasswords(proceed); 1.141 + }); 1.142 + return; 1.143 + } 1.144 + tester(); 1.145 + proceed(); 1.146 + } 1.147 + 1.148 + function proceed() { 1.149 + // run the next test if necessary or proceed with the tests 1.150 + if (testCounter != tests.length) 1.151 + runNextTest(); 1.152 + else 1.153 + endFunction(); 1.154 + } 1.155 + 1.156 + function runNextTest() { 1.157 + let test = tests[testCounter++]; 1.158 + setFilter(test.filter); 1.159 + setTimeout(runOneTest, 0, test); 1.160 + } 1.161 + 1.162 + runNextTest(); 1.163 + } 1.164 + 1.165 + function step1() { 1.166 + runTests(1, step2); 1.167 + } 1.168 + 1.169 + function step2() { 1.170 + toggleShowPasswords(function() { 1.171 + runTests(2, step3); 1.172 + }); 1.173 + } 1.174 + 1.175 + function step3() { 1.176 + toggleShowPasswords(function() { 1.177 + runTests(3, lastStep); 1.178 + }); 1.179 + } 1.180 + 1.181 + function lastStep() { 1.182 + // cleanup 1.183 + Services.ww.registerNotification(function (aSubject, aTopic, aData) { 1.184 + // unregister ourself 1.185 + Services.ww.unregisterNotification(arguments.callee); 1.186 + 1.187 + pwmgr.removeAllLogins(); 1.188 + finish(); 1.189 + }); 1.190 + pwmgrdlg.close(); 1.191 + } 1.192 + 1.193 + step1(); 1.194 + } 1.195 +}