@Test public void networkPolicyNoCache() throws Exception { MockResponse response = new MockResponse().setHeader("Cache-Control", "max-age=31536000").setBody("Hi"); server.enqueue(response); Downloader.Response response1 = downloader.load(uri, 0); assertThat((Boolean) cached.get(response1)).isFalse(); // Exhaust input stream to ensure response is cached. Okio.buffer(Okio.source(response1.getInputStream())).readByteArray(); // Enqueue the same response again but this time use NetworkPolicy.NO_CACHE. server.enqueue(response); Downloader.Response response2 = downloader.load(uri, NO_CACHE); // Response should not be coming from cache even if it was cached previously. assertThat((Boolean) cached.get(response2)).isFalse(); }
/** * 构建Picasso Downloader * * @param context * @param downloader */ public static void build(Context context, Downloader downloader) { Picasso picasso = new Picasso.Builder(context) .downloader(downloader) .build(); Picasso.setSingletonInstance(picasso); }
@Before public void setUp() throws Exception { downloader = new OkHttp3Downloader(temporaryFolder.getRoot()); uri = Uri.parse(server.url("/").toString()); cached = Downloader.Response.class.getDeclaredField("cached"); cached.setAccessible(true); }
@Test public void cachedResponse() throws Exception { server.enqueue(new MockResponse() .setHeader("Cache-Control", "max-age=31536000") .setBody("Hi")); Downloader.Response response1 = downloader.load(uri, 0); assertThat((Boolean) cached.get(response1)).isFalse(); // Exhaust input stream to ensure response is cached. Okio.buffer(Okio.source(response1.getInputStream())).readByteArray(); Downloader.Response response2 = downloader.load(uri, OFFLINE); assertThat((Boolean) cached.get(response2)).isTrue(); }
@Test public void offlineStaleResponse() throws Exception { server.enqueue(new MockResponse() .setHeader("Cache-Control", "max-age=1") .setHeader("Expires", "Mon, 29 Dec 2014 21:44:55 GMT") .setBody("Hi")); Downloader.Response response1 = downloader.load(uri, 0); assertThat((Boolean) cached.get(response1)).isFalse(); // Exhaust input stream to ensure response is cached. Okio.buffer(Okio.source(response1.getInputStream())).readByteArray(); Downloader.Response response2 = downloader.load(uri, OFFLINE); assertThat((Boolean) cached.get(response2)).isTrue(); }
@Test public void throwsResponseException() throws Exception { server.enqueue(new MockResponse().setStatus("HTTP/1.1 401 Not Authorized")); try { downloader.load(uri, 0); fail("Expected ResponseException."); } catch (Downloader.ResponseException e) { assertThat(e).hasMessage("401 Not Authorized"); } }
@Override protected byte[] doInBackground(Void... params) { try { // Grab an input stream to the image OkHttpDownloader downloader = new OkHttpDownloader(getContext()); Downloader.Response response = downloader.load(Uri.parse(url), 0); // Read the image into a byte array return Okio.buffer(Okio.source(response.getInputStream())).readByteArray(); } catch (Exception e) { Log.d(ImageFragment.class.getSimpleName(), "Error fetching image", e); return null; } }
public static Downloader createCacheDownloader(Context context) { try { Class.forName("com.squareup.okhttp.OkHttpClient"); File cacheDir = createDefaultCacheDir(context, CACHE_PATH); long cacheSize = calculateDiskCacheSize(cacheDir); return new OkHttpDownloader(cacheDir, cacheSize); } catch (ClassNotFoundException e) { return new UrlConnectionDownloader(context); } }
private static Downloader createDefaultDownloader(Context context) { try { Class.forName("com.squareup.okhttp.OkHttpClient"); return OkHttpLoaderCreator.create(context); } catch (ClassNotFoundException e) { return new UrlConnectionDownloader(context); } }
@Override public void onError() { if (SadPandaApp.getLastException() instanceof Downloader.ResponseException) { mFailureText.setText(R.string.image_not_found); } else { mFailureText.setText(R.string.image_loading_failed); } mFailureText.setVisibility(View.VISIBLE); mLoadingBar.setVisibility(View.GONE); }
@Test public void readsContentLengthHeader() throws Exception { server.enqueue(new MockResponse().addHeader("Content-Length", 1024)); Downloader.Response response = downloader.load(uri, 0); assertThat(response.getContentLength()).isEqualTo(1024); }
public ActivityModule(Activity activity, Downloader okHttpDownloader) { this.activity = activity; this.okHttpDownloader = okHttpDownloader; }
public TracedPicassoDownloader(Tracer tracer, Downloader downloader) { this.tracer = tracer; this.downloader = downloader; }
static Downloader create(Context context) { return new OkHttpDownloader(context); }
@Before public void setup() { tracer = mock(Tracer.class); downloader = mock(Downloader.class); tested = new TracedPicassoDownloader(tracer, downloader); }