FileDocCategorySizeDatePackage
JavaBridgeArrayCoercionTest.javaAPI DocAndroid 5.1 API27813Thu Mar 12 22:22:44 GMT 2015com.android.webviewtests

JavaBridgeArrayCoercionTest

public class JavaBridgeArrayCoercionTest extends JavaBridgeTestBase
Part of the test suite for the WebView's Java Bridge. This class tests that we correctly convert JavaScript arrays to Java arrays when passing them to the methods of injected Java objects. The conversions should follow http://jdk6.java.net/plugin2/liveconnect/#JS_JAVA_CONVERSIONS. Places in which the implementation differs from the spec are marked with LIVECONNECT_COMPLIANCE. FIXME: Consider making our implementation more compliant, if it will not break backwards-compatibility. See b/4408210. To run this test ... adb shell am instrument -w -e class com.android.webviewtests.JavaBridgeArrayCoercionTest \ com.android.webviewtests/android.test.InstrumentationTestRunner

Fields Summary
private TestObject
mTestObject
Constructors Summary
Methods Summary
protected voidsetUp()

        super.setUp();
        mTestObject = new TestObject();
        setUpWebView(mTestObject, "testObject");
    
public voidtestPassBoolean()

        executeJavaScript("testObject.setBooleanArray([true]);");
        assertTrue(mTestObject.waitForBooleanArray()[0]);
        executeJavaScript("testObject.setBooleanArray([false]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should be 1.
        executeJavaScript("testObject.setByteArray([true]);");
        assertEquals(0, mTestObject.waitForByteArray()[0]);
        executeJavaScript("testObject.setByteArray([false]);");
        assertEquals(0, mTestObject.waitForByteArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should convert to numeric char value 1.
        executeJavaScript("testObject.setCharArray([true]);");
        assertEquals('\u0000", mTestObject.waitForCharArray()[0]);
        executeJavaScript("testObject.setCharArray([false]);");
        assertEquals('\u0000", mTestObject.waitForCharArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should be 1.
        executeJavaScript("testObject.setShortArray([true]);");
        assertEquals(0, mTestObject.waitForShortArray()[0]);
        executeJavaScript("testObject.setShortArray([false]);");
        assertEquals(0, mTestObject.waitForShortArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should be 1.
        executeJavaScript("testObject.setIntArray([true]);");
        assertEquals(0, mTestObject.waitForIntArray()[0]);
        executeJavaScript("testObject.setIntArray([false]);");
        assertEquals(0, mTestObject.waitForIntArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should be 1.
        executeJavaScript("testObject.setLongArray([true]);");
        assertEquals(0L, mTestObject.waitForLongArray()[0]);
        executeJavaScript("testObject.setLongArray([false]);");
        assertEquals(0L, mTestObject.waitForLongArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should be 1.0.
        executeJavaScript("testObject.setFloatArray([true]);");
        assertEquals(0.0f, mTestObject.waitForFloatArray()[0]);
        executeJavaScript("testObject.setFloatArray([false]);");
        assertEquals(0.0f, mTestObject.waitForFloatArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should be 1.0.
        executeJavaScript("testObject.setDoubleArray([true]);");
        assertEquals(0.0, mTestObject.waitForDoubleArray()[0]);
        executeJavaScript("testObject.setDoubleArray([false]);");
        assertEquals(0.0, mTestObject.waitForDoubleArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and create instances of java.lang.Number.
        executeJavaScript("testObject.setObjectArray([true]);");
        assertNull(mTestObject.waitForObjectArray());

        // LIVECONNECT_COMPLIANCE: Should create instances of java.lang.String.
        executeJavaScript("testObject.setStringArray([true]);");
        assertNull(mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setCustomTypeArray([true]);");
        assertNull(mTestObject.waitForCustomTypeArray());
    
public voidtestPassJavaObject()

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setBooleanArray([testObject.getObjectInstance()]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setByteArray([testObject.getObjectInstance()]);");
        assertEquals(0, mTestObject.waitForByteArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setCharArray([testObject.getObjectInstance()]);");
        assertEquals('\u0000", mTestObject.waitForCharArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setShortArray([testObject.getObjectInstance()]);");
        assertEquals(0, mTestObject.waitForShortArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setIntArray([testObject.getObjectInstance()]);");
        assertEquals(0, mTestObject.waitForIntArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setLongArray([testObject.getObjectInstance()]);");
        assertEquals(0L, mTestObject.waitForLongArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setFloatArray([testObject.getObjectInstance()]);");
        assertEquals(0.0f, mTestObject.waitForFloatArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setDoubleArray([testObject.getObjectInstance()]);");
        assertEquals(0.0, mTestObject.waitForDoubleArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create an array and pass Java object.
        executeJavaScript("testObject.setObjectArray([testObject.getObjectInstance()]);");
        assertNull(mTestObject.waitForObjectArray());

        // LIVECONNECT_COMPLIANCE: Should call toString() on object.
        executeJavaScript("testObject.setStringArray([testObject.getObjectInstance()]);");
        assertNull(mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and pass Java object.
        executeJavaScript("testObject.setCustomTypeArray([testObject.getObjectInstance()]);");
        assertNull(mTestObject.waitForCustomTypeArray());
        executeJavaScript("testObject.setCustomTypeArray([testObject.getCustomTypeInstance()]);");
        assertNull(mTestObject.waitForCustomTypeArray());
    
public voidtestPassJavaScriptObject()

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setBooleanArray([{foo: 42}]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setByteArray([{foo: 42}]);");
        assertEquals(0, mTestObject.waitForByteArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setCharArray([{foo: 42}]);");
        assertEquals('\u0000", mTestObject.waitForCharArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setShortArray([{foo: 42}]);");
        assertEquals(0, mTestObject.waitForShortArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setIntArray([{foo: 42}]);");
        assertEquals(0, mTestObject.waitForIntArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setLongArray([{foo: 42}]);");
        assertEquals(0L, mTestObject.waitForLongArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setFloatArray([{foo: 42}]);");
        assertEquals(0.0f, mTestObject.waitForFloatArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setDoubleArray([{foo: 42}]);");
        assertEquals(0.0, mTestObject.waitForDoubleArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setObjectArray([{foo: 42}]);");
        assertNull(mTestObject.waitForObjectArray());

        // LIVECONNECT_COMPLIANCE: Should call toString() on object.
        executeJavaScript("testObject.setStringArray([{foo: 42}]);");
        assertNull(mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setCustomTypeArray([{foo: 42}]);");
        assertNull(mTestObject.waitForCustomTypeArray());
    
public voidtestPassNull()

        executeJavaScript("testObject.setByteArray([null]);");
        assertEquals(0, mTestObject.waitForByteArray()[0]);

        executeJavaScript("testObject.setCharArray([null]);");
        assertEquals('\u0000", mTestObject.waitForCharArray()[0]);

        executeJavaScript("testObject.setShortArray([null]);");
        assertEquals(0, mTestObject.waitForShortArray()[0]);

        executeJavaScript("testObject.setIntArray([null]);");
        assertEquals(0, mTestObject.waitForIntArray()[0]);

        executeJavaScript("testObject.setLongArray([null]);");
        assertEquals(0L, mTestObject.waitForLongArray()[0]);

        executeJavaScript("testObject.setFloatArray([null]);");
        assertEquals(0.0f, mTestObject.waitForFloatArray()[0]);

        executeJavaScript("testObject.setDoubleArray([null]);");
        assertEquals(0.0, mTestObject.waitForDoubleArray()[0]);

        executeJavaScript("testObject.setBooleanArray([null]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and pass null.
        executeJavaScript("testObject.setObjectArray([null]);");
        assertNull(mTestObject.waitForObjectArray());

        executeJavaScript("testObject.setStringArray([null]);");
        assertNull(mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and pass null.
        executeJavaScript("testObject.setCustomTypeArray([null]);");
        assertNull(mTestObject.waitForCustomTypeArray());
    
public voidtestPassNumberDouble()

        // LIVECONNECT_COMPLIANCE: Should convert to boolean.
        executeJavaScript("testObject.setBooleanArray([42.1]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);

        executeJavaScript("testObject.setByteArray([42.1]);");
        assertEquals(42, mTestObject.waitForByteArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should convert to numeric char value.
        executeJavaScript("testObject.setCharArray([42.1]);");
        assertEquals('\u0000", mTestObject.waitForCharArray()[0]);

        executeJavaScript("testObject.setShortArray([42.1]);");
        assertEquals(42, mTestObject.waitForShortArray()[0]);

        executeJavaScript("testObject.setIntArray([42.1]);");
        assertEquals(42, mTestObject.waitForIntArray()[0]);

        executeJavaScript("testObject.setLongArray([42.1]);");
        assertEquals(42L, mTestObject.waitForLongArray()[0]);

        executeJavaScript("testObject.setFloatArray([42.1]);");
        assertEquals(42.1f, mTestObject.waitForFloatArray()[0]);

        executeJavaScript("testObject.setDoubleArray([42.1]);");
        assertEquals(42.1, mTestObject.waitForDoubleArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and create instances of java.lang.Number.
        executeJavaScript("testObject.setObjectArray([42.1]);");
        assertNull(mTestObject.waitForObjectArray());

        // LIVECONNECT_COMPLIANCE: Should create instances of java.lang.String.
        executeJavaScript("testObject.setStringArray([42.1]);");
        assertNull(mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setCustomTypeArray([42.1]);");
        assertNull(mTestObject.waitForCustomTypeArray());
    
public voidtestPassNumberInfinity()

        executeJavaScript("testObject.setBooleanArray([Infinity]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);

        executeJavaScript("testObject.setByteArray([Infinity]);");
        assertEquals(-1, mTestObject.waitForByteArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should convert to maximum numeric char value.
        executeJavaScript("testObject.setCharArray([Infinity]);");
        assertEquals('\u0000", mTestObject.waitForCharArray()[0]);

        executeJavaScript("testObject.setShortArray([Infinity]);");
        assertEquals(-1, mTestObject.waitForShortArray()[0]);

        executeJavaScript("testObject.setIntArray([Infinity]);");
        assertEquals(Integer.MAX_VALUE, mTestObject.waitForIntArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should be Long.MAX_VALUE.
        executeJavaScript("testObject.setLongArray([Infinity]);");
        assertEquals(-1L, mTestObject.waitForLongArray()[0]);

        executeJavaScript("testObject.setFloatArray([Infinity]);");
        assertEquals(Float.POSITIVE_INFINITY, mTestObject.waitForFloatArray()[0]);

        executeJavaScript("testObject.setDoubleArray([Infinity]);");
        assertEquals(Double.POSITIVE_INFINITY, mTestObject.waitForDoubleArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and create instances of java.lang.Number.
        executeJavaScript("testObject.setObjectArray([Infinity]);");
        assertNull(mTestObject.waitForObjectArray());

        // LIVECONNECT_COMPLIANCE: Should create instances of java.lang.String.
        executeJavaScript("testObject.setStringArray([Infinity]);");
        assertNull(mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setCustomTypeArray([Infinity]);");
        assertNull(mTestObject.waitForCustomTypeArray());
    
public voidtestPassNumberInt32()

        executeJavaScript("testObject.setBooleanArray([0]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);
        // LIVECONNECT_COMPLIANCE: Should convert to boolean.
        executeJavaScript("testObject.setBooleanArray([42]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);

        executeJavaScript("testObject.setByteArray([42]);");
        assertEquals(42, mTestObject.waitForByteArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should convert to numeric char value.
        executeJavaScript("testObject.setCharArray([42]);");
        assertEquals('\u0000", mTestObject.waitForCharArray()[0]);

        executeJavaScript("testObject.setShortArray([42]);");
        assertEquals(42, mTestObject.waitForShortArray()[0]);

        executeJavaScript("testObject.setIntArray([42]);");
        assertEquals(42, mTestObject.waitForIntArray()[0]);

        executeJavaScript("testObject.setLongArray([42]);");
        assertEquals(42L, mTestObject.waitForLongArray()[0]);

        executeJavaScript("testObject.setFloatArray([42]);");
        assertEquals(42.0f, mTestObject.waitForFloatArray()[0]);

        executeJavaScript("testObject.setDoubleArray([42]);");
        assertEquals(42.0, mTestObject.waitForDoubleArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and create instances of java.lang.Number.
        executeJavaScript("testObject.setObjectArray([42]);");
        assertNull(mTestObject.waitForObjectArray());

        // LIVECONNECT_COMPLIANCE: Should create instances of java.lang.String.
        executeJavaScript("testObject.setStringArray([42]);");
        assertNull(mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setCustomTypeArray([42]);");
        assertNull(mTestObject.waitForCustomTypeArray());
    
public voidtestPassNumberNaN()

        executeJavaScript("testObject.setBooleanArray([Number.NaN]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);

        executeJavaScript("testObject.setByteArray([Number.NaN]);");
        assertEquals(0, mTestObject.waitForByteArray()[0]);

        executeJavaScript("testObject.setCharArray([Number.NaN]);");
        assertEquals('\u0000", mTestObject.waitForCharArray()[0]);

        executeJavaScript("testObject.setShortArray([Number.NaN]);");
        assertEquals(0, mTestObject.waitForShortArray()[0]);

        executeJavaScript("testObject.setIntArray([Number.NaN]);");
        assertEquals(0, mTestObject.waitForIntArray()[0]);

        executeJavaScript("testObject.setLongArray([Number.NaN]);");
        assertEquals(0L, mTestObject.waitForLongArray()[0]);

        executeJavaScript("testObject.setFloatArray([Number.NaN]);");
        assertEquals(Float.NaN, mTestObject.waitForFloatArray()[0]);

        executeJavaScript("testObject.setDoubleArray([Number.NaN]);");
        assertEquals(Double.NaN, mTestObject.waitForDoubleArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and create instances of java.lang.Number.
        executeJavaScript("testObject.setObjectArray([Number.NaN]);");
        assertNull(mTestObject.waitForObjectArray());

        // LIVECONNECT_COMPLIANCE: Should create instances of java.lang.String.
        executeJavaScript("testObject.setStringArray([Number.NaN]);");
        assertNull(mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setCustomTypeArray([Number.NaN]);");
        assertNull(mTestObject.waitForCustomTypeArray());
    
public voidtestPassString()

        // LIVECONNECT_COMPLIANCE: Non-empty string should convert to true.
        executeJavaScript("testObject.setBooleanArray([\"+042.10\"]);");
        assertFalse(mTestObject.waitForBooleanArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should use valueOf() of appropriate type.
        executeJavaScript("testObject.setByteArray([\"+042.10\"]);");
        assertEquals(0, mTestObject.waitForByteArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should decode and convert to numeric char value.
        executeJavaScript("testObject.setCharArray([\"+042.10\"]);");
        assertEquals(0, mTestObject.waitForCharArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should use valueOf() of appropriate type.
        executeJavaScript("testObject.setShortArray([\"+042.10\"]);");
        assertEquals(0, mTestObject.waitForShortArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should use valueOf() of appropriate type.
        executeJavaScript("testObject.setIntArray([\"+042.10\"]);");
        assertEquals(0, mTestObject.waitForIntArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should use valueOf() of appropriate type.
        executeJavaScript("testObject.setLongArray([\"+042.10\"]);");
        assertEquals(0L, mTestObject.waitForLongArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should use valueOf() of appropriate type.
        executeJavaScript("testObject.setFloatArray([\"+042.10\"]);");
        assertEquals(0.0f, mTestObject.waitForFloatArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should use valueOf() of appropriate type.
        executeJavaScript("testObject.setDoubleArray([\"+042.10\"]);");
        assertEquals(0.0, mTestObject.waitForDoubleArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and create instances of java.lang.Number.
        executeJavaScript("testObject.setObjectArray([\"+042.10\"]);");
        assertNull(mTestObject.waitForObjectArray());

        executeJavaScript("testObject.setStringArray([\"+042.10\"]);");
        assertEquals("+042.10", mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should raise a JavaScript exception.
        executeJavaScript("testObject.setCustomTypeArray([\"+042.10\"]);");
        assertNull(mTestObject.waitForCustomTypeArray());
    
public voidtestPassUndefined()

        executeJavaScript("testObject.setByteArray([undefined]);");
        assertEquals(0, mTestObject.waitForByteArray()[0]);

        executeJavaScript("testObject.setCharArray([undefined]);");
        assertEquals(0, mTestObject.waitForCharArray()[0]);

        executeJavaScript("testObject.setShortArray([undefined]);");
        assertEquals(0, mTestObject.waitForShortArray()[0]);

        executeJavaScript("testObject.setIntArray([undefined]);");
        assertEquals(0, mTestObject.waitForIntArray()[0]);

        executeJavaScript("testObject.setLongArray([undefined]);");
        assertEquals(0L, mTestObject.waitForLongArray()[0]);

        executeJavaScript("testObject.setFloatArray([undefined]);");
        assertEquals(0.0f, mTestObject.waitForFloatArray()[0]);

        executeJavaScript("testObject.setDoubleArray([undefined]);");
        assertEquals(0.0, mTestObject.waitForDoubleArray()[0]);

        executeJavaScript("testObject.setBooleanArray([undefined]);");
        assertEquals(false, mTestObject.waitForBooleanArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and pass null.
        executeJavaScript("testObject.setObjectArray([undefined]);");
        assertNull(mTestObject.waitForObjectArray());

        executeJavaScript("testObject.setStringArray([undefined]);");
        assertNull(mTestObject.waitForStringArray()[0]);

        // LIVECONNECT_COMPLIANCE: Should create array and pass null.
        executeJavaScript("testObject.setCustomTypeArray([undefined]);");
        assertNull(mTestObject.waitForCustomTypeArray());