OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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 test checks that we correctl
y convert Java | 21 * Part of the test suite for the Java Bridge. This test checks that we correctl
y convert Java |
15 * values to JavaScript values when returning them from the methods of injected
Java objects. | 22 * values to JavaScript values when returning them from the methods of injected
Java objects. |
16 * | 23 * |
17 * The conversions should follow | 24 * The conversions should follow |
18 * http://jdk6.java.net/plugin2/liveconnect/#JS_JAVA_CONVERSIONS. Places in | 25 * http://jdk6.java.net/plugin2/liveconnect/#JS_JAVA_CONVERSIONS. Places in |
19 * which the implementation differs from the spec are marked with | 26 * which the implementation differs from the spec are marked with |
20 * LIVECONNECT_COMPLIANCE. | 27 * LIVECONNECT_COMPLIANCE. |
21 * FIXME: Consider making our implementation more compliant, if it will not | 28 * FIXME: Consider making our implementation more compliant, if it will not |
22 * break backwards-compatibility. See b/4408210. | 29 * break backwards-compatibility. See b/4408210. |
23 */ | 30 */ |
24 public class JavaBridgeReturnValuesTest extends JavaBridgeTestBase { | 31 @RunWith(BaseJUnit4ClassRunner.class) |
| 32 public class JavaBridgeReturnValuesTest { |
| 33 @Rule |
| 34 public JavaBridgeActivityTestRule mActivityTestRule = new JavaBridgeActivity
TestRule(); |
| 35 |
25 // An instance of this class is injected into the page to test returning | 36 // An instance of this class is injected into the page to test returning |
26 // Java values to JavaScript. | 37 // Java values to JavaScript. |
27 @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") | 38 @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") |
28 private static class TestObject extends Controller { | 39 private static class TestObject extends Controller { |
29 private String mStringResult; | 40 private String mStringResult; |
30 private boolean mBooleanResult; | 41 private boolean mBooleanResult; |
31 | 42 |
32 // These four methods are used to control the test. | 43 // These four methods are used to control the test. |
33 public synchronized void setStringResult(String x) { | 44 public synchronized void setStringResult(String x) { |
34 mStringResult = x; | 45 mStringResult = x; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 public void getVoidValue() { | 109 public void getVoidValue() { |
99 } | 110 } |
100 } | 111 } |
101 | 112 |
102 // A custom type used when testing passing objects. | 113 // A custom type used when testing passing objects. |
103 private static class CustomType { | 114 private static class CustomType { |
104 } | 115 } |
105 | 116 |
106 TestObject mTestObject; | 117 TestObject mTestObject; |
107 | 118 |
108 @Override | 119 @Before |
109 protected void setUp() throws Exception { | 120 public void setUp() throws Exception { |
110 super.setUp(); | |
111 mTestObject = new TestObject(); | 121 mTestObject = new TestObject(); |
112 injectObjectAndReload(mTestObject, "testObject"); | 122 mActivityTestRule.injectObjectAndReload(mTestObject, "testObject"); |
113 } | 123 } |
114 | 124 |
115 // Note that this requires that we can pass a JavaScript string to Java. | 125 // Note that this requires that we can pass a JavaScript string to Java. |
116 protected String executeJavaScriptAndGetStringResult(String script) throws T
hrowable { | 126 protected String executeJavaScriptAndGetStringResult(String script) throws T
hrowable { |
117 executeJavaScript("testObject.setStringResult(" + script + ");"); | 127 mActivityTestRule.executeJavaScript("testObject.setStringResult(" + scri
pt + ");"); |
118 return mTestObject.waitForStringResult(); | 128 return mTestObject.waitForStringResult(); |
119 } | 129 } |
120 | 130 |
121 // Note that this requires that we can pass a JavaScript boolean to Java. | 131 // Note that this requires that we can pass a JavaScript boolean to Java. |
122 private boolean executeJavaScriptAndGetBooleanResult(String script) throws T
hrowable { | 132 private boolean executeJavaScriptAndGetBooleanResult(String script) throws T
hrowable { |
123 executeJavaScript("testObject.setBooleanResult(" + script + ");"); | 133 mActivityTestRule.executeJavaScript("testObject.setBooleanResult(" + scr
ipt + ");"); |
124 return mTestObject.waitForBooleanResult(); | 134 return mTestObject.waitForBooleanResult(); |
125 } | 135 } |
126 | 136 |
| 137 @Test |
127 @SmallTest | 138 @SmallTest |
128 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 139 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
129 public void testMethodReturnTypes() throws Throwable { | 140 public void testMethodReturnTypes() throws Throwable { |
130 assertEquals("boolean", | 141 Assert.assertEquals("boolean", |
131 executeJavaScriptAndGetStringResult("typeof testObject.getBoolea
nValue()")); | 142 executeJavaScriptAndGetStringResult("typeof testObject.getBoolea
nValue()")); |
132 assertEquals("number", | 143 Assert.assertEquals( |
133 executeJavaScriptAndGetStringResult("typeof testObject.getByteVa
lue()")); | 144 "number", executeJavaScriptAndGetStringResult("typeof testObject
.getByteValue()")); |
134 // char values are returned to JavaScript as numbers. | 145 // char values are returned to JavaScript as numbers. |
135 assertEquals("number", | 146 Assert.assertEquals( |
136 executeJavaScriptAndGetStringResult("typeof testObject.getCharVa
lue()")); | 147 "number", executeJavaScriptAndGetStringResult("typeof testObject
.getCharValue()")); |
137 assertEquals("number", | 148 Assert.assertEquals( |
138 executeJavaScriptAndGetStringResult("typeof testObject.getShortV
alue()")); | 149 "number", executeJavaScriptAndGetStringResult("typeof testObject
.getShortValue()")); |
139 assertEquals("number", | 150 Assert.assertEquals( |
140 executeJavaScriptAndGetStringResult("typeof testObject.getIntVal
ue()")); | 151 "number", executeJavaScriptAndGetStringResult("typeof testObject
.getIntValue()")); |
141 assertEquals("number", | 152 Assert.assertEquals( |
142 executeJavaScriptAndGetStringResult("typeof testObject.getLongVa
lue()")); | 153 "number", executeJavaScriptAndGetStringResult("typeof testObject
.getLongValue()")); |
143 assertEquals("number", | 154 Assert.assertEquals( |
144 executeJavaScriptAndGetStringResult("typeof testObject.getFloatV
alue()")); | 155 "number", executeJavaScriptAndGetStringResult("typeof testObject
.getFloatValue()")); |
145 assertEquals("number", | 156 Assert.assertEquals("number", |
146 executeJavaScriptAndGetStringResult("typeof testObject.getFloatV
alueNoDecimal()")); | 157 executeJavaScriptAndGetStringResult("typeof testObject.getFloatV
alueNoDecimal()")); |
147 assertEquals("number", | 158 Assert.assertEquals("number", |
148 executeJavaScriptAndGetStringResult("typeof testObject.getDouble
Value()")); | 159 executeJavaScriptAndGetStringResult("typeof testObject.getDouble
Value()")); |
149 assertEquals("number", | 160 Assert.assertEquals("number", |
150 executeJavaScriptAndGetStringResult("typeof testObject.getDouble
ValueNoDecimal()")); | 161 executeJavaScriptAndGetStringResult("typeof testObject.getDouble
ValueNoDecimal()")); |
151 assertEquals("string", | 162 Assert.assertEquals("string", |
152 executeJavaScriptAndGetStringResult("typeof testObject.getString
Value()")); | 163 executeJavaScriptAndGetStringResult("typeof testObject.getString
Value()")); |
153 assertEquals("string", | 164 Assert.assertEquals("string", |
154 executeJavaScriptAndGetStringResult("typeof testObject.getEmptyS
tringValue()")); | 165 executeJavaScriptAndGetStringResult("typeof testObject.getEmptyS
tringValue()")); |
155 // LIVECONNECT_COMPLIANCE: This should have type object. | 166 // LIVECONNECT_COMPLIANCE: This should have type object. |
156 assertEquals("undefined", | 167 Assert.assertEquals("undefined", |
157 executeJavaScriptAndGetStringResult("typeof testObject.getNullSt
ringValue()")); | 168 executeJavaScriptAndGetStringResult("typeof testObject.getNullSt
ringValue()")); |
158 assertEquals("object", | 169 Assert.assertEquals("object", |
159 executeJavaScriptAndGetStringResult("typeof testObject.getObject
Value()")); | 170 executeJavaScriptAndGetStringResult("typeof testObject.getObject
Value()")); |
160 assertEquals("object", | 171 Assert.assertEquals("object", |
161 executeJavaScriptAndGetStringResult("typeof testObject.getNullOb
jectValue()")); | 172 executeJavaScriptAndGetStringResult("typeof testObject.getNullOb
jectValue()")); |
162 assertEquals("object", | 173 Assert.assertEquals("object", |
163 executeJavaScriptAndGetStringResult("typeof testObject.getCustom
TypeValue()")); | 174 executeJavaScriptAndGetStringResult("typeof testObject.getCustom
TypeValue()")); |
164 assertEquals("undefined", | 175 Assert.assertEquals("undefined", |
165 executeJavaScriptAndGetStringResult("typeof testObject.getVoidVa
lue()")); | 176 executeJavaScriptAndGetStringResult("typeof testObject.getVoidVa
lue()")); |
166 } | 177 } |
167 | 178 |
| 179 @Test |
168 @SmallTest | 180 @SmallTest |
169 @Feature({"AndroidWebView", "Android-JavaBridge"}) | 181 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
170 public void testMethodReturnValues() throws Throwable { | 182 public void testMethodReturnValues() throws Throwable { |
171 // We do the string comparison in JavaScript, to avoid relying on the | 183 // We do the string comparison in JavaScript, to avoid relying on the |
172 // coercion algorithm from JavaScript to Java. | 184 // coercion algorithm from JavaScript to Java. |
173 assertTrue(executeJavaScriptAndGetBooleanResult("testObject.getBooleanVa
lue()")); | 185 Assert.assertTrue(executeJavaScriptAndGetBooleanResult("testObject.getBo
oleanValue()")); |
174 assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getBy
teValue()")); | 186 Assert.assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObjec
t.getByteValue()")); |
175 // char values are returned to JavaScript as numbers. | 187 // char values are returned to JavaScript as numbers. |
176 assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getCh
arValue()")); | 188 Assert.assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObjec
t.getCharValue()")); |
177 assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getSh
ortValue()")); | 189 Assert.assertTrue( |
178 assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getIn
tValue()")); | 190 executeJavaScriptAndGetBooleanResult("42 === testObject.getShort
Value()")); |
179 assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getLo
ngValue()")); | 191 Assert.assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObjec
t.getIntValue()")); |
180 assertTrue(executeJavaScriptAndGetBooleanResult( | 192 Assert.assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObjec
t.getLongValue()")); |
| 193 Assert.assertTrue(executeJavaScriptAndGetBooleanResult( |
181 "Math.abs(42.1 - testObject.getFloatValue()) < 0.001")); | 194 "Math.abs(42.1 - testObject.getFloatValue()) < 0.001")); |
182 assertTrue(executeJavaScriptAndGetBooleanResult( | 195 Assert.assertTrue(executeJavaScriptAndGetBooleanResult( |
183 "42.0 === testObject.getFloatValueNoDecimal()")); | 196 "42.0 === testObject.getFloatValueNoDecimal()")); |
184 assertTrue(executeJavaScriptAndGetBooleanResult( | 197 Assert.assertTrue(executeJavaScriptAndGetBooleanResult( |
185 "Math.abs(42.1 - testObject.getDoubleValue()) < 0.001")); | 198 "Math.abs(42.1 - testObject.getDoubleValue()) < 0.001")); |
186 assertTrue(executeJavaScriptAndGetBooleanResult( | 199 Assert.assertTrue(executeJavaScriptAndGetBooleanResult( |
187 "42.0 === testObject.getDoubleValueNoDecimal()")); | 200 "42.0 === testObject.getDoubleValueNoDecimal()")); |
188 assertEquals("foo", executeJavaScriptAndGetStringResult("testObject.getS
tringValue()")); | 201 Assert.assertEquals( |
189 assertEquals("", executeJavaScriptAndGetStringResult("testObject.getEmpt
yStringValue()")); | 202 "foo", executeJavaScriptAndGetStringResult("testObject.getString
Value()")); |
190 assertTrue(executeJavaScriptAndGetBooleanResult("undefined === testObjec
t.getVoidValue()")); | 203 Assert.assertEquals( |
| 204 "", executeJavaScriptAndGetStringResult("testObject.getEmptyStri
ngValue()")); |
| 205 Assert.assertTrue( |
| 206 executeJavaScriptAndGetBooleanResult("undefined === testObject.g
etVoidValue()")); |
191 } | 207 } |
192 } | 208 } |
OLD | NEW |