This source file includes following definitions.
- setStringValue
- waitForStringValue
- setBooleanValue
- waitForBooleanValue
- getBooleanValue
- getByteValue
- getCharValue
- getShortValue
- getIntValue
- getLongValue
- getFloatValue
- getFloatValueNoDecimal
- getDoubleValue
- getDoubleValueNoDecimal
- getStringValue
- getEmptyStringValue
- getNullStringValue
- getObjectValue
- getNullObjectValue
- getCustomTypeValue
- getVoidValue
- setUp
- executeJavaScriptAndGetStringResult
- executeJavaScriptAndGetBooleanResult
- Feature
- testMethodReturnTypes
- Feature
- testMethodReturnValues
package org.chromium.content.browser;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.test.util.Feature;
public class JavaBridgeReturnValuesTest extends JavaBridgeTestBase {
private class TestObject extends Controller {
private String mStringValue;
private boolean mBooleanValue;
public synchronized void setStringValue(String x) {
mStringValue = x;
notifyResultIsReady();
}
public synchronized String waitForStringValue() {
waitForResult();
return mStringValue;
}
public synchronized void setBooleanValue(boolean x) {
mBooleanValue = x;
notifyResultIsReady();
}
public synchronized boolean waitForBooleanValue() {
waitForResult();
return mBooleanValue;
}
public boolean getBooleanValue() {
return true;
}
public byte getByteValue() {
return 42;
}
public char getCharValue() {
return '\u002A';
}
public short getShortValue() {
return 42;
}
public int getIntValue() {
return 42;
}
public long getLongValue() {
return 42L;
}
public float getFloatValue() {
return 42.1f;
}
public float getFloatValueNoDecimal() {
return 42.0f;
}
public double getDoubleValue() {
return 42.1;
}
public double getDoubleValueNoDecimal() {
return 42.0;
}
public String getStringValue() {
return "foo";
}
public String getEmptyStringValue() {
return "";
}
public String getNullStringValue() {
return null;
}
public Object getObjectValue() {
return new Object();
}
public Object getNullObjectValue() {
return null;
}
public CustomType getCustomTypeValue() {
return new CustomType();
}
public void getVoidValue() {
}
}
private static class CustomType {
}
TestObject mTestObject;
@Override
protected void setUp() throws Exception {
super.setUp();
mTestObject = new TestObject();
setUpContentView(mTestObject, "testObject");
}
protected String executeJavaScriptAndGetStringResult(String script) throws Throwable {
executeJavaScript("testObject.setStringValue(" + script + ");");
return mTestObject.waitForStringValue();
}
private boolean executeJavaScriptAndGetBooleanResult(String script) throws Throwable {
executeJavaScript("testObject.setBooleanValue(" + script + ");");
return mTestObject.waitForBooleanValue();
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testMethodReturnTypes() throws Throwable {
assertEquals("boolean",
executeJavaScriptAndGetStringResult("typeof testObject.getBooleanValue()"));
assertEquals("number",
executeJavaScriptAndGetStringResult("typeof testObject.getByteValue()"));
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()"));
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()"));
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testMethodReturnValues() throws Throwable {
assertTrue(executeJavaScriptAndGetBooleanResult("testObject.getBooleanValue()"));
assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getByteValue()"));
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()"));
}
}