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 = 'ToDouble-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 // 3.3.6.4 Other JavaScript Objects
73 // Passing a JavaScript object to a java method that that expects a double
75 // 1. Apply the ToPrimitive operator (ECMA 9.3) to the JavaScript object
77 // 2. Convert Result(1) to Java numeric type using the rules in 3.3.3.
79 var bool = new Boolean(true);
81 a[i++] = new TestObject(
82 "dt.setDouble( bool )",
85 "typeof dt.getDouble()",
89 bool = new Boolean(false);
91 a[i++] = new TestObject(
92 "dt.setDouble( bool )",
95 "typeof dt.getDouble()",
99 var number = new Number(0);
101 a[i++] = new TestObject(
102 "dt.setDouble( number )",
105 "typeof dt.getDouble()",
109 nan = new Number(NaN);
111 a[i++] = new TestObject(
112 "dt.setDouble( nan )",
115 "typeof dt.getDouble()",
119 infinity = new Number(Infinity);
121 a[i++] = new TestObject(
122 "dt.setDouble( infinity )",
125 "typeof dt.getDouble()",
129 var neg_infinity = new Number(-Infinity);
131 a[i++] = new TestObject(
132 "dt.setDouble( new Number(neg_infinity))",
135 "typeof dt.getDouble()",
139 var string = new String("JavaScript String Value");
141 a[i++] = new TestObject(
142 "dt.setDouble(string)",
145 "typeof dt.getDouble()",
149 var string = new String("1234567890");
151 a[i++] = new TestObject(
152 "dt.setDouble(string)",
155 "typeof dt.getDouble()",
160 var string = new String("1234567890.123456789");
162 a[i++] = new TestObject(
163 "dt.setDouble(string)",
166 "typeof dt.getDouble()",
167 1234567890.123456789,
170 var myobject = new MyObject( "9876543210" );
172 a[i++] = new TestObject(
173 "dt.setDouble( myobject )",
176 "typeof dt.getDouble()",
180 myobject = new MyOtherObject( "5551212");
182 a[i++] = new TestObject(
183 "dt.setDouble( myobject )",
186 "typeof dt.getDouble()",
190 myobject = new AnotherObject( "6060842");
192 a[i++] = new TestObject(
193 "dt.setDouble( myobject )",
196 "typeof dt.getDouble()",
200 var object = new Object();
202 a[i++] = new TestObject(
203 "dt.setDouble( object )",
206 "typeof dt.getDouble()",
210 a[i++] = new TestObject(
211 "dt.setDouble( MyObject )",
214 "typeof dt.getDouble()",
218 a[i++] = new TestObject(
219 "dt.setDouble( this )",
222 "typeof dt.getDouble()",
226 a[i++] = new TestObject(
227 "dt.setDouble( Math )",
230 "typeof dt.getDouble()",
234 a[i++] = new TestObject(
235 "dt.setDouble( Function )",
238 "typeof dt.getDouble()",
242 for ( i = 0; i < a.length; i++ ) {
244 a[i].description +"; "+ a[i].javaFieldName,
246 a[i].javaFieldValue );
249 a[i].description +"; " + a[i].javaMethodName,
251 a[i].javaMethodValue );
256 a[i].javaTypeValue );
261 function MyObject( stringValue ) {
262 this.stringValue = String(stringValue);
263 this.toString = new Function( "return this.stringValue" );
266 function MyOtherObject( value ) {
267 this.toString = null;
269 this.valueOf = new Function( "return this.value" );
272 function AnotherObject( value ) {
273 this.toString = new Function( "return new Number(666)" );
275 this.valueOf = new Function( "return this.value" );
278 function TestObject( description, javaField, javaMethod, javaType,
283 this.description = description;
284 this.javaFieldName = javaField;
285 this.javaFieldValue = eval( javaField );
286 this.javaMethodName = javaMethod;
287 this.javaMethodValue = eval( javaMethod );
288 this.javaTypeName = javaType,
289 this.javaTypeValue = eval( javaType );
291 this.jsValue = jsValue;
292 this.jsType = jsType;