PageRenderTime 88ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/carrot-jdk6-jnlp-macosx/src/plugin/share/classes/sun/plugin2/test/liveconnect/mozillaTests/number-001.js

https://github.com/carrot-garden/carrot-jnlper
JavaScript | 158 lines | 92 code | 27 blank | 39 comment | 2 complexity | e9d621d3bde338f6bd7aa93eb1198f59 MD5 | raw file
  1. /**
  2. File Name: number-001.js
  3. Description:
  4. If a Java method returns one of the primitive Java types below,
  5. JavaScript should read the value as JavaScript number primitive.
  6. * byte
  7. * short
  8. * int
  9. * long
  10. * float
  11. * double
  12. * char
  13. To test this:
  14. 1. Call a java method that returns one of the primitive java types above.
  15. 2. Check the value of the returned type
  16. 3. Check the type of the returned type, which should be "number"
  17. It is an error if the type of the JavaScript variable is "object" or if
  18. its class is JavaObject or Number.
  19. @author christine@netscape.com
  20. @version 1.00
  21. */
  22. var SECTION = "LiveConnect";
  23. var VERSION = "1_3";
  24. var TITLE = "Java Number Primitive to JavaScript Object: Test #1";
  25. var HEADER = SECTION + " " + TITLE;
  26. var tc = 0;
  27. var testcasesII = new Array();
  28. startTest();
  29. writeHeaderToLog( HEADER );
  30. // In all test cases, the expected type is "number"
  31. var E_TYPE = "number";
  32. // Create arrays of actual results (java_array) and
  33. // expected results (test_array).
  34. var java_array = new Array();
  35. var test_array = new Array();
  36. try {
  37. var i = 0;
  38. // Call a java function that returns a value whose type is int.
  39. java_array[i] = new JavaValue( app.Packages.java.lang.Integer.parseInt('255') );
  40. test_array[i] = new TestValue( "app.Packages.java.lang.Integer.parseInt('255')",
  41. 255,
  42. E_TYPE );
  43. i++;
  44. java_array[i] = new JavaValue( (new app.Packages.java.lang.Double( '123456789' )).intValue() );
  45. test_array[i] = new TestValue( "(new app.Packages.java.lang.Double( '123456789' )).intValue()",
  46. 123456789,
  47. E_TYPE );
  48. i++;
  49. // Call a java function that returns a value whose type is double
  50. java_array[i] = new JavaValue( (new app.Packages.java.lang.Integer( '123456789' )).doubleValue() );
  51. test_array[i] = new TestValue( "(new app.Packages.java.lang.Integer( '123456789' )).doubleValue()",
  52. 123456789,
  53. E_TYPE );
  54. i++;
  55. // Call a java function that returns a value whose type is long
  56. java_array[i] = new JavaValue( (new app.Packages.java.lang.Long('1234567891234567' )).longValue() );
  57. test_array[i] = new TestValue( "(new app.Packages.java.lang.Long( '1234567891234567' )).longValue()",
  58. 1234567891234567,
  59. E_TYPE );
  60. i++;
  61. // Call a java function that returns a value whose type is float
  62. java_array[i] = new JavaValue( (new app.Packages.java.lang.Float( '1.23456789' )).floatValue() );
  63. test_array[i] = new TestValue( "(new app.Packages.java.lang.Float( '1.23456789' )).floatValue()",
  64. 1.23456789,
  65. E_TYPE );
  66. i++;
  67. // Call a java function that returns a value whose type is char
  68. java_array[i] = new JavaValue( (new app.Packages.java.lang.String("hello")).charAt(0) );
  69. test_array[i] = new TestValue( "(new app.Packages.java.lang.String('hello')).charAt(0)",
  70. "h".charCodeAt(0),
  71. E_TYPE );
  72. i++;
  73. // Call a java function that returns a value whose type is short
  74. java_array[i] = new JavaValue( (new app.Packages.java.lang.Byte(127)).shortValue() );
  75. test_array[i] = new TestValue( "(new app.Packages.java.lang.Byte(127)).shortValue()",
  76. 127,
  77. E_TYPE );
  78. i++;
  79. // Call a java function that returns a value whose type is byte
  80. java_array[i] = new JavaValue( (new app.Packages.java.lang.Byte(127)).byteValue() );
  81. test_array[i] = new TestValue( "(new app.Packages.java.lang.Byte(127)).byteValue()",
  82. 127,
  83. E_TYPE );
  84. for ( i = 0; i < java_array.length; i++ ) {
  85. CompareValues( java_array[i], test_array[i] );
  86. }
  87. test();
  88. } catch (e) {
  89. writeExceptionToLog(e);
  90. }
  91. function CompareValues( javaval, testval ) {
  92. // Check value
  93. testcasesII[testcasesII.length] = new TestCase( SECTION,
  94. testval.description,
  95. testval.value,
  96. javaval.value );
  97. // Check type.
  98. testcasesII[testcasesII.length] = new TestCase( SECTION,
  99. "typeof (" + testval.description +")",
  100. testval.type,
  101. javaval.type );
  102. }
  103. function JavaValue( value ) {
  104. this.value = value.valueOf();
  105. this.type = typeof value;
  106. return this;
  107. }
  108. function TestValue( description, value, type, classname ) {
  109. this.description = description;
  110. this.value = value;
  111. this.type = type;
  112. return this;
  113. }
  114. function test() {
  115. for ( tc=0; tc < testcasesII.length; tc++ ) {
  116. testcasesII[tc].passed = writeTestCaseResult(
  117. testcasesII[tc].expect,
  118. testcasesII[tc].actual,
  119. testcasesII[tc].description +" = "+
  120. testcasesII[tc].actual );
  121. testcasesII[tc].reason += ( testcasesII[tc].passed ) ? "" : "wrong value ";
  122. }
  123. stopTest();
  124. return ( testcasesII );
  125. }