DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
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 101 102 103 104 105 106 107 108 109 110
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for Login Manager</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>  
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Login Manager test: simple form with autofillForms disabled and notifying observers
<p id="display"></p>

<div id="content" style="display: block">

  <form id="form1" action="formtest.js">
    <p>This is form 1.</p>
    <input  type="text"       name="uname">
    <input  type="password"   name="pword">

    <button type="submit">Submit</button>
    <button type="reset"> Reset </button>
  </form>

</div>

<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Login Manager: simple form with autofillForms disabled and notifying observers **/

commonInit();
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

var prefs = Cc["@mozilla.org/preferences-service;1"].
            getService(Ci.nsIPrefService);
prefs = prefs.getBranch("signon.");
// Assume that the pref starts out true, so set to false
prefs.setBoolPref("autofillForms", false);

var TestObserver = {
  receivedNotificationFoundForm : false,
  receivedNotificationFoundLogins : false,
  dataFoundForm : "",
  dataFoundLogins : null,
  QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
  observe : function (subject, topic, data) {
    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
    var pwmgr = Cc["@mozilla.org/login-manager;1"].
                getService(Ci.nsILoginManager);
    if (topic == "passwordmgr-found-form") {
      this.receivedNotificationFoundForm = true;
      this.dataFoundForm = data;
      // Now fill the form
      pwmgr.fillForm(subject);
    } else if (topic == "passwordmgr-found-logins") {
      this.receivedNotificationFoundLogins = true;
      this.dataFoundLogins = subject.QueryInterface(Ci.nsIPropertyBag2);
    }
  }
};

// Add the observer
var os = Cc["@mozilla.org/observer-service;1"].
         getService(Ci.nsIObserverService);
os.addObserver(TestObserver, "passwordmgr-found-form", false);
os.addObserver(TestObserver, "passwordmgr-found-logins", false);

function startTest(){
  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  // Test that found-form observer is notified & got correct data
  is(TestObserver.receivedNotificationFoundForm, true, "Checking found-form observer was notified");
  is(TestObserver.dataFoundForm, "noAutofillForms", "Checking found-form observer got correct data");

  // Check that form1 was filled
  is($_(1, "uname").value, "testuser", "Checking for filled username");
  is($_(1, "pword").value, "testpass", "Checking for filled password");

  // Test that found-logins observer is notified & got correct data
  is(TestObserver.receivedNotificationFoundLogins, true, "Checking found-logins observer was notified");
  is(TestObserver.dataFoundLogins.get("didntFillReason"), "noAutofillForms", "Checking didntFillReason is noAutofillForms");
  is(TestObserver.dataFoundLogins.get("usernameField"), $_(1, "uname"), "Checking username field is correct");
  is(TestObserver.dataFoundLogins.get("passwordField"), $_(1, "pword"), "Checking password field is correct");
  is(TestObserver.dataFoundLogins.get("foundLogins").constructor.name, "Array", "Checking foundLogins is array");
  is(TestObserver.dataFoundLogins.get("foundLogins").length, 1, "Checking foundLogins contains one login");
  ok(TestObserver.dataFoundLogins.get("selectedLogin").QueryInterface(Ci.nsILoginInfo), "Checking selectedLogin is nsILoginInfo");
  ok(TestObserver.dataFoundLogins.get("selectedLogin").equals(TestObserver.dataFoundLogins.get("foundLogins")[0]),
     "Checking selectedLogin is found login");

  // Reset pref (since we assumed it was true to start)
  prefs.setBoolPref("autofillForms", true);

  // Remove the observer
  os.removeObserver(TestObserver, "passwordmgr-found-form");
  os.removeObserver(TestObserver, "passwordmgr-found-logins");

  SimpleTest.finish();
}

window.onload = startTest;

SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>