private void logDelivery(BitmapHunter bitmapHunter) { Picasso picasso = bitmapHunter.getPicasso(); if (picasso.loggingEnabled) { log(OWNER_DISPATCHER, VERB_DELIVERED, Utils.getLogIdsForHunter(bitmapHunter)); } }
static Bitmap decodeResource(Context context, Request request) throws IOException { if (SDK_INT >= 28) { return decodeResourceP(context, request); } Resources resources = Utils.getResources(context, request); int id = Utils.getResourceId(resources, request); return decodeResourcePreP(resources, id, request); }
@Override public int compareTo(PicassoFutureTask other) { Picasso.Priority p1 = hunter.getPriority(); Picasso.Priority p2 = other.hunter.getPriority(); // High-priority requests are "lesser" so they are sorted to the front. // Equal priorities are sorted by sequence number to provide FIFO ordering. return (p1 == p2 ? hunter.sequence - other.hunter.sequence : p2.ordinal() - p1.ordinal()); } }
@Test public void performCompleteSetsResultInCache() { Request data = new Request.Builder(URI_1).build(); Action action = noopAction(data); BitmapHunter hunter = new BitmapHunter(mockPicasso(), dispatcher, cache, stats, action, EMPTY_REQUEST_HANDLER); hunter.result = new RequestHandler.Result(bitmap1, MEMORY); dispatcher.performComplete(hunter); assertThat(cache.get(hunter.getKey())).isSameAs(hunter.result.getBitmap()); }
@Test public void performCompleteWithNoStoreMemoryPolicy() { Request data = new Request.Builder(URI_1).memoryPolicy(MemoryPolicy.NO_STORE).build(); Action action = noopAction(data); BitmapHunter hunter = new BitmapHunter(mockPicasso(), dispatcher, cache, stats, action, EMPTY_REQUEST_HANDLER); hunter.result = new RequestHandler.Result(bitmap1, MEMORY); dispatcher.performComplete(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(cache.size()).isEqualTo(0); }
@Test public void performCompleteCleansUpAndPostsToMain() { Request data = new Request.Builder(URI_1).build(); Action action = noopAction(data); BitmapHunter hunter = new BitmapHunter(mockPicasso(), dispatcher, cache, stats, action, EMPTY_REQUEST_HANDLER); hunter.result = new RequestHandler.Result(bitmap1, MEMORY); dispatcher.performComplete(hunter); assertThat(dispatcher.hunterMap).isEmpty(); // TODO verify post to main thread. }
private void performRemoteViewInto(Request request, RemoteViewsAction action) { if (shouldReadFromMemoryCache(request.memoryPolicy)) { Bitmap bitmap = picasso.quickMemoryCacheCheck(action.request.key); if (bitmap != null) { action.complete(new RequestHandler.Result(bitmap, MEMORY)); return; } } if (placeholderResId != 0) { action.setImageResource(placeholderResId); } picasso.enqueueAndSubmit(action); } }
void performComplete(BitmapHunter hunter) { if (shouldWriteToMemoryCache(hunter.data.memoryPolicy)) { RequestHandler.Result result = hunter.getResult(); if (result != null) { Bitmap bitmap = result.getBitmap(); if (bitmap != null) { cache.set(hunter.getKey(), bitmap); } } } hunterMap.remove(hunter.getKey()); deliver(hunter); }
static Action mockAction(String key, Uri uri, Object target, int resourceId, Priority priority, String tag) { Request.Builder builder = new Request.Builder(uri, resourceId, DEFAULT_CONFIG).stableKey(key); if (priority != null) { builder.priority(priority); } Request request = builder.build(); return mockAction(request, target, tag); }
@Test public void intoTargetWithNullUriAndResourceIdSkipsAndCancels() { BitmapTarget target = mockTarget(); Drawable placeHolderDrawable = mock(Drawable.class); new RequestCreator(picasso, null, 0).placeholder(placeHolderDrawable).into(target); verify(picasso).cancelRequest(target); verify(target).onPrepareLoad(placeHolderDrawable); verifyNoMoreInteractions(picasso); }
@Test public void invokesSuccessIfTargetIsNotNull() { Bitmap bitmap = makeBitmap(); BitmapTarget target = mockTarget(); BitmapTargetAction request = new BitmapTargetAction(mock(Picasso.class), target, null, null, 0); request.complete(new RequestHandler.Result(bitmap, MEMORY)); verify(target).onBitmapLoaded(bitmap, MEMORY); }
@Test public void completeSkipsIfNoActions() { BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap, MEMORY)); picasso.complete(hunter); verify(hunter).getAction(); verify(hunter).getActions(); verifyNoMoreInteractions(hunter); }
@Test public void responseExceptionDispatchFailed() { Action action = mockAction(URI_KEY_1, URI_1); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, null, new NetworkRequestHandler.ResponseException(504, 0)); hunter.run(); verify(dispatcher).dispatchFailed(hunter); }
@Override public void load(@NonNull Picasso picasso, @NonNull Request request, @NonNull Callback callback) { Bitmap defaultResult = makeBitmap(); RequestHandler.Result result = new RequestHandler.Result(defaultResult, MEMORY); callback.onSuccess(result); } };
@Test public void runWithResultDispatchComplete() { Action action = mockAction(URI_KEY_1, URI_1); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, bitmap); hunter.run(); verify(dispatcher).dispatchComplete(hunter); }
@Override public void error(Exception e) { if (errorResId != 0) { setImageResource(errorResId); } if (callback != null) { callback.onError(e); } }
private TestableRemoteViewsAction createAction(int errorResId, Callback callback) { return new TestableRemoteViewsAction(picasso, null, errorResId, new RemoteViewsTarget(remoteViews, 1), callback); }
@Override public Future<?> submit(Runnable task) { PicassoFutureTask ftask = new PicassoFutureTask((BitmapHunter) task); execute(ftask); return ftask; }
@Test public void nullDecodeResponseIsError() { Action action = mockAction(URI_KEY_1, URI_1); BitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, null); hunter.run(); verify(dispatcher).dispatchFailed(hunter); }