@Override public void runTest() { getNullModelRequest().into(imageView); } });
@Override public void loadGifThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) { Glide.with(context) .asBitmap() // some .jpeg files are actually gif .load(uri) .apply(new RequestOptions() .override(resize, resize) .placeholder(placeholder) .centerCrop()) .into(imageView); }
@NonNull @CheckResult protected RequestBuilder<File> getDownloadOnlyRequest() { return new RequestBuilder<>(File.class, this).apply(DOWNLOAD_ONLY_OPTIONS); }
@Test public void testClone() { Target<Drawable> firstTarget = mock(Target.class); doAnswer(new CallSizeReady(100, 100)).when(firstTarget).getSize(isA(SizeReadyCallback.class)); Target<Drawable> secondTarget = mock(Target.class); doAnswer(new CallSizeReady(100, 100)).when(secondTarget).getSize(isA(SizeReadyCallback.class)); RequestBuilder<Drawable> firstRequest = requestManager .load(mockUri("content://first")); firstRequest.into(firstTarget); firstRequest.clone() .apply(placeholderOf(new ColorDrawable(Color.RED))) .into(secondTarget); verify(firstTarget).onResourceReady(isA(Drawable.class), isA(Transition.class)); verify(secondTarget).onResourceReady(notNull(Drawable.class), isA(Transition.class)); }
@Test public void load_withStateListDrawableResourceId_asBitmap_withTransformation_nonNullBitmap() throws ExecutionException, InterruptedException { Bitmap bitmap = Glide.with(context) .asBitmap() .load(ResourceIds.drawable.state_list_drawable) .apply(centerCropTransform()) .submit() .get(); assertThat(bitmap).isNotNull(); }
@SuppressLint("CheckResult") @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") protected RequestBuilder(Class<TranscodeType> transcodeClass, RequestBuilder<?> other) { this(other.glide, other.requestManager, transcodeClass, other.context); model = other.model; isModelSet = other.isModelSet; // This is safe because it will always mutate, no one else has access to the object. apply(other); }
@Test public void testListenerApiOverridesListeners() { getNullModelRequest().addListener(listener1).listener(listener2).into(target); verify(requestManager).track(any(Target.class), requestCaptor.capture()); requestCaptor.getValue().onResourceReady(new SimpleResource<>(new Object()), DataSource.LOCAL); // The #listener API removes any previous listeners, so the first listener should not be called. verify(listener1, never()) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); verify(listener2) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); }
private void runTestStringDefaultLoader(String string) { requestManager.load(string).listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { throw new RuntimeException("Load failed"); } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { return false; } }).into(target); requestManager.load(string).into(imageView); verify(target).onResourceReady(isA(BitmapDrawable.class), isA(Transition.class)); verify(target).setRequest((Request) notNull()); assertNotNull(imageView.getDrawable()); }
@Test public void loadBitmapDrawable_asBytes_providesBytesOfBitmap() { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(new BitmapDrawable(context.getResources(), bitmap)) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void testMultipleRequestListeners() { getNullModelRequest().addListener(listener1).addListener(listener2).into(target); verify(requestManager).track(any(Target.class), requestCaptor.capture()); requestCaptor.getValue().onResourceReady(new SimpleResource<>(new Object()), DataSource.LOCAL); verify(listener1) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); verify(listener2) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); }
@Test public void requestBuilder_intoImageView_withSameByteArrayAndMemoryCacheEnabled_loadsFromMemory() throws IOException { final byte[] canonicalBytes = getCanonicalBytes(); concurrency.loadOnMainThread( Glide.with(context) .asDrawable() .load(canonicalBytes) .apply(skipMemoryCacheOf(false)), imageView); Glide.with(context).clear(imageView); concurrency.loadOnMainThread( Glide.with(context) .asDrawable() .load(canonicalBytes) .listener(requestListener) .apply(skipMemoryCacheOf(false)), imageView); verify(requestListener).onResourceReady( anyDrawable(), any(), anyDrawableTarget(), eq(DataSource.MEMORY_CACHE), anyBoolean()); }
@Test public void requestManager_intoImageView_withSameByteArrayAndMemoryCacheEnabled_loadsFromMemory() throws IOException { final byte[] canonicalBytes = getCanonicalBytes(); concurrency.loadOnMainThread( Glide.with(context) .load(canonicalBytes) .apply(skipMemoryCacheOf(false)), imageView); Glide.with(context).clear(imageView); concurrency.loadOnMainThread( Glide.with(context) .load(canonicalBytes) .listener(requestListener) .apply(skipMemoryCacheOf(false)), imageView); verify(requestListener).onResourceReady( anyDrawable(), any(), anyDrawableTarget(), eq(DataSource.MEMORY_CACHE), anyBoolean()); }
@Nullable @Override public RequestBuilder<Drawable> getPreloadRequestBuilder(@NonNull Api.GifResult item) { return requestBuilder.load(item); } }
@SuppressLint("CheckResult") @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") protected RequestBuilder( @NonNull Glide glide, RequestManager requestManager, Class<TranscodeType> transcodeClass, Context context) { this.glide = glide; this.requestManager = requestManager; this.transcodeClass = transcodeClass; this.context = context; this.transitionOptions = requestManager.getDefaultTransitionOptions(transcodeClass); this.glideContext = glide.getGlideContext(); initRequestListeners(requestManager.getDefaultRequestListeners()); apply(requestManager.getDefaultRequestOptions()); }
private RequestBuilder<Object> getNullModelRequest() { when(glideContext.buildImageViewTarget(isA(ImageView.class), isA(Class.class))) .thenReturn(mock(ViewTarget.class)); when(glideContext.getDefaultRequestOptions()).thenReturn(new RequestOptions()); when(requestManager.getDefaultRequestOptions()) .thenReturn(new RequestOptions()); when(requestManager.getDefaultTransitionOptions(any(Class.class))) .thenReturn(new GenericTransitionOptions<>()); return new RequestBuilder<>(glide, requestManager, Object.class, context) .load((Object) null); } }
@Test public void load_whenEncoderFails_callsUncaughtThrowableStrategy() { WaitForErrorStrategy strategy = new WaitForErrorStrategy(); Glide.init(context, new GlideBuilder() .setAnimationExecutor(GlideExecutor.newAnimationExecutor(/*threadCount=*/ 1, strategy)) .setSourceExecutor(GlideExecutor.newSourceExecutor(strategy)) .setDiskCacheExecutor(GlideExecutor.newDiskCacheExecutor(strategy))); Glide.get(context).getRegistry().prepend(Bitmap.class, new FailEncoder()); concurrency.get( Glide.with(context) .load(ResourceIds.raw.canonical) .listener(requestListener) .submit()); // Writing to the disk cache and therefore the exception caused by our FailEncoder may happen // after the request completes, so we should wait for the expected error explicitly. ConcurrencyHelper.waitOnLatch(strategy.latch); assertThat(strategy.error).isEqualTo(FailEncoder.TO_THROW); verify(requestListener, never()) .onLoadFailed(any(GlideException.class), any(), anyDrawableTarget(), anyBoolean()); }