| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.support.test.filters.SmallTest; | 7 import android.support.test.filters.SmallTest; |
| 8 | 8 |
| 9 import org.junit.Assert; |
| 10 import org.junit.Before; |
| 11 import org.junit.Rule; |
| 12 import org.junit.Test; |
| 13 import org.junit.runner.RunWith; |
| 14 |
| 9 import org.chromium.base.annotations.SuppressFBWarnings; | 15 import org.chromium.base.annotations.SuppressFBWarnings; |
| 16 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 10 import org.chromium.base.test.util.Feature; | 17 import org.chromium.base.test.util.Feature; |
| 11 import org.chromium.content.browser.JavaBridgeTestCommon.Controller; | 18 import org.chromium.content.browser.JavaBridgeTestCommon.Controller; |
| 12 | 19 |
| 13 /** | 20 /** |
| 14 * Part of the test suite for the Java Bridge. This class tests the general use
of arrays. | 21 * Part of the test suite for the Java Bridge. This class tests the general use
of arrays. |
| 15 * | 22 * |
| 16 * The conversions should follow | 23 * The conversions should follow |
| 17 * http://jdk6.java.net/plugin2/liveconnect/#JS_JAVA_CONVERSIONS. Places in | 24 * http://jdk6.java.net/plugin2/liveconnect/#JS_JAVA_CONVERSIONS. Places in |
| 18 * which the implementation differs from the spec are marked with | 25 * which the implementation differs from the spec are marked with |
| 19 * LIVECONNECT_COMPLIANCE. | 26 * LIVECONNECT_COMPLIANCE. |
| 20 * FIXME: Consider making our implementation more compliant, if it will not | 27 * FIXME: Consider making our implementation more compliant, if it will not |
| 21 * break backwards-compatibility. See b/4408210. | 28 * break backwards-compatibility. See b/4408210. |
| 22 */ | 29 */ |
| 23 public class JavaBridgeArrayTest extends JavaBridgeTestBase { | 30 @RunWith(BaseJUnit4ClassRunner.class) |
| 31 public class JavaBridgeArrayTest { |
| 32 @Rule |
| 33 public JavaBridgeActivityTestRule mActivityTestRule = new JavaBridgeActivity
TestRule(); |
| 34 |
| 24 @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") | 35 @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") |
| 25 private static class TestObject extends Controller { | 36 private static class TestObject extends Controller { |
| 26 private boolean mBooleanValue; | 37 private boolean mBooleanValue; |
| 27 private int mIntValue; | 38 private int mIntValue; |
| 28 private String mStringValue; | 39 private String mStringValue; |
| 29 | 40 |
| 30 private int[] mIntArray; | 41 private int[] mIntArray; |
| 31 private int[][] mIntIntArray; | 42 private int[][] mIntIntArray; |
| 32 | 43 |
| 33 private boolean mWasArrayMethodCalled; | 44 private boolean mWasArrayMethodCalled; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 return new int[] {42, 43, 44}; | 92 return new int[] {42, 43, 44}; |
| 82 } | 93 } |
| 83 | 94 |
| 84 public synchronized boolean wasArrayMethodCalled() { | 95 public synchronized boolean wasArrayMethodCalled() { |
| 85 return mWasArrayMethodCalled; | 96 return mWasArrayMethodCalled; |
| 86 } | 97 } |
| 87 } | 98 } |
| 88 | 99 |
| 89 private TestObject mTestObject; | 100 private TestObject mTestObject; |
| 90 | 101 |
| 91 @Override | 102 @Before |
| 92 protected void setUp() throws Exception { | 103 public void setUp() throws Exception { |
| 93 super.setUp(); | |
| 94 mTestObject = new TestObject(); | 104 mTestObject = new TestObject(); |
| 95 injectObjectAndReload(mTestObject, "testObject"); | 105 mActivityTestRule.injectObjectAndReload(mTestObject, "testObject"); |
| 96 } | 106 } |
| 97 | 107 |
| 108 @Test |
| 98 @SmallTest | 109 @SmallTest |
| 99 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 110 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 100 public void testArrayLength() throws Throwable { | 111 public void testArrayLength() throws Throwable { |
| 101 executeJavaScript("testObject.setIntArray([42, 43, 44]);"); | 112 mActivityTestRule.executeJavaScript("testObject.setIntArray([42, 43, 44]
);"); |
| 102 int[] result = mTestObject.waitForIntArray(); | 113 int[] result = mTestObject.waitForIntArray(); |
| 103 assertEquals(3, result.length); | 114 Assert.assertEquals(3, result.length); |
| 104 assertEquals(42, result[0]); | 115 Assert.assertEquals(42, result[0]); |
| 105 assertEquals(43, result[1]); | 116 Assert.assertEquals(43, result[1]); |
| 106 assertEquals(44, result[2]); | 117 Assert.assertEquals(44, result[2]); |
| 107 } | 118 } |
| 108 | 119 |
| 120 @Test |
| 109 @SmallTest | 121 @SmallTest |
| 110 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 122 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 111 public void testPassNull() throws Throwable { | 123 public void testPassNull() throws Throwable { |
| 112 executeJavaScript("testObject.setIntArray(null);"); | 124 mActivityTestRule.executeJavaScript("testObject.setIntArray(null);"); |
| 113 assertNull(mTestObject.waitForIntArray()); | 125 Assert.assertNull(mTestObject.waitForIntArray()); |
| 114 } | 126 } |
| 115 | 127 |
| 128 @Test |
| 116 @SmallTest | 129 @SmallTest |
| 117 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 130 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 118 public void testPassUndefined() throws Throwable { | 131 public void testPassUndefined() throws Throwable { |
| 119 executeJavaScript("testObject.setIntArray(undefined);"); | 132 mActivityTestRule.executeJavaScript("testObject.setIntArray(undefined);"
); |
| 120 assertNull(mTestObject.waitForIntArray()); | 133 Assert.assertNull(mTestObject.waitForIntArray()); |
| 121 } | 134 } |
| 122 | 135 |
| 136 @Test |
| 123 @SmallTest | 137 @SmallTest |
| 124 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 138 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 125 public void testPassEmptyArray() throws Throwable { | 139 public void testPassEmptyArray() throws Throwable { |
| 126 executeJavaScript("testObject.setIntArray([]);"); | 140 mActivityTestRule.executeJavaScript("testObject.setIntArray([]);"); |
| 127 assertEquals(0, mTestObject.waitForIntArray().length); | 141 Assert.assertEquals(0, mTestObject.waitForIntArray().length); |
| 128 } | 142 } |
| 129 | 143 |
| 130 // Note that this requires being able to pass a string from JavaScript to | 144 // Note that this requires being able to pass a string from JavaScript to |
| 131 // Java. | 145 // Java. |
| 146 @Test |
| 132 @SmallTest | 147 @SmallTest |
| 133 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 148 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 134 public void testPassArrayToStringMethod() throws Throwable { | 149 public void testPassArrayToStringMethod() throws Throwable { |
| 135 // LIVECONNECT_COMPLIANCE: Should call toString() on array. | 150 // LIVECONNECT_COMPLIANCE: Should call toString() on array. |
| 136 executeJavaScript("testObject.setStringValue([42, 42, 42]);"); | 151 mActivityTestRule.executeJavaScript("testObject.setStringValue([42, 42,
42]);"); |
| 137 assertEquals("undefined", mTestObject.waitForStringValue()); | 152 Assert.assertEquals("undefined", mTestObject.waitForStringValue()); |
| 138 } | 153 } |
| 139 | 154 |
| 140 // Note that this requires being able to pass an integer from JavaScript to | 155 // Note that this requires being able to pass an integer from JavaScript to |
| 141 // Java. | 156 // Java. |
| 157 @Test |
| 142 @SmallTest | 158 @SmallTest |
| 143 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 159 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 144 public void testPassArrayToNonStringNonArrayMethod() throws Throwable { | 160 public void testPassArrayToNonStringNonArrayMethod() throws Throwable { |
| 145 // LIVECONNECT_COMPLIANCE: Should raise JavaScript exception. | 161 // LIVECONNECT_COMPLIANCE: Should raise JavaScript exception. |
| 146 executeJavaScript("testObject.setIntValue([42, 42, 42]);"); | 162 mActivityTestRule.executeJavaScript("testObject.setIntValue([42, 42, 42]
);"); |
| 147 assertEquals(0, mTestObject.waitForIntValue()); | 163 Assert.assertEquals(0, mTestObject.waitForIntValue()); |
| 148 } | 164 } |
| 149 | 165 |
| 166 @Test |
| 150 @SmallTest | 167 @SmallTest |
| 151 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 168 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 152 public void testPassNonArrayToArrayMethod() throws Throwable { | 169 public void testPassNonArrayToArrayMethod() throws Throwable { |
| 153 // LIVECONNECT_COMPLIANCE: Should raise JavaScript exception. | 170 // LIVECONNECT_COMPLIANCE: Should raise JavaScript exception. |
| 154 executeJavaScript("testObject.setIntArray(42);"); | 171 mActivityTestRule.executeJavaScript("testObject.setIntArray(42);"); |
| 155 assertNull(mTestObject.waitForIntArray()); | 172 Assert.assertNull(mTestObject.waitForIntArray()); |
| 156 } | 173 } |
| 157 | 174 |
| 175 @Test |
| 158 @SmallTest | 176 @SmallTest |
| 159 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 177 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 160 public void testObjectWithLengthProperty() throws Throwable { | 178 public void testObjectWithLengthProperty() throws Throwable { |
| 161 executeJavaScript("testObject.setIntArray({length: 3, 1: 42});"); | 179 mActivityTestRule.executeJavaScript("testObject.setIntArray({length: 3,
1: 42});"); |
| 162 int[] result = mTestObject.waitForIntArray(); | 180 int[] result = mTestObject.waitForIntArray(); |
| 163 assertEquals(3, result.length); | 181 Assert.assertEquals(3, result.length); |
| 164 assertEquals(0, result[0]); | 182 Assert.assertEquals(0, result[0]); |
| 165 assertEquals(42, result[1]); | 183 Assert.assertEquals(42, result[1]); |
| 166 assertEquals(0, result[2]); | 184 Assert.assertEquals(0, result[2]); |
| 167 } | 185 } |
| 168 | 186 |
| 187 @Test |
| 169 @SmallTest | 188 @SmallTest |
| 170 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 189 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 171 public void testNonNumericLengthProperty() throws Throwable { | 190 public void testNonNumericLengthProperty() throws Throwable { |
| 172 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we | 191 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we |
| 173 // should raise a JavaScript exception. | 192 // should raise a JavaScript exception. |
| 174 executeJavaScript("testObject.setIntArray({length: \"foo\"});"); | 193 mActivityTestRule.executeJavaScript("testObject.setIntArray({length: \"f
oo\"});"); |
| 175 assertNull(mTestObject.waitForIntArray()); | 194 Assert.assertNull(mTestObject.waitForIntArray()); |
| 176 } | 195 } |
| 177 | 196 |
| 197 @Test |
| 178 @SmallTest | 198 @SmallTest |
| 179 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 199 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 180 public void testLengthOutOfBounds() throws Throwable { | 200 public void testLengthOutOfBounds() throws Throwable { |
| 181 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we | 201 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we |
| 182 // should raise a JavaScript exception. | 202 // should raise a JavaScript exception. |
| 183 executeJavaScript("testObject.setIntArray({length: -1});"); | 203 mActivityTestRule.executeJavaScript("testObject.setIntArray({length: -1}
);"); |
| 184 assertNull(mTestObject.waitForIntArray()); | 204 Assert.assertNull(mTestObject.waitForIntArray()); |
| 185 | 205 |
| 186 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we | 206 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we |
| 187 // should raise a JavaScript exception. | 207 // should raise a JavaScript exception. |
| 188 long length = Integer.MAX_VALUE + 1L; | 208 long length = Integer.MAX_VALUE + 1L; |
| 189 executeJavaScript("testObject.setIntArray({length: " + length + "});"); | 209 mActivityTestRule.executeJavaScript("testObject.setIntArray({length: " +
length + "});"); |
| 190 assertNull(mTestObject.waitForIntArray()); | 210 Assert.assertNull(mTestObject.waitForIntArray()); |
| 191 | 211 |
| 192 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we | 212 // LIVECONNECT_COMPLIANCE: This should not count as an array, so we |
| 193 // should raise a JavaScript exception. | 213 // should raise a JavaScript exception. |
| 194 length = Integer.MAX_VALUE + 1L - Integer.MIN_VALUE + 1L; | 214 length = Integer.MAX_VALUE + 1L - Integer.MIN_VALUE + 1L; |
| 195 executeJavaScript("testObject.setIntArray({length: " + length + "});"); | 215 mActivityTestRule.executeJavaScript("testObject.setIntArray({length: " +
length + "});"); |
| 196 assertNull(mTestObject.waitForIntArray()); | 216 Assert.assertNull(mTestObject.waitForIntArray()); |
| 197 } | 217 } |
| 198 | 218 |
| 219 @Test |
| 199 @SmallTest | 220 @SmallTest |
| 200 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 221 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 201 public void testSparseArray() throws Throwable { | 222 public void testSparseArray() throws Throwable { |
| 202 executeJavaScript("var x = [42, 43]; x[3] = 45; testObject.setIntArray(x
);"); | 223 mActivityTestRule.executeJavaScript( |
| 224 "var x = [42, 43]; x[3] = 45; testObject.setIntArray(x);"); |
| 203 int[] result = mTestObject.waitForIntArray(); | 225 int[] result = mTestObject.waitForIntArray(); |
| 204 assertEquals(4, result.length); | 226 Assert.assertEquals(4, result.length); |
| 205 assertEquals(42, result[0]); | 227 Assert.assertEquals(42, result[0]); |
| 206 assertEquals(43, result[1]); | 228 Assert.assertEquals(43, result[1]); |
| 207 assertEquals(0, result[2]); | 229 Assert.assertEquals(0, result[2]); |
| 208 assertEquals(45, result[3]); | 230 Assert.assertEquals(45, result[3]); |
| 209 } | 231 } |
| 210 | 232 |
| 211 // Note that this requires being able to pass a boolean from JavaScript to | 233 // Note that this requires being able to pass a boolean from JavaScript to |
| 212 // Java. | 234 // Java. |
| 235 @Test |
| 213 @SmallTest | 236 @SmallTest |
| 214 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 237 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 215 public void testMethodReturningArrayNotCalled() throws Throwable { | 238 public void testMethodReturningArrayNotCalled() throws Throwable { |
| 216 // We don't invoke methods which return arrays, but note that no | 239 // We don't invoke methods which return arrays, but note that no |
| 217 // exception is raised. | 240 // exception is raised. |
| 218 // LIVECONNECT_COMPLIANCE: Should call method and convert result to | 241 // LIVECONNECT_COMPLIANCE: Should call method and convert result to |
| 219 // JavaScript array. | 242 // JavaScript array. |
| 220 executeJavaScript("testObject.setBooleanValue(undefined === testObject.a
rrayMethod())"); | 243 mActivityTestRule.executeJavaScript( |
| 221 assertTrue(mTestObject.waitForBooleanValue()); | 244 "testObject.setBooleanValue(undefined === testObject.arrayMethod
())"); |
| 222 assertFalse(mTestObject.wasArrayMethodCalled()); | 245 Assert.assertTrue(mTestObject.waitForBooleanValue()); |
| 246 Assert.assertFalse(mTestObject.wasArrayMethodCalled()); |
| 223 } | 247 } |
| 224 | 248 |
| 249 @Test |
| 225 @SmallTest | 250 @SmallTest |
| 226 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 251 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 227 public void testMultiDimensionalArrayMethod() throws Throwable { | 252 public void testMultiDimensionalArrayMethod() throws Throwable { |
| 228 // LIVECONNECT_COMPLIANCE: Should handle multi-dimensional arrays. | 253 // LIVECONNECT_COMPLIANCE: Should handle multi-dimensional arrays. |
| 229 executeJavaScript("testObject.setIntIntArray([ [42, 43], [44, 45] ]);"); | 254 mActivityTestRule.executeJavaScript("testObject.setIntIntArray([ [42, 43
], [44, 45] ]);"); |
| 230 assertNull(mTestObject.waitForIntIntArray()); | 255 Assert.assertNull(mTestObject.waitForIntIntArray()); |
| 231 } | 256 } |
| 232 | 257 |
| 258 @Test |
| 233 @SmallTest | 259 @SmallTest |
| 234 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 260 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 235 public void testPassMultiDimensionalArray() throws Throwable { | 261 public void testPassMultiDimensionalArray() throws Throwable { |
| 236 // LIVECONNECT_COMPLIANCE: Should handle multi-dimensional arrays. | 262 // LIVECONNECT_COMPLIANCE: Should handle multi-dimensional arrays. |
| 237 executeJavaScript("testObject.setIntArray([ [42, 43], [44, 45] ]);"); | 263 mActivityTestRule.executeJavaScript("testObject.setIntArray([ [42, 43],
[44, 45] ]);"); |
| 238 int[] result = mTestObject.waitForIntArray(); | 264 int[] result = mTestObject.waitForIntArray(); |
| 239 assertEquals(2, result.length); | 265 Assert.assertEquals(2, result.length); |
| 240 assertEquals(0, result[0]); | 266 Assert.assertEquals(0, result[0]); |
| 241 assertEquals(0, result[1]); | 267 Assert.assertEquals(0, result[1]); |
| 242 } | 268 } |
| 243 | 269 |
| 244 // Verify that ArrayBuffers are not converted into arrays when passed to Jav
a. | 270 // Verify that ArrayBuffers are not converted into arrays when passed to Jav
a. |
| 245 // The LiveConnect spec doesn't mention ArrayBuffers, so it doesn't seem to | 271 // The LiveConnect spec doesn't mention ArrayBuffers, so it doesn't seem to |
| 246 // be a compliance issue. | 272 // be a compliance issue. |
| 273 @Test |
| 247 @SmallTest | 274 @SmallTest |
| 248 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 275 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 249 public void testPassArrayBuffer() throws Throwable { | 276 public void testPassArrayBuffer() throws Throwable { |
| 250 executeJavaScript("buffer = new ArrayBuffer(16);"); | 277 mActivityTestRule.executeJavaScript("buffer = new ArrayBuffer(16);"); |
| 251 executeJavaScript("testObject.setIntArray(buffer);"); | 278 mActivityTestRule.executeJavaScript("testObject.setIntArray(buffer);"); |
| 252 assertNull(mTestObject.waitForIntArray()); | 279 Assert.assertNull(mTestObject.waitForIntArray()); |
| 253 } | 280 } |
| 254 | 281 |
| 255 // Verify that ArrayBufferViews are not converted into arrays when passed to
Java. | 282 // Verify that ArrayBufferViews are not converted into arrays when passed to
Java. |
| 256 // The LiveConnect spec doesn't mention ArrayBufferViews, so it doesn't seem
to | 283 // The LiveConnect spec doesn't mention ArrayBufferViews, so it doesn't seem
to |
| 257 // be a compliance issue. | 284 // be a compliance issue. |
| 258 // Here, a DataView is used as an ArrayBufferView instance (since the latter
is | 285 // Here, a DataView is used as an ArrayBufferView instance (since the latter
is |
| 259 // an interface and can't be instantiated directly). See also JavaBridgeArra
yCoercionTest | 286 // an interface and can't be instantiated directly). See also JavaBridgeArra
yCoercionTest |
| 260 // for typed arrays (that also subclass ArrayBufferView) tests. | 287 // for typed arrays (that also subclass ArrayBufferView) tests. |
| 288 @Test |
| 261 @SmallTest | 289 @SmallTest |
| 262 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 290 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 263 public void testPassDataView() throws Throwable { | 291 public void testPassDataView() throws Throwable { |
| 264 executeJavaScript("buffer = new ArrayBuffer(16);"); | 292 mActivityTestRule.executeJavaScript("buffer = new ArrayBuffer(16);"); |
| 265 executeJavaScript("testObject.setIntArray(new DataView(buffer));"); | 293 mActivityTestRule.executeJavaScript("testObject.setIntArray(new DataView
(buffer));"); |
| 266 assertNull(mTestObject.waitForIntArray()); | 294 Assert.assertNull(mTestObject.waitForIntArray()); |
| 267 } | 295 } |
| 268 } | 296 } |
| OLD | NEW |