Back to Repositories

Testing LazyBitmapDrawable Resource Management in Glide

This test suite validates the LazyBitmapDrawableResource functionality in the Glide image loading library, focusing on bitmap resource management and drawable initialization. The tests ensure proper handling of bitmap resources, recycling behavior, and initialization of drawable resources.

Test Coverage Overview

The test suite provides comprehensive coverage of LazyBitmapDrawableResource operations including:

  • Resource obtaining and null handling
  • Size calculation and verification
  • Resource recycling mechanics
  • Bitmap drawable creation and management
  • Initialization behavior for different resource types

Implementation Analysis

The testing approach utilizes JUnit and Robolectric for Android environment simulation, with Mockito for dependency mocking. The tests follow a systematic pattern of setup-execute-verify, with careful attention to resource lifecycle management and state verification.

Test methods are organized to isolate specific behaviors and edge cases of the LazyBitmapDrawableResource class.

Technical Details

  • Testing Framework: JUnit with RobolectricTestRunner
  • Mocking Framework: Mockito
  • Assertion Library: Truth
  • Test Environment: Android Resources simulation
  • Key Components: BitmapDrawable, Resource interface, Initializable interface

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, comprehensive mock usage, and clear test method naming. Each test focuses on a single responsibility with explicit setup and verification steps.

  • Proper resource cleanup and management
  • Clear separation of concerns in test cases
  • Effective use of mock objects and verification
  • Comprehensive edge case handling

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/resource/bitmap/LazyBitmapDrawableResourceTest.java

            
package com.bumptech.glide.load.resource.bitmap;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import androidx.test.core.app.ApplicationProvider;
import com.bumptech.glide.load.engine.Initializable;
import com.bumptech.glide.load.engine.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class LazyBitmapDrawableResourceTest {
  @Mock private Resource<Bitmap> bitmapResource;
  private LazyBitmapDrawableResource resource;
  private Resources resources;
  private Bitmap bitmap;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);

    bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    when(bitmapResource.get()).thenReturn(bitmap);

    resources = ApplicationProvider.getApplicationContext().getResources();
    resource =
        (LazyBitmapDrawableResource) LazyBitmapDrawableResource.obtain(resources, bitmapResource);
  }

  @Test
  public void obtain_withNullBitmapResource_returnsNull() {
    assertThat(LazyBitmapDrawableResource.obtain(resources, null)).isNull();
  }

  @Test
  public void getSize_returnsSizeOfWrappedResource() {
    when(bitmapResource.getSize()).thenReturn(100);
    assertThat(resource.getSize()).isEqualTo(100);
  }

  @Test
  public void recycle_callsRecycleOnWrappedResource() {
    resource.recycle();
    verify(bitmapResource).recycle();
  }

  @Test
  public void recycle_doesNotRecycleWrappedBitmap() {
    resource.recycle();
    assertThat(bitmap.isRecycled()).isFalse();
  }

  @Test
  public void get_returnsDrawableContainingWrappedBitmap() {
    BitmapDrawable drawable = resource.get();
    assertThat(drawable.getBitmap()).isSameInstanceAs(bitmap);
  }

  @Test
  public void initialize_withNonInitializableResource_doesNothing() {
    resource.initialize();
  }

  @Test
  public void initialize_withWrappedInitializableResource_callsInitializeOnWrapped() {
    InitializableBitmapResource bitmapResource = mock(InitializableBitmapResource.class);
    resource =
        (LazyBitmapDrawableResource) LazyBitmapDrawableResource.obtain(resources, bitmapResource);
    resource.initialize();

    verify(bitmapResource).initialize();
  }

  private interface InitializableBitmapResource extends Initializable, Resource<Bitmap> {
    // Intentionally empty.
  }
}