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