1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 gTestfile = 'object-001.js';
42 * Verify that for-in loops can be used with java objects.
44 * Java array members of object instances should be enumerated in
48 var SECTION = "for...in";
50 var TITLE = "LiveConnect 3.0";
54 // we just need to know the names of all the expected enumerated
55 // properties. we will get the values to the original objects.
57 var dt = new Packages.com.netscape.javascript.qa.liveconnect.DataTypeClass;
61 a[a.length] = new TestObject(
63 "var dt = new Packages.com.netscape.javascript.qa.liveconnect.DataTypeClass; dt.getString()",
67 a[a.length] = new TestObject(
73 a[a.length] = new TestObject(
74 new java.awt.Rectangle(5,6,7,8),
75 "new java.awt.Rectangle(5,6,7,8)",
77 "java.awt.Rectangle");
79 for ( var i = 0; i < a.length; i++ ) {
80 // check the value of each indexed array item
82 for ( var arrayItem = 0; arrayItem < a[i].items; arrayItem++ ) {
85 a[i].javaObject[arrayItem],
86 a[i].enumedObject[arrayItem] );
89 // verify that all non-static properties are enumerated
92 var fieldArray = getFields( a[i].javaClass );
94 for ( var fieldIndex = 0; fieldIndex < fieldArray.length; fieldIndex++ ) {
95 var f = fieldArray[fieldIndex] +"";
97 if ( ! isStatic( f ) ) {
98 var currentField = getFieldName( f );
101 a[i].javaClass+"." + currentField + " enumerated ",
103 a[i].enumedObject[currentField]+"" == a[i].javaObject[currentField] +"");
107 // verify that all non-static methods are enumerated
109 var methodArray = getMethods(a[i].javaClass);
111 for ( var methodIndex = 0; methodIndex < methodArray.length; methodIndex++ ) {
112 var m = methodArray[methodIndex] +"";
114 if ( ! isStatic(m) && inClass( a[i].javaClass, m)) {
115 var currentMethod = getMethodName(m);
118 a[i].javaClass+"."+currentMethod + " enumerated ",
120 a[i].enumedObject[currentMethod] +"" == a[i].javaObject[currentMethod] +"");
130 function TestObject( ob, descr, len, jc) {
132 this.description = descr;
135 this.enumedObject = new enumObject(ob);
138 function enumObject( o ) {
146 // only return if the method is a method of the class, not an inherited
149 function inClass( javaClass, m ) {
150 var argIndex = m.lastIndexOf( "(", m.length );
151 var methodIndex = m.lastIndexOf( ".", argIndex );
152 var classIndex = m.lastIndexOf( " ", methodIndex );
153 var className = m.substr(classIndex +1, methodIndex - classIndex -1 );
155 if ( className == javaClass ) {
160 function isStatic( m ) {
161 if ( m.lastIndexOf ( "static" ) > 0 ) {
166 function getMethods( javaString ) {
167 return java.lang.Class.forName( javaString ).getMethods();
169 function getArguments( m ) {
170 var argIndex = m.lastIndexOf("(", m.length());
171 var argString = m.substr(argIndex+1, m.length() - argIndex -2);
172 return argString.split( "," );
174 function getMethodName( m ) {
175 var argIndex = m.lastIndexOf( "(", m.length);
176 var nameIndex = m.lastIndexOf( ".", argIndex);
177 return m.substr( nameIndex +1, argIndex - nameIndex -1 );
179 function getFields( javaClassName ) {
180 return java.lang.Class.forName( javaClassName ).getFields();
182 function getFieldName( f ) {
183 return f.substr( f.lastIndexOf(".", f.length)+1, f.length );
185 function getFieldNames( javaClassName ) {
186 var javaFieldArray = getFields( javaClassName );
188 for ( var i = 0, jsFieldArray = new Array(); i < javaFieldArray.length; i++ ) {
189 var f = javaFieldArray[i] +"";
190 jsFieldArray[i] = f.substr( f.lastIndexOf(".", f.length)+1, f.length );
194 function hasMethod( m, noArgs ) {
195 for ( var i = 0; i < methods.length; i++ ) {
196 if ( (m == methods[i][0]) && (noArgs == methods[i][1].length)) {