1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.content.Context;
4 import android.util.AttributeSet;
5 import android.webkit.WebChromeClient;
6 import android.webkit.WebSettings;
7 import android.webkit.TestWebSettings;
8 import android.webkit.WebView;
9 import android.webkit.WebViewClient;
10 import com.xtremelabs.robolectric.Robolectric;
11 import com.xtremelabs.robolectric.internal.Implementation;
12 import com.xtremelabs.robolectric.internal.Implements;
13 
14 import java.util.HashMap;
15 
16 @SuppressWarnings({"UnusedDeclaration"})
17 @Implements(WebView.class)
18 public class ShadowWebView extends ShadowAbsoluteLayout {
19 
20     private String lastUrl;
21     private HashMap<String, Object> javascriptInterfaces = new HashMap<String, Object>();
22     private WebSettings webSettings = new TestWebSettings();
23     private WebViewClient webViewClient = null;
24     private boolean runFlag = false;
25     private boolean clearCacheCalled = false;
26     private boolean clearCacheIncludeDiskFiles = false;
27     private boolean clearFormDataCalled = false;
28     private boolean clearHistoryCalled = false;
29     private boolean clearViewCalled = false;
30     private boolean destroyCalled = false;
31     private WebChromeClient webChromeClient;
32     private boolean canGoBack;
33     private int goBackInvocations = 0;
34     private ShadowWebView.LoadData lastLoadData;
35     private LoadDataWithBaseURL lastLoadDataWithBaseURL;
36     private WebView.PictureListener pictureListener;
37 
38     @Override
__constructor__(Context context, AttributeSet attributeSet)39     public void __constructor__(Context context, AttributeSet attributeSet) {
40         super.__constructor__(context, attributeSet);
41     }
42 
43     @Implementation
loadUrl(String url)44     public void loadUrl(String url) {
45         lastUrl = url;
46     }
47 
48     @Implementation
loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)49     public void loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) {
50         lastLoadDataWithBaseURL = new LoadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
51     }
52 
53     @Implementation
loadData(String data, String mimeType, String encoding)54     public void loadData(String data, String mimeType, String encoding) {
55         lastLoadData = new LoadData(data, mimeType, encoding);
56     }
57 
58     /**
59      * Non-Android accessor.
60      *
61      * @return the last loaded url
62      */
getLastLoadedUrl()63     public String getLastLoadedUrl() {
64         return lastUrl;
65     }
66 
clearLastLoadedUrl()67     public void clearLastLoadedUrl() {
68         lastUrl = null;
69     }
70 
71     @Implementation
getSettings()72     public WebSettings getSettings() {
73         return webSettings;
74     }
75 
76     @Implementation
setWebViewClient(WebViewClient client)77     public void setWebViewClient(WebViewClient client) {
78         webViewClient = client;
79     }
80 
81     @Implementation
setWebChromeClient(WebChromeClient client)82     public void setWebChromeClient(WebChromeClient client) {
83         webChromeClient = client;
84     }
85 
getWebViewClient()86     public WebViewClient getWebViewClient() {
87         return webViewClient;
88     }
89 
90     @Implementation
setPictureListener(WebView.PictureListener listener)91     public void setPictureListener(WebView.PictureListener listener) {
92     	pictureListener = listener;
93     }
94 
getPictureListener()95     public WebView.PictureListener getPictureListener() {
96     	return pictureListener;
97     }
98 
99     @Implementation
addJavascriptInterface(Object obj, String interfaceName)100     public void addJavascriptInterface(Object obj, String interfaceName) {
101         javascriptInterfaces.put(interfaceName, obj);
102     }
103 
getJavascriptInterface(String interfaceName)104     public Object getJavascriptInterface(String interfaceName) {
105         return javascriptInterfaces.get(interfaceName);
106     }
107 
108     @Implementation
clearCache(boolean includeDiskFiles)109     public void clearCache(boolean includeDiskFiles) {
110         clearCacheCalled = true;
111         clearCacheIncludeDiskFiles = includeDiskFiles;
112     }
113 
wasClearCacheCalled()114     public boolean wasClearCacheCalled() {
115         return clearCacheCalled;
116     }
117 
didClearCacheIncludeDiskFiles()118     public boolean didClearCacheIncludeDiskFiles() {
119         return clearCacheIncludeDiskFiles;
120     }
121 
122     @Implementation
clearFormData()123     public void clearFormData() {
124         clearFormDataCalled = true;
125     }
126 
wasClearFormDataCalled()127     public boolean wasClearFormDataCalled() {
128         return clearFormDataCalled;
129     }
130 
131     @Implementation
clearHistory()132     public void clearHistory() {
133         clearHistoryCalled = true;
134     }
135 
wasClearHistoryCalled()136     public boolean wasClearHistoryCalled() {
137         return clearHistoryCalled;
138     }
139 
140     @Implementation
clearView()141     public void clearView() {
142         clearViewCalled = true;
143     }
144 
wasClearViewCalled()145     public boolean wasClearViewCalled() {
146         return clearViewCalled;
147     }
148 
149     @Implementation
destroy()150     public void destroy() {
151         destroyCalled = true;
152     }
153 
wasDestroyCalled()154     public boolean wasDestroyCalled() {
155         return destroyCalled;
156     }
157 
158     @Implementation
post(Runnable action)159     public void post(Runnable action) {
160         action.run();
161         runFlag = true;
162     }
163 
getRunFlag()164     public boolean getRunFlag() {
165         return runFlag;
166     }
167 
168 
169     /**
170      * Non-Android accessor.
171      *
172      * @return webChromeClient
173      */
getWebChromeClient()174     public WebChromeClient getWebChromeClient() {
175         return webChromeClient;
176     }
177 
178     @Implementation
canGoBack()179     public boolean canGoBack() {
180         return canGoBack;
181     }
182 
183     @Implementation
goBack()184     public void goBack() {
185         goBackInvocations++;
186     }
187 
188 
189     /**
190      * Non-Android accessor.
191      *
192      * @return goBackInvocations the number of times {@code android.webkit.WebView#goBack()}
193      * was invoked
194      */
getGoBackInvocations()195     public int getGoBackInvocations() {
196         return goBackInvocations;
197     }
198 
199     /**
200      * Non-Android setter.
201      *
202      * Sets the value to return from {@code android.webkit.WebView#canGoBack()}
203      *
204      * @param canGoBack
205      */
setCanGoBack(boolean canGoBack)206     public void setCanGoBack(boolean canGoBack) {
207         this.canGoBack = canGoBack;
208     }
209 
getLastLoadData()210     public LoadData getLastLoadData() {
211         return lastLoadData;
212     }
213 
getLastLoadDataWithBaseURL()214     public LoadDataWithBaseURL getLastLoadDataWithBaseURL() {
215         return lastLoadDataWithBaseURL;
216     }
217 
218     public class LoadDataWithBaseURL {
219         public final String baseUrl;
220         public final String data;
221         public final String mimeType;
222         public final String encoding;
223         public final String historyUrl;
224 
LoadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)225         public LoadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) {
226             this.baseUrl = baseUrl;
227             this.data = data;
228             this.mimeType = mimeType;
229             this.encoding = encoding;
230             this.historyUrl = historyUrl;
231         }
232     }
233 
234     public class LoadData {
235         public final String data;
236         public final String mimeType;
237         public final String encoding;
238 
LoadData(String data, String mimeType, String encoding)239         public LoadData(String data, String mimeType, String encoding) {
240             this.data = data;
241             this.mimeType = mimeType;
242             this.encoding = encoding;
243         }
244     }
245 }
246