1 /* -*- Mode: java; tab-width: 8 -*-
2 * Copyright (C) 1997, 1998 Netscape Communications Corporation,
5 /* ***** BEGIN LICENSE BLOCK *****
6 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
8 * The contents of this file are subject to the Mozilla Public License Version
9 * 1.1 (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS" basis,
14 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15 * for the specific language governing rights and limitations under the
18 * The Original Code is Mozilla Communicator client code, released
21 * The Initial Developer of the Original Code is
22 * Netscape Communications Corporation.
23 * Portions created by the Initial Developer are Copyright (C) 1998
24 * the Initial Developer. All Rights Reserved.
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 gTestfile = 'ToArray-001.js';
45 * JavaScript to Java type conversion.
47 * This test passes JavaScript number values to several Java methods
48 * that expect arguments of various types, and verifies that the value is
49 * converted to the correct value and type.
51 * This tests instance methods, and not static methods.
53 * Running these tests successfully requires you to have
54 * com.netscape.javascript.qa.liveconnect.DataTypeClass on your classpath.
56 * Specification: Method Overloading Proposal for Liveconnect 3.0
58 * @author: christine@netscape.com
61 var SECTION = "JavaArray to String";
63 var TITLE = "LiveConnect 3.0 JavaScript to Java Data Type Conversion " +
72 // Passing a JavaArray to a method that expects a java.lang.String should
73 // call the unwrapped array's toString method and return the result as a
74 // new java.lang.String.
76 // pass a byte array to a method that expects a string
78 a[i++] = new TestObject (
79 "dt.setStringObject(java.lang.String(new java.lang.String(\"hello\").getBytes()))",
81 "dt.getStringObject() +''",
82 "typeof dt.getStringObject()",
86 // pass a char array to a method that expects a string
88 a[i++] = new TestObject (
89 "dt.setStringObject(java.lang.String(new java.lang.String(\"goodbye\").toCharArray()))",
91 "dt.getStringObject() +''",
92 "typeof dt.getStringObject()",
96 a[i++] = new TestObject (
97 "dt.setStringObject(java.lang.String(new java.lang.String(\"goodbye\").toCharArray()))",
99 "dt.getStringObject() +''",
100 "typeof dt.getStringObject()",
104 // Vector.copyInto expects an object array
106 a[i++] = new TestObject(
107 "var vector = new java.util.Vector(); "+
108 "vector.addElement( \"a\" ); vector.addElement( \"b\" ); "+
109 "vector.copyInto( DT.PUB_STATIC_ARRAY_OBJECT )",
110 "DT.PUB_STATIC_ARRAY_OBJECT[0] +''",
111 "DT.staticGetObjectArray()[0] +''",
112 "typeof DT.staticGetObjectArray()[0]",
116 a[i++] = new TestObject(
117 "var vector = new java.util.Vector(); "+
118 "vector.addElement( \"a\" ); vector.addElement( 3 ); "+
119 "vector.copyInto( DT.PUB_STATIC_ARRAY_OBJECT )",
120 "DT.PUB_STATIC_ARRAY_OBJECT[1] +''",
121 "DT.staticGetObjectArray()[1] +''",
122 "DT.staticGetObjectArray()[1].getClass().getName() +''",
124 "java.lang.Double" );
128 var random = Math.random() +"";
130 for ( counter = 0; counter < random.length; counter ++ ) {
131 a[i++] = new TestObject (
132 "dt.setByteArray(java.lang.String(\""+random+"\").getBytes());",
133 "dt.PUB_STATIC_ARRAY_BYTE["+counter+"]",
134 "dt.getByteArray()["+counter+"]",
135 "typeof dt.getByteArray()["+counter+"]",
136 random[counter].charCodeAt(0),
142 random = Math.random() +"";
144 for ( counter = 0; counter < random.length; counter ++ ) {
145 a[i++] = new TestObject (
146 "dt.setCharArray(java.lang.String(\""+random+"\").toCharArray());",
147 "dt.PUB_STATIC_ARRAY_CHAR["+counter+"]",
148 "dt.getCharArray()["+counter+"]",
149 "typeof dt.getCharArray()["+counter+"]",
150 random[counter].charCodeAt(0),
156 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_INT.length))) +"";
157 for ( counter = 0; counter < random.length; counter++ ) {
158 dt.PUB_ARRAY_INT[counter] = random[counter];
160 for ( counter = 0; counter < random.length; counter ++ ) {
161 a[i++] = new TestObject (
162 "dt.setIntArray(dt.PUB_ARRAY_INT)",
163 "DT.PUB_STATIC_ARRAY_INT["+counter+"]",
164 "dt.getIntArray()["+counter+"]",
165 "typeof dt.getIntArray()["+counter+"]",
166 Number(random[counter]),
172 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_SHORT.length))) +"";
173 for ( counter = 0; counter < random.length; counter++ ) {
174 dt.PUB_ARRAY_SHORT[counter] = random[counter];
176 for ( counter = 0; counter < random.length; counter ++ ) {
177 a[i++] = new TestObject (
178 "dt.setShortArray(dt.PUB_ARRAY_SHORT)",
179 "DT.PUB_STATIC_ARRAY_SHORT["+counter+"]",
180 "dt.getShortArray()["+counter+"]",
181 "typeof dt.getShortArray()["+counter+"]",
182 Number(random[counter]),
188 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_LONG.length))) +"";
189 for ( counter = 0; counter < random.length; counter++ ) {
190 dt.PUB_ARRAY_LONG[counter] = random[counter];
192 for ( counter = 0; counter < random.length; counter ++ ) {
193 a[i++] = new TestObject (
194 "dt.setLongArray(dt.PUB_ARRAY_LONG)",
195 "DT.PUB_STATIC_ARRAY_LONG["+counter+"]",
196 "dt.getLongArray()["+counter+"]",
197 "typeof dt.getLongArray()["+counter+"]",
198 Number(random[counter]),
205 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_DOUBLE.length))) +"";
206 for ( counter = 0; counter < random.length; counter++ ) {
207 dt.PUB_ARRAY_DOUBLE[counter] = random[counter];
209 for ( counter = 0; counter < random.length; counter ++ ) {
210 a[i++] = new TestObject (
211 "dt.setDoubleArray(dt.PUB_ARRAY_DOUBLE)",
212 "DT.PUB_STATIC_ARRAY_DOUBLE["+counter+"]",
213 "dt.getDoubleArray()["+counter+"]",
214 "typeof dt.getDoubleArray()["+counter+"]",
215 Number(random[counter]),
221 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_FLOAT.length))) +"";
222 for ( counter = 0; counter < random.length; counter++ ) {
223 dt.PUB_ARRAY_FLOAT[counter] = random[counter];
225 for ( counter = 0; counter < random.length; counter ++ ) {
226 a[i++] = new TestObject (
227 "dt.setFloatArray(dt.PUB_ARRAY_FLOAT)",
228 "DT.PUB_STATIC_ARRAY_FLOAT["+counter+"]",
229 "dt.getFloatArray()["+counter+"]",
230 "typeof dt.getFloatArray()["+counter+"]",
231 Number(random[counter]),
235 for ( i = 0; i < a.length; i++ ) {
237 a[i].description +"; "+ a[i].javaFieldName,
239 a[i].javaFieldValue );
242 a[i].description +"; " + a[i].javaMethodName,
244 a[i].javaMethodValue );
249 a[i].javaTypeValue );
255 function TestObject( description, javaField, javaMethod, javaType,
260 this.description = description;
261 this.javaFieldName = javaField;
262 this.javaFieldValue = eval( javaField );
263 this.javaMethodName = javaMethod;
264 this.javaMethodValue = eval( javaMethod );
265 this.javaTypeName = javaType,
266 this.javaTypeValue = eval(javaType);
268 this.jsValue = jsValue;
269 this.jsType = jsType;