1 package org.robolectric.fakes; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.webkit.WebSettings; 6 import androidx.test.ext.junit.runners.AndroidJUnit4; 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 import org.robolectric.annotation.internal.DoNotInstrument; 10 11 @DoNotInstrument 12 @RunWith(AndroidJUnit4.class) 13 public class RoboWebSettingsTest { 14 private final RoboWebSettings webSettings = new RoboWebSettings(); 15 private static final boolean[] TRUE_AND_FALSE = {true, false}; 16 17 @Test testDefaults()18 public void testDefaults() { 19 assertThat(webSettings.getAllowContentAccess()).isTrue(); 20 assertThat(webSettings.getAllowFileAccess()).isTrue(); 21 assertThat(webSettings.getAppCacheEnabled()).isFalse(); 22 assertThat(webSettings.getBlockNetworkImage()).isFalse(); 23 assertThat(webSettings.getBlockNetworkLoads()).isFalse(); 24 assertThat(webSettings.getBuiltInZoomControls()).isTrue(); 25 assertThat(webSettings.getDatabaseEnabled()).isFalse(); 26 assertThat(webSettings.getDomStorageEnabled()).isFalse(); 27 assertThat(webSettings.getGeolocationEnabled()).isFalse(); 28 assertThat(webSettings.getJavaScriptEnabled()).isFalse(); 29 assertThat(webSettings.getLightTouchEnabled()).isFalse(); 30 assertThat(webSettings.getLoadWithOverviewMode()).isFalse(); 31 assertThat(webSettings.getMediaPlaybackRequiresUserGesture()).isTrue(); 32 assertThat(webSettings.getPluginState()).isEqualTo(WebSettings.PluginState.OFF); 33 assertThat(webSettings.getSaveFormData()).isFalse(); 34 assertThat(webSettings.getTextZoom()).isEqualTo(100); 35 assertThat(webSettings.getDefaultTextEncodingName()).isEqualTo("UTF-8"); 36 assertThat(webSettings.getDefaultFontSize()).isEqualTo(16); 37 38 // deprecated methods 39 assertThat(webSettings.getPluginsEnabled()).isFalse(); 40 41 // obsoleted methods 42 assertThat(webSettings.getNeedInitialFocus()).isFalse(); 43 assertThat(webSettings.getSupportMultipleWindows()).isFalse(); 44 assertThat(webSettings.getSupportZoom()).isTrue(); 45 } 46 47 @Test testAllowContentAccess()48 public void testAllowContentAccess() { 49 for (boolean value : TRUE_AND_FALSE) { 50 webSettings.setAllowContentAccess(value); 51 assertThat(webSettings.getAllowContentAccess()).isEqualTo(value); 52 } 53 } 54 55 @Test testAllowFileAccess()56 public void testAllowFileAccess() { 57 for (boolean value : TRUE_AND_FALSE) { 58 webSettings.setAllowFileAccess(value); 59 assertThat(webSettings.getAllowFileAccess()).isEqualTo(value); 60 } 61 } 62 63 @Test testAllowFileAccessFromFileURLs()64 public void testAllowFileAccessFromFileURLs() { 65 for (boolean value : TRUE_AND_FALSE) { 66 webSettings.setAllowFileAccessFromFileURLs(value); 67 assertThat(webSettings.getAllowFileAccessFromFileURLs()).isEqualTo(value); 68 } 69 } 70 71 @Test testAllowUniversalAccessFromFileURLs()72 public void testAllowUniversalAccessFromFileURLs() { 73 for (boolean value : TRUE_AND_FALSE) { 74 webSettings.setAllowUniversalAccessFromFileURLs(value); 75 assertThat(webSettings.getAllowUniversalAccessFromFileURLs()).isEqualTo(value); 76 } 77 } 78 79 @Test testBlockNetworkImage()80 public void testBlockNetworkImage() { 81 for (boolean value : TRUE_AND_FALSE) { 82 webSettings.setBlockNetworkImage(value); 83 assertThat(webSettings.getBlockNetworkImage()).isEqualTo(value); 84 } 85 } 86 87 @Test testBlockNetworkLoads()88 public void testBlockNetworkLoads() { 89 for (boolean value : TRUE_AND_FALSE) { 90 webSettings.setBlockNetworkLoads(value); 91 assertThat(webSettings.getBlockNetworkLoads()).isEqualTo(value); 92 } 93 } 94 95 @Test testBuiltInZoomControls()96 public void testBuiltInZoomControls() { 97 for (boolean value : TRUE_AND_FALSE) { 98 webSettings.setBuiltInZoomControls(value); 99 assertThat(webSettings.getBuiltInZoomControls()).isEqualTo(value); 100 } 101 } 102 103 @Test testDatabaseEnabled()104 public void testDatabaseEnabled() { 105 for (boolean value : TRUE_AND_FALSE) { 106 webSettings.setDatabaseEnabled(value); 107 assertThat(webSettings.getDatabaseEnabled()).isEqualTo(value); 108 } 109 } 110 111 @Test testDomStorageEnabled()112 public void testDomStorageEnabled() { 113 for (boolean value : TRUE_AND_FALSE) { 114 webSettings.setDomStorageEnabled(value); 115 assertThat(webSettings.getDomStorageEnabled()).isEqualTo(value); 116 } 117 } 118 119 @Test testJavaScriptEnabled()120 public void testJavaScriptEnabled() { 121 for (boolean value : TRUE_AND_FALSE) { 122 webSettings.setJavaScriptEnabled(value); 123 assertThat(webSettings.getJavaScriptEnabled()).isEqualTo(value); 124 } 125 } 126 127 @Test testLightTouchEnabled()128 public void testLightTouchEnabled() { 129 for (boolean value : TRUE_AND_FALSE) { 130 webSettings.setLightTouchEnabled(value); 131 assertThat(webSettings.getLightTouchEnabled()).isEqualTo(value); 132 } 133 } 134 135 @Test testLoadWithOverviewMode()136 public void testLoadWithOverviewMode() { 137 for (boolean value : TRUE_AND_FALSE) { 138 webSettings.setLoadWithOverviewMode(value); 139 assertThat(webSettings.getLoadWithOverviewMode()).isEqualTo(value); 140 } 141 } 142 143 @Test testMediaPlaybackRequiresUserGesture()144 public void testMediaPlaybackRequiresUserGesture() throws Exception { 145 for (boolean value : TRUE_AND_FALSE) { 146 webSettings.setMediaPlaybackRequiresUserGesture(value); 147 assertThat(webSettings.getMediaPlaybackRequiresUserGesture()).isEqualTo(value); 148 } 149 } 150 151 @Test testNeedInitialFocus()152 public void testNeedInitialFocus() { 153 for (boolean value : TRUE_AND_FALSE) { 154 webSettings.setNeedInitialFocus(value); 155 assertThat(webSettings.getNeedInitialFocus()).isEqualTo(value); 156 } 157 } 158 159 @Test testPluginsEnabled()160 public void testPluginsEnabled() { 161 for (boolean value : TRUE_AND_FALSE) { 162 webSettings.setPluginsEnabled(value); 163 assertThat(webSettings.getPluginsEnabled()).isEqualTo(value); 164 } 165 } 166 167 @Test testPluginState()168 public void testPluginState() { 169 170 for (WebSettings.PluginState state : WebSettings.PluginState.values()) { 171 webSettings.setPluginState(state); 172 assertThat(webSettings.getPluginState()).isEqualTo(state); 173 } 174 } 175 176 @Test testSupportMultipleWindows()177 public void testSupportMultipleWindows() { 178 for (boolean value : TRUE_AND_FALSE) { 179 webSettings.setSupportMultipleWindows(value); 180 assertThat(webSettings.getSupportMultipleWindows()).isEqualTo(value); 181 } 182 } 183 184 @Test testSupportZoom()185 public void testSupportZoom() { 186 for (boolean value : TRUE_AND_FALSE) { 187 webSettings.setSupportZoom(value); 188 assertThat(webSettings.getSupportZoom()).isEqualTo(value); 189 } 190 } 191 192 @Test testSetCacheMode()193 public void testSetCacheMode() throws Exception { 194 webSettings.setCacheMode(7); 195 assertThat(webSettings.getCacheMode()).isEqualTo(7); 196 } 197 198 @Test testSetUseWideViewPort()199 public void testSetUseWideViewPort() throws Exception { 200 for (boolean value : TRUE_AND_FALSE) { 201 webSettings.setUseWideViewPort(value); 202 assertThat(webSettings.getUseWideViewPort()).isEqualTo(value); 203 } 204 } 205 206 @Test testSetAppCacheEnabled()207 public void testSetAppCacheEnabled() throws Exception { 208 for (boolean value : TRUE_AND_FALSE) { 209 webSettings.setAppCacheEnabled(value); 210 assertThat(webSettings.getAppCacheEnabled()).isEqualTo(value); 211 } 212 } 213 214 @Test testSetGeolocationEnabled()215 public void testSetGeolocationEnabled() throws Exception { 216 for (boolean value : TRUE_AND_FALSE) { 217 webSettings.setGeolocationEnabled(value); 218 assertThat(webSettings.getGeolocationEnabled()).isEqualTo(value); 219 } 220 } 221 222 @Test testSetSaveFormData()223 public void testSetSaveFormData() throws Exception { 224 for (boolean value : TRUE_AND_FALSE) { 225 webSettings.setSaveFormData(value); 226 assertThat(webSettings.getSaveFormData()).isEqualTo(value); 227 } 228 } 229 230 @Test testSetDatabasePath()231 public void testSetDatabasePath() throws Exception { 232 webSettings.setDatabasePath("new_path"); 233 assertThat(webSettings.getDatabasePath()).isEqualTo("new_path"); 234 } 235 236 @Test testSetRenderPriority()237 public void testSetRenderPriority() throws Exception { 238 webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); 239 assertThat(webSettings.getRenderPriority()).isEqualTo(WebSettings.RenderPriority.HIGH); 240 } 241 242 @Test testSetAppCachePath()243 public void testSetAppCachePath() throws Exception { 244 webSettings.setAppCachePath("new_path"); 245 assertThat(webSettings.getAppCachePath()).isEqualTo("new_path"); 246 } 247 248 @Test testSetAppCacheMaxSize()249 public void testSetAppCacheMaxSize() throws Exception { 250 webSettings.setAppCacheMaxSize(100); 251 assertThat(webSettings.getAppCacheMaxSize()).isEqualTo(100); 252 } 253 254 @Test testSetGeolocationDatabasePath()255 public void testSetGeolocationDatabasePath() throws Exception { 256 webSettings.setGeolocationDatabasePath("new_path"); 257 assertThat(webSettings.getGeolocationDatabasePath()).isEqualTo("new_path"); 258 } 259 260 @Test testSetJavascriptCanOpenWindowsAutomaticallyIsTrue()261 public void testSetJavascriptCanOpenWindowsAutomaticallyIsTrue() throws Exception { 262 webSettings.setJavaScriptCanOpenWindowsAutomatically(true); 263 assertThat(webSettings.getJavaScriptCanOpenWindowsAutomatically()).isTrue(); 264 } 265 266 @Test testSetJavascriptCanOpenWindowsAutomaticallyIsFalse()267 public void testSetJavascriptCanOpenWindowsAutomaticallyIsFalse() throws Exception { 268 webSettings.setJavaScriptCanOpenWindowsAutomatically(false); 269 assertThat(webSettings.getJavaScriptCanOpenWindowsAutomatically()).isFalse(); 270 } 271 272 @Test testSetTextZoom()273 public void testSetTextZoom() throws Exception { 274 webSettings.setTextZoom(50); 275 assertThat(webSettings.getTextZoom()).isEqualTo(50); 276 } 277 278 @Test setDefaultTextEncodingName_shouldGetSetValue()279 public void setDefaultTextEncodingName_shouldGetSetValue() { 280 webSettings.setDefaultTextEncodingName("UTF-16"); 281 assertThat(webSettings.getDefaultTextEncodingName()).isEqualTo("UTF-16"); 282 } 283 284 @Test setDefaultFontSize_shouldGetSetValues()285 public void setDefaultFontSize_shouldGetSetValues() { 286 webSettings.setDefaultFontSize(2); 287 assertThat(webSettings.getDefaultFontSize()).isEqualTo(2); 288 } 289 } 290