40d5c2389b8651243991884b2321caad5d7bab5c
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core / javatests / src / org / xwalk / core / xwview / test / AddJavascriptInterfaceTest.java
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2014 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 package org.xwalk.core.xwview.test;
7
8 import android.test.suitebuilder.annotation.SmallTest;
9
10 import org.chromium.base.test.util.Feature;
11
12 import org.xwalk.core.JavascriptInterface;
13
14 /**
15  * Test suite for addJavascriptInterface().
16  */
17 public class AddJavascriptInterfaceTest extends XWalkViewTestBase {
18     final String mExpectedStr = "xwalk";
19
20     @Override
21     public void setUp() throws Exception {
22         super.setUp();
23
24         setXWalkClient(new XWalkViewTestBase.TestXWalkClient());
25     }
26
27     class TestJavascriptInterface {
28         public String getTextWithoutAnnotation() {
29             return mExpectedStr;
30         }
31
32         @JavascriptInterface
33         public String getText() {
34             return mExpectedStr;
35         }
36     }
37
38     private void addJavascriptInterface() {
39         getInstrumentation().runOnMainSync(new Runnable() {
40             @Override
41             public void run() {
42                 getXWalkView().addJavascriptInterface(new TestJavascriptInterface(),
43                         "testInterface");
44             }
45         });
46     }
47
48     private void raisesExceptionAndSetTitle(String script) throws Throwable {
49         executeJavaScriptAndWaitForResult("try { var title = " +
50                                           script + ";" +
51                                           "  document.title = title;" +
52                                           "} catch (exception) {" +
53                                           "  document.title = \"xwalk\";" +
54                                           "}");
55     }
56
57     @SmallTest
58     @Feature({"AddJavascriptInterface"})
59     public void testAddJavascriptInterface() throws Throwable {
60         final String name = "add_js_interface.html";
61
62         addJavascriptInterface();
63         loadAssetFile(name);
64         assertEquals(mExpectedStr, getTitleOnUiThread());
65     }
66
67     @SmallTest
68     @Feature({"AddJavascriptInterface"})
69     public void testAddJavascriptInterfaceWithAnnotation() throws Throwable {
70         final String name = "index.html";
71         final String xwalkStr = "\"xwalk\"";
72         String result;
73
74         addJavascriptInterface();
75         loadAssetFile(name);
76
77         result = executeJavaScriptAndWaitForResult("testInterface.getText()");
78         assertEquals(xwalkStr, result);
79
80         raisesExceptionAndSetTitle("testInterface.getTextWithoutAnnotation()");
81
82         String title = getTitleOnUiThread();
83         assertEquals(mExpectedStr, title);
84     }
85 }