gecko-dev/testing/mochitest/tests/test_SpecialPowersExtension...

145 строки
5.5 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Test for SpecialPowers extension</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="starttest();">
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var eventCount = 0;
function testEventListener(e) {
++eventCount;
}
function testEventListener2(e) {
++eventCount;
}
function dispatchTestEvent() {
var e = document.createEvent("Event");
e.initEvent("TestEvent", true, true);
window.dispatchEvent(e);
}
dump("\nSPECIALPTEST:::Test script loaded " + (new Date).getTime() + "\n");
SimpleTest.waitForExplicitFinish();
var startTime = new Date();
function starttest(){
dump("\nSPECIALPTEST:::Test script running after load " + (new Date).getTime() + "\n");
/** Test for SpecialPowers extension **/
is(SpecialPowers.sanityCheck(), "foo", "check to see whether the Special Powers extension is installed.");
// Test a sync call into chrome
SpecialPowers.setBoolPref('extensions.checkCompatibility', true);
is(SpecialPowers.getBoolPref('extensions.checkCompatibility'), true, "Check to see if we can set a preference properly");
// Test a int pref
SpecialPowers.setIntPref('extensions.foobar', 42);
is(SpecialPowers.getIntPref('extensions.foobar'), 42, "Check int pref");
// Test a string pref
SpecialPowers.setCharPref("extensions.foobaz", "hi there");
is(SpecialPowers.getCharPref("extensions.foobaz"), "hi there", "Check string pref");
// Test an invalid pref
var retVal = null;
try {
retVal = SpecialPowers.getBoolPref('extensions.checkCompat0123456789');
} catch (ex) {
retVal = ex;
}
is(retVal, 'Error getting pref', "received an exception trying to get an unset preference value");
SpecialPowers.addChromeEventListener("TestEvent", testEventListener, true, true);
SpecialPowers.addChromeEventListener("TestEvent", testEventListener2, true, false);
dispatchTestEvent();
is(eventCount, 1, "Should have got an event!");
SpecialPowers.removeChromeEventListener("TestEvent", testEventListener, true);
SpecialPowers.removeChromeEventListener("TestEvent", testEventListener2, true);
dispatchTestEvent();
is(eventCount, 1, "Shouldn't have got an event!");
// Test Complex Pref - TODO: Without chrome access, I don't know how you'd actually
// set this preference since you have to create an XPCOM object.
// Leaving untested for now.
// Test a DOMWindowUtils method and property
is(SpecialPowers.DOMWindowUtils.getClassName(window), "Proxy");
is(SpecialPowers.DOMWindowUtils.docCharsetIsForced, false);
//Run the createSystemXHR method
var xhr = SpecialPowers.createSystemXHR();
ok(xhr, "createSystemXHR should not return null");
is(xhr.readyState, XMLHttpRequest.UNSENT, "createSystemXHR should create an unsent XMLHttpRequest object");
// QueryInterface and getPrivilegedProps tests
is(SpecialPowers.can_QI(SpecialPowers), false);
ok(SpecialPowers.can_QI(window));
ok(SpecialPowers.do_QueryInterface(window, "nsIDOMWindow"));
is(SpecialPowers.getPrivilegedProps(SpecialPowers.do_QueryInterface(window, "nsIDOMWindow"), "document.nodeName"), "#document");
//try to run garbage collection
SpecialPowers.gc();
//
// Test the SpecialPowers wrapper.
//
// Try some basic stuff with XHR.
var xhr2 = SpecialPowers.wrap(Components).classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);
is(xhr2.toString(), SpecialPowers.unwrap(xhr2).toString(), "toString should be transparently delegated");
is(xhr.readyState, XMLHttpRequest.UNSENT, "Should be able to get props off privileged objects");
var testURI = SpecialPowers.wrap(Components).classes['@mozilla.org/network/standard-url;1']
.createInstance(Components.interfaces.nsIURI);
testURI.spec = "http://www.foobar.org/";
is(testURI.spec, "http://www.foobar.org/", "Getters/Setters should work correctly");
is(SpecialPowers.wrap(document).getElementsByTagName('details').length, 0, "Should work with proxy-based DOM bindings.");
// Play with the window object.
var webnav = SpecialPowers.wrap(window).QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation);
webnav.QueryInterface(Components.interfaces.nsIDocShell);
ok(webnav.allowJavascript, "Able to pull properties off of docshell!");
// Make sure Xray-wrapped functions work.
try {
SpecialPowers.wrap(Components).ID('{00000000-0000-0000-0000-000000000000}');
ok(true, "Didn't throw");
}
catch (e) {
ok(false, "Threw while trying to call Xray-wrapped function.");
}
// Check functions that return null.
var returnsNull = function() { return null; }
is(SpecialPowers.wrap(returnsNull)(), null, "Should be able to handle functions that return null.");
// Play around with a JS object to check the non-xray path.
var noxray_proto = {a: 3, b: 12};
var noxray = {a: 5, c: 32};
noxray.__proto__ = noxray_proto;
var noxray_wrapper = SpecialPowers.wrap(noxray);
is(noxray_wrapper.c, 32, "Regular properties should work.");
is(noxray_wrapper.a, 5, "Shadow properties should work.");
is(noxray_wrapper.b, 12, "Proto properties should work.");
noxray.b = 122;
is(noxray_wrapper.b, 122, "Should be able to shadow.");
info("\nProfile::SpecialPowersRunTime: " + (new Date() - startTime) + "\n");
SimpleTest.finish();
}
</script>
</pre>
</body>
</html>