This source file includes following definitions.
- setBooleanValue
- setIntValue
- setStringValue
- waitForBooleanValue
- waitForIntValue
- waitForStringValue
- setIntArray
- setIntIntArray
- waitForIntArray
- waitForIntIntArray
- arrayMethod
- wasArrayMethodCalled
- setUp
- Feature
- testArrayLength
- Feature
- testPassNull
- Feature
- testPassUndefined
- Feature
- testPassEmptyArray
- Feature
- testPassArrayToStringMethod
- Feature
- testPassArrayToNonStringNonArrayMethod
- Feature
- testPassNonArrayToArrayMethod
- Feature
- testObjectWithLengthProperty
- Feature
- testNonNumericLengthProperty
- Feature
- testLengthOutOfBounds
- Feature
- testSparseArray
- Feature
- testMethodReturningArrayNotCalled
- Feature
- testMultiDimensionalArrayMethod
- Feature
- testPassMultiDimensionalArray
package org.chromium.content.browser;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.test.util.Feature;
public class JavaBridgeArrayTest extends JavaBridgeTestBase {
private class TestObject extends Controller {
private boolean mBooleanValue;
private int mIntValue;
private String mStringValue;
private int[] mIntArray;
private int[][] mIntIntArray;
private boolean mWasArrayMethodCalled;
public synchronized void setBooleanValue(boolean x) {
mBooleanValue = x;
notifyResultIsReady();
}
public synchronized void setIntValue(int x) {
mIntValue = x;
notifyResultIsReady();
}
public synchronized void setStringValue(String x) {
mStringValue = x;
notifyResultIsReady();
}
public synchronized boolean waitForBooleanValue() {
waitForResult();
return mBooleanValue;
}
public synchronized int waitForIntValue() {
waitForResult();
return mIntValue;
}
public synchronized String waitForStringValue() {
waitForResult();
return mStringValue;
}
public synchronized void setIntArray(int[] x) {
mIntArray = x;
notifyResultIsReady();
}
public synchronized void setIntIntArray(int[][] x) {
mIntIntArray = x;
notifyResultIsReady();
}
public synchronized int[] waitForIntArray() {
waitForResult();
return mIntArray;
}
public synchronized int[][] waitForIntIntArray() {
waitForResult();
return mIntIntArray;
}
public synchronized int[] arrayMethod() {
mWasArrayMethodCalled = true;
return new int[] {42, 43, 44};
}
public synchronized boolean wasArrayMethodCalled() {
return mWasArrayMethodCalled;
}
}
private TestObject mTestObject;
@Override
protected void setUp() throws Exception {
super.setUp();
mTestObject = new TestObject();
setUpContentView(mTestObject, "testObject");
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testArrayLength() throws Throwable {
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]);
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testPassNull() throws Throwable {
executeJavaScript("testObject.setIntArray(null);");
assertNull(mTestObject.waitForIntArray());
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testPassUndefined() throws Throwable {
executeJavaScript("testObject.setIntArray(undefined);");
assertNull(mTestObject.waitForIntArray());
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testPassEmptyArray() throws Throwable {
executeJavaScript("testObject.setIntArray([]);");
assertEquals(0, mTestObject.waitForIntArray().length);
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testPassArrayToStringMethod() throws Throwable {
executeJavaScript("testObject.setStringValue([42, 42, 42]);");
assertEquals("undefined", mTestObject.waitForStringValue());
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testPassArrayToNonStringNonArrayMethod() throws Throwable {
executeJavaScript("testObject.setIntValue([42, 42, 42]);");
assertEquals(0, mTestObject.waitForIntValue());
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testPassNonArrayToArrayMethod() throws Throwable {
executeJavaScript("testObject.setIntArray(42);");
assertNull(mTestObject.waitForIntArray());
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testObjectWithLengthProperty() throws Throwable {
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]);
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testNonNumericLengthProperty() throws Throwable {
executeJavaScript("testObject.setIntArray({length: \"foo\"});");
assertNull(mTestObject.waitForIntArray());
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testLengthOutOfBounds() throws Throwable {
executeJavaScript("testObject.setIntArray({length: -1});");
assertNull(mTestObject.waitForIntArray());
long length = Integer.MAX_VALUE + 1L;
executeJavaScript("testObject.setIntArray({length: " + length + "});");
assertNull(mTestObject.waitForIntArray());
length = Integer.MAX_VALUE + 1L - Integer.MIN_VALUE + 1L;
executeJavaScript("testObject.setIntArray({length: " + length + "});");
assertNull(mTestObject.waitForIntArray());
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testSparseArray() throws Throwable {
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]);
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testMethodReturningArrayNotCalled() throws Throwable {
executeJavaScript("testObject.setBooleanValue(undefined === testObject.arrayMethod())");
assertTrue(mTestObject.waitForBooleanValue());
assertFalse(mTestObject.wasArrayMethodCalled());
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testMultiDimensionalArrayMethod() throws Throwable {
executeJavaScript("testObject.setIntIntArray([ [42, 43], [44, 45] ]);");
assertNull(mTestObject.waitForIntIntArray());
}
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testPassMultiDimensionalArray() throws Throwable {
executeJavaScript("testObject.setIntArray([ [42, 43], [44, 45] ]);");
int[] result = mTestObject.waitForIntArray();
assertEquals(2, result.length);
assertEquals(0, result[0]);
assertEquals(0, result[1]);
}
}