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 ***** */
38 import java
.io
.FileFilter
;
39 import java
.io
.IOException
;
40 import java
.io
.UnsupportedEncodingException
;
41 import java
.lang
.reflect
.InvocationTargetException
;
42 import java
.lang
.reflect
.Method
;
43 import java
.util
.Arrays
;
45 import org
.mozilla
.interfaces
.nsIEcho
;
46 import org
.mozilla
.interfaces
.nsIJXTestArrayParams
;
47 import org
.mozilla
.interfaces
.nsIJXTestParams
;
48 import org
.mozilla
.interfaces
.nsIStackFrame
;
49 import org
.mozilla
.interfaces
.nsISupports
;
50 import org
.mozilla
.interfaces
.nsITestXPCFunctionCallback
;
51 import org
.mozilla
.interfaces
.nsITestXPCSomeUselessThing
;
52 import org
.mozilla
.interfaces
.nsIXPCTestArray
;
53 import org
.mozilla
.interfaces
.nsIXPCTestIn
;
54 import org
.mozilla
.xpcom
.Mozilla
;
55 import org
.mozilla
.xpcom
.XPCOMException
;
59 * Tests the marshalling and unmarshalling of method parameters using the
60 * XPConnect testcases from mozilla/js/src/xpconnect/tests.
62 public class TestParams
{
64 private static File grePath
;
66 private static String testString
= "this is a test string";
67 private static String emptyString
= "";
68 private static String utf8String
=
69 "Non-Ascii 1 byte chars: Ž‰ŠˆŒ�, 2 byte chars: \u1234 \u1235 \u1236";
70 //private static String sharedString = "a shared string";
74 * @param args 0 - full path to XULRunner binary directory
76 public static void main(String
[] args
) {
79 } catch (IllegalArgumentException e
) {
83 Mozilla mozilla
= Mozilla
.getInstance();
84 mozilla
.initialize(grePath
);
88 profile
= createTempProfileDir();
89 LocationProvider locProvider
= new LocationProvider(grePath
,
91 mozilla
.initEmbedding(grePath
, grePath
, locProvider
);
92 } catch (IOException e
) {
103 } catch (Exception e
) {
108 nsIJXTestParams jstest
= (nsIJXTestParams
) Mozilla
.getInstance()
109 .getComponentManager()
110 .createInstanceByContractID("@mozilla.org/javaxpcom/tests/params;1",
111 null, nsIJXTestParams
.NS_IJXTESTPARAMS_IID
);
113 jstest
.runTests(new JavaTests());
114 } catch (XPCOMException e
) {
121 mozilla
.termEmbedding();
125 private static void checkArgs(String
[] args
) {
126 if (args
.length
!= 1) {
128 throw new IllegalArgumentException();
131 grePath
= new File(args
[0]);
132 if (!grePath
.exists() || !grePath
.isDirectory()) {
133 System
.err
.println("ERROR: given path doesn't exist");
135 throw new IllegalArgumentException();
139 private static void printUsage() {
140 // TODO Auto-generated method stub
143 private static File
createTempProfileDir() throws IOException
{
144 // Get name of temporary profile directory
145 File profile
= File
.createTempFile("mozilla-test-", null);
148 // On some operating systems (particularly Windows), the previous
149 // temporary profile may not have been deleted. Delete them now.
150 File
[] files
= profile
.getParentFile()
151 .listFiles(new FileFilter() {
152 public boolean accept(File file
) {
153 if (file
.getName().startsWith("mozilla-test-")) {
159 for (int i
= 0; i
< files
.length
; i
++) {
163 // Create temporary profile directory
169 private static void deleteDir(File dir
) {
170 File
[] files
= dir
.listFiles();
171 for (int i
= 0; i
< files
.length
; i
++) {
172 if (files
[i
].isDirectory()) {
181 * @param aCallee Object on which to run tests
182 * @param aTests An array of tests to run. The format looks like this:
183 * index 0: method name of test to run
184 * 1: Object array of method arguments
186 * 3: comment to use when logging test result; if
187 * <code>null</code>, uses method name
188 * @throws RuntimeException if any of the tests fails
190 private static void runTestsArray(Object aCallee
, Object
[][] aTests
) {
191 boolean succeeded
= true;
192 for (int i
= 0; i
< aTests
.length
; i
++) {
193 Method method
= getMethod(aCallee
.getClass(), (String
) aTests
[i
][0]);
195 String comment
= (aTests
[i
][3] != null) ?
(String
) aTests
[i
][3] :
196 (String
) aTests
[i
][0];
198 Object result
= null;
199 boolean passed
= false;
201 result
= method
.invoke(aCallee
, (Object
[]) aTests
[i
][1]);
202 if (result
!= null) {
203 passed
= (result
.equals(aTests
[i
][2]));
205 passed
= (result
== aTests
[i
][2]);
207 } catch (Exception e
) {
209 System
.err
.println("*** TEST " + comment
+ " threw exception:");
211 logResult(comment
, false);
215 logResult(comment
, passed
);
219 System
.err
.println("*** TEST " + comment
+ " FAILED: expected "
220 + aTests
[i
][2] + ", returned " + result
);
225 throw new RuntimeException("RunTestsArray did not succeed");
230 * @param aCallee Object on which to run tests
231 * @param aTests An array of tests to run. The format looks like this:
232 * index 0: method name of test to run
233 * 1: Object array of method arguments
234 * 2: comparator method that checks result
235 * 3: comment to use when logging test result; if
236 * <code>null</code>, uses method name
237 * @throws RuntimeException if any of the tests fails
239 private static void runTestsArrayWithComparator(Object aCallee
,
241 boolean succeeded
= true;
242 for (int i
= 0; i
< aTests
.length
; i
++) {
243 Method method
= getMethod(aCallee
.getClass(), (String
) aTests
[i
][0]);
245 String comment
= (aTests
[i
][3] != null) ?
(String
) aTests
[i
][3] :
246 (String
) aTests
[i
][0];
248 Object result
= null;
249 boolean passed
= false;
250 Exception exp
= null;
251 Object
[] args
= (Object
[]) aTests
[i
][1];
253 result
= method
.invoke(aCallee
, args
);
254 } catch (Exception e
) {
256 if (exp
instanceof InvocationTargetException
) {
257 exp
= (Exception
) ((InvocationTargetException
) exp
).getCause();
261 // call comparator method to see if test passed
263 IResultComparator r
= (IResultComparator
) aTests
[i
][2];
264 passed
= r
.didPass(args
, result
, exp
);
265 } catch (Exception e
) {
270 logResult(comment
, passed
);
275 System
.err
.println("*** TEST " + comment
+ " threw exception:");
276 exp
.printStackTrace();
282 throw new RuntimeException("RunTestsArray did not succeed");
286 private static Method
getMethod(Class aClass
, String aMethodName
) {
287 Method
[] methods
= aClass
.getMethods();
288 for (int i
= 0; i
< methods
.length
; i
++) {
289 if (methods
[i
].getName().equals(aMethodName
)) {
296 private static void logResult(String comment
, boolean passed
) {
297 System
.out
.println((passed ?
"passed" : "FAILED") + ": " + comment
);
300 private static void runEchoTests() {
301 nsIEcho nativeEcho
= (nsIEcho
) Mozilla
.getInstance()
302 .getComponentManager()
303 .createInstanceByContractID("@mozilla.org/javaxpcom/tests/xpc;1",
304 null, nsIEcho
.NS_IECHO_IID
);
306 Object
[][] tests
= new Object
[][] {
308 { "in2OutOneDOMString", new Object
[] { testString
}, testString
,
309 "in2OutOneDOMString w/ '" + testString
+ "'" },
310 { "in2OutOneDOMString", new Object
[] { emptyString
}, emptyString
,
311 "in2OutOneDomString w/ empty string" },
312 { "in2OutOneDOMString", new Object
[] { null }, null,
313 "in2OutOneDOMString w/ null value" },
315 { "in2OutOneAString", new Object
[] { testString
}, testString
,
316 "in2OutOneAString w/ '" + testString
+ "'" },
317 { "in2OutOneAString", new Object
[] { emptyString
}, emptyString
,
318 "in2OutOneAString w/ empty string" },
319 { "in2OutOneAString", new Object
[] { utf8String
}, utf8String
,
320 "in2OutOneAString w/ '" + utf8String
+ "'" },
321 { "in2OutOneAString", new Object
[] { null }, null,
322 "in2OutOneAString w/ null value" },
323 /* test AUTF8String */
324 { "in2OutOneUTF8String", new Object
[] { testString
}, testString
,
325 "in2OutOneUTF8String w/ '" + testString
+ "'" },
326 { "in2OutOneUTF8String", new Object
[] { emptyString
}, emptyString
,
327 "in2OutOneUTF8String w/ empty string" },
328 { "in2OutOneUTF8String", new Object
[] { utf8String
}, utf8String
,
329 "in2OutOneUTF8String w/ '" + utf8String
+ "'" },
330 { "in2OutOneUTF8String", new Object
[] { null }, null,
331 "in2OutOneUTF8String w/ null value" },
333 { "in2OutOneCString", new Object
[] { testString
}, testString
,
334 "in2OutOneCString w/ '" + testString
+ "'" },
335 { "in2OutOneCString", new Object
[] { emptyString
}, emptyString
,
336 "in2OutOneCString w/ empty string" },
337 { "in2OutOneCString", new Object
[] { null }, null,
338 "in2OutOneCString w/ null value" },
339 /* test normal strings */
340 { "in2OutOneString", new Object
[] { testString
}, testString
,
341 "in2OutOneString w/ '" + testString
+ "'" },
342 { "in2OutOneString", new Object
[] { emptyString
}, emptyString
,
343 "in2OutOneString w/ empty string" }
346 runTestsArray(nativeEcho
, tests
);
349 Object result
= null;
350 boolean passed
= false;
351 String comment
= "in2OutOneString w/ null value";
353 result
= nativeEcho
.in2OutOneString(null);
354 passed
= (result
== null);
355 } catch (Exception e
) {
356 // Calling nativeEcho.in2OutOneString(null) should throw an
357 // exception, with errorcode == NS_ERROR_FAILURE.
358 if (e
instanceof XPCOMException
&&
359 ((XPCOMException
) e
).errorcode
== Mozilla
.NS_ERROR_FAILURE
) {
364 System
.err
.println("*** TEST " + comment
+
365 " threw exception:");
367 logResult(comment
, false);
372 logResult(comment
, passed
);
375 System
.err
.println("*** TEST " + comment
+
376 " FAILED: expected null, returned " + result
);
380 tests
= new Object
[][] {
381 { "setAString", new Object
[] { testString
}, null,
382 "setAString w/ '" + testString
+ "'" },
383 { "getAString", null, testString
,
384 "getAString w/ '" + testString
+ "'" },
385 { "setAString", new Object
[] { emptyString
}, null,
386 "setAString w/ empty string" },
387 { "getAString", null, emptyString
,
388 "getAString w/ empty string" },
389 { "setAString", new Object
[] { null }, null,
390 "setAString w/ null value" },
391 { "getAString", null, null,
392 "getAString w/ null value" },
393 /* Is the 'shared' attribute even used in scriptable functions?
394 { "sharedString", null, sharedString, null } */
397 runTestsArray(nativeEcho
, tests
);
400 private static void runInTests() {
401 nsIXPCTestIn nativeInTest
= (nsIXPCTestIn
) Mozilla
.getInstance()
402 .getComponentManager()
403 .createInstanceByContractID("@mozilla.org/javaxpcom/tests/xpc;1",
404 null, nsIXPCTestIn
.NS_IXPCTESTIN_IID
);
406 /* XPConnect converts 64-bit values into doubles when calling into
407 * Javascript. If we use Long.MIN_VALUE or Long.MAX_VALUE, the values
408 * are rounded 'up' when converted to double. Then, when converted back
409 * to a 64-bit integer, the conversion overflows, always resulting in
410 * Long.MIN_VALUE. To workaround that, we just call with very large
411 * values, but ones that won't overflow in conversion.
413 //Object minLong = new Long(Long.MIN_VALUE);
414 //Object maxLong = new Long(Long.MAX_VALUE);
415 Object minLong
= new Long(-9223372036854774784L);
416 Object maxLong
= new Long(9223372036854774784L);
417 Object zeroLong
= new Long(0);
418 Object negLong
= new Long(-1);
419 Object minInt
= new Integer(Integer
.MIN_VALUE
);
420 Object maxInt
= new Integer(Integer
.MAX_VALUE
);
421 Object zeroInt
= new Integer(0);
422 Object negInt
= new Integer(-1);
423 Object maxUInt
= new Long(4294967295L);
424 Object minShort
= new Short(Short
.MIN_VALUE
);
425 Object maxShort
= new Short(Short
.MAX_VALUE
);
426 Object zeroShort
= new Short((short) 0);
427 Object negShort
= new Short((short) -1);
428 Object maxUShort
= new Integer(65535);
429 Object charA
= new Character('a');
430 Object charZ
= new Character('Z');
431 Object charWide
= new Character('\u1234');
432 Object maxUByte
= new Short((short) 255);
433 Object minFloat
= new Float(Float
.MIN_VALUE
);
434 Object maxFloat
= new Float(Float
.MAX_VALUE
);
435 Object minDouble
= new Double(Double
.MIN_VALUE
);
436 Object maxDouble
= new Double(Double
.MAX_VALUE
);
438 Object
[][] tests
= new Object
[][] {
439 { "echoLong", new Object
[] { minInt
}, minInt
,
440 "echoLong w/ " + minInt
},
441 { "echoLong", new Object
[] { maxInt
}, maxInt
,
442 "echoLong w/ " + maxInt
},
443 { "echoShort", new Object
[] { minShort
}, minShort
,
444 "echoShort w/ " + minShort
},
445 { "echoShort", new Object
[] { maxShort
}, maxShort
,
446 "echoShort w/ " + maxShort
},
447 { "echoChar", new Object
[] { charA
}, charA
,
448 "echoChar w/ " + charA
},
449 { "echoChar", new Object
[] { charZ
}, charZ
,
450 "echoChar w/ " + charZ
},
451 { "echoBoolean", new Object
[] { Boolean
.FALSE
}, Boolean
.FALSE
,
452 "echoBoolean w/ " + Boolean
.FALSE
},
453 { "echoBoolean", new Object
[] { Boolean
.TRUE
}, Boolean
.TRUE
,
454 "echoBoolean w/ " + Boolean
.TRUE
},
455 { "echoOctet", new Object
[] { zeroShort
}, zeroShort
,
456 "echoOctet w/ zero"},
457 { "echoOctet", new Object
[] { maxUByte
}, maxUByte
,
458 "echoOctet w/ " + maxUByte
},
459 { "echoLongLong", new Object
[] { minLong
}, minLong
,
460 "echoLongLong w/ " + minLong
},
461 { "echoLongLong", new Object
[] { maxLong
}, maxLong
,
462 "echoLongLong w/ " + maxLong
},
463 { "echoUnsignedShort", new Object
[] { maxUShort
}, maxUShort
,
464 "echoUnsignedShort w/ " + maxUShort
},
465 { "echoUnsignedShort", new Object
[] { zeroInt
}, zeroInt
,
466 "echoUnsignedShort w/ zero" },
467 // is this test case correct/valid?
468 { "echoUnsignedShort", new Object
[] { negInt
}, maxUShort
,
469 "echoUnsignedShort w/ -1" },
470 { "echoUnsignedLong", new Object
[] { maxUInt
}, maxUInt
,
471 "echoUnsignedLong w/ " + maxUInt
},
472 { "echoUnsignedLong", new Object
[] { zeroLong
}, zeroLong
,
473 "echoUnsignedLong w/ zero" },
474 { "echoUnsignedLong", new Object
[] { negLong
}, maxUInt
,
475 "echoUnsignedLong w/ -1" },
476 { "echoFloat", new Object
[] { minFloat
}, minFloat
,
477 "echoFloat w/ " + minFloat
},
478 { "echoFloat", new Object
[] { maxFloat
}, maxFloat
,
479 "echoFloat w/ " + maxFloat
},
480 { "echoDouble", new Object
[] { minDouble
}, minDouble
,
481 "echoDouble w/ " + minDouble
},
482 { "echoDouble", new Object
[] { maxDouble
}, maxDouble
,
483 "echoDouble w/ " + maxDouble
},
484 { "echoWchar", new Object
[] { charA
}, charA
,
485 "echoWchar w/ " + charA
},
486 { "echoWchar", new Object
[] { charZ
}, charZ
,
487 "echoWchar w/ " + charZ
},
488 { "echoWchar", new Object
[] { charWide
}, charWide
,
489 "echoWchar w/ " + charWide
},
490 { "echoString", new Object
[] { testString
}, testString
,
491 "echoString w/ '" + testString
+ "'" },
492 { "echoString", new Object
[] { emptyString
}, emptyString
,
493 "echoString w/ empty string" },
494 { "echoString", new Object
[] { utf8String
}, utf8String
,
495 "echoString w/ '" + utf8String
+ "'" },
496 { "echoString", new Object
[] { null }, null,
497 "echoString w/ null value" },
500 runTestsArray(nativeInTest
, tests
);
502 // Max unsigned long long = 18446744073709551615UL
503 Object maxULong
= new Double(1.8446744073709550E19
);
504 Object zeroDouble
= new Double(0.0);
506 tests
= new Object
[][] {
507 { "echoPRBool", new Object
[] { Boolean
.FALSE
}, Boolean
.FALSE
,
508 "echoPRBool w/ false" },
509 { "echoPRBool", new Object
[] { Boolean
.TRUE
}, Boolean
.TRUE
,
510 "echoPRBool w/ true" },
511 { "echoPRInt32", new Object
[] { minInt
}, minInt
,
512 "echoPRInt32 w/ " + minInt
},
513 { "echoPRInt32", new Object
[] { maxInt
}, maxInt
,
514 "echoPRInt32 w/ " + maxInt
},
515 { "echoPRInt16", new Object
[] { minShort
}, minShort
,
516 "echoPRInt16 w/ " + minShort
},
517 { "echoPRInt16", new Object
[] { maxShort
}, maxShort
,
518 "echoPRInt16 w/ " + maxShort
},
519 { "echoPRInt64", new Object
[] { minLong
}, minLong
,
520 "echoPRInt64 w/ " + minLong
},
521 { "echoPRInt64", new Object
[] { maxLong
}, maxLong
,
522 "echoPRInt64 w/ " + maxLong
},
523 { "echoPRUint8", new Object
[] { maxUByte
}, maxUByte
,
524 "echoPRUint8 w/ " + maxUByte
},
525 { "echoPRUint8", new Object
[] { zeroShort
}, zeroShort
,
526 "echoPRUint8 w/ zero" },
527 { "echoPRUint8", new Object
[] { negShort
}, maxUByte
,
528 "echoPRUint8 w/ -1" },
529 { "echoPRUint16", new Object
[] { maxUShort
}, maxUShort
,
530 "echoPRUint16 w/ " + maxUShort
},
531 { "echoPRUint16", new Object
[] { zeroInt
}, zeroInt
,
532 "echoPRUint16 w/ zero" },
533 { "echoPRUint16", new Object
[] { negInt
}, maxUShort
,
534 "echoPRUint16 w/ -1" },
535 { "echoPRUint32", new Object
[] { maxUInt
}, maxUInt
,
536 "echoPRUint32 w/ " + maxUInt
},
537 { "echoPRUint32", new Object
[] { zeroLong
}, zeroLong
,
538 "echoPRUint32 w/ zero" },
539 { "echoPRUint32", new Object
[] { negLong
}, maxUInt
,
540 "echoPRUint32 w/ -1" },
541 { "echoPRUint64", new Object
[] { maxULong
}, maxULong
,
542 "echoPRUint64 w/ " + maxULong
},
543 { "echoPRUint64", new Object
[] { zeroDouble
}, zeroDouble
,
544 "echoPRUint64 w/ zero" },
546 { "echoPRUint64", new Object[] { negInt }, maxULong,
547 "echoPRUint64 w/ -1" }, */
550 runTestsArray(nativeInTest
, tests
);
553 private static void runArrayTests() {
554 /*** nsIXPCTestArray ***/
556 nsIXPCTestArray xpcArrayTests
= (nsIXPCTestArray
) Mozilla
.getInstance()
557 .getComponentManager()
558 .createInstanceByContractID("@mozilla.org/javaxpcom/tests/xpc;1",
559 null, nsIXPCTestArray
.NS_IXPCTESTARRAY_IID
);
561 boolean succeeded
= true;
563 final int[][] intArray
= { { 1, 2, 3, 4, 0, -1, -2, -3, -4 } };
564 int[][] intArrayCopy
= (int[][]) intArray
.clone();
565 Integer multiple
= new Integer(2);
567 final String
[][] str
= { { "this", "is", "to", "be", "reversed" } };
568 String
[][] strCopy
= (String
[][]) str
.clone();
569 long[] count
= { strCopy
[0].length
};
571 String
[][] strCopy2
= (String
[][]) str
.clone();
573 Object
[][] tests
= new Object
[][] {
574 { "multiplyEachItemInIntegerArray",
575 new Object
[] { multiple
, new Long(intArrayCopy
[0].length
), intArrayCopy
},
576 new IResultComparator() {
577 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
580 int m
= ((Integer
) args
[0]).intValue();
581 int[][] resultArray
= (int[][]) args
[2];
582 for (int i
= 0; i
< intArray
[0].length
; i
++) {
583 if (intArray
[0][i
] * m
!= resultArray
[0][i
])
591 { "doubleStringArray",
592 new Object
[] { count
, strCopy
},
593 new IResultComparator() {
594 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
597 int inLength
= str
[0].length
;
598 int outLength
= (int) ((long[]) args
[0])[0];
599 if (outLength
!= inLength
* 2)
602 String
[][] resultArray
= (String
[][]) args
[1];
603 for (int i
= 0; i
< inLength
; i
++) {
604 String doubled
= doubleString(str
[0][i
]);
605 if (!doubled
.equals(resultArray
[0][i
*2]) ||
606 !doubled
.equals(resultArray
[0][i
*2+1]))
612 private String
doubleString(String string
) {
613 StringBuffer buf
= new StringBuffer(string
.length()*2);
614 for (int i
= 0; i
< string
.length(); i
++) {
615 buf
.append(string
.charAt(i
));
616 buf
.append(string
.charAt(i
));
618 return buf
.toString();
623 { "reverseStringArray",
624 new Object
[] { new Long(strCopy2
[0].length
), strCopy2
},
625 new IResultComparator() {
626 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
629 int length
= ((Long
) args
[0]).intValue();
630 String
[][] resultArray
= (String
[][]) args
[1];
631 for (int i
= 0; i
< length
; i
++) {
632 if (!str
[0][i
].equals(resultArray
[0][length
- 1 - i
]))
642 runTestsArrayWithComparator(xpcArrayTests
, tests
);
645 /*** nsIJXTestArrayParams ***/
647 nsIJXTestArrayParams arrayTests
= (nsIJXTestArrayParams
) xpcArrayTests
648 .queryInterface(nsIJXTestArrayParams
.NS_IJXTESTARRAYPARAMS_IID
);
650 System
.arraycopy(intArray
[0], 0, intArrayCopy
[0], 0, intArray
[0].length
);
651 int[][] resultArray
= new int[1][];
652 IResultComparator copyIntArrayResultComparator
= new IResultComparator() {
653 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
656 int inLength
= ((Long
) args
[1]).intValue();
657 int[][] resultArray
= (int[][]) args
[2];
658 for (int i
= 0; i
< inLength
; i
++) {
659 if (intArray
[0][i
] != resultArray
[0][i
])
665 // for tests that are expected to throw ArrayIndexOutOfBoundsException
666 IResultComparator arrayIOBExpComparator
= new IResultComparator() {
667 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
669 if (e
instanceof ArrayIndexOutOfBoundsException
)
676 String
[] returnString
= new String
[1];
677 IResultComparator stringComparator
= new IResultComparator() {
678 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
681 String inString
= (String
) args
[0];
682 int inLength
= ((Long
) args
[1]).intValue();
683 String
[] outString
= (String
[]) args
[2];
684 if (outString
[0].length() != inLength
)
686 if (!inString
.substring(0, inLength
).equals(outString
[0]))
692 IResultComparator returnStringComparator
= new IResultComparator() {
693 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
696 String inString
= (String
) args
[0];
697 int inLength
= ((Long
) args
[1]).intValue();
698 String outString
= (String
) result
;
699 if (outString
.length() != inLength
)
701 if (!inString
.substring(0, inLength
).equals(outString
))
707 byte[] testBytes
= null;
708 byte[][] returnBytes
= new byte[1][];
710 testBytes
= utf8String
.getBytes("UTF-8");
711 } catch (UnsupportedEncodingException e1
) {
712 e1
.printStackTrace();
715 tests
= new Object
[][] {
716 { "multiplyEachItemInIntegerArray2",
717 new Object
[] { multiple
, intArrayCopy
, new Long(intArrayCopy
[0].length
) },
718 new IResultComparator() {
719 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
722 int m
= ((Integer
) args
[0]).intValue();
723 int[][] resultArray
= (int[][]) args
[1];
724 for (int i
= 0; i
< intArray
[0].length
; i
++) {
725 if (intArray
[0][i
] * m
!= resultArray
[0][i
])
734 new Object
[] { intArray
[0], new Long(intArray
[0].length
), resultArray
},
735 copyIntArrayResultComparator
,
739 new Object
[] { intArray
[0], new Long(intArray
[0].length
- 2), resultArray
},
740 copyIntArrayResultComparator
,
741 "copyIntArray w/ smaller count"
744 new Object
[] { intArray
[0], new Long(intArray
[0].length
+ 2), resultArray
},
745 arrayIOBExpComparator
,
746 "copyIntArray w/ larger count"
749 new Object
[] { intArray
[0], new Long(intArray
[0].length
) },
750 new IResultComparator() {
751 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
754 int[] inputArray
= (int[]) args
[0];
755 int[] resultArray
= (int[]) result
;
756 return Arrays
.equals(resultArray
, inputArray
);
762 new Object
[] { intArray
[0], new Long(intArray
[0].length
- 2) },
763 new IResultComparator() {
764 public boolean didPass(Object
[] args
, Object result
, Exception e
) {
767 int[] inputArray
= (int[]) args
[0];
768 int inLength
= ((Long
) args
[1]).intValue();
769 int[] resultArray
= (int[]) result
;
770 if (resultArray
.length
!= inLength
)
773 for (int i
= 0; i
< inLength
; i
++) {
774 if (resultArray
[i
] != inputArray
[i
])
780 "returnIntArray w/ smaller count"
783 new Object
[] { intArray
[0], new Long(intArray
[0].length
+ 2) },
784 arrayIOBExpComparator
,
785 "returnIntArray w/ larger count"
788 new Object
[] { testBytes
, new Long(testBytes
.length
), returnBytes
},
789 new IResultComparator() {
790 public boolean didPass(Object
[] args
, Object result
,
794 byte[] inArray
= (byte[]) args
[0];
795 byte[][] outArray
= (byte[][]) args
[2];
796 return Arrays
.equals(inArray
, outArray
[0]);
802 new Object
[] { testBytes
, new Long(testBytes
.length
) },
803 new IResultComparator() {
804 public boolean didPass(Object
[] args
, Object result
,
808 byte[] inArray
= (byte[]) args
[0];
809 byte[] outArray
= (byte[]) result
;
810 return Arrays
.equals(inArray
, outArray
);
816 new Object
[] { testString
, new Long(testString
.length()), returnString
},
820 { "returnSizedString",
821 new Object
[] { testString
, new Long(testString
.length()) },
822 returnStringComparator
,
825 { "copySizedWString",
826 new Object
[] { utf8String
, new Long(utf8String
.length()), returnString
},
830 { "returnSizedWString",
831 new Object
[] { utf8String
, new Long(utf8String
.length()) },
832 returnStringComparator
,
837 runTestsArrayWithComparator(arrayTests
, tests
);
840 throw new RuntimeException("Array tests did not succeed");
846 class JavaTests
implements nsIEcho
, nsIXPCTestIn
{
848 private nsIEcho receiver
;
849 private String savedString
;
850 private Object
[] results
= new Object
[8];
854 public nsISupports
queryInterface(String aIID
) {
855 return Mozilla
.queryInterface(this, aIID
);
860 public void callFunction(nsITestXPCFunctionCallback callback
, String s
) {
861 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
864 public void callFunctionWithThis(nsITestXPCFunctionCallback callback
,
865 nsISupports self
, String s
) {
866 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
869 public void callReceiverSometimeLater() {
870 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
873 public void debugDumpJSStack() {
874 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
877 public String
echoIn2OutOneAString(String input
) {
878 if (receiver
!= null) {
879 return receiver
.echoIn2OutOneAString(input
);
881 return in2OutOneAString(input
);
884 public String
echoIn2OutOneCString(String input
) {
885 if (receiver
!= null) {
886 return receiver
.echoIn2OutOneCString(input
);
888 return in2OutOneCString(input
);
891 public String
echoIn2OutOneDOMString(String input
) {
892 if (receiver
!= null) {
893 return receiver
.echoIn2OutOneDOMString(input
);
895 return in2OutOneDOMString(input
);
898 public String
echoIn2OutOneUTF8String(String input
) {
899 if (receiver
!= null) {
900 return receiver
.echoIn2OutOneUTF8String(input
);
902 return in2OutOneUTF8String(input
);
905 public void failInJSTest(int fail
) {
906 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
909 public String
getAString() {
913 public int getSomeValue() {
914 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
917 public nsIStackFrame
getStack() {
918 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
921 public short getThrowInGetter() {
922 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
925 public int in2OutAddTwoInts(int input1
, int input2
, int[] output1
,
927 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
930 public String
in2OutOneAString(String input
) {
932 return new String(input
);
936 public String
in2OutOneCString(String input
) {
938 return new String(input
);
942 public String
in2OutOneDOMString(String input
) {
944 return new String(input
);
948 public int in2OutOneInt(int input
) {
949 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
952 public String
in2OutOneString(String input
) {
954 return new String(input
);
958 public String
in2OutOneUTF8String(String input
) {
960 return new String(input
);
964 public void methodWithForwardDeclaredParam(nsITestXPCSomeUselessThing sut
) {
965 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
968 public void printArgTypes() {
969 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
972 public nsISupports
pseudoQueryInterface(String uuid
) {
973 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
976 public void returnCode(int code
) {
977 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
980 public void returnCode_NS_ERROR_NULL_POINTER() {
981 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
984 public void returnCode_NS_ERROR_OUT_OF_MEMORY() {
985 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
988 public void returnCode_NS_ERROR_UNEXPECTED() {
989 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
992 public void returnCode_NS_OK() {
993 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
996 public nsISupports
returnInterface(nsISupports obj
) {
997 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
1000 public void sendInOutManyTypes(short[] p1
, short[] p2
, int[] p3
, long[] p4
,
1001 short[] p5
, int[] p6
, long[] p7
, double[] p8
, float[] p9
,
1002 double[] p10
, boolean[] p11
, char[] p12
, char[] p13
, String
[] p14
,
1003 String
[] p15
, String
[] p16
) {
1004 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
1007 public void sendManyTypes(short p1
, short p2
, int p3
, long p4
, short p5
,
1008 int p6
, long p7
, double p8
, float p9
, double p10
, boolean p11
,
1009 char p12
, char p13
, String p14
, String p15
, String p16
) {
1010 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
1013 public void sendOneString(String str
) {
1014 if (receiver
!= null) {
1015 receiver
.sendOneString(str
);
1020 public void setAString(String aAString
) {
1021 savedString
= aAString
;
1024 public void setReceiver(nsIEcho aReceiver
) {
1025 System
.out
.println("Setting receiver to nsIEcho impl = " + aReceiver
);
1026 receiver
= aReceiver
;
1029 public void setReceiverReturnOldReceiver(nsIEcho
[] aReceiver
) {
1030 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
1033 public void setSomeValue(int aSomeValue
) {
1034 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
1037 public String
sharedString() {
1038 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
1041 public void simpleCallNoEcho() {
1042 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
1045 public void throwArg() {
1046 throw new XPCOMException(Mozilla
.NS_ERROR_NOT_IMPLEMENTED
);
1051 public boolean echoBoolean(boolean b
) {
1055 public char echoChar(char c
) {
1059 public double echoDouble(double d
) {
1063 public float echoFloat(float f
) {
1067 public int echoLong(int l
) {
1071 public long echoLongLong(long ll
) {
1075 public short echoOctet(short o
) {
1079 public boolean echoPRBool(boolean b
) {
1083 public short echoPRInt16(short l
) {
1087 public int echoPRInt32(int l
) {
1091 public long echoPRInt64(long i
) {
1095 public int echoPRUint16(int i
) {
1099 public long echoPRUint32(long i
) {
1103 public long echoPRUint32_2(long i
) {
1107 public double echoPRUint64(double i
) {
1111 public short echoPRUint8(short i
) {
1115 public short echoShort(short a
) {
1119 public String
echoString(String ws
) {
1123 public long echoUnsignedLong(long ul
) {
1127 public int echoUnsignedShort(int us
) {
1131 public void echoVoid() {
1134 public char echoWchar(char wc
) {
1138 public void copyIntArray(int[] srcArray
, long count
, int[][] dstArray
) {
1139 System
.arraycopy(srcArray
, 0, dstArray
[0], 0, (int) count
);
1142 public void copySizedString(String srcString
, long count
, String
[] dstString
) {
1143 dstString
[0] = srcString
.substring(0, (int) count
);
1146 public void multiplyEachItemInIntegerArray2(int val
, int[][] valueArray
,
1148 for(int i
= 0; i
< count
; i
++) {
1149 valueArray
[0][i
] *= val
;
1153 public int[] returnIntArray(int[] srcArray
, long count
) {
1154 int[] array
= new int[(int) count
];
1155 System
.arraycopy(srcArray
, 0, array
, 0, (int) count
);
1159 public String
returnSizedString(String srcString
, long count
) {
1160 return srcString
.substring(0, (int) count
);
1165 interface IResultComparator
{
1167 boolean didPass(Object
[] args
, Object result
, Exception e
);