gecko-dev/toolkit/components/passwordmgr/test/test_basic_form_observer_au...

108 строки
3.4 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Test for Login Manager</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<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 autocomplete off and notifying observers & normal form
<p id="display"></p>
<div id="content" style="display: block">
<form id="form1" action="formtest.js" autocomplete="off">
<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>
<form id="form2" action="formtest.js">
<p>This is form 2.</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 autocomplete off and notifying observers & normal form **/
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 TestObserver = {
receivedNotification1 : false,
receivedNotification2 : false,
data1 : "",
data2 : "",
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") {
if (subject.id == "form1") {
this.receivedNotification1 = true;
this.data1 = data;
} else if (subject.id == "form2") {
this.receivedNotification2 = true;
this.data2 = data;
}
// Now fill the form
pwmgr.fillForm(subject);
}
}
};
// Add the observer
var os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
os.addObserver(TestObserver, "passwordmgr-found-form", false);
function startTest(){
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
// Test that observer is notified & got correct data
is(TestObserver.receivedNotification1, true, "Checking observer was notified");
is(TestObserver.data1, "autocompleteOff", "Checking observer got correct data");
// Check that form1 was filled
is($_(1, "uname").value, "testuser", "Checking for filled username 1");
is($_(1, "pword").value, "testpass", "Checking for filled password 1");
// Test that observer wasn't notified & didn't get data
is(TestObserver.receivedNotification2, false, "Checking observer was not notified");
is(TestObserver.data2, "", "Checking observer didn't get data");
// Check that form2 was filled
is($_(2, "uname").value, "testuser", "Checking for filled username 2");
is($_(2, "pword").value, "testpass", "Checking for filled password 2");
// Remove the observer
os.removeObserver(TestObserver, "passwordmgr-found-form");
SimpleTest.finish();
}
window.onload = startTest;
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>