| 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 = 'long-001.js'; |
| 7 | |
| 8 | /** |
| 9 | Template for LiveConnect Tests |
| 10 | |
| 11 | File Name: number-004.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 creating java.lang.Long objects. |
| 42 | |
| 43 | @author [email protected] |
| 44 | @version 1.00 |
| 45 | */ |
| 46 | var SECTION = "LiveConnect"; |
| 47 | var VERSION = "1_3"; |
| 48 | var TITLE = "LiveConnect JavaScript to Java Data Type Conversion"; |
| 49 | |
| 50 | startTest(); |
| 51 | writeHeaderToLog( SECTION + " "+ TITLE); |
| 52 | |
| 53 | // typeof all resulting objects is "object"; |
| 54 | var E_TYPE = "object"; |
| 55 | |
| 56 | // JS class of all resulting objects is "JavaObject"; |
| 57 | var E_JSCLASS = "[object JavaObject]"; |
| 58 | |
| 59 | var a = new Array(); |
| 60 | var i = 0; |
| 61 | /* |
| 62 | a[i++] = new TestObject( "new java.lang.Long(\"0.0\")", |
| 63 | new java.lang.Long("0.0"), 0 ); |
| 64 | */ |
| 65 | a[i++] = new TestObject( "new java.lang.Long(\"0.\")", |
| 66 | new java.lang.Long("0"), 0 ); |
| 67 | |
| 68 | a[i++] = new TestObject( "new java.lang.Long( 5 )", |
| 69 | new java.lang.Long(5), 5 ); |
| 70 | |
| 71 | a[i++] = new TestObject( "new java.lang.Long( 5.5 )", |
| 72 | new java.lang.Long(5.5), 5 ); |
| 73 | |
| 74 | a[i++] = new TestObject( "new java.lang.Long( Number(5) )", |
| 75 | new java.lang.Long(Number(5)), 5 ); |
| 76 | |
| 77 | a[i++] = new TestObject( "new java.lang.Long( Number(5.5) )", |
| 78 | new java.lang.Long(Number(5.5)), 5 ); |
| 79 | |
| 80 | for ( var i = 0; i < a.length; i++ ) { |
| 81 | |
| 82 | // check typeof |
| 83 | new TestCase( |
| 84 | SECTION, |
| 85 | "typeof (" + a[i].description +")", |
| 86 | a[i].type, |
| 87 | typeof a[i].javavalue ); |
| 88 | /* |
| 89 | // check the js class |
| 90 | new TestCase( |
| 91 | SECTION, |
| 92 | "("+ a[i].description +").getJSClass()", |
| 93 | E_JSCLASS, |
| 94 | a[i].jsclass ); |
| 95 | */ |
| 96 | // check the number value of the object |
| 97 | new TestCase( |
| 98 | SECTION, |
| 99 | "Number(" + a[i].description +")", |
| 100 | a[i].jsvalue, |
| 101 | Number( a[i].javavalue ) ); |
| 102 | } |
| 103 | |
| 104 | test(); |
| 105 | |
| 106 | function TestObject( description, javavalue, jsvalue ) { |
| 107 | this.description = description; |
| 108 | this.javavalue = javavalue; |
| 109 | this.jsvalue = jsvalue; |
| 110 | this.type = E_TYPE; |
| 111 | // LC2 does not support the __proto__ property in Java objects. |
| 112 | // this.javavalue.__proto__.getJSClass = Object.prototype.toString; |
| 113 | // this.jsclass = this.javavalue.getJSClass(); |
| 114 | return this; |
| 115 | } |