FileDocCategorySizeDatePackage
JavaBridgeArrayTest.javaAPI DocAndroid 5.1 API8546Thu Mar 12 22:22:44 GMT 2015com.android.webviewtests

JavaBridgeArrayTest

public class JavaBridgeArrayTest extends JavaBridgeTestBase
Part of the test suite for the WebView's Java Bridge. This class tests the general use of arrays. 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.JavaBridgeArrayTest \ 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 voidtestArrayLength()

        executeJavaScript("testObject.setIntArray([42, 43, 44]);");
        int[] result = mTestObject.waitForIntArray();
        assertEquals(3, result.length);
        assertEquals(42, result[0]);
        assertEquals(43, result[1]);
        assertEquals(44, result[2]);
    
public voidtestLengthOutOfBounds()

        // LIVECONNECT_COMPLIANCE: This should not count as an array, so we
        // should raise a JavaScript exception.
        executeJavaScript("testObject.setIntArray({length: -1});");
        assertNull(mTestObject.waitForIntArray());

        // LIVECONNECT_COMPLIANCE: This should not count as an array, so we
        // should raise a JavaScript exception.
        long length = (long)Integer.MAX_VALUE + 1L;
        executeJavaScript("testObject.setIntArray({length: " + length + "});");
        assertNull(mTestObject.waitForIntArray());

        // LIVECONNECT_COMPLIANCE: This should not count as an array, so we
        // should raise a JavaScript exception.
        length = (long)Integer.MAX_VALUE + 1L - (long)Integer.MIN_VALUE + 1L;
        executeJavaScript("testObject.setIntArray({length: " + length + "});");
        assertNull(mTestObject.waitForIntArray());
    
public voidtestMethodReturningArrayNotCalled()

        // We don't invoke methods which return arrays, but note that no
        // exception is raised.
        // LIVECONNECT_COMPLIANCE: Should call method and convert result to
        // JavaScript array.
        executeJavaScript("testObject.setBooleanValue(undefined === testObject.arrayMethod())");
        assertTrue(mTestObject.waitForBooleanValue());
        assertFalse(mTestObject.wasArrayMethodCalled());
    
public voidtestMultiDimensionalArrayMethod()

        // LIVECONNECT_COMPLIANCE: Should handle multi-dimensional arrays.
        executeJavaScript("testObject.setIntIntArray([ [42, 43], [44, 45] ]);");
        assertNull(mTestObject.waitForIntIntArray());
    
public voidtestNonNumericLengthProperty()

        // LIVECONNECT_COMPLIANCE: This should not count as an array, so we
        // should raise a JavaScript exception.
        executeJavaScript("testObject.setIntArray({length: \"foo\"});");
        assertNull(mTestObject.waitForIntArray());
    
public voidtestObjectWithLengthProperty()

        executeJavaScript("testObject.setIntArray({length: 3, 1: 42});");
        int[] result = mTestObject.waitForIntArray();
        assertEquals(3, result.length);
        assertEquals(0, result[0]);
        assertEquals(42, result[1]);
        assertEquals(0, result[2]);
    
public voidtestPassArrayToNonStringNonArrayMethod()

        // LIVECONNECT_COMPLIANCE: Should raise JavaScript exception.
        executeJavaScript("testObject.setIntValue([42, 42, 42]);");
        assertEquals(0, mTestObject.waitForIntValue());
    
public voidtestPassArrayToStringMethod()

        // LIVECONNECT_COMPLIANCE: Should call toString() on array.
        executeJavaScript("testObject.setStringValue([42, 42, 42]);");
        assertEquals("undefined", mTestObject.waitForStringValue());
    
public voidtestPassEmptyArray()

        executeJavaScript("testObject.setIntArray([]);");
        assertEquals(0, mTestObject.waitForIntArray().length);
    
public voidtestPassMultiDimensionalArray()

        // LIVECONNECT_COMPLIANCE: Should handle multi-dimensional arrays.
        executeJavaScript("testObject.setIntArray([ [42, 43], [44, 45] ]);");
        int[] result = mTestObject.waitForIntArray();
        assertEquals(2, result.length);
        assertEquals(0, result[0]);
        assertEquals(0, result[1]);
    
public voidtestPassNonArrayToArrayMethod()

        // LIVECONNECT_COMPLIANCE: Should raise JavaScript exception.
        executeJavaScript("testObject.setIntArray(42);");
        assertNull(mTestObject.waitForIntArray());
    
public voidtestPassNull()

        executeJavaScript("testObject.setIntArray(null);");
        assertNull(mTestObject.waitForIntArray());
    
public voidtestPassUndefined()

        executeJavaScript("testObject.setIntArray(undefined);");
        assertNull(mTestObject.waitForIntArray());
    
public voidtestSparseArray()

        executeJavaScript("var x = [42, 43]; x[3] = 45; testObject.setIntArray(x);");
        int[] result = mTestObject.waitForIntArray();
        assertEquals(4, result.length);
        assertEquals(42, result[0]);
        assertEquals(43, result[1]);
        assertEquals(0, result[2]);
        assertEquals(45, result[3]);