Home | History | Annotate | Download | only in webviewtests
      1 /*
      2  * Copyright (C) 2012 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.
     19  *
     20  * Ensures that injected objects are exposed to child frames as well as the
     21  * main frame.
     22  *
     23  * To run this test ...
     24  *  adb shell am instrument -w -e class com.android.webviewtests.JavaBridgeChildFrameTest \
     25  *          com.android.webviewtests/android.test.InstrumentationTestRunner
     26  */
     27 
     28 package com.android.webviewtests;
     29 
     30 public class JavaBridgeChildFrameTest extends JavaBridgeTestBase {
     31     private class TestController extends Controller {
     32         private String mStringValue;
     33 
     34        public synchronized void setStringValue(String x) {
     35             mStringValue = x;
     36             notifyResultIsReady();
     37         }
     38        public synchronized String waitForStringValue() {
     39             waitForResult();
     40             return mStringValue;
     41         }
     42     }
     43 
     44     TestController mTestController;
     45 
     46     @Override
     47     protected void setUp() throws Exception {
     48         super.setUp();
     49         mTestController = new TestController();
     50         setUpWebView(mTestController, "testController");
     51     }
     52 
     53     public void testInjectedObjectPresentInChildFrame() throws Throwable {
     54         // In the case that the test fails (i.e. the child frame doesn't get the injected object,
     55         // the call to testController.setStringValue in the child frame's onload handler will
     56         // not be made.
     57         getActivity().getWebView().loadData(
     58                 "<html><head></head><body>" +
     59                 "<iframe id=\"childFrame\" onload=\"testController.setStringValue('PASS');\" />" +
     60                 "</body></html>", "text/html", null);
     61         assertEquals("PASS", mTestController.waitForStringValue());
     62     }
     63 }
     64