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 39 public void __constructor__(Context context, AttributeSet attributeSet) { 40 super.__constructor__(context, attributeSet); 41 } 42 43 @Implementation 44 public void loadUrl(String url) { 45 lastUrl = url; 46 } 47 48 @Implementation 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 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 */ 63 public String getLastLoadedUrl() { 64 return lastUrl; 65 } 66 67 @Implementation 68 public WebSettings getSettings() { 69 return webSettings; 70 } 71 72 @Implementation 73 public void setWebViewClient(WebViewClient client) { 74 webViewClient = client; 75 } 76 77 @Implementation 78 public void setWebChromeClient(WebChromeClient client) { 79 webChromeClient = client; 80 } 81 82 public WebViewClient getWebViewClient() { 83 return webViewClient; 84 } 85 86 @Implementation 87 public void setPictureListener(WebView.PictureListener listener) { 88 pictureListener = listener; 89 } 90 91 public WebView.PictureListener getPictureListener() { 92 return pictureListener; 93 } 94 95 @Implementation 96 public void addJavascriptInterface(Object obj, String interfaceName) { 97 javascriptInterfaces.put(interfaceName, obj); 98 } 99 100 public Object getJavascriptInterface(String interfaceName) { 101 return javascriptInterfaces.get(interfaceName); 102 } 103 104 @Implementation 105 public void clearCache(boolean includeDiskFiles) { 106 clearCacheCalled = true; 107 clearCacheIncludeDiskFiles = includeDiskFiles; 108 } 109 110 public boolean wasClearCacheCalled() { 111 return clearCacheCalled; 112 } 113 114 public boolean didClearCacheIncludeDiskFiles() { 115 return clearCacheIncludeDiskFiles; 116 } 117 118 @Implementation 119 public void clearFormData() { 120 clearFormDataCalled = true; 121 } 122 123 public boolean wasClearFormDataCalled() { 124 return clearFormDataCalled; 125 } 126 127 @Implementation 128 public void clearHistory() { 129 clearHistoryCalled = true; 130 } 131 132 public boolean wasClearHistoryCalled() { 133 return clearHistoryCalled; 134 } 135 136 @Implementation 137 public void clearView() { 138 clearViewCalled = true; 139 } 140 141 public boolean wasClearViewCalled() { 142 return clearViewCalled; 143 } 144 145 @Implementation 146 public void destroy() { 147 destroyCalled = true; 148 } 149 150 public boolean wasDestroyCalled() { 151 return destroyCalled; 152 } 153 154 @Implementation 155 public void post(Runnable action) { 156 action.run(); 157 runFlag = true; 158 } 159 160 public boolean getRunFlag() { 161 return runFlag; 162 } 163 164 165 /** 166 * Non-Android accessor. 167 * 168 * @return webChromeClient 169 */ 170 public WebChromeClient getWebChromeClient() { 171 return webChromeClient; 172 } 173 174 @Implementation 175 public boolean canGoBack() { 176 return canGoBack; 177 } 178 179 @Implementation 180 public void goBack() { 181 goBackInvocations++; 182 } 183 184 185 /** 186 * Non-Android accessor. 187 * 188 * @return goBackInvocations the number of times {@code android.webkit.WebView#goBack()} 189 * was invoked 190 */ 191 public int getGoBackInvocations() { 192 return goBackInvocations; 193 } 194 195 /** 196 * Non-Android setter. 197 * 198 * Sets the value to return from {@code android.webkit.WebView#canGoBack()} 199 * 200 * @param canGoBack 201 */ 202 public void setCanGoBack(boolean canGoBack) { 203 this.canGoBack = canGoBack; 204 } 205 206 public LoadData getLastLoadData() { 207 return lastLoadData; 208 } 209 210 public LoadDataWithBaseURL getLastLoadDataWithBaseURL() { 211 return lastLoadDataWithBaseURL; 212 } 213 214 public class LoadDataWithBaseURL { 215 public final String baseUrl; 216 public final String data; 217 public final String mimeType; 218 public final String encoding; 219 public final String historyUrl; 220 221 public LoadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) { 222 this.baseUrl = baseUrl; 223 this.data = data; 224 this.mimeType = mimeType; 225 this.encoding = encoding; 226 this.historyUrl = historyUrl; 227 } 228 } 229 230 public class LoadData { 231 public final String data; 232 public final String mimeType; 233 public final String encoding; 234 235 public LoadData(String data, String mimeType, String encoding) { 236 this.data = data; 237 this.mimeType = mimeType; 238 this.encoding = encoding; 239 } 240 } 241 } 242