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