Debian Codegraph @

rhino/testsrc/tests/lc2/Methods/method-002.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'method-002.js';
7
8 /**
9 File Name: method-002.js
10 Description:
11
12 Call a method of a JavaObject instance and verify that return value.
13 This is covered more thouroughly in the type conversion test cases.
14
15 @author [email protected]
16 @version 1.00
17 */
18 var SECTION = "LiveConnect Objects";
19 var VERSION = "1_3";
20 var TITLE = "Invoking Java Methods";
21
22 startTest();
23 writeHeaderToLog( SECTION + " "+ TITLE);
24
25 // All JavaObjects are of the type "object"
26
27 var E_TYPE = "object";
28
29 // All JavaObjects [[Class]] property is JavaObject
30 var E_JSCLASS = "[object JavaObject]";
31
32 // Create arrays of actual results (java_array) and
33 // expected results (test_array).
34
35 var java_array = new Array();
36 var test_array = new Array();
37
38 var i = 0;
39
40 // method returns an object
41
42 var rect = new java.awt.Rectangle(1,2,3,4);
43 var size = rect.getSize();
44
45 new TestCase(
46 SECTION,
47 "var size = (new java.awt.Rectangle(1,2,3,4)).getSize(); "+
48 "size.getClass().equals(java.lang.Class.forName(\""+
49 "java.awt.Dimension\"))",
50 true,
51 size.getClass().equals(java.lang.Class.forName("java.awt.Dimension")));
52
53 new TestCase(
54 SECTION,
55 "size.width",
56 3,
57 size.width );
58
59 new TestCase(
60 SECTION,
61 "size.height",
62 4,
63 size.height );
64
65 // method returns void
66 var r = rect.setSize(5,6);
67
68 new TestCase(
69 SECTION,
70 "var r = rect.setSize(5,6); r",
71 void 0,
72 r );
73
74 // method returns a string
75
76 var string = new java.lang.String( " hello " );
77 s = string.trim()
78
79 new TestCase(
80 SECTION,
81 "var string = new java.lang.String(\" hello \"); "+
82 "var s = string.trim(); s.getClass().equals("+
83 "java.lang.Class.forName(\"java.lang.String\")",
84 true,
85 s.getClass().equals(java.lang.Class.forName("java.lang.String")) );
86
87 // method returns an int
88 new TestCase(
89 SECTION,
90 "s.length()",
91 5,
92 s.length() );
93
94 test();
95
96 function CompareValues( javaval, testval ) {
97 // Check type, which should be E_TYPE
98 new TestCase( SECTION,
99 "typeof (" + testval.description +" )",
100 testval.type,
101 javaval.type );
102
103 // Check JavaScript class, which should be E_JSCLASS
104 new TestCase( SECTION,
105 "(" + testval.description +" ).getJSClass()",
106 testval.jsclass,
107 javaval.jsclass );
108 // Check the JavaClass, which should be the same as the result as Class.forName(description).
109 new TestCase( SECTION,
110 testval.description +".getClass().equals( " +
111 "java.lang.Class.forName( '" + testval.classname +
112 "' ) )",
113 true,
114 javaval.javaclass.equals( testval.javaclass ) );
115 }
116 function JavaValue( value ) {
117 this.type = typeof value;
118 // Object.prototype.toString will show its JavaScript wrapper object.
119 value.__proto__.getJSClass = Object.prototype.toString;
120 this.jsclass = value.getJSClass();
121 this.javaclass = value.getClass();
122 return this;
123 }
124 function TestValue( description, classname ) {
125 this.description = description;
126 this.classname = classname;
127 this.type = E_TYPE;
128 this.jsclass = E_JSCLASS;
129 this.javaclass = java.lang.Class.forName( classname );
130 return this;
131 }