1 package com.xtremelabs.robolectric.shadows; 2 3 import android.graphics.Picture; 4 import android.webkit.WebChromeClient; 5 import android.webkit.WebSettings; 6 import android.webkit.WebView; 7 import android.webkit.WebViewClient; 8 import com.xtremelabs.robolectric.Robolectric; 9 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 10 import org.junit.Before; 11 import org.junit.Test; 12 import org.junit.runner.RunWith; 13 14 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 15 import static junit.framework.Assert.assertEquals; 16 import static junit.framework.Assert.assertTrue; 17 import static org.hamcrest.CoreMatchers.*; 18 import static org.junit.Assert.assertFalse; 19 import static org.junit.Assert.assertThat; 20 21 @RunWith(WithTestDefaultsRunner.class) 22 public class WebViewTest { 23 24 private WebView webView; 25 private ShadowWebView shadowWebView; 26 27 @Before 28 public void setUp() throws Exception { 29 webView = new WebView(null); 30 shadowWebView = Robolectric.shadowOf(webView); 31 } 32 33 @Test 34 public void shouldRecordLastLoadedUrl() { 35 webView.loadUrl("http://example.com"); 36 assertThat(shadowOf(webView).getLastLoadedUrl(), equalTo("http://example.com")); 37 } 38 39 @Test 40 public void shouldRecordLastLoadedData() { 41 webView.loadData("<html><body><h1>Hi</h1></body></html>", "text/html", "utf-8"); 42 ShadowWebView.LoadData lastLoadData = shadowOf(webView).getLastLoadData(); 43 assertThat(lastLoadData.data, equalTo("<html><body><h1>Hi</h1></body></html>")); 44 assertThat(lastLoadData.mimeType, equalTo("text/html")); 45 assertThat(lastLoadData.encoding, equalTo("utf-8")); 46 } 47 48 @Test 49 public void shouldRecordLastLoadDataWithBaseURL() throws Exception { 50 webView.loadDataWithBaseURL("base/url", "<html><body><h1>Hi</h1></body></html>", "text/html", "utf-8", "history/url"); 51 ShadowWebView.LoadDataWithBaseURL lastLoadData = shadowOf(webView).getLastLoadDataWithBaseURL(); 52 assertThat(lastLoadData.baseUrl, equalTo("base/url")); 53 assertThat(lastLoadData.data, equalTo("<html><body><h1>Hi</h1></body></html>")); 54 assertThat(lastLoadData.mimeType, equalTo("text/html")); 55 assertThat(lastLoadData.encoding, equalTo("utf-8")); 56 assertThat(lastLoadData.historyUrl, equalTo("history/url")); 57 } 58 59 @Test 60 public void shouldReturnSettings() { 61 WebSettings webSettings = webView.getSettings(); 62 63 assertThat(webSettings, notNullValue()); 64 } 65 66 @Test 67 public void shouldRecordWebViewClient() { 68 WebViewClient webViewClient = new WebViewClient(); 69 70 assertThat(shadowWebView.getWebViewClient(), nullValue()); 71 webView.setWebViewClient(webViewClient); 72 assertThat(shadowWebView.getWebViewClient(), sameInstance(webViewClient)); 73 } 74 75 @Test 76 public void shouldRecordWebChromeClient() { 77 WebChromeClient webChromeClient = new WebChromeClient(); 78 assertThat(shadowWebView.getWebChromeClient(), nullValue()); 79 webView.setWebChromeClient(webChromeClient); 80 assertThat(shadowWebView.getWebChromeClient(), sameInstance(webChromeClient)); 81 } 82 83 @Test 84 public void shouldRecordPictureListener() { 85 WebView.PictureListener pictureListener = new WebView.PictureListener() { 86 @Override 87 public void onNewPicture(WebView view, Picture picture) { 88 ; 89 } 90 }; 91 92 assertThat(shadowWebView.getPictureListener(), nullValue()); 93 webView.setPictureListener(pictureListener); 94 assertThat(shadowWebView.getPictureListener(), sameInstance(pictureListener)); 95 } 96 97 @Test 98 public void shouldRecordJavascriptInteraces() { 99 String[] names = {"name1", "name2"}; 100 for (String name : names) { 101 Object obj = new Object(); 102 assertThat(shadowWebView.getJavascriptInterface(name), nullValue()); 103 webView.addJavascriptInterface(obj, name); 104 assertThat(shadowWebView.getJavascriptInterface(name), sameInstance(obj)); 105 } 106 } 107 108 @Test 109 public void shouldStartPostRun() { 110 Runnable testRun = new Runnable() { 111 public void run() { 112 //Do something... 113 return; 114 } 115 }; 116 assertThat(shadowWebView.getRunFlag(), equalTo(false)); 117 shadowWebView.post(testRun); 118 assertThat(shadowWebView.getRunFlag(), equalTo(true)); 119 } 120 121 @Test 122 public void shouldStoreCanGoBack() throws Exception { 123 shadowWebView.setCanGoBack(false); 124 assertFalse(webView.canGoBack()); 125 shadowWebView.setCanGoBack(true); 126 assertTrue(webView.canGoBack()); 127 } 128 129 @Test 130 public void shouldStoreTheNumberOfTimesGoBackWasCalled() throws Exception { 131 assertEquals(0, shadowWebView.getGoBackInvocations()); 132 webView.goBack(); 133 assertEquals(1, shadowWebView.getGoBackInvocations()); 134 webView.goBack(); 135 webView.goBack(); 136 assertEquals(3, shadowWebView.getGoBackInvocations()); 137 } 138 139 @Test 140 public void shouldRecordClearCacheWithoutDiskFiles() { 141 assertThat(shadowWebView.wasClearCacheCalled(), equalTo(false)); 142 143 webView.clearCache(false); 144 assertThat(shadowWebView.wasClearCacheCalled(), equalTo(true)); 145 assertThat(shadowWebView.didClearCacheIncludeDiskFiles(), equalTo(false)); 146 } 147 148 @Test 149 public void shouldRecordClearCacheWithDiskFiles() { 150 assertThat(shadowWebView.wasClearCacheCalled(), equalTo(false)); 151 152 webView.clearCache(true); 153 assertThat(shadowWebView.wasClearCacheCalled(), equalTo(true)); 154 assertThat(shadowWebView.didClearCacheIncludeDiskFiles(), equalTo(true)); 155 } 156 157 @Test 158 public void shouldRecordClearFormData() { 159 assertThat(shadowWebView.wasClearFormDataCalled(), equalTo(false)); 160 webView.clearFormData(); 161 assertThat(shadowWebView.wasClearFormDataCalled(), equalTo(true)); 162 } 163 164 @Test 165 public void shouldRecordClearHistory() { 166 assertThat(shadowWebView.wasClearHistoryCalled(), equalTo(false)); 167 webView.clearHistory(); 168 assertThat(shadowWebView.wasClearHistoryCalled(), equalTo(true)); 169 } 170 171 @Test 172 public void shouldRecordClearView() { 173 assertThat(shadowWebView.wasClearViewCalled(), equalTo(false)); 174 webView.clearView(); 175 assertThat(shadowWebView.wasClearViewCalled(), equalTo(true)); 176 } 177 178 @Test 179 public void shouldRecordDestroy() { 180 assertThat(shadowWebView.wasDestroyCalled(), equalTo(false)); 181 webView.destroy(); 182 assertThat(shadowWebView.wasDestroyCalled(), equalTo(true)); 183 } 184 } 185