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.

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

mercurial