Back to Repositories

Testing ImageViewTargetFactory Type Handling in Glide

This test suite validates the ImageViewTargetFactory class in Glide, ensuring proper target creation for different image types in Android ImageViews. It verifies the factory’s ability to handle Bitmaps, BitmapDrawables, and Drawables while maintaining type safety.

Test Coverage Overview

The test suite provides comprehensive coverage of ImageViewTargetFactory functionality, focusing on target creation for different image types.

  • Tests target creation for Bitmap objects
  • Validates BitmapDrawable handling
  • Verifies general Drawable support
  • Ensures proper error handling for unsupported types

Implementation Analysis

The testing approach utilizes JUnit with Robolectric for Android environment simulation. The implementation follows a clear pattern of creating targets and verifying their types using Truth assertions.

Each test case follows a consistent structure: create resource, build target, verify target type, ensuring thorough validation of the factory’s behavior.

Technical Details

  • Uses JUnit 4 testing framework
  • Implements Robolectric for Android testing
  • Leverages Truth assertion library
  • Uses ApplicationProvider for context access
  • Configured with specific Robolectric SDK version

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Android development.

  • Proper test setup with @Before annotation
  • Clear test method naming conventions
  • Specific exception testing using expected parameter
  • Isolated test cases for each target type
  • Appropriate use of assertion libraries

bumptech/glide

library/test/src/test/java/com/bumptech/glide/request/target/ImageViewTargetFactoryTest.java

            
package com.bumptech.glide.request.target;

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static com.google.common.truth.Truth.assertThat;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = ROBOLECTRIC_SDK)
public class ImageViewTargetFactoryTest {
  private ImageViewTargetFactory factory;
  private ImageView view;

  @Before
  public void setUp() {
    factory = new ImageViewTargetFactory();
    view = new ImageView(ApplicationProvider.getApplicationContext());
  }

  @Test
  public void testReturnsTargetForBitmaps() {
    Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
    Target<Bitmap> target = factory.buildTarget(view, Bitmap.class);
    target.onResourceReady(bitmap, null);
    assertThat(target).isInstanceOf(BitmapImageViewTarget.class);
  }

  @Test
  public void testReturnsTargetForBitmapDrawables() {
    BitmapDrawable drawable =
        new BitmapDrawable(
            ApplicationProvider.getApplicationContext().getResources(),
            Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444));

    Target<BitmapDrawable> target = factory.buildTarget(view, BitmapDrawable.class);
    target.onResourceReady(drawable, null);
    assertThat(target).isInstanceOf(DrawableImageViewTarget.class);
  }

  @Test
  public void testReturnsTargetForDrawables() {
    Target<Drawable> target = factory.buildTarget(view, Drawable.class);
    target.onResourceReady(new ColorDrawable(Color.RED), null);
    assertThat(target).isInstanceOf(DrawableImageViewTarget.class);
  }

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsForUnknownType() {
    factory.buildTarget(view, Object.class);
  }
}