1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * Part of the test suite for the WebView's Java Bridge. This test tests the 19 * use of fields. 20 * 21 * To run this test ... 22 * adb shell am instrument -w -e class com.android.webviewtests.JavaBridgeFieldsTest \ 23 * com.android.webviewtests/android.test.InstrumentationTestRunner 24 */ 25 26 package com.android.webviewtests; 27 28 public class JavaBridgeFieldsTest extends JavaBridgeTestBase { 29 private class TestObject extends Controller { 30 private String mStringValue; 31 32 // These methods are used to control the test. setStringValue(String x)33 public synchronized void setStringValue(String x) { 34 mStringValue = x; 35 notifyResultIsReady(); 36 } waitForStringValue()37 public synchronized String waitForStringValue() { 38 waitForResult(); 39 return mStringValue; 40 } 41 42 public boolean booleanField = true; 43 public byte byteField = 42; 44 public char charField = '\u002A'; 45 public short shortField = 42; 46 public int intField = 42; 47 public long longField = 42L; 48 public float floatField = 42.0f; 49 public double doubleField = 42.0; 50 public String stringField = "foo"; 51 public Object objectField = new Object(); 52 public CustomType customTypeField = new CustomType(); 53 } 54 55 // A custom type used when testing passing objects. 56 private class CustomType { 57 } 58 59 TestObject mTestObject; 60 61 @Override setUp()62 protected void setUp() throws Exception { 63 super.setUp(); 64 mTestObject = new TestObject(); 65 setUpWebView(mTestObject, "testObject"); 66 } 67 68 // Note that this requires that we can pass a JavaScript string to Java. executeJavaScriptAndGetStringResult(String script)69 protected String executeJavaScriptAndGetStringResult(String script) throws Throwable { 70 executeJavaScript("testObject.setStringValue(" + script + ");"); 71 return mTestObject.waitForStringValue(); 72 } 73 74 // The Java bridge does not provide access to fields. 75 // FIXME: Consider providing support for this. See See b/4408210. testFieldTypes()76 public void testFieldTypes() throws Throwable { 77 assertEquals("undefined", 78 executeJavaScriptAndGetStringResult("typeof testObject.booleanField")); 79 assertEquals("undefined", 80 executeJavaScriptAndGetStringResult("typeof testObject.byteField")); 81 assertEquals("undefined", 82 executeJavaScriptAndGetStringResult("typeof testObject.charField")); 83 assertEquals("undefined", 84 executeJavaScriptAndGetStringResult("typeof testObject.shortField")); 85 assertEquals("undefined", 86 executeJavaScriptAndGetStringResult("typeof testObject.intField")); 87 assertEquals("undefined", 88 executeJavaScriptAndGetStringResult("typeof testObject.longField")); 89 assertEquals("undefined", 90 executeJavaScriptAndGetStringResult("typeof testObject.floatField")); 91 assertEquals("undefined", 92 executeJavaScriptAndGetStringResult("typeof testObject.doubleField")); 93 assertEquals("undefined", 94 executeJavaScriptAndGetStringResult("typeof testObject.objectField")); 95 assertEquals("undefined", 96 executeJavaScriptAndGetStringResult("typeof testObject.stringField")); 97 assertEquals("undefined", 98 executeJavaScriptAndGetStringResult("typeof testObject.customTypeField")); 99 } 100 } 101