testing/mochitest/tests/Harness_sanity/test_SpecialPowersExtension.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mochitest/tests/Harness_sanity/test_SpecialPowersExtension.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,193 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test for SpecialPowers extension</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.10 +</head>
    1.11 +<body onload="starttest();">
    1.12 +
    1.13 +<div id="content" style="display: none">
    1.14 +  <canvas id="testcanvas" width="200" height="200">
    1.15 +</div>
    1.16 +<pre id="test">
    1.17 +<script class="testbody" type="text/javascript">
    1.18 +
    1.19 +var eventCount = 0;
    1.20 +function testEventListener(e) {
    1.21 +  ++eventCount;
    1.22 +}
    1.23 +
    1.24 +function testEventListener2(e) {
    1.25 +  ++eventCount;
    1.26 +}
    1.27 +
    1.28 +function dispatchTestEvent() {
    1.29 +  var e = document.createEvent("Event");
    1.30 +  e.initEvent("TestEvent", true, true);
    1.31 +  window.dispatchEvent(e);
    1.32 +}
    1.33 +
    1.34 +dump("\nSPECIALPTEST:::Test script loaded " + (new Date).getTime() + "\n");
    1.35 +SimpleTest.waitForExplicitFinish();
    1.36 +var startTime = new Date();
    1.37 +function starttest(){
    1.38 +  dump("\nSPECIALPTEST:::Test script running after load " + (new Date).getTime() + "\n");
    1.39 +
    1.40 +  /** Test for SpecialPowers extension **/
    1.41 +  is(SpecialPowers.sanityCheck(), "foo", "check to see whether the Special Powers extension is installed.");
    1.42 +
    1.43 +  // Test a sync call into chrome
    1.44 +  SpecialPowers.setBoolPref('extensions.checkCompatibility', true);
    1.45 +  is(SpecialPowers.getBoolPref('extensions.checkCompatibility'), true, "Check to see if we can set a preference properly");
    1.46 +  SpecialPowers.clearUserPref('extensions.checkCompatibility');
    1.47 +
    1.48 +  // Test a int pref
    1.49 +  SpecialPowers.setIntPref('extensions.foobar', 42);
    1.50 +  is(SpecialPowers.getIntPref('extensions.foobar'), 42, "Check int pref");
    1.51 +  SpecialPowers.clearUserPref('extensions.foobar');
    1.52 +
    1.53 +  // Test a string pref
    1.54 +  SpecialPowers.setCharPref("extensions.foobaz", "hi there");
    1.55 +  is(SpecialPowers.getCharPref("extensions.foobaz"), "hi there", "Check string pref");
    1.56 +  SpecialPowers.clearUserPref("extensions.foobaz");
    1.57 +
    1.58 +  // Test an invalid pref
    1.59 +  var retVal = null;
    1.60 +  try {
    1.61 +    retVal = SpecialPowers.getBoolPref('extensions.checkCompat0123456789');
    1.62 +  } catch (ex) {
    1.63 +    retVal = ex;
    1.64 +  }
    1.65 +  is(retVal, 'Error getting pref', "received an exception trying to get an unset preference value");
    1.66 +
    1.67 +  SpecialPowers.addChromeEventListener("TestEvent", testEventListener, true, true);
    1.68 +  SpecialPowers.addChromeEventListener("TestEvent", testEventListener2, true, false);
    1.69 +  dispatchTestEvent();
    1.70 +  is(eventCount, 1, "Should have got an event!");
    1.71 +
    1.72 +  SpecialPowers.removeChromeEventListener("TestEvent", testEventListener, true);
    1.73 +  SpecialPowers.removeChromeEventListener("TestEvent", testEventListener2, true);
    1.74 +  dispatchTestEvent();
    1.75 +  is(eventCount, 1, "Shouldn't have got an event!");
    1.76 +  
    1.77 +  // Test Complex Pref - TODO: Without chrome access, I don't know how you'd actually
    1.78 +  // set this preference since you have to create an XPCOM object.
    1.79 +  // Leaving untested for now.
    1.80 +
    1.81 +  // Test a DOMWindowUtils method and property
    1.82 +  is(SpecialPowers.DOMWindowUtils.getClassName(window), "Proxy");
    1.83 +  is(SpecialPowers.DOMWindowUtils.docCharsetIsForced, false);
    1.84 +  
    1.85 +  // QueryInterface and getPrivilegedProps tests
    1.86 +  is(SpecialPowers.can_QI(SpecialPowers), false);
    1.87 +  ok(SpecialPowers.can_QI(window));
    1.88 +  ok(SpecialPowers.do_QueryInterface(window, "nsIDOMWindow"));
    1.89 +  is(SpecialPowers.getPrivilegedProps(SpecialPowers.do_QueryInterface(window, "nsIDOMWindow"), "document.nodeName"), "#document");
    1.90 +  
    1.91 +  //try to run garbage collection
    1.92 +  SpecialPowers.gc();
    1.93 +
    1.94 +  //
    1.95 +  // Test the SpecialPowers wrapper.
    1.96 +  //
    1.97 +
    1.98 +  // Try some basic stuff with XHR.
    1.99 +  var xhr2 = SpecialPowers.Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(SpecialPowers.Ci.nsIXMLHttpRequest);
   1.100 +  is(xhr2.readyState, XMLHttpRequest.UNSENT, "Should be able to get props off privileged objects");
   1.101 +  var testURI = SpecialPowers.Cc['@mozilla.org/network/standard-url;1']
   1.102 +                                              .createInstance(SpecialPowers.Ci.nsIURI);
   1.103 +  testURI.spec = "http://www.foobar.org/";
   1.104 +  is(testURI.spec, "http://www.foobar.org/", "Getters/Setters should work correctly");
   1.105 +  is(SpecialPowers.wrap(document).getElementsByTagName('details').length, 0, "Should work with proxy-based DOM bindings.");
   1.106 +
   1.107 +  // Play with the window object.
   1.108 +  var webnav = SpecialPowers.wrap(window).QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
   1.109 +                                         .getInterface(SpecialPowers.Ci.nsIWebNavigation);
   1.110 +  webnav.QueryInterface(SpecialPowers.Ci.nsIDocShell);
   1.111 +  ok(webnav.allowJavascript, "Able to pull properties off of docshell!");
   1.112 +
   1.113 +  // Make sure Xray-wrapped functions work.
   1.114 +  try {
   1.115 +    SpecialPowers.wrap(SpecialPowers.Components).ID('{00000000-0000-0000-0000-000000000000}');
   1.116 +    ok(true, "Didn't throw");
   1.117 +  }
   1.118 +  catch (e) {
   1.119 +    ok(false, "Threw while trying to call Xray-wrapped function.");
   1.120 +  }
   1.121 +
   1.122 +  // Check constructors.
   1.123 +  var BinaryInputStream = SpecialPowers.wrap(SpecialPowers.Components).Constructor("@mozilla.org/binaryinputstream;1");
   1.124 +  var bis = new BinaryInputStream();
   1.125 +  ok(/nsISupports/.exec(bis.toString()), "Should get the proper object out of the constructor");
   1.126 +  function TestConstructor() {
   1.127 +    SpecialPowers.wrap(this).foo = 2;
   1.128 +  }
   1.129 +  var WrappedConstructor = SpecialPowers.wrap(TestConstructor);
   1.130 +  is((new WrappedConstructor()).foo, 2, "JS constructors work properly when wrapped");
   1.131 +
   1.132 +  // Try messing around with QuickStubbed getters/setters and make sure the wrapper deals.
   1.133 +  var ctx = SpecialPowers.wrap(document).getElementById('testcanvas').getContext('2d');
   1.134 +  var pixels = ctx.getImageData(0,0,1,1);
   1.135 +  try {
   1.136 +    pixels.data;
   1.137 +    ok(true, "Didn't throw getting quickstubbed accessor prop from proto");
   1.138 +  }
   1.139 +  catch (e) {
   1.140 +    ok(false, "Threw while getting quickstubbed accessor prop from proto");
   1.141 +  }
   1.142 +
   1.143 +  // Check functions that return null.
   1.144 +  var returnsNull = function() { return null; }
   1.145 +  is(SpecialPowers.wrap(returnsNull)(), null, "Should be able to handle functions that return null.");
   1.146 +
   1.147 +  // Check a function that throws.
   1.148 +  var thrower = function() { throw new Error('hah'); }
   1.149 +  try {
   1.150 +    SpecialPowers.wrap(thrower)();
   1.151 +    ok(false, "Should have thrown");
   1.152 +  } catch (e) {
   1.153 +    ok(SpecialPowers.isWrapper(e), "Exceptions should be wrapped for call");
   1.154 +    is(e.message, 'hah', "Correct message");
   1.155 +  }
   1.156 +  try {
   1.157 +    var ctor = SpecialPowers.wrap(thrower);
   1.158 +    new ctor();
   1.159 +    ok(false, "Should have thrown");
   1.160 +  } catch (e) {
   1.161 +    ok(SpecialPowers.isWrapper(e), "Exceptions should be wrapped for construct");
   1.162 +    is(e.message, 'hah', "Correct message");
   1.163 +  }
   1.164 +
   1.165 +  // Play around with a JS object to check the non-xray path.
   1.166 +  var noxray_proto = {a: 3, b: 12};
   1.167 +  var noxray = {a: 5, c: 32};
   1.168 +  noxray.__proto__ = noxray_proto;
   1.169 +  var noxray_wrapper = SpecialPowers.wrap(noxray);
   1.170 +  is(noxray_wrapper.c, 32, "Regular properties should work.");
   1.171 +  is(noxray_wrapper.a, 5, "Shadow properties should work.");
   1.172 +  is(noxray_wrapper.b, 12, "Proto properties should work.");
   1.173 +  noxray.b = 122;
   1.174 +  is(noxray_wrapper.b, 122, "Should be able to shadow.");
   1.175 +
   1.176 +  // Try setting file input values via an Xray wrapper.
   1.177 +  SpecialPowers.wrap(document).title = "foo";
   1.178 +  is(document.title, "foo", "Set property correctly on Xray-wrapped DOM object");
   1.179 +  is(SpecialPowers.wrap(document).URI, document.URI, "Got property correctly on Xray-wrapped DOM object");
   1.180 +
   1.181 +  info("\nProfile::SpecialPowersRunTime: " + (new Date() - startTime) + "\n");
   1.182 +
   1.183 +  // bug 855192
   1.184 +  ok(SpecialPowers.MockPermissionPrompt, "check mock permission prompt");
   1.185 +
   1.186 +  // Set a pref using pushPrefEnv to make sure that flushPrefEnv is
   1.187 +  // automatically called before we invoke
   1.188 +  // test_SpecialPowersExtension2.html.
   1.189 +  SpecialPowers.pushPrefEnv({set: [['testing.some_arbitrary_pref', true]]},
   1.190 +                            function() { SimpleTest.finish(); });
   1.191 +}
   1.192 +</script>
   1.193 +</pre>
   1.194 +</body>
   1.195 +</html>
   1.196 +

mercurial