Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <title>Test for SpecialPowers extension</title> |
michael@0 | 5 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 6 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 7 | </head> |
michael@0 | 8 | <body onload="starttest();"> |
michael@0 | 9 | |
michael@0 | 10 | <div id="content" style="display: none"> |
michael@0 | 11 | <canvas id="testcanvas" width="200" height="200"> |
michael@0 | 12 | </div> |
michael@0 | 13 | <pre id="test"> |
michael@0 | 14 | <script class="testbody" type="text/javascript"> |
michael@0 | 15 | |
michael@0 | 16 | var eventCount = 0; |
michael@0 | 17 | function testEventListener(e) { |
michael@0 | 18 | ++eventCount; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | function testEventListener2(e) { |
michael@0 | 22 | ++eventCount; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | function dispatchTestEvent() { |
michael@0 | 26 | var e = document.createEvent("Event"); |
michael@0 | 27 | e.initEvent("TestEvent", true, true); |
michael@0 | 28 | window.dispatchEvent(e); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | dump("\nSPECIALPTEST:::Test script loaded " + (new Date).getTime() + "\n"); |
michael@0 | 32 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 33 | var startTime = new Date(); |
michael@0 | 34 | function starttest(){ |
michael@0 | 35 | dump("\nSPECIALPTEST:::Test script running after load " + (new Date).getTime() + "\n"); |
michael@0 | 36 | |
michael@0 | 37 | /** Test for SpecialPowers extension **/ |
michael@0 | 38 | is(SpecialPowers.sanityCheck(), "foo", "check to see whether the Special Powers extension is installed."); |
michael@0 | 39 | |
michael@0 | 40 | // Test a sync call into chrome |
michael@0 | 41 | SpecialPowers.setBoolPref('extensions.checkCompatibility', true); |
michael@0 | 42 | is(SpecialPowers.getBoolPref('extensions.checkCompatibility'), true, "Check to see if we can set a preference properly"); |
michael@0 | 43 | SpecialPowers.clearUserPref('extensions.checkCompatibility'); |
michael@0 | 44 | |
michael@0 | 45 | // Test a int pref |
michael@0 | 46 | SpecialPowers.setIntPref('extensions.foobar', 42); |
michael@0 | 47 | is(SpecialPowers.getIntPref('extensions.foobar'), 42, "Check int pref"); |
michael@0 | 48 | SpecialPowers.clearUserPref('extensions.foobar'); |
michael@0 | 49 | |
michael@0 | 50 | // Test a string pref |
michael@0 | 51 | SpecialPowers.setCharPref("extensions.foobaz", "hi there"); |
michael@0 | 52 | is(SpecialPowers.getCharPref("extensions.foobaz"), "hi there", "Check string pref"); |
michael@0 | 53 | SpecialPowers.clearUserPref("extensions.foobaz"); |
michael@0 | 54 | |
michael@0 | 55 | // Test an invalid pref |
michael@0 | 56 | var retVal = null; |
michael@0 | 57 | try { |
michael@0 | 58 | retVal = SpecialPowers.getBoolPref('extensions.checkCompat0123456789'); |
michael@0 | 59 | } catch (ex) { |
michael@0 | 60 | retVal = ex; |
michael@0 | 61 | } |
michael@0 | 62 | is(retVal, 'Error getting pref', "received an exception trying to get an unset preference value"); |
michael@0 | 63 | |
michael@0 | 64 | SpecialPowers.addChromeEventListener("TestEvent", testEventListener, true, true); |
michael@0 | 65 | SpecialPowers.addChromeEventListener("TestEvent", testEventListener2, true, false); |
michael@0 | 66 | dispatchTestEvent(); |
michael@0 | 67 | is(eventCount, 1, "Should have got an event!"); |
michael@0 | 68 | |
michael@0 | 69 | SpecialPowers.removeChromeEventListener("TestEvent", testEventListener, true); |
michael@0 | 70 | SpecialPowers.removeChromeEventListener("TestEvent", testEventListener2, true); |
michael@0 | 71 | dispatchTestEvent(); |
michael@0 | 72 | is(eventCount, 1, "Shouldn't have got an event!"); |
michael@0 | 73 | |
michael@0 | 74 | // Test Complex Pref - TODO: Without chrome access, I don't know how you'd actually |
michael@0 | 75 | // set this preference since you have to create an XPCOM object. |
michael@0 | 76 | // Leaving untested for now. |
michael@0 | 77 | |
michael@0 | 78 | // Test a DOMWindowUtils method and property |
michael@0 | 79 | is(SpecialPowers.DOMWindowUtils.getClassName(window), "Proxy"); |
michael@0 | 80 | is(SpecialPowers.DOMWindowUtils.docCharsetIsForced, false); |
michael@0 | 81 | |
michael@0 | 82 | // QueryInterface and getPrivilegedProps tests |
michael@0 | 83 | is(SpecialPowers.can_QI(SpecialPowers), false); |
michael@0 | 84 | ok(SpecialPowers.can_QI(window)); |
michael@0 | 85 | ok(SpecialPowers.do_QueryInterface(window, "nsIDOMWindow")); |
michael@0 | 86 | is(SpecialPowers.getPrivilegedProps(SpecialPowers.do_QueryInterface(window, "nsIDOMWindow"), "document.nodeName"), "#document"); |
michael@0 | 87 | |
michael@0 | 88 | //try to run garbage collection |
michael@0 | 89 | SpecialPowers.gc(); |
michael@0 | 90 | |
michael@0 | 91 | // |
michael@0 | 92 | // Test the SpecialPowers wrapper. |
michael@0 | 93 | // |
michael@0 | 94 | |
michael@0 | 95 | // Try some basic stuff with XHR. |
michael@0 | 96 | var xhr2 = SpecialPowers.Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(SpecialPowers.Ci.nsIXMLHttpRequest); |
michael@0 | 97 | is(xhr2.readyState, XMLHttpRequest.UNSENT, "Should be able to get props off privileged objects"); |
michael@0 | 98 | var testURI = SpecialPowers.Cc['@mozilla.org/network/standard-url;1'] |
michael@0 | 99 | .createInstance(SpecialPowers.Ci.nsIURI); |
michael@0 | 100 | testURI.spec = "http://www.foobar.org/"; |
michael@0 | 101 | is(testURI.spec, "http://www.foobar.org/", "Getters/Setters should work correctly"); |
michael@0 | 102 | is(SpecialPowers.wrap(document).getElementsByTagName('details').length, 0, "Should work with proxy-based DOM bindings."); |
michael@0 | 103 | |
michael@0 | 104 | // Play with the window object. |
michael@0 | 105 | var webnav = SpecialPowers.wrap(window).QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor) |
michael@0 | 106 | .getInterface(SpecialPowers.Ci.nsIWebNavigation); |
michael@0 | 107 | webnav.QueryInterface(SpecialPowers.Ci.nsIDocShell); |
michael@0 | 108 | ok(webnav.allowJavascript, "Able to pull properties off of docshell!"); |
michael@0 | 109 | |
michael@0 | 110 | // Make sure Xray-wrapped functions work. |
michael@0 | 111 | try { |
michael@0 | 112 | SpecialPowers.wrap(SpecialPowers.Components).ID('{00000000-0000-0000-0000-000000000000}'); |
michael@0 | 113 | ok(true, "Didn't throw"); |
michael@0 | 114 | } |
michael@0 | 115 | catch (e) { |
michael@0 | 116 | ok(false, "Threw while trying to call Xray-wrapped function."); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | // Check constructors. |
michael@0 | 120 | var BinaryInputStream = SpecialPowers.wrap(SpecialPowers.Components).Constructor("@mozilla.org/binaryinputstream;1"); |
michael@0 | 121 | var bis = new BinaryInputStream(); |
michael@0 | 122 | ok(/nsISupports/.exec(bis.toString()), "Should get the proper object out of the constructor"); |
michael@0 | 123 | function TestConstructor() { |
michael@0 | 124 | SpecialPowers.wrap(this).foo = 2; |
michael@0 | 125 | } |
michael@0 | 126 | var WrappedConstructor = SpecialPowers.wrap(TestConstructor); |
michael@0 | 127 | is((new WrappedConstructor()).foo, 2, "JS constructors work properly when wrapped"); |
michael@0 | 128 | |
michael@0 | 129 | // Try messing around with QuickStubbed getters/setters and make sure the wrapper deals. |
michael@0 | 130 | var ctx = SpecialPowers.wrap(document).getElementById('testcanvas').getContext('2d'); |
michael@0 | 131 | var pixels = ctx.getImageData(0,0,1,1); |
michael@0 | 132 | try { |
michael@0 | 133 | pixels.data; |
michael@0 | 134 | ok(true, "Didn't throw getting quickstubbed accessor prop from proto"); |
michael@0 | 135 | } |
michael@0 | 136 | catch (e) { |
michael@0 | 137 | ok(false, "Threw while getting quickstubbed accessor prop from proto"); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | // Check functions that return null. |
michael@0 | 141 | var returnsNull = function() { return null; } |
michael@0 | 142 | is(SpecialPowers.wrap(returnsNull)(), null, "Should be able to handle functions that return null."); |
michael@0 | 143 | |
michael@0 | 144 | // Check a function that throws. |
michael@0 | 145 | var thrower = function() { throw new Error('hah'); } |
michael@0 | 146 | try { |
michael@0 | 147 | SpecialPowers.wrap(thrower)(); |
michael@0 | 148 | ok(false, "Should have thrown"); |
michael@0 | 149 | } catch (e) { |
michael@0 | 150 | ok(SpecialPowers.isWrapper(e), "Exceptions should be wrapped for call"); |
michael@0 | 151 | is(e.message, 'hah', "Correct message"); |
michael@0 | 152 | } |
michael@0 | 153 | try { |
michael@0 | 154 | var ctor = SpecialPowers.wrap(thrower); |
michael@0 | 155 | new ctor(); |
michael@0 | 156 | ok(false, "Should have thrown"); |
michael@0 | 157 | } catch (e) { |
michael@0 | 158 | ok(SpecialPowers.isWrapper(e), "Exceptions should be wrapped for construct"); |
michael@0 | 159 | is(e.message, 'hah', "Correct message"); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | // Play around with a JS object to check the non-xray path. |
michael@0 | 163 | var noxray_proto = {a: 3, b: 12}; |
michael@0 | 164 | var noxray = {a: 5, c: 32}; |
michael@0 | 165 | noxray.__proto__ = noxray_proto; |
michael@0 | 166 | var noxray_wrapper = SpecialPowers.wrap(noxray); |
michael@0 | 167 | is(noxray_wrapper.c, 32, "Regular properties should work."); |
michael@0 | 168 | is(noxray_wrapper.a, 5, "Shadow properties should work."); |
michael@0 | 169 | is(noxray_wrapper.b, 12, "Proto properties should work."); |
michael@0 | 170 | noxray.b = 122; |
michael@0 | 171 | is(noxray_wrapper.b, 122, "Should be able to shadow."); |
michael@0 | 172 | |
michael@0 | 173 | // Try setting file input values via an Xray wrapper. |
michael@0 | 174 | SpecialPowers.wrap(document).title = "foo"; |
michael@0 | 175 | is(document.title, "foo", "Set property correctly on Xray-wrapped DOM object"); |
michael@0 | 176 | is(SpecialPowers.wrap(document).URI, document.URI, "Got property correctly on Xray-wrapped DOM object"); |
michael@0 | 177 | |
michael@0 | 178 | info("\nProfile::SpecialPowersRunTime: " + (new Date() - startTime) + "\n"); |
michael@0 | 179 | |
michael@0 | 180 | // bug 855192 |
michael@0 | 181 | ok(SpecialPowers.MockPermissionPrompt, "check mock permission prompt"); |
michael@0 | 182 | |
michael@0 | 183 | // Set a pref using pushPrefEnv to make sure that flushPrefEnv is |
michael@0 | 184 | // automatically called before we invoke |
michael@0 | 185 | // test_SpecialPowersExtension2.html. |
michael@0 | 186 | SpecialPowers.pushPrefEnv({set: [['testing.some_arbitrary_pref', true]]}, |
michael@0 | 187 | function() { SimpleTest.finish(); }); |
michael@0 | 188 | } |
michael@0 | 189 | </script> |
michael@0 | 190 | </pre> |
michael@0 | 191 | </body> |
michael@0 | 192 | </html> |
michael@0 | 193 |