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 * Common functionality for testing the WebView's Java Bridge.
19 */
20
21package com.android.webviewtests;
22
23import android.test.ActivityInstrumentationTestCase2;
24import android.util.Log;
25import android.webkit.WebView;
26import android.webkit.WebViewClient;
27
28import junit.framework.Assert;
29
30public class JavaBridgeTestBase extends ActivityInstrumentationTestCase2<WebViewStubActivity> {
31    protected class TestWebViewClient extends WebViewClient {
32        private boolean mIsPageFinished;
33        @Override
34        public synchronized void onPageFinished(WebView webView, String url) {
35            mIsPageFinished = true;
36            notify();
37        }
38        public synchronized void waitForOnPageFinished() throws RuntimeException {
39            while (!mIsPageFinished) {
40                try {
41                    wait(5000);
42                } catch (Exception e) {
43                    continue;
44                }
45                if (!mIsPageFinished) {
46                    throw new RuntimeException("Timed out waiting for onPageFinished()");
47                }
48            }
49            mIsPageFinished = false;
50        }
51    }
52
53    protected class Controller {
54        private boolean mIsResultReady;
55
56        protected synchronized void notifyResultIsReady() {
57            mIsResultReady = true;
58            notify();
59        }
60        protected synchronized void waitForResult() {
61            while (!mIsResultReady) {
62                try {
63                    wait(5000);
64                } catch (Exception e) {
65                    continue;
66                }
67                if (!mIsResultReady) {
68                    Assert.fail("Wait timed out");
69                }
70            }
71            mIsResultReady = false;
72        }
73    }
74
75    protected TestWebViewClient mWebViewClient;
76
77    public JavaBridgeTestBase() {
78        super(WebViewStubActivity.class);
79    }
80
81    // Sets up the WebView and injects the supplied object. Intended to be called from setUp().
82    protected void setUpWebView(final Object object, final String name) throws Exception {
83        mWebViewClient = new TestWebViewClient();
84        // This starts the activity, so must be called on the test thread.
85        final WebViewStubActivity activity = getActivity();
86        // On the UI thread, load an empty page and wait for it to finish
87        // loading so that the Java object is injected.
88        try {
89            runTestOnUiThread(new Runnable() {
90                @Override
91                public void run() {
92                    WebView webView = activity.getWebView();
93                    webView.addJavascriptInterface(object, name);
94                    webView.getSettings().setJavaScriptEnabled(true);
95                    webView.setWebViewClient(mWebViewClient);
96                    webView.loadData("<!DOCTYPE html><title></title>", "text/html", null);
97                }
98            });
99            mWebViewClient.waitForOnPageFinished();
100        } catch (Throwable e) {
101            throw new RuntimeException("Failed to set up WebView: " + Log.getStackTraceString(e));
102        }
103    }
104
105    protected void executeJavaScript(final String script) throws Throwable {
106        runTestOnUiThread(new Runnable() {
107            @Override
108            public void run() {
109                // When a JavaScript URL is executed, if the value of the last
110                // expression evaluated is not 'undefined', this value is
111                // converted to a string and used as the new document for the
112                // frame. We don't want this behaviour, so wrap the script in
113                // an anonymous function.
114                getWebView().loadUrl("javascript:(function() { " + script + " })()");
115            }
116        });
117    }
118
119    protected WebView getWebView() {
120        return getActivity().getWebView();
121    }
122}
123