| 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 = 'integer-002.js'; |
| 7 | |
| 8 | /** |
| 9 | Template for LiveConnect Tests |
| 10 | |
| 11 | File Name: number-001.js |
| 12 | Description: |
| 13 | |
| 14 | When setting the value of a Java field with a JavaScript number or |
| 15 | when passing a JavaScript number to a Java method as an argument, |
| 16 | LiveConnect should be able to convert the number to any one of the |
| 17 | following types: |
| 18 | |
| 19 | byte |
| 20 | short |
| 21 | int |
| 22 | long |
| 23 | float |
| 24 | double |
| 25 | char |
| 26 | java.lang.Double |
| 27 | |
| 28 | Note that the value of the Java field may not be as precise as the |
| 29 | JavaScript value's number, if for example, you pass a large number to |
| 30 | a short or byte, or a float to integer. |
| 31 | |
| 32 | JavaScript numbers cannot be converted to instances of java.lang.Float |
| 33 | or java.lang.Integer. |
| 34 | |
| 35 | This test does not cover the cases in which a Java method returns one |
| 36 | of the above primitive Java types. |
| 37 | |
| 38 | Currently split up into numerous tests, since rhino live connect fails |
| 39 | to translate numbers into the correct types. |
| 40 | |
| 41 | Test for passing JavasScript numbers to static java.lang.Integer methods. |
| 42 | |
| 43 | |
| 44 | @author [email protected] |
| 45 | @version 1.00 |
| 46 | */ |
| 47 | var SECTION = "LiveConnect"; |
| 48 | var VERSION = "1_3"; |
| 49 | var TITLE = "LiveConnect JavaScript to Java Data Type Conversion"; |
| 50 | |
| 51 | startTest(); |
| 52 | writeHeaderToLog( SECTION + " "+ TITLE); |
| 53 | |
| 54 | // typeof all resulting objects is "object"; |
| 55 | var E_TYPE = "object"; |
| 56 | |
| 57 | // JS class of all resulting objects is "JavaObject"; |
| 58 | var E_JSCLASS = "[object JavaObject]"; |
| 59 | |
| 60 | var a = new Array(); |
| 61 | var i = 0; |
| 62 | |
| 63 | a[i++] = new TestObject( "java.lang.Integer.toString(0)", |
| 64 | java.lang.Integer.toString(0), "0" ); |
| 65 | |
| 66 | // a[i++] = new TestObject( "java.lang.Integer.toString(NaN)", |
| 67 | // java.lang.Integer.toString(NaN), "NaN" ); |
| 68 | |
| 69 | a[i++] = new TestObject( "java.lang.Integer.toString(5)", |
| 70 | java.lang.Integer.toString(5), "5" ); |
| 71 | |
| 72 | a[i++] = new TestObject( "java.lang.Integer.toString(9.9)", |
| 73 | java.lang.Integer.toString(9.9), "9" ); |
| 74 | |
| 75 | a[i++] = new TestObject( "java.lang.Integer.toString(-9.9)", |
| 76 | java.lang.Integer.toString(-9.9), "-9" ); |
| 77 | |
| 78 | for ( var i = 0; i < a.length; i++ ) { |
| 79 | |
| 80 | // check typeof |
| 81 | new TestCase( |
| 82 | SECTION, |
| 83 | "typeof (" + a[i].description +")", |
| 84 | a[i].type, |
| 85 | typeof a[i].javavalue ); |
| 86 | /* |
| 87 | // check the js class |
| 88 | new TestCase( |
| 89 | SECTION, |
| 90 | "("+ a[i].description +").getJSClass()", |
| 91 | E_JSCLASS, |
| 92 | a[i].jsclass ); |
| 93 | */ |
| 94 | // check the number value of the object |
| 95 | new TestCase( |
| 96 | SECTION, |
| 97 | "String(" + a[i].description +")", |
| 98 | a[i].jsvalue, |
| 99 | String( a[i].javavalue ) ); |
| 100 | } |
| 101 | |
| 102 | test(); |
| 103 | |
| 104 | function TestObject( description, javavalue, jsvalue ) { |
| 105 | this.description = description; |
| 106 | this.javavalue = javavalue; |
| 107 | this.jsvalue = jsvalue; |
| 108 | this.type = E_TYPE; |
| 109 | // this.javavalue.__proto__.getJSClass = Object.prototype.toString; |
| 110 | // this.jsclass = this.javavalue.getJSClass(); |
| 111 | return this; |
| 112 | } |