toolkit/components/passwordmgr/test/browser/browser_passwordmgrdlg.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 function test() {
michael@0 6 waitForExplicitFinish();
michael@0 7
michael@0 8 let pwmgr = Cc["@mozilla.org/login-manager;1"].
michael@0 9 getService(Ci.nsILoginManager);
michael@0 10 pwmgr.removeAllLogins();
michael@0 11
michael@0 12 // Add some initial logins
michael@0 13 let urls = [
michael@0 14 "http://example.com/",
michael@0 15 "http://example.org/",
michael@0 16 "http://mozilla.com/",
michael@0 17 "http://mozilla.org/",
michael@0 18 "http://spreadfirefox.com/",
michael@0 19 "http://planet.mozilla.org/",
michael@0 20 "https://developer.mozilla.org/",
michael@0 21 "http://hg.mozilla.org/",
michael@0 22 "http://mxr.mozilla.org/",
michael@0 23 "http://feeds.mozilla.org/",
michael@0 24 ];
michael@0 25 let nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
michael@0 26 Ci.nsILoginInfo, "init");
michael@0 27 let logins = [
michael@0 28 new nsLoginInfo(urls[0], urls[0], null, "user", "password", "u1", "p1"),
michael@0 29 new nsLoginInfo(urls[1], urls[1], null, "username", "password", "u2", "p2"),
michael@0 30 new nsLoginInfo(urls[2], urls[2], null, "ehsan", "mypass", "u3", "p3"),
michael@0 31 new nsLoginInfo(urls[3], urls[3], null, "ehsan", "mypass", "u4", "p4"),
michael@0 32 new nsLoginInfo(urls[4], urls[4], null, "john", "smith", "u5", "p5"),
michael@0 33 new nsLoginInfo(urls[5], urls[5], null, "what?", "very secret", "u6", "p6"),
michael@0 34 new nsLoginInfo(urls[6], urls[6], null, "really?", "super secret", "u7", "p7"),
michael@0 35 new nsLoginInfo(urls[7], urls[7], null, "you sure?", "absolutely", "u8", "p8"),
michael@0 36 new nsLoginInfo(urls[8], urls[8], null, "my user name", "mozilla", "u9", "p9"),
michael@0 37 new nsLoginInfo(urls[9], urls[9], null, "my username", "mozilla.com", "u10", "p10"),
michael@0 38 ];
michael@0 39 logins.forEach(function (login) pwmgr.addLogin(login));
michael@0 40
michael@0 41 // Open the password manager dialog
michael@0 42 const PWMGR_DLG = "chrome://passwordmgr/content/passwordManager.xul";
michael@0 43 let pwmgrdlg = window.openDialog(PWMGR_DLG, "Toolkit:PasswordManager", "");
michael@0 44 SimpleTest.waitForFocus(doTest, pwmgrdlg);
michael@0 45
michael@0 46 // the meat of the test
michael@0 47 function doTest() {
michael@0 48 let doc = pwmgrdlg.document;
michael@0 49 let win = doc.defaultView;
michael@0 50 let filter = doc.getElementById("filter");
michael@0 51 let tree = doc.getElementById("signonsTree");
michael@0 52 let view = tree.treeBoxObject.view;
michael@0 53
michael@0 54 is(filter.value, "", "Filter box should initially be empty");
michael@0 55 is(view.rowCount, 10, "There should be 10 passwords initially");
michael@0 56
michael@0 57 // Prepare a set of tests
michael@0 58 // filter: the text entered in the filter search box
michael@0 59 // count: the number of logins which should match the respective filter
michael@0 60 // count2: the number of logins which should match the respective filter
michael@0 61 // if the passwords are being shown as well
michael@0 62 // Note: if a test doesn't have count2 set, count is used instead.
michael@0 63 let tests = [
michael@0 64 {filter: "pass", count: 0, count2: 4},
michael@0 65 {filter: "", count: 10}, // test clearing the filter
michael@0 66 {filter: "moz", count: 7},
michael@0 67 {filter: "mozi", count: 7},
michael@0 68 {filter: "mozil", count: 7},
michael@0 69 {filter: "mozill", count: 7},
michael@0 70 {filter: "mozilla", count: 7},
michael@0 71 {filter: "mozilla.com", count: 1, count2: 2},
michael@0 72 {filter: "user", count: 4},
michael@0 73 {filter: "user ", count: 1},
michael@0 74 {filter: " user", count: 2},
michael@0 75 {filter: "http", count: 10},
michael@0 76 {filter: "https", count: 1},
michael@0 77 {filter: "secret", count: 0, count2: 2},
michael@0 78 {filter: "secret!", count: 0},
michael@0 79 ];
michael@0 80
michael@0 81 let toggleCalls = 0;
michael@0 82 function toggleShowPasswords(func) {
michael@0 83 let toggleButton = doc.getElementById("togglePasswords");
michael@0 84 let showMode = (toggleCalls++ % 2) == 0;
michael@0 85
michael@0 86 // only watch for a confirmation dialog every other time being called
michael@0 87 if (showMode) {
michael@0 88 Services.ww.registerNotification(function (aSubject, aTopic, aData) {
michael@0 89 if (aTopic == "domwindowclosed")
michael@0 90 Services.ww.unregisterNotification(arguments.callee);
michael@0 91 else if (aTopic == "domwindowopened") {
michael@0 92 let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
michael@0 93 SimpleTest.waitForFocus(function() {
michael@0 94 EventUtils.sendKey("RETURN", win);
michael@0 95 }, win);
michael@0 96 }
michael@0 97 });
michael@0 98 }
michael@0 99
michael@0 100 Services.obs.addObserver(function (aSubject, aTopic, aData) {
michael@0 101 if (aTopic == "passwordmgr-password-toggle-complete") {
michael@0 102 Services.obs.removeObserver(arguments.callee, aTopic);
michael@0 103 func();
michael@0 104 }
michael@0 105 }, "passwordmgr-password-toggle-complete", false);
michael@0 106
michael@0 107 EventUtils.synthesizeMouse(toggleButton, 1, 1, {}, win);
michael@0 108 }
michael@0 109
michael@0 110 function runTests(mode, endFunction) {
michael@0 111 let testCounter = 0;
michael@0 112
michael@0 113 function setFilter(string) {
michael@0 114 filter.value = string;
michael@0 115 filter.doCommand();
michael@0 116 }
michael@0 117
michael@0 118 function runOneTest(test) {
michael@0 119 function tester() {
michael@0 120 is(view.rowCount, expected, expected + " logins should match '" + test.filter + "'");
michael@0 121 }
michael@0 122
michael@0 123 let expected;
michael@0 124 switch (mode) {
michael@0 125 case 1: // without showing passwords
michael@0 126 expected = test.count;
michael@0 127 break;
michael@0 128 case 2: // showing passwords
michael@0 129 expected = ("count2" in test) ? test.count2 : test.count;
michael@0 130 break;
michael@0 131 case 3: // toggle
michael@0 132 expected = test.count;
michael@0 133 tester();
michael@0 134 toggleShowPasswords(function () {
michael@0 135 expected = ("count2" in test) ? test.count2 : test.count;
michael@0 136 tester();
michael@0 137 toggleShowPasswords(proceed);
michael@0 138 });
michael@0 139 return;
michael@0 140 }
michael@0 141 tester();
michael@0 142 proceed();
michael@0 143 }
michael@0 144
michael@0 145 function proceed() {
michael@0 146 // run the next test if necessary or proceed with the tests
michael@0 147 if (testCounter != tests.length)
michael@0 148 runNextTest();
michael@0 149 else
michael@0 150 endFunction();
michael@0 151 }
michael@0 152
michael@0 153 function runNextTest() {
michael@0 154 let test = tests[testCounter++];
michael@0 155 setFilter(test.filter);
michael@0 156 setTimeout(runOneTest, 0, test);
michael@0 157 }
michael@0 158
michael@0 159 runNextTest();
michael@0 160 }
michael@0 161
michael@0 162 function step1() {
michael@0 163 runTests(1, step2);
michael@0 164 }
michael@0 165
michael@0 166 function step2() {
michael@0 167 toggleShowPasswords(function() {
michael@0 168 runTests(2, step3);
michael@0 169 });
michael@0 170 }
michael@0 171
michael@0 172 function step3() {
michael@0 173 toggleShowPasswords(function() {
michael@0 174 runTests(3, lastStep);
michael@0 175 });
michael@0 176 }
michael@0 177
michael@0 178 function lastStep() {
michael@0 179 // cleanup
michael@0 180 Services.ww.registerNotification(function (aSubject, aTopic, aData) {
michael@0 181 // unregister ourself
michael@0 182 Services.ww.unregisterNotification(arguments.callee);
michael@0 183
michael@0 184 pwmgr.removeAllLogins();
michael@0 185 finish();
michael@0 186 });
michael@0 187 pwmgrdlg.close();
michael@0 188 }
michael@0 189
michael@0 190 step1();
michael@0 191 }
michael@0 192 }

mercurial