blob: a9ab3b74f4a597fcc92b671635c5133c2a4da800 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Common functionality for testing the WebView's Java Bridge.
*/
package com.android.webviewtests;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import junit.framework.Assert;
public class JavaBridgeTestBase extends ActivityInstrumentationTestCase2<WebViewStubActivity> {
protected class TestWebViewClient extends WebViewClient {
private boolean mIsPageFinished;
@Override
public synchronized void onPageFinished(WebView webView, String url) {
mIsPageFinished = true;
notify();
}
public synchronized void waitForOnPageFinished() throws RuntimeException {
while (!mIsPageFinished) {
try {
wait(5000);
} catch (Exception e) {
continue;
}
if (!mIsPageFinished) {
throw new RuntimeException("Timed out waiting for onPageFinished()");
}
}
mIsPageFinished = false;
}
}
protected class Controller {
private boolean mIsResultReady;
protected synchronized void notifyResultIsReady() {
mIsResultReady = true;
notify();
}
protected synchronized void waitForResult() {
while (!mIsResultReady) {
try {
wait(5000);
} catch (Exception e) {
continue;
}
if (!mIsResultReady) {
Assert.fail("Wait timed out");
}
}
mIsResultReady = false;
}
}
protected TestWebViewClient mWebViewClient;
public JavaBridgeTestBase() {
super(WebViewStubActivity.class);
}
// Sets up the WebView and injects the supplied object. Intended to be called from setUp().
protected void setUpWebView(final Object object, final String name) throws Exception {
mWebViewClient = new TestWebViewClient();
// This starts the activity, so must be called on the test thread.
final WebViewStubActivity activity = getActivity();
// On the UI thread, load an empty page and wait for it to finish
// loading so that the Java object is injected.
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
WebView webView = activity.getWebView();
webView.addJavascriptInterface(object, name);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(mWebViewClient);
webView.loadData("<html><head></head><body></body></html>", "text/html", null);
}
});
mWebViewClient.waitForOnPageFinished();
} catch (Throwable e) {
throw new RuntimeException("Failed to set up WebView: " + Log.getStackTraceString(e));
}
}
protected void executeJavaScript(final String script) throws Throwable {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
getWebView().loadUrl("javascript:" + script);
}
});
}
protected WebView getWebView() {
return getActivity().getWebView();
}
}
|