• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.browser;
18 
19 import android.test.AndroidTestCase;
20 import android.util.Log;
21 
22 import java.util.Arrays;
23 
24 import junit.framework.AssertionFailedError;
25 
26 public class JNIBindingsTest extends AndroidTestCase {
27 
28     private final static String LOGTAG = "JNIBindingsTest";
29     private JNIBindingsTestApp mTestApp;
30 
31     public int mInt = 123;
32     public String mString = "Hello World";
33 
JNIBindingsTest(JNIBindingsTestApp testApp)34     public JNIBindingsTest(JNIBindingsTestApp testApp) {
35         mTestApp = testApp;
36     }
37 
notifyComplete()38     public void notifyComplete() {
39         Log.v(LOGTAG, "Completing the test.");
40         mTestApp.notifyComplete();
41     }
42 
printAssertionFailed(AssertionFailedError e)43     public void printAssertionFailed(AssertionFailedError e) {
44         Log.e(LOGTAG, "");
45         Log.e(LOGTAG, "*** ASSERTION FAILED: " + e.getMessage());
46         Log.e(LOGTAG, "*** Stack trace:");
47         StackTraceElement[] trace = e.getStackTrace();
48         for(StackTraceElement elem : trace) {
49             Log.e(LOGTAG, "***\t" + elem.toString());
50         }
51         Log.e(LOGTAG, "");
52     }
53 
testPrimitiveTypes(byte byteParam, char charParam, double doubleParam, float floatParam, int intParam, long longParam, short shortParam, boolean booleanParam)54     public boolean testPrimitiveTypes(byte byteParam, char charParam, double doubleParam,
55             float floatParam, int intParam, long longParam, short shortParam,
56             boolean booleanParam) {
57         byte expectedByteParam = 100;
58         char expectedCharParam = 'c';
59         double expectedDoubleParam = 123.34567890;
60         float expectedFloatParam = 456.789f;
61         int expectedIntParam = 1234567;
62         long expectedLongParam = 1234567890L;
63         short expectedShortParam = 6000;
64         boolean expectedBooleanParam = true;
65 
66         try {
67             assertEquals(expectedByteParam, byteParam);
68 
69             // EMULATE_JSC_BINDINGS: JSC does not pass chars correctly
70             // assertEquals(expectedCharParam, charParam);
71 
72             assertEquals(expectedDoubleParam, doubleParam);
73             assertEquals(expectedFloatParam, floatParam);
74             assertEquals(expectedIntParam, intParam);
75             assertEquals(expectedLongParam, longParam);
76             assertEquals(expectedShortParam, shortParam);
77             assertEquals(expectedBooleanParam, booleanParam);
78         } catch (AssertionFailedError e) {
79             printAssertionFailed(e);
80            return false;
81         }
82         return true;
83     }
84 
testObjectTypes(String stringParam, String emptyString, String nullString, Object objectParam, Object emptyObject)85     public boolean testObjectTypes(String stringParam, String emptyString, String nullString,
86             Object objectParam, Object emptyObject) {
87         String expectedString = "Foo";
88         String expectedEmptyString = "";
89 
90         try {
91             assertNotNull(stringParam);
92             assertNotNull(emptyString);
93             assertEquals(expectedString, stringParam);
94             assertEquals(expectedEmptyString, emptyString);
95             assertNull(nullString);
96             assertNull(objectParam);
97             assertNull(emptyObject);
98         } catch (AssertionFailedError e) {
99             printAssertionFailed(e);
100             return false;
101         }
102         return true;
103     }
104 
testArray(byte[] byteArray, char[] charArray, double[] doubleArray, float[] floatArray, int[] intArray, long[] longArray, short[] shortArray, boolean[] booleanArray)105     public boolean testArray(byte[] byteArray, char[] charArray, double[] doubleArray,
106             float[] floatArray, int[] intArray, long[] longArray, short[] shortArray,
107             boolean[] booleanArray) {
108         byte[] expectedByteArray = { 1,2,3};
109         char[] expectedCharArray = {'d', 'o', 'g'};
110         double[] expectedDoubleArray = {1.2,2.3,3.4};
111         float[] expectedFloatArray = {4.5F,5.6F,6.7F};
112         int[] expectedIntArray = {1,2,3};
113         long[] expectedLongArray = {4L,5L,6L};
114         short[] expectedShortArray = {7,8,9};
115         boolean[] expectedBooleanArray = {true, false};
116 
117         try {
118             assertNotNull(byteArray);
119             assertNotNull(charArray);
120             assertNotNull(doubleArray);
121             assertNotNull(floatArray);
122             assertNotNull(intArray);
123             assertNotNull(longArray);
124             assertNotNull(shortArray);
125             assertNotNull(booleanArray);
126             assertEquals(Arrays.toString(expectedByteArray), Arrays.toString(byteArray));
127             assertEquals(Arrays.toString(expectedCharArray), Arrays.toString(charArray));
128             assertEquals(Arrays.toString(expectedDoubleArray), Arrays.toString(doubleArray));
129             assertEquals(Arrays.toString(expectedFloatArray), Arrays.toString(floatArray));
130             assertEquals(Arrays.toString(expectedIntArray), Arrays.toString(intArray));
131             assertEquals(Arrays.toString(expectedLongArray), Arrays.toString(longArray));
132             assertEquals(Arrays.toString(expectedShortArray), Arrays.toString(shortArray));
133             assertEquals(Arrays.toString(expectedBooleanArray), Arrays.toString(booleanArray));
134         } catch (AssertionFailedError e) {
135             printAssertionFailed(e);
136             return false;
137         }
138         return true;
139     }
140 
testObjectArray(String[] stringArray, Object[] emptyArray, Object[] objectArray)141     public boolean testObjectArray(String[] stringArray, Object[] emptyArray,
142             Object[] objectArray) {
143         String[] expectedStringArray = {"Hello", "World", "!"};
144         String expectedStringArrayClassName = "[Ljava.lang.String;";
145         Object[] expectedObjectArray = {};
146 
147         try {
148             assertNotNull(stringArray);
149 
150             // EMULATE_JSC_BINDINGS JSC pass null for object arrays that are not strings.
151             // Should be an empty array?
152             assertNull(emptyArray);
153             assertNull(objectArray);
154 
155             assertEquals(Arrays.toString(expectedStringArray), Arrays.toString(stringArray));
156             assertEquals(expectedStringArrayClassName, stringArray.getClass().getName());
157 
158             // EMULATE_JSC_BINDINGS
159             // assertEquals(Arrays.toString(expectedObjectArray), Arrays.toString(emptyArray));
160             // assertEquals(expectedObjectArrayClassName, emptyArray.getClass().getName());
161             // assertEquals(Arrays.toString(expectedObjectArray), Arrays.toString(objectArray));
162             // assertEquals(expectedStringObjectClassName, objectArray.getClass().getName());
163 
164         } catch (AssertionFailedError e) {
165             printAssertionFailed(e);
166             return false;
167         }
168         return true;
169     }
170 
testObjectMembers(boolean boolParam, byte byteParam, char charParam, double doubleParam, float floatParam, int intParam, long longParam, short shortParam, String stringParam, int[] intArrayParam, String[] stringArrayParam, Object objectParam)171     public boolean testObjectMembers(boolean boolParam, byte byteParam, char charParam,
172             double doubleParam, float floatParam, int intParam, long longParam, short shortParam,
173             String stringParam, int[] intArrayParam, String[] stringArrayParam,
174             Object objectParam) {
175         boolean expectedBoolParam = true;
176         byte expectedByteParam = 101;
177         char expectedCharParam = 'd';
178         double expectedDoubleParam = 123.456;
179         float expectedFloatParam = 456.789F;
180         int expectedIntParam = 102;
181         long expectedLongParam = 103L;
182         short expectedShortParam = 104;
183         String expectedStringParam = "Hello World";
184         int[] expectedIntArray = {1,2,3};
185         String[] expectedStringArrayParam = {"foo", "bar", "baz"};
186         String expectedStringArrayClassName = "[Ljava.lang.String;";
187 
188         try {
189             assertEquals(expectedBoolParam, boolParam);
190             assertEquals(expectedByteParam, byteParam);
191 
192             // EMULATE_JSC_BINDINGS: JSC does not pass chars correctly. (chars are strings in JS)
193             // assertEquals(expectedCharParam, charParam);
194 
195             assertEquals(expectedDoubleParam, doubleParam);
196             assertEquals(expectedFloatParam, floatParam);
197             assertEquals(expectedIntParam, intParam);
198             assertEquals(expectedLongParam, longParam);
199             assertEquals(expectedShortParam, shortParam);
200             assertEquals(expectedStringParam, stringParam);
201             assertEquals(Arrays.toString(expectedIntArray), Arrays.toString(intArrayParam));
202             assertEquals(Arrays.toString(expectedStringArrayParam),
203                     Arrays.toString(stringArrayParam));
204             assertEquals(expectedStringArrayClassName, stringArrayParam.getClass().getName());
205             assertNull(objectParam);
206         } catch (AssertionFailedError e) {
207             printAssertionFailed(e);
208             return false;
209         }
210         return true;
211     }
212 
testJSPrimitivesToStringsInJava(String intParam, String nullParam, String doubleParam, String booleanParam, String charParam, String undefinedParam)213     public boolean testJSPrimitivesToStringsInJava(String intParam, String nullParam,
214             String doubleParam, String booleanParam, String charParam,
215             String undefinedParam) {
216         String expectedIntParam = "123";
217         String expectedDoubleParam = "456.789";
218         String expectedBooleanParam = "true";
219         String expectedCharParam = "d";
220 
221         // EMULATE_JSC_BINDINGS JSC passes "undefined" for undefined types. Should be null?
222         String expectedUndefinedParam = "undefined";
223 
224         try {
225             assertNotNull(intParam);
226             assertNull(nullParam);
227             assertNotNull(doubleParam);
228             assertNotNull(booleanParam);
229             assertNotNull(charParam);
230 
231             // EMULATE_JSC_BINDINGS JSC passes "undefined" for undefined types.
232             assertNotNull(undefinedParam);
233 
234             assertEquals(expectedIntParam, intParam);
235             assertEquals(expectedDoubleParam, doubleParam);
236             assertEquals(expectedBooleanParam, booleanParam);
237             assertEquals(expectedCharParam, charParam);
238 
239             // EMULATE_JSC_BINDINGS  JSC passes "undefined" for undefined types.
240             assertEquals(expectedUndefinedParam, undefinedParam);
241 
242         } catch (AssertionFailedError e) {
243             printAssertionFailed(e);
244             return false;
245         }
246         return true;
247     }
248 
testParameterTypeMismatch(String[] stringArrayParam)249     public boolean testParameterTypeMismatch(String[] stringArrayParam) {
250         // The JS test will pass a string, not an array to this test.
251         try {
252             assertNull(stringArrayParam);
253         } catch (AssertionFailedError e) {
254             printAssertionFailed(e);
255             return false;
256         }
257 
258         return true;
259     }
260 
returnBool()261     public boolean returnBool() { return true; }
returnByte()262     public byte returnByte() { return 1; }
returnChar()263     public char returnChar() { return 'b'; }
returnDouble()264     public double returnDouble() { return 123.456; }
returnFloat()265     public float returnFloat() { return 456.789F; }
returnInt()266     public int returnInt() { return 123; }
returnLong()267     public long returnLong() { return 1234L; }
returnShort()268     public short returnShort() { return 12345; }
returnString()269     public String returnString() { return "Hello World!"; }
returnNullString()270     public String returnNullString() { return null; }
271 
272     public class TestObject {
273         public int x = 123;
274         public String s = "Hello World!";
275 
aMethod()276         public boolean aMethod() { return true; }
anotherMethod()277         public String anotherMethod() { return "Hello World"; }
278     }
279 
returnObject()280     public TestObject returnObject() { return new TestObject(); }
281 
returnArray()282     public int[] returnArray() {
283         int[] array = {1,2,3,4,5};
284         return array;
285     }
286 
returnVoid()287     public void returnVoid() { }
288 }
289