summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_passwordmgr_contextmenu.js
blob: ece2b731f5ac1e8f8b456b3724743e3aa363def1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

function test() {
    waitForExplicitFinish();

    Services.logins.removeAllLogins();

    // Add some initial logins
    let urls = [
        "http://example.com/",
        "http://mozilla.org/",
        "http://spreadfirefox.com/",
        "https://support.mozilla.org/",
        "http://hg.mozilla.org/"
    ];
    let nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
                                                 Ci.nsILoginInfo, "init");
    let logins = [
        new nsLoginInfo(urls[0], urls[0], null, "", "o hai", "u1", "p1"),
        new nsLoginInfo(urls[1], urls[1], null, "ehsan", "coded", "u2", "p2"),
        new nsLoginInfo(urls[2], urls[2], null, "this", "awesome", "u3", "p3"),
        new nsLoginInfo(urls[3], urls[3], null, "array of", "logins", "u4", "p4"),
        new nsLoginInfo(urls[4], urls[4], null, "then", "i wrote the test", "u5", "p5")
    ];
    logins.forEach(login => Services.logins.addLogin(login));

    // Open the password manager dialog
    const PWMGR_DLG = "chrome://passwordmgr/content/passwordManager.xul";
    let pwmgrdlg = window.openDialog(PWMGR_DLG, "Toolkit:PasswordManager", "");
    SimpleTest.waitForFocus(doTest, pwmgrdlg);

    // Test if "Copy Username" and "Copy Password" works
    function doTest() {
        let doc = pwmgrdlg.document;
        let selection = doc.getElementById("signonsTree").view.selection;
        let menuitem = doc.getElementById("context-copyusername");

        function copyField() {
            info("Select all");
            selection.selectAll();
            assertMenuitemEnabled("copyusername", false);
            assertMenuitemEnabled("editusername", false);
            assertMenuitemEnabled("copypassword", false);
            assertMenuitemEnabled("editpassword", false);

            info("Select the first row (with an empty username)");
            selection.select(0);
            assertMenuitemEnabled("copyusername", false, "empty username");
            assertMenuitemEnabled("editusername", true);
            assertMenuitemEnabled("copypassword", true);
            assertMenuitemEnabled("editpassword", false, "password column hidden");

            info("Clear the selection");
            selection.clearSelection();
            assertMenuitemEnabled("copyusername", false);
            assertMenuitemEnabled("editusername", false);
            assertMenuitemEnabled("copypassword", false);
            assertMenuitemEnabled("editpassword", false);

            info("Select the third row and making the password column visible");
            selection.select(2);
            doc.getElementById("passwordCol").hidden = false;
            assertMenuitemEnabled("copyusername", true);
            assertMenuitemEnabled("editusername", true);
            assertMenuitemEnabled("copypassword", true);
            assertMenuitemEnabled("editpassword", true, "password column visible");
            menuitem.doCommand();
        }

        function assertMenuitemEnabled(idSuffix, expected, reason = "") {
            doc.defaultView.UpdateContextMenu();
            let actual = !doc.getElementById("context-" + idSuffix).getAttribute("disabled");
            is(actual, expected, idSuffix + " should be " + (expected ? "enabled" : "disabled") +
               (reason ? ": " + reason : ""));
        }

        function cleanUp() {
            Services.ww.registerNotification(function (aSubject, aTopic, aData) {
                Services.ww.unregisterNotification(arguments.callee);
                Services.logins.removeAllLogins();
                doc.getElementById("passwordCol").hidden = true;
                finish();
            });
            pwmgrdlg.close();
        }

        function testPassword() {
            info("Testing Copy Password");
            waitForClipboard("coded", function copyPassword() {
                menuitem = doc.getElementById("context-copypassword");
                menuitem.doCommand();
            }, cleanUp, cleanUp);
        }

        info("Testing Copy Username");
        waitForClipboard("ehsan", copyField, testPassword, testPassword);
    }
}