1 package org.robolectric.shadows; 2 3 import static org.assertj.core.api.Assertions.assertThat; 4 5 import android.net.http.HttpResponseCache; 6 import java.io.File; 7 import org.junit.After; 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 import org.robolectric.RobolectricTestRunner; 12 13 @RunWith(RobolectricTestRunner.class) 14 public class ShadowHttpResponseCacheTest { 15 @Before 16 public void setUp() { 17 // If someone else installed a cache, clear it. 18 ShadowHttpResponseCache.installed = null; 19 } 20 21 @After 22 public void tearDown() { 23 // Ensure we don't leak an installed cache from a test. 24 ShadowHttpResponseCache.installed = null; 25 } 26 27 @Test 28 public void installedCacheIsReturned() throws Exception { 29 assertThat(HttpResponseCache.getInstalled()).isNull(); 30 HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); 31 HttpResponseCache installed = HttpResponseCache.getInstalled(); 32 assertThat(installed).isSameAs(cache); 33 assertThat(installed.maxSize()).isEqualTo(42); 34 } 35 36 @Test 37 public void countsStartAtZero() throws Exception { 38 HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); 39 assertThat(cache.getHitCount()).isZero(); 40 assertThat(cache.getNetworkCount()).isZero(); 41 assertThat(cache.getRequestCount()).isZero(); 42 } 43 44 @Test 45 public void deleteRemovesReference() throws Exception { 46 HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); 47 cache.delete(); 48 assertThat(HttpResponseCache.getInstalled()).isNull(); 49 } 50 51 @Test 52 public void closeRemovesReference() throws Exception { 53 HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); 54 cache.close(); 55 assertThat(HttpResponseCache.getInstalled()).isNull(); 56 } 57 } 58