Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / android_webview / javatests / src / org / chromium / android_webview / test / AwJavaBridgeTest.java
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.android_webview.test;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8
9 import org.chromium.android_webview.AwContents;
10 import org.chromium.base.test.util.Feature;
11 import org.chromium.content.browser.JavascriptInterface;
12
13 /**
14  * Test suite for the WebView specific JavaBridge features.
15  */
16 public class AwJavaBridgeTest extends AwTestBase {
17
18     private TestAwContentsClient mContentsClient = new TestAwContentsClient();
19     private AwTestContainerView mTestContainerView;
20
21     @Override
22     protected void setUp() throws Exception {
23         super.setUp();
24         mTestContainerView = createAwTestContainerViewOnMainSync(mContentsClient);
25     }
26
27     @SmallTest
28     @Feature({"AndroidWebView", "Android-JavaBridge"})
29     public void testDestroyFromJavaObject() throws Throwable {
30         final String html = "<html>Hello World</html>";
31         final TestAwContentsClient client2 = new TestAwContentsClient();
32         final AwTestContainerView view2 = createAwTestContainerViewOnMainSync(client2);
33         final AwContents awContents = mTestContainerView.getAwContents();
34
35         class Test {
36             @JavascriptInterface
37             public void destroy() {
38                 try {
39                     runTestOnUiThread(new Runnable() {
40                             @Override
41                             public void run() {
42                                 awContents.destroy();
43                             }
44                     });
45                     // Destroying one AwContents from within the JS callback should still
46                     // leave others functioning.
47                     loadDataSync(view2.getAwContents(), client2.getOnPageFinishedHelper(),
48                             html, "text/html", false);
49                 } catch (Throwable t) {
50                     throw new RuntimeException(t);
51                 }
52             }
53         }
54
55         enableJavaScriptOnUiThread(awContents);
56         runTestOnUiThread(new Runnable() {
57                 @Override
58                 public void run() {
59                     awContents.addPossiblyUnsafeJavascriptInterface(new Test(), "test", null);
60             }
61         });
62
63         loadDataSync(awContents, mContentsClient.getOnPageFinishedHelper(), html,
64                 "text/html", false);
65
66         // Ensure the JS interface object is there, and invoke the test method.
67         assertEquals("\"function\"", executeJavaScriptAndWaitForResult(
68                 awContents, mContentsClient, "typeof test.destroy"));
69         awContents.evaluateJavaScript("test.destroy()", null);
70
71         client2.getOnPageFinishedHelper().waitForCallback(
72                 client2.getOnPageFinishedHelper().getCallCount());
73     }
74 }