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.
6 package org.xwalk.core.xwview.test;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import org.chromium.base.test.util.Feature;
12 import org.xwalk.core.JavascriptInterface;
15 * Test suite for addJavascriptInterface().
17 public class AddJavascriptInterfaceTest extends XWalkViewTestBase {
18 final String mExpectedStr = "xwalk";
21 public void setUp() throws Exception {
24 setXWalkClient(new XWalkViewTestBase.TestXWalkClient());
27 class TestJavascriptInterface {
28 public String getTextWithoutAnnotation() {
33 public String getText() {
38 private void addJavascriptInterface() {
39 getInstrumentation().runOnMainSync(new Runnable() {
42 getXWalkView().addJavascriptInterface(new TestJavascriptInterface(),
48 private void raisesExceptionAndSetTitle(String script) throws Throwable {
49 executeJavaScriptAndWaitForResult("try { var title = " +
51 " document.title = title;" +
52 "} catch (exception) {" +
53 " document.title = \"xwalk\";" +
58 @Feature({"AddJavascriptInterface"})
59 public void testAddJavascriptInterface() throws Throwable {
60 final String name = "add_js_interface.html";
62 addJavascriptInterface();
64 assertEquals(mExpectedStr, getTitleOnUiThread());
68 @Feature({"AddJavascriptInterface"})
69 public void testAddJavascriptInterfaceWithAnnotation() throws Throwable {
70 final String name = "index.html";
71 final String xwalkStr = "\"xwalk\"";
74 addJavascriptInterface();
77 result = executeJavaScriptAndWaitForResult("testInterface.getText()");
78 assertEquals(xwalkStr, result);
80 raisesExceptionAndSetTitle("testInterface.getTextWithoutAnnotation()");
82 String title = getTitleOnUiThread();
83 assertEquals(mExpectedStr, title);