summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/browser/JNIBindingsTest.java
blob: bfa3ac157a6152d800a470783e8d2e8678167ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser;

import android.test.AndroidTestCase;
import android.util.Log;

import java.util.Arrays;
import junit.framework.AssertionFailedError;

public class JNIBindingsTest extends AndroidTestCase {

    private final static String LOGTAG = "JNIBindingsTest";
    private JNIBindingsTestApp mTestApp;

    public int mInt = 123;
    public String mString = "Hello World";

    public JNIBindingsTest(JNIBindingsTestApp testApp) {
        mTestApp = testApp;
    }

    public void testComplete() {
        Log.v(LOGTAG, "Completing the test.");
        mTestApp.testComplete();
    }

    public void printAssertionFailed(AssertionFailedError e) {
        Log.e(LOGTAG, "");
        Log.e(LOGTAG, "*** ASSERTION FAILED: " + e.getMessage());
        Log.e(LOGTAG, "*** Stack trace:");
        StackTraceElement[] trace = e.getStackTrace();
        for(StackTraceElement elem : trace) {
            Log.e(LOGTAG, "***\t" + elem.toString());
        }
        Log.e(LOGTAG, "");
    }

    public boolean testPrimitiveTypes(byte byteParam, char charParam, double doubleParam,
            float floatParam, int intParam, long longParam, short shortParam,
            boolean booleanParam) {
        byte expectedByteParam = 100;
        char expectedCharParam = 'c';
        double expectedDoubleParam = 123.34567890;
        float expectedFloatParam = 456.789f;
        int expectedIntParam = 1234567;
        long expectedLongParam = 1234567890L;
        short expectedShortParam = 6000;
        boolean expectedBooleanParam = true;

        try {
            assertEquals(expectedByteParam, byteParam);

            // EMULATE_JSC_BINDINGS: JSC does not pass chars correctly
            // assertEquals(expectedCharParam, charParam);

            assertEquals(expectedDoubleParam, doubleParam);
            assertEquals(expectedFloatParam, floatParam);
            assertEquals(expectedIntParam, intParam);
            assertEquals(expectedLongParam, longParam);
            assertEquals(expectedShortParam, shortParam);
            assertEquals(expectedBooleanParam, booleanParam);
        } catch (AssertionFailedError e) {
            printAssertionFailed(e);
           return false;
        }
        return true;
    }

    public boolean testObjectTypes(String stringParam, String emptyString, Object objectParam,
            Object emptyObject) {
        String expectedString = "Foo";
        String expectedEmptyString = "";

        try {
            assertNotNull(stringParam);
            assertNotNull(emptyString);
            assertEquals(expectedString, stringParam);
            assertEquals(expectedEmptyString, emptyString);
            assertNull(objectParam);
            assertNull(emptyObject);
        } catch (AssertionFailedError e) {
            printAssertionFailed(e);
            return false;
        }
        return true;
    }

    public boolean testArray(byte[] byteArray, char[] charArray, double[] doubleArray,
            float[] floatArray, int[] intArray, long[] longArray, short[] shortArray,
            boolean[] booleanArray) {
        byte[] expectedByteArray = { 1,2,3};
        char[] expectedCharArray = {'d', 'o', 'g'};
        double[] expectedDoubleArray = {1.2,2.3,3.4};
        float[] expectedFloatArray = {4.5F,5.6F,6.7F};
        int[] expectedIntArray = {1,2,3};
        long[] expectedLongArray = {4L,5L,6L};
        short[] expectedShortArray = {7,8,9};
        boolean[] expectedBooleanArray = {true, false};

        try {
            assertNotNull(byteArray);
            assertNotNull(charArray);
            assertNotNull(doubleArray);
            assertNotNull(floatArray);
            assertNotNull(intArray);
            assertNotNull(longArray);
            assertNotNull(shortArray);
            assertNotNull(booleanArray);
            assertEquals(Arrays.toString(expectedByteArray), Arrays.toString(byteArray));
            assertEquals(Arrays.toString(expectedCharArray), Arrays.toString(charArray));
            assertEquals(Arrays.toString(expectedDoubleArray), Arrays.toString(doubleArray));
            assertEquals(Arrays.toString(expectedFloatArray), Arrays.toString(floatArray));
            assertEquals(Arrays.toString(expectedIntArray), Arrays.toString(intArray));
            assertEquals(Arrays.toString(expectedLongArray), Arrays.toString(longArray));
            assertEquals(Arrays.toString(expectedShortArray), Arrays.toString(shortArray));
            assertEquals(Arrays.toString(expectedBooleanArray), Arrays.toString(booleanArray));
        } catch (AssertionFailedError e) {
            printAssertionFailed(e);
            return false;
        }
        return true;
    }

    public boolean testObjectArray(String[] stringArray, Object[] emptyArray,
            Object[] objectArray) {
        String[] expectedStringArray = {"Hello", "World", "!"};
        String expectedStringArrayClassName = "[Ljava.lang.String;";
        Object[] expectedObjectArray = {};

        try {
            assertNotNull(stringArray);

            // EMULATE_JSC_BINDINGS JSC pass null for object arrays that are not strings.
            // Should be an empty array?
            assertNull(emptyArray);
            assertNull(objectArray);

            assertEquals(Arrays.toString(expectedStringArray), Arrays.toString(stringArray));
            assertEquals(expectedStringArrayClassName, stringArray.getClass().getName());

            // EMULATE_JSC_BINDINGS
            // assertEquals(Arrays.toString(expectedObjectArray), Arrays.toString(emptyArray));
            // assertEquals(expectedObjectArrayClassName, emptyArray.getClass().getName());
            // assertEquals(Arrays.toString(expectedObjectArray), Arrays.toString(objectArray));
            // assertEquals(expectedStringObjectClassName, objectArray.getClass().getName());

        } catch (AssertionFailedError e) {
            printAssertionFailed(e);
            return false;
        }
        return true;
    }

    public boolean 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) {
        boolean expectedBoolParam = true;
        byte expectedByteParam = 101;
        char expectedCharParam = 'd';
        double expectedDoubleParam = 123.456;
        float expectedFloatParam = 456.789F;
        int expectedIntParam = 102;
        long expectedLongParam = 103L;
        short expectedShortParam = 104;
        String expectedStringParam = "Hello World";
        int[] expectedIntArray = {1,2,3};
        String[] expectedStringArrayParam = {"foo", "bar", "baz"};
        String expectedStringArrayClassName = "[Ljava.lang.String;";

        try {
            assertEquals(expectedBoolParam, boolParam);
            assertEquals(expectedByteParam, byteParam);

            // EMULATE_JSC_BINDINGS: JSC does not pass chars correctly. (chars are strings in JS)
            // assertEquals(expectedCharParam, charParam);

            assertEquals(expectedDoubleParam, doubleParam);
            assertEquals(expectedFloatParam, floatParam);
            assertEquals(expectedIntParam, intParam);
            assertEquals(expectedLongParam, longParam);
            assertEquals(expectedShortParam, shortParam);
            assertEquals(expectedStringParam, stringParam);
            assertEquals(Arrays.toString(expectedIntArray), Arrays.toString(intArrayParam));
            assertEquals(Arrays.toString(expectedStringArrayParam),
                    Arrays.toString(stringArrayParam));
            assertEquals(expectedStringArrayClassName, stringArrayParam.getClass().getName());
            assertNull(objectParam);
        } catch (AssertionFailedError e) {
            printAssertionFailed(e);
            return false;
        }
        return true;
    }

    public boolean testJSPrimitivesToStringsInJava(String intParam, String nullParam,
            String doubleParam, String booleanParam, String charParam,
            String undefinedParam) {
        String expectedIntParam = "123";
        String expectedDoubleParam = "456.789";
        String expectedBooleanParam = "true";
        String expectedCharParam = "d";

        // EMULATE_JSC_BINDINGS JSC passes "undefined" for undefined types. Should be null?
        String expectedUndefinedParam = "undefined";

        try {
            assertNotNull(intParam);
            assertNull(nullParam);
            assertNotNull(doubleParam);
            assertNotNull(booleanParam);
            assertNotNull(charParam);

            // EMULATE_JSC_BINDINGS JSC passes "undefined" for undefined types.
            assertNotNull(undefinedParam);

            assertEquals(expectedIntParam, intParam);
            assertEquals(expectedDoubleParam, doubleParam);
            assertEquals(expectedBooleanParam, booleanParam);
            assertEquals(expectedCharParam, charParam);;

            // EMULATE_JSC_BINDINGS  JSC passes "undefined" for undefined types.
            assertEquals(expectedUndefinedParam, undefinedParam);

        } catch (AssertionFailedError e) {
            printAssertionFailed(e);
            return false;
        }
        return true;
    }

    public boolean testParameterTypeMismatch(String[] stringArrayParam) {
        // The JS test will pass a string, not an array to this test.
        try {
            assertNull(stringArrayParam);
        } catch (AssertionFailedError e) {
            printAssertionFailed(e);
            return false;
        }

        return true;
    }

    public boolean returnBool() { return true; }
    public byte returnByte() { return 1; }
    public char returnChar() { return 'b'; }
    public double returnDouble() { return 123.456; }
    public float returnFloat() { return 456.789F; }
    public int returnInt() { return 123; }
    public long returnLong() { return 1234L; }
    public short returnShort() { return 12345; }
    public String returnString() { return "Hello World!"; }

    public class TestObject {
        public int x = 123;
        public String s = "Hello World!";

        public boolean aMethod() { return true; }
        public String anotherMethod() { return "Hello World"; }
    }

    public TestObject returnObject() { return new TestObject(); }

    public int[] returnArray() {
        int[] array = {1,2,3,4,5};
        return array;
    }

    public void returnVoid() { }
}