| 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 = 'float-001.js'; |
| 7 | |
| 8 | /** |
| 9 | Template for LiveConnect Tests |
| 10 | |
| 11 | File Name: number-002.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.Float objects. |
| 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 | |
| 58 | // JS class of all resulting objects is "JavaObject"; |
| 59 | var E_JSCLASS = "[object JavaObject]"; |
| 60 | |
| 61 | |
| 62 | var a = new Array(); |
| 63 | var i = 0; |
| 64 | |
| 65 | a[i++] = new TestObject( "new java.lang.Float(\"0.0\")", |
| 66 | new java.lang.Float("0.0"), 0 ); |
| 67 | |
| 68 | a[i++] = new TestObject( "new java.lang.Float(\"0.\")", |
| 69 | new java.lang.Float("0"), 0 ); |
| 70 | |
| 71 | a[i++] = new TestObject( "new java.lang.Float( 5 )", |
| 72 | new java.lang.Float(5), 5 ); |
| 73 | |
| 74 | a[i++] = new TestObject( "new java.lang.Float( 5.5 )", |
| 75 | new java.lang.Float(5.5), 5.5 ); |
| 76 | |
| 77 | a[i++] = new TestObject( "new java.lang.Float( Number(5) )", |
| 78 | new java.lang.Float(Number(5)), 5 ); |
| 79 | |
| 80 | a[i++] = new TestObject( "new java.lang.Float( Number(5.5) )", |
| 81 | new java.lang.Float(Number(5.5)), 5.5 ); |
| 82 | |
| 83 | for ( var i = 0; i < a.length; i++ ) { |
| 84 | |
| 85 | // check typeof |
| 86 | new TestCase( |
| 87 | SECTION, |
| 88 | "typeof (" + a[i].description +")", |
| 89 | a[i].type, |
| 90 | typeof a[i].javavalue ); |
| 91 | /* |
| 92 | // check the js class |
| 93 | new TestCase( |
| 94 | SECTION, |
| 95 | "("+ a[i].description +").getJSClass()", |
| 96 | E_JSCLASS, |
| 97 | a[i].jsclass ); |
| 98 | */ |
| 99 | // check the number value of the object |
| 100 | new TestCase( |
| 101 | SECTION, |
| 102 | "Number(" + a[i].description +")", |
| 103 | a[i].jsvalue, |
| 104 | Number( a[i].javavalue ) ); |
| 105 | } |
| 106 | |
| 107 | test(); |
| 108 | |
| 109 | function TestObject( description, javavalue, jsvalue ) { |
| 110 | this.description = description; |
| 111 | this.javavalue = javavalue; |
| 112 | this.jsvalue = jsvalue; |
| 113 | this.type = E_TYPE; |
| 114 | // LC2 does not support the proto property in Java objects |
| 115 | // this.javavalue.__proto__.getJSClass = Object.prototype.toString; |
| 116 | // this.jsclass = this.javavalue.getJSClass(); |
| 117 | return this; |
| 118 | } |