blob: c66eed0bd0fc2a8f75a4855a217d2099c8fc8d7a [file] [log] [blame]
ap@apple.comfd21c992010-03-03 19:15:20 +00001/* ***** BEGIN LICENSE BLOCK *****
2 *
3 * Copyright (C) 1997, 1998 Netscape Communications Corporation.
4 * Copyright (C) 2010 Apple Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 * ***** END LICENSE BLOCK ***** */
22
23gTestfile = 'ToFloat-001.js';
24
25/**
26 * JavaScript to Java type conversion.
27 *
28 * This test passes JavaScript number values to several Java methods
29 * that expect arguments of various types, and verifies that the value is
30 * converted to the correct value and type.
31 *
32 * This tests instance methods, and not static methods.
33 *
34 * Running these tests successfully requires you to have
35 * com.netscape.javascript.qa.liveconnect.DataTypeClass on your classpath.
36 *
37 * Specification: Method Overloading Proposal for Liveconnect 3.0
38 *
39 * @author: christine@netscape.com
40 *
41 */
42var SECTION = "JavaScript Object to java.lang.String";
43var VERSION = "1_4";
44var TITLE = "LiveConnect 3.0 JavaScript to Java Data Type Conversion " +
45 SECTION;
46var BUGNUMBER = "335899";
47startTest();
48
49var dt = applet.createQAObject("com.netscape.javascript.qa.liveconnect.DataTypeClass");
50
51var a = new Array();
52var i = 0;
53
54// 3.3.6.4 Other JavaScript Objects
55// Passing a JavaScript object to a java method that that expects a float
56// should:
57// 1. Apply the ToPrimitive operator (ECMA 9.3) to the JavaScript object
58// with hint Number
59// 2. Convert Result(1) to Java numeric type using the rules in 3.3.3.
60
61a[i++] = new TestObject(
62 "dt.setFloat(new Boolean(true))",
63 "dt.PUB_FLOAT",
64 "dt.getFloat()",
65 "typeof dt.getFloat()",
66 '1',
67 '"number"');
68
69a[i++] = new TestObject(
70 "dt.setFloat(new Boolean(false))",
71 "dt.PUB_FLOAT",
72 "dt.getFloat()",
73 "typeof dt.getFloat()",
74 '0',
75 '"number"');
76
77a[i++] = new TestObject(
78 "dt.setFloat(new Number(0))",
79 "dt.PUB_FLOAT",
80 "dt.getFloat()",
81 "typeof dt.getFloat()",
82 '0',
83 '"number"');
84
85a[i++] = new TestObject(
86 "dt.setFloat(new Number(NaN))",
87 "dt.PUB_FLOAT",
88 "dt.getFloat()",
89 "typeof dt.getFloat()",
90 'NaN',
91 '"number"');
92
93a[i++] = new TestObject(
94 "dt.setFloat(new Number(Infinity))",
95 "dt.PUB_FLOAT",
96 "dt.getFloat()",
97 "typeof dt.getFloat()",
98 'Infinity',
99 '"number"');
100
101a[i++] = new TestObject(
102 "dt.setFloat( new Number(new Number(-Infinity)))",
103 "dt.PUB_FLOAT",
104 "dt.getFloat()",
105 "typeof dt.getFloat()",
106 '-Infinity',
107 '"number"');
108
109a[i++] = new TestObject(
110 "dt.setFloat(new String('JavaScript String Value'))",
111 "dt.PUB_FLOAT",
112 "dt.getFloat()",
113 "typeof dt.getFloat()",
114 'NaN',
115 '"number"');
116
117a[i++] = new TestObject(
118 "dt.setFloat(new String('1234567'))",
119 "dt.PUB_FLOAT",
120 "dt.getFloat()",
121 "typeof dt.getFloat()",
122 '1234567',
123 '"number"');
124
125a[i++] = new TestObject(
126 "dt.setFloat(new String('123456'))",
127 "dt.PUB_FLOAT",
128 "dt.getFloat()",
129 "typeof dt.getFloat()",
130 '123456',
131 '"number"');
132
133a[i++] = new TestObject(
134 "dt.setFloat(new MyObject('5555555'))",
135 "dt.PUB_FLOAT",
136 "dt.getFloat()",
137 "typeof dt.getFloat()",
138 '5555555',
139 '"number"');
140
141a[i++] = new TestObject(
142 "dt.setFloat(new MyOtherObject('5551212'))",
143 "dt.PUB_FLOAT",
144 "dt.getFloat()",
145 "typeof dt.getFloat()",
146 '5551212',
147 '"number"');
148
149a[i++] = new TestObject(
150 "dt.setFloat(new AnotherObject('6060842'))",
151 "dt.PUB_FLOAT",
152 "dt.getFloat()",
153 "typeof dt.getFloat()",
154 '6060842',
155 '"number"');
156
157a[i++] = new TestObject(
158 "dt.setFloat(new Object())",
159 "dt.PUB_FLOAT",
160 "dt.getFloat()",
161 "typeof dt.getFloat()",
162 'NaN',
163 '"number"');
164
165a[i++] = new TestObject(
166 "dt.setFloat( MyObject )",
167 "dt.PUB_FLOAT",
168 "dt.getFloat()",
169 "typeof dt.getFloat()",
170 'NaN',
171 '"number"');
172
173a[i++] = new TestObject(
174 "dt.setFloat( this )",
175 "dt.PUB_FLOAT",
176 "dt.getFloat()",
177 "typeof dt.getFloat()",
178 'NaN',
179 '"number"');
180
181a[i++] = new TestObject(
182 "dt.setFloat( Math )",
183 "dt.PUB_FLOAT",
184 "dt.getFloat()",
185 "typeof dt.getFloat()",
186 'NaN',
187 '"number"');
188
189a[i++] = new TestObject(
190 "dt.setFloat( Function )",
191 "dt.PUB_FLOAT",
192 "dt.getFloat()",
193 "typeof dt.getFloat()",
194 'NaN',
195 '"number"');
196
197for ( i = 0; i < a.length; i++ ) {
198 shouldBeWithErrorCheck(
199 a[i].description +"; "+ a[i].javaFieldName,
200 a[i].jsValue);
201
202 shouldBeWithErrorCheck(
203 a[i].description +"; " + a[i].javaMethodName,
204 a[i].jsValue);
205
206 shouldBeWithErrorCheck(
207 a[i].javaTypeName,
208 a[i].jsType);
209}
210
211function MyObject( stringValue ) {
212 this.stringValue = String(stringValue);
213 this.toString = new Function( "return this.stringValue" );
214}
215
216function MyOtherObject( value ) {
217 this.toString = null;
218 this.value = value;
219 this.valueOf = new Function( "return this.value" );
220}
221
222function AnotherObject( value ) {
223 this.toString = new Function( "return new Number(666)" );
224 this.value = value;
225 this.valueOf = new Function( "return this.value" );
226}
227
228function TestObject(description, javaField, javaMethod, javaType, jsValue, jsType)
229{
230 this.description = description;
231 this.javaFieldName = javaField;
232 this.javaMethodName = javaMethod;
233 this.javaTypeName = javaType,
234 this.jsValue = jsValue;
235 this.jsType = jsType;
236}