/** * Specify the the thread factory for loading images in the background. * <p> * Note: Calling {@link #executor(ExecutorService)} overwrites this value. */ @NonNull public Builder threadFactory(@NonNull ThreadFactory threadFactory) { checkNotNull(threadFactory, "threadFactory == null"); service = new PicassoExecutorService(threadFactory); return this; }
@Override public Future<?> submit(Runnable task) { PicassoFutureTask ftask = new PicassoFutureTask((BitmapHunter) task); execute(ftask); return ftask; }
@Test public void performSubmitWithShutdownServiceIgnoresRequest() { when(service.isShutdown()).thenReturn(true); Action action = mockAction(URI_KEY_1, URI_1); dispatcher.performSubmit(action); assertThat(dispatcher.hunterMap).isEmpty(); verify(service, never()).submit(any(BitmapHunter.class)); }
@Test public void performSubmitWithNewRequestQueuesHunter() { Action action = mockAction(URI_KEY_1, URI_1); dispatcher.performSubmit(action); assertThat(dispatcher.hunterMap).hasSize(1); verify(service).submit(any(BitmapHunter.class)); }
@Test public void shutdownStopsService() { dispatcher.shutdown(); verify(service).shutdown(); }
@Test public void performNetworkStateChangeFlushesFailedHunters() { PicassoExecutorService service = mock(PicassoExecutorService.class); NetworkInfo info = mockNetworkInfo(true); Dispatcher dispatcher = createDispatcher(service); Action failedAction1 = mockAction(URI_KEY_1, URI_1); Action failedAction2 = mockAction(URI_KEY_2, URI_2); dispatcher.failedActions.put(URI_KEY_1, failedAction1); dispatcher.failedActions.put(URI_KEY_2, failedAction2); dispatcher.performNetworkStateChange(info); verify(service, times(2)).submit(any(BitmapHunter.class)); assertThat(dispatcher.failedActions).isEmpty(); }
@Test public void performPauseTagQueuesNewRequestDoesNotSubmit() { dispatcher.performPauseTag("tag"); Action action = mockAction(URI_KEY_1, URI_1, "tag"); dispatcher.performSubmit(action); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.pausedActions).hasSize(1); assertThat(dispatcher.pausedActions.containsValue(action)).isTrue(); verify(service, never()).submit(any(BitmapHunter.class)); }
@Test public void performRetrySkipIfServiceShutdown() { when(service.isShutdown()).thenReturn(true); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY)); dispatcher.performRetry(hunter); verify(service, never()).submit(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); }
/** Create the {@link Picasso} instance. */ @NonNull public Picasso build() { Context context = this.context; okhttp3.Cache unsharedCache = null; if (callFactory == null) { File cacheDir = createDefaultCacheDir(context); long maxSize = calculateDiskCacheSize(cacheDir); unsharedCache = new okhttp3.Cache(cacheDir, maxSize); callFactory = new OkHttpClient.Builder() .cache(unsharedCache) .build(); } if (cache == null) { cache = new PlatformLruCache(Utils.calculateMemoryCacheSize(context)); } if (service == null) { service = new PicassoExecutorService(new PicassoThreadFactory()); } Stats stats = new Stats(cache); Dispatcher dispatcher = new Dispatcher(context, service, HANDLER, cache, stats); return new Picasso(context, dispatcher, callFactory, unsharedCache, cache, listener, requestTransformers, requestHandlers, stats, defaultBitmapConfig, indicatorsEnabled, loggingEnabled); } }
@Test public void performRetryDoesNotMarkForReplayIfNotSupported() { NetworkInfo networkInfo = mockNetworkInfo(true); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), mockAction(URI_KEY_1, URI_1)); when(hunter.supportsReplay()).thenReturn(false); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(false); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); verify(service, never()).submit(hunter); }
@Test public void performSubmitWithShutdownAttachesRequest() { BitmapHunter hunter = mockHunter(URI_KEY_1 + KEY_SEPARATOR, new RequestHandler.Result(bitmap1, MEMORY)); dispatcher.hunterMap.put(URI_KEY_1 + KEY_SEPARATOR, hunter); when(service.isShutdown()).thenReturn(true); Action action = mockAction(URI_KEY_1, URI_1); dispatcher.performSubmit(action); assertThat(dispatcher.hunterMap).hasSize(1); verify(hunter).attach(action); verify(service, never()).submit(any(BitmapHunter.class)); }
static Picasso defaultPicasso(Context context, boolean hasRequestHandlers, boolean hasTransformers) { Picasso.Builder builder = new Picasso.Builder(context); if (hasRequestHandlers) { builder.addRequestHandler(NOOP_REQUEST_HANDLER); } if (hasTransformers) { builder.addRequestTransformer(NOOP_TRANSFORMER); } return builder .callFactory(UNUSED_CALL_FACTORY) .defaultBitmapConfig(DEFAULT_CONFIG) .executor(new PicassoExecutorService(new PicassoThreadFactory())) .indicatorsEnabled(true) .listener(NOOP_LISTENER) .loggingEnabled(true) .withCacheSize(DEFAULT_CACHE_SIZE) .build(); }
@Test public void performRetryMarksForReplayIfSupportedScansNetworkChangesAndShouldNotRetry() { NetworkInfo networkInfo = mockNetworkInfo(true); Action action = mockAction(URI_KEY_1, URI_1, mockTarget()); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), action); when(hunter.supportsReplay()).thenReturn(true); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(false); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).hasSize(1); verify(service, never()).submit(hunter); }
@Test public void performSubmitWithTwoDifferentRequestsQueuesHunters() { Action action1 = mockAction(URI_KEY_1, URI_1); Action action2 = mockAction(URI_KEY_2, URI_2); dispatcher.performSubmit(action1); dispatcher.performSubmit(action2); assertThat(dispatcher.hunterMap).hasSize(2); verify(service, times(2)).submit(any(BitmapHunter.class)); }
@Test public void performPauseTagDoesNotQueueUnrelatedRequest() { dispatcher.performPauseTag("tag"); Action action = mockAction(URI_KEY_1, URI_1, "anothertag"); dispatcher.performSubmit(action); assertThat(dispatcher.hunterMap).hasSize(1); assertThat(dispatcher.pausedActions).isEmpty(); verify(service).submit(any(BitmapHunter.class)); }
@Test public void performRetryMarksForReplayIfSupportsReplayAndShouldNotRetry() { Action action = mockAction(URI_KEY_1, URI_1, mockTarget()); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), action); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(false); when(hunter.supportsReplay()).thenReturn(true); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).hasSize(1); verify(service, never()).submit(hunter); }
@Test public void performRetryDoesNotMarkForReplayIfNoNetworkScanning() { BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), mockAction(URI_KEY_1, URI_1)); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(false); when(hunter.supportsReplay()).thenReturn(true); Dispatcher dispatcher = createDispatcher(false); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); verify(service, never()).submit(hunter); }
@Test public void performSubmitWithExistingRequestAttachesToHunter() { Action action1 = mockAction(URI_KEY_1, URI_1); Action action2 = mockAction(URI_KEY_1, URI_1); dispatcher.performSubmit(action1); dispatcher.performSubmit(action2); assertThat(dispatcher.hunterMap).hasSize(1); verify(service).submit(any(BitmapHunter.class)); }
@Test public void performRetryRetriesIfShouldRetry() { Action action = mockAction(URI_KEY_1, URI_1, mockTarget()); BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), action); when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(true); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); verify(service).submit(hunter); }
@Test public void performRetryRetriesIfNoNetworkScanning() { BitmapHunter hunter = mockHunter(URI_KEY_1, new RequestHandler.Result(bitmap1, MEMORY), mockAction(URI_KEY_1, URI_1)); when(hunter.shouldRetry(anyBoolean(), isNull(NetworkInfo.class))).thenReturn(true); Dispatcher dispatcher = createDispatcher(false); dispatcher.performRetry(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(dispatcher.failedActions).isEmpty(); verify(service).submit(hunter); }