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 = 'ToString-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 = "JavaScript Object to java.lang.String";
63 var TITLE = "LiveConnect 3.0 JavaScript to Java Data Type Conversion " +
72 function MyObject( stringValue ) {
73 this.stringValue = String(stringValue);
74 this.toString = new Function( "return this.stringValue" );
77 function MyFunction() {
80 MyFunction.toString = new Function( "return \"MyFunction\";" );
82 // 3.3.6.4 Other JavaScript Objects
83 // Passing a JavaScript Object to a Java method that expects a
84 // java.lang.String calls the JS object's toString method, and
85 // returns the result as a java.lang.String.
88 var stringValue = "JavaScript String Value";
90 a[i++] = new TestObject(
91 "dt.setStringObject( new String(\""+stringValue+"\"))",
93 "dt.getStringObject() +''",
94 "dt.getStringObject().getClass()",
96 java.lang.Class.forName("java.lang.String"));
98 a[i++] = new TestObject(
99 "dt.setStringObject( new MyObject(\""+stringValue+"\"))",
101 "dt.getStringObject() +''",
102 "dt.getStringObject().getClass()",
104 java.lang.Class.forName("java.lang.String"));
106 a[i++] = new TestObject(
107 "dt.setStringObject( new Boolean(true))",
109 "dt.getStringObject() +''",
110 "dt.getStringObject().getClass()",
112 java.lang.Class.forName("java.lang.String"));
114 a[i++] = new TestObject(
115 "dt.setStringObject( new Boolean(false))",
117 "dt.getStringObject() +''",
118 "dt.getStringObject().getClass()",
120 java.lang.Class.forName("java.lang.String"));
122 a[i++] = new TestObject(
123 "dt.setStringObject( new Object())",
125 "dt.getStringObject() +''",
126 "dt.getStringObject().getClass()",
128 java.lang.Class.forName("java.lang.String"));
130 a[i++] = new TestObject(
131 "dt.setStringObject( new Number(0))",
133 "dt.getStringObject() +''",
134 "dt.getStringObject().getClass()",
136 java.lang.Class.forName("java.lang.String"));
138 a[i++] = new TestObject(
139 "dt.setStringObject( new Number(NaN))",
141 "dt.getStringObject() +''",
142 "dt.getStringObject().getClass()",
144 java.lang.Class.forName("java.lang.String"));
146 a[i++] = new TestObject(
147 "dt.setStringObject( new Number(Infinity))",
149 "dt.getStringObject() +''",
150 "dt.getStringObject().getClass()",
152 java.lang.Class.forName("java.lang.String"));
154 a[i++] = new TestObject(
155 "dt.setStringObject( new Number(-Infinity))",
157 "dt.getStringObject() +''",
158 "dt.getStringObject().getClass()",
160 java.lang.Class.forName("java.lang.String"));
162 a[i++] = new TestObject(
163 "dt.setStringObject( new Array(1,2,3))",
165 "dt.getStringObject() +''",
166 "dt.getStringObject().getClass()",
168 java.lang.Class.forName("java.lang.String"));
170 a[i++] = new TestObject(
171 "dt.setStringObject( MyObject )",
173 "dt.getStringObject() +''",
174 "dt.getStringObject().getClass()",
176 java.lang.Class.forName("java.lang.String"));
180 a[i++] = new TestObject(
181 "dt.setStringObject( THIS )",
183 "dt.getStringObject() +''",
184 "dt.getStringObject().getClass()",
186 java.lang.Class.forName("java.lang.String"));
188 a[i++] = new TestObject(
189 "dt.setStringObject( Math )",
191 "dt.getStringObject() +''",
192 "dt.getStringObject().getClass()",
194 java.lang.Class.forName("java.lang.String"));
196 a[i++] = new TestObject(
197 "dt.setStringObject( MyFunction )",
199 "dt.getStringObject() +''",
200 "dt.getStringObject().getClass()",
202 java.lang.Class.forName("java.lang.String"));
204 for ( i = 0; i < a.length; i++ ) {
206 a[i].description +"; "+ a[i].javaFieldName,
208 a[i].javaFieldValue );
211 a[i].description +"; " + a[i].javaMethodName,
213 a[i].javaMethodValue );
218 a[i].javaTypeValue );
223 function TestObject( description, javaField, javaMethod, javaType,
228 this.description = description;
229 this.javaFieldName = javaField;
230 this.javaFieldValue = eval( javaField );
231 this.javaMethodName = javaMethod;
232 this.javaMethodValue = eval( javaMethod );
233 this.javaTypeName = javaType,
234 this.javaTypeValue = eval( javaType );
236 this.jsValue = jsValue;
237 this.jsType = jsType;