1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content.browser;
7 import android.test.suitebuilder.annotation.SmallTest;
9 import org.chromium.base.test.util.Feature;
12 * Part of the test suite for the Java Bridge. This class tests the general use of arrays.
14 * The conversions should follow
15 * http://jdk6.java.net/plugin2/liveconnect/#JS_JAVA_CONVERSIONS. Places in
16 * which the implementation differs from the spec are marked with
17 * LIVECONNECT_COMPLIANCE.
18 * FIXME: Consider making our implementation more compliant, if it will not
19 * break backwards-compatibility. See b/4408210.
21 public class JavaBridgeArrayTest extends JavaBridgeTestBase {
22 private class TestObject extends Controller {
23 private boolean mBooleanValue;
24 private int mIntValue;
25 private String mStringValue;
27 private int[] mIntArray;
28 private int[][] mIntIntArray;
30 private boolean mWasArrayMethodCalled;
32 public synchronized void setBooleanValue(boolean x) {
34 notifyResultIsReady();
36 public synchronized void setIntValue(int x) {
38 notifyResultIsReady();
40 public synchronized void setStringValue(String x) {
42 notifyResultIsReady();
45 public synchronized boolean waitForBooleanValue() {
49 public synchronized int waitForIntValue() {
53 public synchronized String waitForStringValue() {
58 public synchronized void setIntArray(int[] x) {
60 notifyResultIsReady();
62 public synchronized void setIntIntArray(int[][] x) {
64 notifyResultIsReady();
67 public synchronized int[] waitForIntArray() {
71 public synchronized int[][] waitForIntIntArray() {
76 public synchronized int[] arrayMethod() {
77 mWasArrayMethodCalled = true;
78 return new int[] {42, 43, 44};
81 public synchronized boolean wasArrayMethodCalled() {
82 return mWasArrayMethodCalled;
86 private TestObject mTestObject;
89 protected void setUp() throws Exception {
91 mTestObject = new TestObject();
92 setUpContentView(mTestObject, "testObject");
96 @Feature({"AndroidWebView", "Android-JavaBridge"})
97 public void testArrayLength() throws Throwable {
98 executeJavaScript("testObject.setIntArray([42, 43, 44]);");
99 int[] result = mTestObject.waitForIntArray();
100 assertEquals(3, result.length);
101 assertEquals(42, result[0]);
102 assertEquals(43, result[1]);
103 assertEquals(44, result[2]);
107 @Feature({"AndroidWebView", "Android-JavaBridge"})
108 public void testPassNull() throws Throwable {
109 executeJavaScript("testObject.setIntArray(null);");
110 assertNull(mTestObject.waitForIntArray());
114 @Feature({"AndroidWebView", "Android-JavaBridge"})
115 public void testPassUndefined() throws Throwable {
116 executeJavaScript("testObject.setIntArray(undefined);");
117 assertNull(mTestObject.waitForIntArray());
121 @Feature({"AndroidWebView", "Android-JavaBridge"})
122 public void testPassEmptyArray() throws Throwable {
123 executeJavaScript("testObject.setIntArray([]);");
124 assertEquals(0, mTestObject.waitForIntArray().length);
127 // Note that this requires being able to pass a string from JavaScript to
130 @Feature({"AndroidWebView", "Android-JavaBridge"})
131 public void testPassArrayToStringMethod() throws Throwable {
132 // LIVECONNECT_COMPLIANCE: Should call toString() on array.
133 executeJavaScript("testObject.setStringValue([42, 42, 42]);");
134 assertEquals("undefined", mTestObject.waitForStringValue());
137 // Note that this requires being able to pass an integer from JavaScript to
140 @Feature({"AndroidWebView", "Android-JavaBridge"})
141 public void testPassArrayToNonStringNonArrayMethod() throws Throwable {
142 // LIVECONNECT_COMPLIANCE: Should raise JavaScript exception.
143 executeJavaScript("testObject.setIntValue([42, 42, 42]);");
144 assertEquals(0, mTestObject.waitForIntValue());
148 @Feature({"AndroidWebView", "Android-JavaBridge"})
149 public void testPassNonArrayToArrayMethod() throws Throwable {
150 // LIVECONNECT_COMPLIANCE: Should raise JavaScript exception.
151 executeJavaScript("testObject.setIntArray(42);");
152 assertNull(mTestObject.waitForIntArray());
156 @Feature({"AndroidWebView", "Android-JavaBridge"})
157 public void testObjectWithLengthProperty() throws Throwable {
158 executeJavaScript("testObject.setIntArray({length: 3, 1: 42});");
159 int[] result = mTestObject.waitForIntArray();
160 assertEquals(3, result.length);
161 assertEquals(0, result[0]);
162 assertEquals(42, result[1]);
163 assertEquals(0, result[2]);
167 @Feature({"AndroidWebView", "Android-JavaBridge"})
168 public void testNonNumericLengthProperty() throws Throwable {
169 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we
170 // should raise a JavaScript exception.
171 executeJavaScript("testObject.setIntArray({length: \"foo\"});");
172 assertNull(mTestObject.waitForIntArray());
176 @Feature({"AndroidWebView", "Android-JavaBridge"})
177 public void testLengthOutOfBounds() throws Throwable {
178 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we
179 // should raise a JavaScript exception.
180 executeJavaScript("testObject.setIntArray({length: -1});");
181 assertNull(mTestObject.waitForIntArray());
183 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we
184 // should raise a JavaScript exception.
185 long length = Integer.MAX_VALUE + 1L;
186 executeJavaScript("testObject.setIntArray({length: " + length + "});");
187 assertNull(mTestObject.waitForIntArray());
189 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we
190 // should raise a JavaScript exception.
191 length = Integer.MAX_VALUE + 1L - Integer.MIN_VALUE + 1L;
192 executeJavaScript("testObject.setIntArray({length: " + length + "});");
193 assertNull(mTestObject.waitForIntArray());
197 @Feature({"AndroidWebView", "Android-JavaBridge"})
198 public void testSparseArray() throws Throwable {
199 executeJavaScript("var x = [42, 43]; x[3] = 45; testObject.setIntArray(x);");
200 int[] result = mTestObject.waitForIntArray();
201 assertEquals(4, result.length);
202 assertEquals(42, result[0]);
203 assertEquals(43, result[1]);
204 assertEquals(0, result[2]);
205 assertEquals(45, result[3]);
208 // Note that this requires being able to pass a boolean from JavaScript to
211 @Feature({"AndroidWebView", "Android-JavaBridge"})
212 public void testMethodReturningArrayNotCalled() throws Throwable {
213 // We don't invoke methods which return arrays, but note that no
214 // exception is raised.
215 // LIVECONNECT_COMPLIANCE: Should call method and convert result to
217 executeJavaScript("testObject.setBooleanValue(undefined === testObject.arrayMethod())");
218 assertTrue(mTestObject.waitForBooleanValue());
219 assertFalse(mTestObject.wasArrayMethodCalled());
223 @Feature({"AndroidWebView", "Android-JavaBridge"})
224 public void testMultiDimensionalArrayMethod() throws Throwable {
225 // LIVECONNECT_COMPLIANCE: Should handle multi-dimensional arrays.
226 executeJavaScript("testObject.setIntIntArray([ [42, 43], [44, 45] ]);");
227 assertNull(mTestObject.waitForIntIntArray());
231 @Feature({"AndroidWebView", "Android-JavaBridge"})
232 public void testPassMultiDimensionalArray() throws Throwable {
233 // LIVECONNECT_COMPLIANCE: Should handle multi-dimensional arrays.
234 executeJavaScript("testObject.setIntArray([ [42, 43], [44, 45] ]);");
235 int[] result = mTestObject.waitForIntArray();
236 assertEquals(2, result.length);
237 assertEquals(0, result[0]);
238 assertEquals(0, result[1]);
241 // Verify that ArrayBuffers are not converted into arrays when passed to Java.
242 // The LiveConnect spec doesn't mention ArrayBuffers, so it doesn't seem to
243 // be a compliance issue.
245 @Feature({"AndroidWebView", "Android-JavaBridge"})
246 public void testPassArrayBuffer() throws Throwable {
247 executeJavaScript("buffer = new ArrayBuffer(16);");
248 executeJavaScript("testObject.setIntArray(buffer);");
249 assertNull(mTestObject.waitForIntArray());
252 // Verify that ArrayBufferViews are not converted into arrays when passed to Java.
253 // The LiveConnect spec doesn't mention ArrayBufferViews, so it doesn't seem to
254 // be a compliance issue.
255 // Here, a DataView is used as an ArrayBufferView instance (since the latter is
256 // an interface and can't be instantiated directly). See also JavaBridgeArrayCoercionTest
257 // for typed arrays (that also subclass ArrayBufferView) tests.
259 @Feature({"AndroidWebView", "Android-JavaBridge"})
260 public void testPassDataView() throws Throwable {
261 executeJavaScript("buffer = new ArrayBuffer(16);");
262 executeJavaScript("testObject.setIntArray(new DataView(buffer));");
263 assertNull(mTestObject.waitForIntArray());