Home | History | Annotate | Download | only in browser
      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 
     34     public JNIBindingsTest(JNIBindingsTestApp testApp) {
     35         mTestApp = testApp;
     36     }
     37 
     38     public void notifyComplete() {
     39         Log.v(LOGTAG, "Completing the test.");
     40         mTestApp.notifyComplete();
     41     }
     42 
     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 
     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 
     85     public boolean testObjectTypes(String stringParam, String emptyString, Object objectParam,
     86             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(objectParam);
     96             assertNull(emptyObject);
     97         } catch (AssertionFailedError e) {
     98             printAssertionFailed(e);
     99             return false;
    100         }
    101         return true;
    102     }
    103 
    104     public boolean testArray(byte[] byteArray, char[] charArray, double[] doubleArray,
    105             float[] floatArray, int[] intArray, long[] longArray, short[] shortArray,
    106             boolean[] booleanArray) {
    107         byte[] expectedByteArray = { 1,2,3};
    108         char[] expectedCharArray = {'d', 'o', 'g'};
    109         double[] expectedDoubleArray = {1.2,2.3,3.4};
    110         float[] expectedFloatArray = {4.5F,5.6F,6.7F};
    111         int[] expectedIntArray = {1,2,3};
    112         long[] expectedLongArray = {4L,5L,6L};
    113         short[] expectedShortArray = {7,8,9};
    114         boolean[] expectedBooleanArray = {true, false};
    115 
    116         try {
    117             assertNotNull(byteArray);
    118             assertNotNull(charArray);
    119             assertNotNull(doubleArray);
    120             assertNotNull(floatArray);
    121             assertNotNull(intArray);
    122             assertNotNull(longArray);
    123             assertNotNull(shortArray);
    124             assertNotNull(booleanArray);
    125             assertEquals(Arrays.toString(expectedByteArray), Arrays.toString(byteArray));
    126             assertEquals(Arrays.toString(expectedCharArray), Arrays.toString(charArray));
    127             assertEquals(Arrays.toString(expectedDoubleArray), Arrays.toString(doubleArray));
    128             assertEquals(Arrays.toString(expectedFloatArray), Arrays.toString(floatArray));
    129             assertEquals(Arrays.toString(expectedIntArray), Arrays.toString(intArray));
    130             assertEquals(Arrays.toString(expectedLongArray), Arrays.toString(longArray));
    131             assertEquals(Arrays.toString(expectedShortArray), Arrays.toString(shortArray));
    132             assertEquals(Arrays.toString(expectedBooleanArray), Arrays.toString(booleanArray));
    133         } catch (AssertionFailedError e) {
    134             printAssertionFailed(e);
    135             return false;
    136         }
    137         return true;
    138     }
    139 
    140     public boolean testObjectArray(String[] stringArray, Object[] emptyArray,
    141             Object[] objectArray) {
    142         String[] expectedStringArray = {"Hello", "World", "!"};
    143         String expectedStringArrayClassName = "[Ljava.lang.String;";
    144         Object[] expectedObjectArray = {};
    145 
    146         try {
    147             assertNotNull(stringArray);
    148 
    149             // EMULATE_JSC_BINDINGS JSC pass null for object arrays that are not strings.
    150             // Should be an empty array?
    151             assertNull(emptyArray);
    152             assertNull(objectArray);
    153 
    154             assertEquals(Arrays.toString(expectedStringArray), Arrays.toString(stringArray));
    155             assertEquals(expectedStringArrayClassName, stringArray.getClass().getName());
    156 
    157             // EMULATE_JSC_BINDINGS
    158             // assertEquals(Arrays.toString(expectedObjectArray), Arrays.toString(emptyArray));
    159             // assertEquals(expectedObjectArrayClassName, emptyArray.getClass().getName());
    160             // assertEquals(Arrays.toString(expectedObjectArray), Arrays.toString(objectArray));
    161             // assertEquals(expectedStringObjectClassName, objectArray.getClass().getName());
    162 
    163         } catch (AssertionFailedError e) {
    164             printAssertionFailed(e);
    165             return false;
    166         }
    167         return true;
    168     }
    169 
    170     public boolean testObjectMembers(boolean boolParam, byte byteParam, char charParam,
    171             double doubleParam, float floatParam, int intParam, long longParam, short shortParam,
    172             String stringParam, int[] intArrayParam, String[] stringArrayParam,
    173             Object objectParam) {
    174         boolean expectedBoolParam = true;
    175         byte expectedByteParam = 101;
    176         char expectedCharParam = 'd';
    177         double expectedDoubleParam = 123.456;
    178         float expectedFloatParam = 456.789F;
    179         int expectedIntParam = 102;
    180         long expectedLongParam = 103L;
    181         short expectedShortParam = 104;
    182         String expectedStringParam = "Hello World";
    183         int[] expectedIntArray = {1,2,3};
    184         String[] expectedStringArrayParam = {"foo", "bar", "baz"};
    185         String expectedStringArrayClassName = "[Ljava.lang.String;";
    186 
    187         try {
    188             assertEquals(expectedBoolParam, boolParam);
    189             assertEquals(expectedByteParam, byteParam);
    190 
    191             // EMULATE_JSC_BINDINGS: JSC does not pass chars correctly. (chars are strings in JS)
    192             // assertEquals(expectedCharParam, charParam);
    193 
    194             assertEquals(expectedDoubleParam, doubleParam);
    195             assertEquals(expectedFloatParam, floatParam);
    196             assertEquals(expectedIntParam, intParam);
    197             assertEquals(expectedLongParam, longParam);
    198             assertEquals(expectedShortParam, shortParam);
    199             assertEquals(expectedStringParam, stringParam);
    200             assertEquals(Arrays.toString(expectedIntArray), Arrays.toString(intArrayParam));
    201             assertEquals(Arrays.toString(expectedStringArrayParam),
    202                     Arrays.toString(stringArrayParam));
    203             assertEquals(expectedStringArrayClassName, stringArrayParam.getClass().getName());
    204             assertNull(objectParam);
    205         } catch (AssertionFailedError e) {
    206             printAssertionFailed(e);
    207             return false;
    208         }
    209         return true;
    210     }
    211 
    212     public boolean testJSPrimitivesToStringsInJava(String intParam, String nullParam,
    213             String doubleParam, String booleanParam, String charParam,
    214             String undefinedParam) {
    215         String expectedIntParam = "123";
    216         String expectedDoubleParam = "456.789";
    217         String expectedBooleanParam = "true";
    218         String expectedCharParam = "d";
    219 
    220         // EMULATE_JSC_BINDINGS JSC passes "undefined" for undefined types. Should be null?
    221         String expectedUndefinedParam = "undefined";
    222 
    223         try {
    224             assertNotNull(intParam);
    225             assertNull(nullParam);
    226             assertNotNull(doubleParam);
    227             assertNotNull(booleanParam);
    228             assertNotNull(charParam);
    229 
    230             // EMULATE_JSC_BINDINGS JSC passes "undefined" for undefined types.
    231             assertNotNull(undefinedParam);
    232 
    233             assertEquals(expectedIntParam, intParam);
    234             assertEquals(expectedDoubleParam, doubleParam);
    235             assertEquals(expectedBooleanParam, booleanParam);
    236             assertEquals(expectedCharParam, charParam);
    237 
    238             // EMULATE_JSC_BINDINGS  JSC passes "undefined" for undefined types.
    239             assertEquals(expectedUndefinedParam, undefinedParam);
    240 
    241         } catch (AssertionFailedError e) {
    242             printAssertionFailed(e);
    243             return false;
    244         }
    245         return true;
    246     }
    247 
    248     public boolean testParameterTypeMismatch(String[] stringArrayParam) {
    249         // The JS test will pass a string, not an array to this test.
    250         try {
    251             assertNull(stringArrayParam);
    252         } catch (AssertionFailedError e) {
    253             printAssertionFailed(e);
    254             return false;
    255         }
    256 
    257         return true;
    258     }
    259 
    260     public boolean returnBool() { return true; }
    261     public byte returnByte() { return 1; }
    262     public char returnChar() { return 'b'; }
    263     public double returnDouble() { return 123.456; }
    264     public float returnFloat() { return 456.789F; }
    265     public int returnInt() { return 123; }
    266     public long returnLong() { return 1234L; }
    267     public short returnShort() { return 12345; }
    268     public String returnString() { return "Hello World!"; }
    269 
    270     public class TestObject {
    271         public int x = 123;
    272         public String s = "Hello World!";
    273 
    274         public boolean aMethod() { return true; }
    275         public String anotherMethod() { return "Hello World"; }
    276     }
    277 
    278     public TestObject returnObject() { return new TestObject(); }
    279 
    280     public int[] returnArray() {
    281         int[] array = {1,2,3,4,5};
    282         return array;
    283     }
    284 
    285     public void returnVoid() { }
    286 }
    287