JNIBindingsTest.java revision e902e1a71a4bb31b9db665a7ecdb853d1538eb39
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
17package com.android.browser;
18
19import android.test.AndroidTestCase;
20import android.util.Log;
21
22import java.util.Arrays;
23import junit.framework.AssertionFailedError;
24
25public class JNIBindingsTest extends AndroidTestCase {
26
27    private final static String LOGTAG = "JNIBindingsTest";
28    private JNIBindingsTestApp mTestApp;
29
30    public int mInt = 123;
31    public String mString = "Hello World";
32
33    public JNIBindingsTest(JNIBindingsTestApp testApp) {
34        mTestApp = testApp;
35    }
36
37    public void testComplete() {
38        Log.v(LOGTAG, "Completing the test.");
39        mTestApp.testComplete();
40    }
41
42    public void printAssertionFailed(AssertionFailedError e) {
43        Log.e(LOGTAG, "");
44        Log.e(LOGTAG, "*** ASSERTION FAILED: " + e.getMessage());
45        Log.e(LOGTAG, "*** Stack trace:");
46        StackTraceElement[] trace = e.getStackTrace();
47        for(StackTraceElement elem : trace) {
48            Log.e(LOGTAG, "***\t" + elem.toString());
49        }
50        Log.e(LOGTAG, "");
51    }
52
53    public boolean testPrimitiveTypes(byte byteParam, char charParam, double doubleParam,
54            float floatParam, int intParam, long longParam, short shortParam,
55            boolean booleanParam) {
56        byte expectedByteParam = 100;
57        char expectedCharParam = 'c';
58        double expectedDoubleParam = 123.34567890;
59        float expectedFloatParam = 456.789f;
60        int expectedIntParam = 1234567;
61        long expectedLongParam = 1234567890L;
62        short expectedShortParam = 6000;
63        boolean expectedBooleanParam = true;
64
65        try {
66            assertEquals(expectedByteParam, byteParam);
67
68            // EMULATE_JSC_BINDINGS: JSC does not pass chars correctly
69            // assertEquals(expectedCharParam, charParam);
70
71            assertEquals(expectedDoubleParam, doubleParam);
72            assertEquals(expectedFloatParam, floatParam);
73            assertEquals(expectedIntParam, intParam);
74            assertEquals(expectedLongParam, longParam);
75            assertEquals(expectedShortParam, shortParam);
76            assertEquals(expectedBooleanParam, booleanParam);
77        } catch (AssertionFailedError e) {
78            printAssertionFailed(e);
79           return false;
80        }
81        return true;
82    }
83
84    public boolean testObjectTypes(String stringParam, String emptyString, Object objectParam,
85            Object emptyObject) {
86        String expectedString = "Foo";
87        String expectedEmptyString = "";
88
89        try {
90            assertNotNull(stringParam);
91            assertNotNull(emptyString);
92            assertEquals(expectedString, stringParam);
93            assertEquals(expectedEmptyString, emptyString);
94            assertNull(objectParam);
95            assertNull(emptyObject);
96        } catch (AssertionFailedError e) {
97            printAssertionFailed(e);
98            return false;
99        }
100        return true;
101    }
102
103    public boolean testArray(byte[] byteArray, char[] charArray, double[] doubleArray,
104            float[] floatArray, int[] intArray, long[] longArray, short[] shortArray,
105            boolean[] booleanArray) {
106        byte[] expectedByteArray = { 1,2,3};
107        char[] expectedCharArray = {'d', 'o', 'g'};
108        double[] expectedDoubleArray = {1.2,2.3,3.4};
109        float[] expectedFloatArray = {4.5F,5.6F,6.7F};
110        int[] expectedIntArray = {1,2,3};
111        long[] expectedLongArray = {4L,5L,6L};
112        short[] expectedShortArray = {7,8,9};
113        boolean[] expectedBooleanArray = {true, false};
114
115        try {
116            assertNotNull(byteArray);
117            assertNotNull(charArray);
118            assertNotNull(doubleArray);
119            assertNotNull(floatArray);
120            assertNotNull(intArray);
121            assertNotNull(longArray);
122            assertNotNull(shortArray);
123            assertNotNull(booleanArray);
124            assertEquals(Arrays.toString(expectedByteArray), Arrays.toString(byteArray));
125            assertEquals(Arrays.toString(expectedCharArray), Arrays.toString(charArray));
126            assertEquals(Arrays.toString(expectedDoubleArray), Arrays.toString(doubleArray));
127            assertEquals(Arrays.toString(expectedFloatArray), Arrays.toString(floatArray));
128            assertEquals(Arrays.toString(expectedIntArray), Arrays.toString(intArray));
129            assertEquals(Arrays.toString(expectedLongArray), Arrays.toString(longArray));
130            assertEquals(Arrays.toString(expectedShortArray), Arrays.toString(shortArray));
131            assertEquals(Arrays.toString(expectedBooleanArray), Arrays.toString(booleanArray));
132        } catch (AssertionFailedError e) {
133            printAssertionFailed(e);
134            return false;
135        }
136        return true;
137    }
138
139    public boolean testObjectArray(String[] stringArray, Object[] emptyArray,
140            Object[] objectArray) {
141        String[] expectedStringArray = {"Hello", "World", "!"};
142        Object[] expectedObjectArray = {};
143
144        try {
145            assertNotNull(stringArray);
146
147            // EMULATE_JSC_BINDINGS JSC pass null for object arrays that are not strings.
148            // Should be an empty array?
149            assertNull(emptyArray);
150            assertNull(objectArray);
151
152            assertEquals(Arrays.toString(expectedStringArray), Arrays.toString(stringArray));
153
154            // EMULATE_JSC_BINDINGS
155            // assertEquals(Arrays.toString(expectedObjectArray), Arrays.toString(emptyArray));
156            // assertEquals(Arrays.toString(expectedObjectArray), Arrays.toString(objectArray));
157
158        } catch (AssertionFailedError e) {
159            printAssertionFailed(e);
160            return false;
161        }
162        return true;
163    }
164
165    public boolean testObjectMembers(boolean boolParam, byte byteParam, char charParam,
166            double doubleParam, float floatParam, int intParam, long longParam, short shortParam,
167            String stringParam, int[] intArrayParam, String[] stringArrayParam,
168            Object objectParam) {
169        boolean expectedBoolParam = true;
170        byte expectedByteParam = 101;
171        char expectedCharParam = 'd';
172        double expectedDoubleParam = 123.456;
173        float expectedFloatParam = 456.789F;
174        int expectedIntParam = 102;
175        long expectedLongParam = 103L;
176        short expectedShortParam = 104;
177        String expectedStringParam = "Hello World";
178        int[] expectedIntArray = {1,2,3};
179        String[] expectedStringArrayParam = {"foo", "bar", "baz"};
180
181        try {
182            assertEquals(expectedBoolParam, boolParam);
183            assertEquals(expectedByteParam, byteParam);
184
185            // EMULATE_JSC_BINDINGS: JSC does not pass chars correctly. (chars are strings in JS)
186            // assertEquals(expectedCharParam, charParam);
187
188            assertEquals(expectedDoubleParam, doubleParam);
189            assertEquals(expectedFloatParam, floatParam);
190            assertEquals(expectedIntParam, intParam);
191            assertEquals(expectedLongParam, longParam);
192            assertEquals(expectedShortParam, shortParam);
193            assertEquals(expectedStringParam, stringParam);
194            assertEquals(Arrays.toString(expectedIntArray), Arrays.toString(intArrayParam));
195            assertEquals(Arrays.toString(expectedStringArrayParam),
196                    Arrays.toString(stringArrayParam));
197            assertNull(objectParam);
198        } catch (AssertionFailedError e) {
199            printAssertionFailed(e);
200            return false;
201        }
202        return true;
203    }
204
205    public boolean testJSPrimitivesToStringsInJava(String intParam, String nullParam,
206            String doubleParam, String booleanParam, String charParam,
207            String undefinedParam) {
208        String expectedIntParam = "123";
209        String expectedDoubleParam = "456.789";
210        String expectedBooleanParam = "true";
211        String expectedCharParam = "d";
212
213        // EMULATE_JSC_BINDINGS JSC passes "undefined" for undefined types. Should be null?
214        String expectedUndefinedParam = "undefined";
215
216        try {
217            assertNotNull(intParam);
218            assertNull(nullParam);
219            assertNotNull(doubleParam);
220            assertNotNull(booleanParam);
221            assertNotNull(charParam);
222
223            // EMULATE_JSC_BINDINGS JSC passes "undefined" for undefined types.
224            assertNotNull(undefinedParam);
225
226            assertEquals(expectedIntParam, intParam);
227            assertEquals(expectedDoubleParam, doubleParam);
228            assertEquals(expectedBooleanParam, booleanParam);
229            assertEquals(expectedCharParam, charParam);;
230
231            // EMULATE_JSC_BINDINGS  JSC passes "undefined" for undefined types.
232            assertEquals(expectedUndefinedParam, undefinedParam);
233
234        } catch (AssertionFailedError e) {
235            printAssertionFailed(e);
236            return false;
237        }
238        return true;
239    }
240
241    public boolean returnBool() { return true; }
242    public byte returnByte() { return 1; }
243    public char returnChar() { return 'b'; }
244    public double returnDouble() { return 123.456; }
245    public float returnFloat() { return 456.789F; }
246    public int returnInt() { return 123; }
247    public long returnLong() { return 1234L; }
248    public short returnShort() { return 12345; }
249    public String returnString() { return "Hello World!"; }
250
251    public class TestObject {
252        public int x = 123;
253        public String s = "Hello World!";
254
255        public boolean aMethod() { return true; }
256        public String anotherMethod() { return "Hello World"; }
257    }
258
259    public TestObject returnObject() { return new TestObject(); }
260
261    public int[] returnArray() {
262        int[] array = {1,2,3,4,5};
263        return array;
264    }
265
266    public void returnVoid() { }
267}
268