1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Java XPCOM Bindings.
16 * The Initial Developer of the Original Code is IBM Corporation.
17 * Portions created by the Initial Developer are Copyright (C) 2007
18 * IBM Corporation. All Rights Reserved.
21 * Javier Pedemonte (jhpedemonte@gmail.com)
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 function logResult(testname
, passed
) {
38 dump((passed
? "passed" : "FAILED") + ": " + testname
+ "\n");
41 function runTestsArray(callee
, tests
) {
46 var comment
= tests
[i
][3] ? tests
[i
][3] : tests
[i
][0];
49 result
= callee
[tests
[i
][0]](tests
[i
][1]);
50 passed
= (result
== tests
[i
][2] ? true : false);
53 dump("*** TEST " + comment
+ " threw exception: " + e
+ "\n");
54 logResult(comment
, false);
58 logResult(comment
, passed
);
62 dump("*** TEST " + comment
+ " FAILED: expected " + tests
[i
][2] +
63 ", returned " + result
+ "\n");
68 throw Components
.results
.NS_ERROR_FAILURE
;
72 function runEchoTests(tests
) {
73 var javaEcho
= tests
.QueryInterface(Components
.interfaces
.nsIEcho
);
75 var testString
= "this is a test string";
78 "Non-Ascii 1 byte chars: éâäàåç, 2 byte chars: \u1234 \u1235 \u1236";
80 var tests
= new Array(
82 [ "In2OutOneDOMString", testString
, testString
,
83 "In2OutOneDOMString w/ '" + testString
+ "'"],
84 [ "In2OutOneDOMString", emptyString
, emptyString
,
85 "In2OutOneDOMString w/ empty string" ],
86 [ "In2OutOneDOMString", null, null,
87 "In2OutOneDOMString w/ null value" ],
89 [ "In2OutOneAString", testString
, testString
,
90 "In2OutOneAString w/ '" + testString
+ "'" ],
91 [ "In2OutOneAString", emptyString
, emptyString
,
92 "In2OutOneAString w/ empty string" ],
93 [ "In2OutOneAString", utf8String
, utf8String
,
94 "In2OutOneAString w/ '" + utf8String
+ "'" ],
95 [ "In2OutOneAString", null, null,
96 "In2OutOneAString w/ null value" ],
97 /* test AUTF8String */
98 [ "In2OutOneUTF8String", testString
, testString
,
99 "In2OutOneUTF8String w/ '" + testString
+ "'" ],
100 [ "In2OutOneUTF8String", emptyString
, emptyString
,
101 "In2OutOneUTF8String w/ empty string" ],
102 [ "In2OutOneUTF8String", utf8String
, utf8String
,
103 "In2OutOneUTF8String w/ '" + utf8String
+ "'" ],
104 [ "In2OutOneUTF8String", null, null,
105 "In2OutOneUTF8String w/ null value" ],
107 [ "In2OutOneCString", testString
, testString
,
108 "In2OutOneCString w/ '" + testString
+ "'" ],
109 [ "In2OutOneCString", emptyString
, emptyString
,
110 "In2OutOneCString w/ empty string" ],
111 [ "In2OutOneCString", null, null,
112 "In2OutOneCString w/ null value" ],
113 /* test normal strings */
114 [ "In2OutOneString", testString
, testString
,
115 "In2OutOneString w/ '" + testString
+ "'" ],
116 [ "In2OutOneString", emptyString
, emptyString
,
117 "In2OutOneString w/ empty string" ],
118 [ "In2OutOneString", null, null,
119 "In2OutOneString w/ null value" ]
122 runTestsArray(javaEcho
, tests
);
126 javaEcho
.aString
= testString
;
127 if (javaEcho
.aString
== testString
)
129 } catch(e
) { alert("exception: " + e
); }
130 logResult("set/getAString w/ '" + testString
+ "'", passed
);
134 javaEcho
.aString
= emptyString
;
135 if (javaEcho
.aString
== emptyString
)
138 logResult("set/getAString w/ empty string", passed
);
142 javaEcho
.aString
= null;
143 if (javaEcho
.aString
== null)
146 logResult("set/getAString w/ null value", passed
);
149 function runInTests(tests
) {
150 var javaIn
= tests
.QueryInterface(Components
.interfaces
.nsIXPCTestIn
);
152 var testString
= "this is a test string";
153 var emptyString
= "";
155 "Non-Ascii 1 byte chars: éâäàåç, 2 byte chars: \u1234 \u1235 \u1236";
157 var minInt
= -Math
.pow(2,31);
158 var maxInt
= Math
.pow(2,31) - 1;
159 var maxUInt
= Math
.pow(2,32) - 1;
160 var minShort
= -Math
.pow(2,15);
161 var maxShort
= Math
.pow(2,15) - 1;
162 var maxUShort
= Math
.pow(2,16) - 1;
165 var charWide
= '\u1234';
166 var maxOctet
= Math
.pow(2,8) - 1;
167 // Since 64 bit integers are treated as doubles in Javascript, rounding
168 // issues prevent us from actually using the max signed 64 bit values.
169 //var lowLong = -Math.pow(2,63);
170 //var highLong = Math.pow(2,63) - 1;
171 var lowLong
= -9223372036854775000;
172 var highLong
= 9223372036854775000;
173 var minFloat
= 1.40129846432481707e-45;
174 var maxFloat
= 3.40282346638528860e+38;
175 var minDouble
= 2.2250738585072014e-308;
176 var maxDouble
= 1.7976931348623157e+308;
179 [ "EchoLong", minInt
, minInt
, "EchoLong w/ " + minInt
],
180 [ "EchoLong", maxInt
, maxInt
, "EchoLong w/ " + maxInt
],
181 [ "EchoShort", minShort
, minShort
, "EchoShort w/ " + minShort
],
182 [ "EchoShort", maxShort
, maxShort
, "EchoShort w/ " + maxShort
],
183 [ "EchoChar", charA
, charA
, "EchoChar w/ " + charA
],
184 [ "EchoChar", charZ
, charZ
, "EchoChar w/ " + charZ
],
185 [ "EchoBoolean", true, true, "EchoBoolean w/ " + true ],
186 [ "EchoBoolean", false, false, "EchoBoolean w/ " + false ],
187 [ "EchoOctet", 0, 0, "EchoOctet w/ " + 0 ],
188 [ "EchoOctet", maxOctet
, maxOctet
, "EchoOctet w/ " + maxOctet
],
189 [ "EchoLongLong", lowLong
, lowLong
, "EchoLongLong w/ " + lowLong
],
190 [ "EchoLongLong", highLong
, highLong
, "EchoLongLong w/ " + highLong
],
191 [ "EchoUnsignedShort", maxUShort
, maxUShort
,
192 "EchoUnsignedShort w/ " + maxUShort
],
193 [ "EchoUnsignedShort", 0, 0, "EchoUnsignedShort w/ zero" ],
194 // XXX is this test case correct/valid?
195 [ "EchoUnsignedShort", -1, maxUShort
, "EchoUnsignedShort w/ -1" ],
196 [ "EchoUnsignedLong", maxUInt
, maxUInt
, "EchoUnsignedLong w/ " + maxUInt
],
197 [ "EchoUnsignedLong", 0, 0, "EchoUnsignedLong w/ zero" ],
198 [ "EchoUnsignedLong", -1, maxUInt
, "EchoUnsignedLong w/ -1" ],
199 [ "EchoFloat", minFloat
, minFloat
, "EchoFloat w/ " + minFloat
],
200 [ "EchoFloat", maxFloat
, maxFloat
, "EchoFloat w/ " + maxFloat
],
201 [ "EchoDouble", minDouble
, minDouble
, "EchoDouble w/ " + minDouble
],
202 [ "EchoDouble", maxDouble
, maxDouble
, "EchoDouble w/ " + maxDouble
],
203 [ "EchoWchar", charA
, charA
, "EchoWchar w/ " + charA
],
204 [ "EchoWchar", charZ
, charZ
, "EchoWchar w/ " + charZ
],
205 [ "EchoWchar", charWide
, charWide
, "EchoWchar w/ " + charWide
],
206 [ "EchoString", testString
, testString
,
207 "EchoString w/ '" + testString
+ "'" ],
208 [ "EchoString", emptyString
, emptyString
, "EchoString w/ empty string" ],
209 [ "EchoString", utf8String
, utf8String
,
210 "EchoString w/ '" + utf8String
+ "'" ],
211 [ "EchoString", null, null, "EchoString w/ null value" ]
214 runTestsArray(javaIn
, tests
);
216 // Since 64 bit integers are treated as doubles in Javascript, rounding
217 // issues prevent us from actually using the max unsigned 64 bit value.
218 // var highULong = Math.pow(2,64) - 1;
219 var highULong
= 18446744073709550000;
222 [ "EchoPRBool", false, false, "EchoPRBool w/ false" ],
223 [ "EchoPRBool", true, true, "EchoPRBool w/ true" ],
224 [ "EchoPRInt32", minInt
, minInt
, "EchoPRInt32 w/ " + minInt
],
225 [ "EchoPRInt32", maxInt
, maxInt
, "EchoPRInt32 w/ " + maxInt
],
226 [ "EchoPRInt16", minShort
, minShort
, "EchoPRInt16 w/ " + minShort
],
227 [ "EchoPRInt16", maxShort
, maxShort
, "EchoPRInt16 w/ " + maxShort
],
228 [ "EchoPRInt64", lowLong
, lowLong
, "EchoPRInt64 w/ " + lowLong
],
229 [ "EchoPRInt64", highLong
, highLong
, "EchoPRInt64 w/ " + highLong
],
230 [ "EchoPRUint8", maxOctet
, maxOctet
, "EchoPRUint8 w/ " + maxOctet
],
231 [ "EchoPRUint8", 0, 0, "EchoPRUint8 w/ zero" ],
232 [ "EchoPRUint8", -1, maxOctet
, "EchoPRUint8 w/ -1" ],
233 [ "EchoPRUint16", maxUShort
, maxUShort
, "EchoPRUint16 w/ " + maxUShort
],
234 [ "EchoPRUint16", 0, 0, "EchoPRUint16 w/ zero" ],
235 [ "EchoPRUint16", -1, maxUShort
, "EchoPRUint16 w/ -1" ],
236 [ "EchoPRUint32", maxUInt
, maxUInt
, "EchoPRUint32 w/ " + maxUInt
],
237 [ "EchoPRUint32", 0, 0, "EchoPRUint32 w/ zero" ],
238 [ "EchoPRUint32", -1, maxUInt
, "EchoPRUint32 w/ -1" ],
240 [ "EchoPRUint64", highULong, highULong, "EchoPRUint64 w/ " + highULong ],
242 [ "EchoPRUint64", 0, 0, "EchoPRUint64 w/ zero" ]
245 runTestsArray(javaIn
, tests
);
248 /***********************************************************
250 ***********************************************************/
252 const nsIJXTestParams
= Components
.interfaces
.nsIJXTestParams
;
253 const nsISupports
= Components
.interfaces
.nsISupports
;
255 const CLASS_ID
= Components
.ID("{f0882957-bcc1-4854-a2cb-94051bad4193}");
256 const CLASS_NAME
= "JavaXPCOM Test Params Javascript XPCOM Component";
257 const CONTRACT_ID
= "@mozilla.org/javaxpcom/tests/params;1";
259 /***********************************************************
261 ***********************************************************/
264 function TestParams() {
268 TestParams
.prototype = {
270 // define the function we want to expose in our interface
271 runTests: function(tests
) {
276 QueryInterface: function(aIID
) {
277 if (!aIID
.equals(nsIJXTestParams
) &&
278 !aIID
.equals(nsISupports
))
279 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
284 /***********************************************************
286 ***********************************************************/
287 var TestParamsFactory
= {
288 createInstance: function (aOuter
, aIID
)
291 throw Components
.results
.NS_ERROR_NO_AGGREGATION
;
292 return (new TestParams()).QueryInterface(aIID
);
296 /***********************************************************
297 module definition (xpcom registration)
298 ***********************************************************/
299 var TestParamsModule
= {
301 registerSelf: function(aCompMgr
, aFileSpec
, aLocation
, aType
)
304 QueryInterface(Components
.interfaces
.nsIComponentRegistrar
);
305 aCompMgr
.registerFactoryLocation(CLASS_ID
, CLASS_NAME
,
306 CONTRACT_ID
, aFileSpec
, aLocation
, aType
);
309 unregisterSelf: function(aCompMgr
, aLocation
, aType
)
312 QueryInterface(Components
.interfaces
.nsIComponentRegistrar
);
313 aCompMgr
.unregisterFactoryLocation(CLASS_ID
, aLocation
);
316 getClassObject: function(aCompMgr
, aCID
, aIID
)
318 if (!aIID
.equals(Components
.interfaces
.nsIFactory
))
319 throw Components
.results
.NS_ERROR_NOT_IMPLEMENTED
;
321 if (aCID
.equals(CLASS_ID
))
322 return TestParamsFactory
;
324 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
327 canUnload: function(aCompMgr
)
333 /***********************************************************
334 module initialization
335 ***********************************************************/
336 function NSGetModule(aCompMgr
, aFileSpec
)
338 return TestParamsModule
;