FileDocCategorySizeDatePackage
JavaBridgeReturnValuesTest.javaAPI DocAndroid 5.1 API8438Thu Mar 12 22:22:44 GMT 2015com.android.webviewtests

JavaBridgeReturnValuesTest

public class JavaBridgeReturnValuesTest extends JavaBridgeTestBase
Part of the test suite for the WebView's Java Bridge. This test checks that we correctly convert Java values to JavaScript values when returning them from 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.JavaBridgeReturnValuesTest \ com.android.webviewtests/android.test.InstrumentationTestRunner

Fields Summary
TestObject
mTestObject
Constructors Summary
Methods Summary
private booleanexecuteJavaScriptAndGetBooleanResult(java.lang.String script)

        executeJavaScript("testObject.setBooleanValue(" + script + ");");
        return mTestObject.waitForBooleanValue();
    
protected java.lang.StringexecuteJavaScriptAndGetStringResult(java.lang.String script)

        executeJavaScript("testObject.setStringValue(" + script + ");");
        return mTestObject.waitForStringValue();
    
protected voidsetUp()

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

        assertEquals("boolean",
                executeJavaScriptAndGetStringResult("typeof testObject.getBooleanValue()"));
        assertEquals("number",
                executeJavaScriptAndGetStringResult("typeof testObject.getByteValue()"));
        // char values are returned to JavaScript as numbers.
        assertEquals("number",
                executeJavaScriptAndGetStringResult("typeof testObject.getCharValue()"));
        assertEquals("number",
                executeJavaScriptAndGetStringResult("typeof testObject.getShortValue()"));
        assertEquals("number",
                executeJavaScriptAndGetStringResult("typeof testObject.getIntValue()"));
        assertEquals("number",
                executeJavaScriptAndGetStringResult("typeof testObject.getLongValue()"));
        assertEquals("number",
                executeJavaScriptAndGetStringResult("typeof testObject.getFloatValue()"));
        assertEquals("number",
                executeJavaScriptAndGetStringResult("typeof testObject.getFloatValueNoDecimal()"));
        assertEquals("number",
                executeJavaScriptAndGetStringResult("typeof testObject.getDoubleValue()"));
        assertEquals("number",
                executeJavaScriptAndGetStringResult("typeof testObject.getDoubleValueNoDecimal()"));
        assertEquals("string",
                executeJavaScriptAndGetStringResult("typeof testObject.getStringValue()"));
        assertEquals("string",
                executeJavaScriptAndGetStringResult("typeof testObject.getEmptyStringValue()"));
        // LIVECONNECT_COMPLIANCE: This should have type object.
        assertEquals("undefined",
                executeJavaScriptAndGetStringResult("typeof testObject.getNullStringValue()"));
        assertEquals("object",
                executeJavaScriptAndGetStringResult("typeof testObject.getObjectValue()"));
        assertEquals("object",
                executeJavaScriptAndGetStringResult("typeof testObject.getNullObjectValue()"));
        assertEquals("object",
                executeJavaScriptAndGetStringResult("typeof testObject.getCustomTypeValue()"));
        assertEquals("undefined",
                executeJavaScriptAndGetStringResult("typeof testObject.getVoidValue()"));
    
public voidtestMethodReturnValues()

        // We do the string comparison in JavaScript, to avoid relying on the
        // coercion algorithm from JavaScript to Java.
        assertTrue(executeJavaScriptAndGetBooleanResult("testObject.getBooleanValue()"));
        assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getByteValue()"));
        // char values are returned to JavaScript as numbers.
        assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getCharValue()"));
        assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getShortValue()"));
        assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getIntValue()"));
        assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getLongValue()"));
        assertTrue(executeJavaScriptAndGetBooleanResult(
                "Math.abs(42.1 - testObject.getFloatValue()) < 0.001"));
        assertTrue(executeJavaScriptAndGetBooleanResult(
                "42.0 === testObject.getFloatValueNoDecimal()"));
        assertTrue(executeJavaScriptAndGetBooleanResult(
                "Math.abs(42.1 - testObject.getDoubleValue()) < 0.001"));
        assertTrue(executeJavaScriptAndGetBooleanResult(
                "42.0 === testObject.getDoubleValueNoDecimal()"));
        assertEquals("foo", executeJavaScriptAndGetStringResult("testObject.getStringValue()"));
        assertEquals("", executeJavaScriptAndGetStringResult("testObject.getEmptyStringValue()"));
        assertTrue(executeJavaScriptAndGetBooleanResult("undefined === testObject.getVoidValue()"));