2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2007-05-25 17:19:18 -07:00
|
|
|
|
|
|
|
gTestfile = 'object-001.js';
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Verify that for-in loops can be used with java objects.
|
|
|
|
*
|
|
|
|
* Java array members of object instances should be enumerated in
|
|
|
|
* for... in loops.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
var SECTION = "for...in";
|
|
|
|
var VERSION = "1_4";
|
|
|
|
var TITLE = "LiveConnect 3.0";
|
|
|
|
SECTION;
|
|
|
|
startTest();
|
|
|
|
|
|
|
|
// we just need to know the names of all the expected enumerated
|
|
|
|
// properties. we will get the values to the original objects.
|
|
|
|
|
|
|
|
var dt = new Packages.com.netscape.javascript.qa.liveconnect.DataTypeClass;
|
|
|
|
|
|
|
|
var a = new Array();
|
|
|
|
|
|
|
|
a[a.length] = new TestObject(
|
2007-05-25 17:19:18 -07:00
|
|
|
dt.PUB_STRING,
|
|
|
|
"var dt = new Packages.com.netscape.javascript.qa.liveconnect.DataTypeClass; dt.getString()",
|
|
|
|
0,
|
|
|
|
"java.lang.String");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
a[a.length] = new TestObject(
|
2007-05-25 17:19:18 -07:00
|
|
|
dt.getLongObject(),
|
|
|
|
"dt.getLongObject()",
|
|
|
|
0,
|
|
|
|
"java.lang.Long");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
a[a.length] = new TestObject(
|
2007-05-25 17:19:18 -07:00
|
|
|
new java.awt.Rectangle(5,6,7,8),
|
|
|
|
"new java.awt.Rectangle(5,6,7,8)",
|
|
|
|
0,
|
|
|
|
"java.awt.Rectangle");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for ( var i = 0; i < a.length; i++ ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
// check the value of each indexed array item
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
for ( var arrayItem = 0; arrayItem < a[i].items; arrayItem++ ) {
|
|
|
|
new TestCase(
|
|
|
|
"["+arrayItem+"]",
|
|
|
|
a[i].javaObject[arrayItem],
|
|
|
|
a[i].enumedObject[arrayItem] );
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
// verify that all non-static properties are enumerated
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
var fieldArray = getFields( a[i].javaClass );
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
for ( var fieldIndex = 0; fieldIndex < fieldArray.length; fieldIndex++ ) {
|
|
|
|
var f = fieldArray[fieldIndex] +"";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
if ( ! isStatic( f ) ) {
|
|
|
|
var currentField = getFieldName( f );
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
new TestCase(
|
|
|
|
a[i].javaClass+"." + currentField + " enumerated ",
|
|
|
|
true,
|
|
|
|
a[i].enumedObject[currentField]+"" == a[i].javaObject[currentField] +"");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-05-25 17:19:18 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
// verify that all non-static methods are enumerated
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
var methodArray = getMethods(a[i].javaClass);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
for ( var methodIndex = 0; methodIndex < methodArray.length; methodIndex++ ) {
|
|
|
|
var m = methodArray[methodIndex] +"";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
if ( ! isStatic(m) && inClass( a[i].javaClass, m)) {
|
|
|
|
var currentMethod = getMethodName(m);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
new TestCase(
|
|
|
|
a[i].javaClass+"."+currentMethod + " enumerated ",
|
|
|
|
true,
|
|
|
|
a[i].enumedObject[currentMethod] +"" == a[i].javaObject[currentMethod] +"");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
}
|
2007-05-25 17:19:18 -07:00
|
|
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test();
|
|
|
|
|
|
|
|
function TestObject( ob, descr, len, jc) {
|
2007-05-25 17:19:18 -07:00
|
|
|
this.javaObject= ob;
|
|
|
|
this.description = descr;
|
|
|
|
this.items = len;
|
|
|
|
this.javaClass = jc;
|
|
|
|
this.enumedObject = new enumObject(ob);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function enumObject( o ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
this.pCount = 0;
|
|
|
|
for ( var p in o ) {
|
|
|
|
this.pCount++;
|
|
|
|
this[p] = o[p];
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// only return if the method is a method of the class, not an inherited
|
|
|
|
// method
|
|
|
|
|
|
|
|
function inClass( javaClass, m ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
var argIndex = m.lastIndexOf( "(", m.length );
|
|
|
|
var methodIndex = m.lastIndexOf( ".", argIndex );
|
|
|
|
var classIndex = m.lastIndexOf( " ", methodIndex );
|
|
|
|
var className = m.substr(classIndex +1, methodIndex - classIndex -1 );
|
|
|
|
|
|
|
|
if ( className == javaClass ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
function isStatic( m ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
if ( m.lastIndexOf ( "static" ) > 0 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
function getMethods( javaString ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
return java.lang.Class.forName( javaString ).getMethods();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
function getArguments( m ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
var argIndex = m.lastIndexOf("(", m.length());
|
|
|
|
var argString = m.substr(argIndex+1, m.length() - argIndex -2);
|
|
|
|
return argString.split( "," );
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
function getMethodName( m ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
var argIndex = m.lastIndexOf( "(", m.length);
|
|
|
|
var nameIndex = m.lastIndexOf( ".", argIndex);
|
|
|
|
return m.substr( nameIndex +1, argIndex - nameIndex -1 );
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
function getFields( javaClassName ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
return java.lang.Class.forName( javaClassName ).getFields();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
function getFieldName( f ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
return f.substr( f.lastIndexOf(".", f.length)+1, f.length );
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
function getFieldNames( javaClassName ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
var javaFieldArray = getFields( javaClassName );
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-25 17:19:18 -07:00
|
|
|
for ( var i = 0, jsFieldArray = new Array(); i < javaFieldArray.length; i++ ) {
|
|
|
|
var f = javaFieldArray[i] +"";
|
|
|
|
jsFieldArray[i] = f.substr( f.lastIndexOf(".", f.length)+1, f.length );
|
|
|
|
}
|
|
|
|
return jsFieldArray;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
function hasMethod( m, noArgs ) {
|
2007-05-25 17:19:18 -07:00
|
|
|
for ( var i = 0; i < methods.length; i++ ) {
|
|
|
|
if ( (m == methods[i][0]) && (noArgs == methods[i][1].length)) {
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-05-25 17:19:18 -07:00
|
|
|
}
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|