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