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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/passwordmgr/test/browser/browser_passwordmgrcopypwd.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     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://mozilla.org/",
    1.19 +        "http://spreadfirefox.com/",
    1.20 +        "https://developer.mozilla.org/",
    1.21 +        "http://hg.mozilla.org/"
    1.22 +    ];
    1.23 +    let nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
    1.24 +                                                 Ci.nsILoginInfo, "init");
    1.25 +    let logins = [
    1.26 +        new nsLoginInfo(urls[0], urls[0], null, "o", "hai", "u1", "p1"),
    1.27 +        new nsLoginInfo(urls[1], urls[1], null, "ehsan", "coded", "u2", "p2"),
    1.28 +        new nsLoginInfo(urls[2], urls[2], null, "this", "awesome", "u3", "p3"),
    1.29 +        new nsLoginInfo(urls[3], urls[3], null, "array of", "logins", "u4", "p4"),
    1.30 +        new nsLoginInfo(urls[4], urls[4], null, "then", "i wrote the test", "u5", "p5")
    1.31 +    ];
    1.32 +    logins.forEach(function (login) pwmgr.addLogin(login));
    1.33 +
    1.34 +    // Open the password manager dialog
    1.35 +    const PWMGR_DLG = "chrome://passwordmgr/content/passwordManager.xul";
    1.36 +    let pwmgrdlg = window.openDialog(PWMGR_DLG, "Toolkit:PasswordManager", "");
    1.37 +    SimpleTest.waitForFocus(doTest, pwmgrdlg);
    1.38 +
    1.39 +    // Test if "Copy Username" and "Copy Password" works
    1.40 +    function doTest() {
    1.41 +        let doc = pwmgrdlg.document;
    1.42 +        let selection = doc.getElementById("signonsTree").view.selection;
    1.43 +        let menuitem = doc.getElementById("context-copyusername");
    1.44 +
    1.45 +        function copyField() {
    1.46 +            selection.selectAll();
    1.47 +            is(isMenuitemEnabled(), false, "Copy should be disabled");
    1.48 +
    1.49 +            selection.select(0);
    1.50 +            is(isMenuitemEnabled(), true, "Copy should be enabled");
    1.51 +
    1.52 +            selection.clearSelection();
    1.53 +            is(isMenuitemEnabled(), false, "Copy should be disabled");
    1.54 +
    1.55 +            selection.select(2);
    1.56 +            is(isMenuitemEnabled(), true, "Copy should be enabled");
    1.57 +            menuitem.doCommand();
    1.58 +        }
    1.59 +
    1.60 +        function isMenuitemEnabled() {
    1.61 +            doc.defaultView.UpdateCopyPassword();
    1.62 +            return !menuitem.getAttribute("disabled");
    1.63 +        }
    1.64 +
    1.65 +        function cleanUp() {
    1.66 +            Services.ww.registerNotification(function (aSubject, aTopic, aData) {
    1.67 +                Services.ww.unregisterNotification(arguments.callee);
    1.68 +                pwmgr.removeAllLogins();
    1.69 +                finish();
    1.70 +            });
    1.71 +            pwmgrdlg.close();
    1.72 +        }
    1.73 +        
    1.74 +        function testPassword() {
    1.75 +            menuitem = doc.getElementById("context-copypassword");
    1.76 +            info("Testing Copy Password");
    1.77 +            waitForClipboard("coded", copyField, cleanUp, cleanUp);
    1.78 +        }
    1.79 +        
    1.80 +        info("Testing Copy Username");
    1.81 +        waitForClipboard("ehsan", copyField, testPassword, testPassword);
    1.82 +    }
    1.83 +}

mercurial