michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: let pwmgr = Cc["@mozilla.org/login-manager;1"]. michael@0: getService(Ci.nsILoginManager); michael@0: pwmgr.removeAllLogins(); michael@0: michael@0: // Add some initial logins michael@0: let urls = [ michael@0: "http://example.com/", michael@0: "http://example.org/", michael@0: "http://mozilla.com/", michael@0: "http://mozilla.org/", michael@0: "http://spreadfirefox.com/", michael@0: "http://planet.mozilla.org/", michael@0: "https://developer.mozilla.org/", michael@0: "http://hg.mozilla.org/", michael@0: "http://mxr.mozilla.org/", michael@0: "http://feeds.mozilla.org/", michael@0: ]; michael@0: let nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", michael@0: Ci.nsILoginInfo, "init"); michael@0: let logins = [ michael@0: new nsLoginInfo(urls[0], urls[0], null, "user", "password", "u1", "p1"), michael@0: new nsLoginInfo(urls[1], urls[1], null, "username", "password", "u2", "p2"), michael@0: new nsLoginInfo(urls[2], urls[2], null, "ehsan", "mypass", "u3", "p3"), michael@0: new nsLoginInfo(urls[3], urls[3], null, "ehsan", "mypass", "u4", "p4"), michael@0: new nsLoginInfo(urls[4], urls[4], null, "john", "smith", "u5", "p5"), michael@0: new nsLoginInfo(urls[5], urls[5], null, "what?", "very secret", "u6", "p6"), michael@0: new nsLoginInfo(urls[6], urls[6], null, "really?", "super secret", "u7", "p7"), michael@0: new nsLoginInfo(urls[7], urls[7], null, "you sure?", "absolutely", "u8", "p8"), michael@0: new nsLoginInfo(urls[8], urls[8], null, "my user name", "mozilla", "u9", "p9"), michael@0: new nsLoginInfo(urls[9], urls[9], null, "my username", "mozilla.com", "u10", "p10"), michael@0: ]; michael@0: logins.forEach(function (login) pwmgr.addLogin(login)); michael@0: michael@0: // Open the password manager dialog michael@0: const PWMGR_DLG = "chrome://passwordmgr/content/passwordManager.xul"; michael@0: let pwmgrdlg = window.openDialog(PWMGR_DLG, "Toolkit:PasswordManager", ""); michael@0: SimpleTest.waitForFocus(doTest, pwmgrdlg); michael@0: michael@0: // the meat of the test michael@0: function doTest() { michael@0: let doc = pwmgrdlg.document; michael@0: let win = doc.defaultView; michael@0: let filter = doc.getElementById("filter"); michael@0: let tree = doc.getElementById("signonsTree"); michael@0: let view = tree.treeBoxObject.view; michael@0: michael@0: is(filter.value, "", "Filter box should initially be empty"); michael@0: is(view.rowCount, 10, "There should be 10 passwords initially"); michael@0: michael@0: // Prepare a set of tests michael@0: // filter: the text entered in the filter search box michael@0: // count: the number of logins which should match the respective filter michael@0: // count2: the number of logins which should match the respective filter michael@0: // if the passwords are being shown as well michael@0: // Note: if a test doesn't have count2 set, count is used instead. michael@0: let tests = [ michael@0: {filter: "pass", count: 0, count2: 4}, michael@0: {filter: "", count: 10}, // test clearing the filter michael@0: {filter: "moz", count: 7}, michael@0: {filter: "mozi", count: 7}, michael@0: {filter: "mozil", count: 7}, michael@0: {filter: "mozill", count: 7}, michael@0: {filter: "mozilla", count: 7}, michael@0: {filter: "mozilla.com", count: 1, count2: 2}, michael@0: {filter: "user", count: 4}, michael@0: {filter: "user ", count: 1}, michael@0: {filter: " user", count: 2}, michael@0: {filter: "http", count: 10}, michael@0: {filter: "https", count: 1}, michael@0: {filter: "secret", count: 0, count2: 2}, michael@0: {filter: "secret!", count: 0}, michael@0: ]; michael@0: michael@0: let toggleCalls = 0; michael@0: function toggleShowPasswords(func) { michael@0: let toggleButton = doc.getElementById("togglePasswords"); michael@0: let showMode = (toggleCalls++ % 2) == 0; michael@0: michael@0: // only watch for a confirmation dialog every other time being called michael@0: if (showMode) { michael@0: Services.ww.registerNotification(function (aSubject, aTopic, aData) { michael@0: if (aTopic == "domwindowclosed") michael@0: Services.ww.unregisterNotification(arguments.callee); michael@0: else if (aTopic == "domwindowopened") { michael@0: let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget); michael@0: SimpleTest.waitForFocus(function() { michael@0: EventUtils.sendKey("RETURN", win); michael@0: }, win); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: Services.obs.addObserver(function (aSubject, aTopic, aData) { michael@0: if (aTopic == "passwordmgr-password-toggle-complete") { michael@0: Services.obs.removeObserver(arguments.callee, aTopic); michael@0: func(); michael@0: } michael@0: }, "passwordmgr-password-toggle-complete", false); michael@0: michael@0: EventUtils.synthesizeMouse(toggleButton, 1, 1, {}, win); michael@0: } michael@0: michael@0: function runTests(mode, endFunction) { michael@0: let testCounter = 0; michael@0: michael@0: function setFilter(string) { michael@0: filter.value = string; michael@0: filter.doCommand(); michael@0: } michael@0: michael@0: function runOneTest(test) { michael@0: function tester() { michael@0: is(view.rowCount, expected, expected + " logins should match '" + test.filter + "'"); michael@0: } michael@0: michael@0: let expected; michael@0: switch (mode) { michael@0: case 1: // without showing passwords michael@0: expected = test.count; michael@0: break; michael@0: case 2: // showing passwords michael@0: expected = ("count2" in test) ? test.count2 : test.count; michael@0: break; michael@0: case 3: // toggle michael@0: expected = test.count; michael@0: tester(); michael@0: toggleShowPasswords(function () { michael@0: expected = ("count2" in test) ? test.count2 : test.count; michael@0: tester(); michael@0: toggleShowPasswords(proceed); michael@0: }); michael@0: return; michael@0: } michael@0: tester(); michael@0: proceed(); michael@0: } michael@0: michael@0: function proceed() { michael@0: // run the next test if necessary or proceed with the tests michael@0: if (testCounter != tests.length) michael@0: runNextTest(); michael@0: else michael@0: endFunction(); michael@0: } michael@0: michael@0: function runNextTest() { michael@0: let test = tests[testCounter++]; michael@0: setFilter(test.filter); michael@0: setTimeout(runOneTest, 0, test); michael@0: } michael@0: michael@0: runNextTest(); michael@0: } michael@0: michael@0: function step1() { michael@0: runTests(1, step2); michael@0: } michael@0: michael@0: function step2() { michael@0: toggleShowPasswords(function() { michael@0: runTests(2, step3); michael@0: }); michael@0: } michael@0: michael@0: function step3() { michael@0: toggleShowPasswords(function() { michael@0: runTests(3, lastStep); michael@0: }); michael@0: } michael@0: michael@0: function lastStep() { michael@0: // cleanup michael@0: Services.ww.registerNotification(function (aSubject, aTopic, aData) { michael@0: // unregister ourself michael@0: Services.ww.unregisterNotification(arguments.callee); michael@0: michael@0: pwmgr.removeAllLogins(); michael@0: finish(); michael@0: }); michael@0: pwmgrdlg.close(); michael@0: } michael@0: michael@0: step1(); michael@0: } michael@0: }