Back to Repositories

Validating Bitmap Resource Management in Glide

This test suite validates the BitmapResource class functionality in Glide’s bitmap handling system. It ensures proper bitmap resource management, memory allocation, and error handling for Android bitmap operations.

Test Coverage Overview

The test suite provides comprehensive coverage of BitmapResource operations, including bitmap retrieval, size calculations, and resource recycling.

  • Bitmap access and retrieval validation
  • Pre-KitKat size calculation verification
  • Bitmap pool recycling operations
  • Null parameter handling and validation

Implementation Analysis

The testing approach utilizes JUnit with Robolectric for Android environment simulation. The implementation employs a test harness pattern for consistent bitmap resource testing across multiple scenarios.

Tests leverage Mockito for bitmap pool interactions and version-specific Android SDK behavior verification.

Technical Details

Testing Infrastructure:
  • JUnit 4 test framework
  • Robolectric for Android SDK simulation
  • Mockito for mock object creation
  • Custom BitmapResourceHarness for test setup
  • SDK version manipulation utilities

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, clear test naming, and comprehensive error case coverage.

  • Systematic setup and teardown procedures
  • Explicit exception testing
  • Resource cleanup verification
  • Modular test harness implementation

bumptech/glide

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

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

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import android.graphics.Bitmap;
import android.os.Build;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.tests.Util;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

// TODO: add a test for bitmap size using getAllocationByteSize when robolectric supports kitkat.
@RunWith(RobolectricTestRunner.class)
@Config(sdk = ROBOLECTRIC_SDK)
public class BitmapResourceTest {
  private int currentBuildVersion;
  private BitmapResourceHarness harness;

  @Before
  public void setUp() {
    currentBuildVersion = Build.VERSION.SDK_INT;
    harness = new BitmapResourceHarness();
  }

  @After
  public void tearDown() {
    Util.setSdkVersionInt(currentBuildVersion);
  }

  @Test
  public void testCanGetBitmap() {
    assertEquals(harness.bitmap, harness.resource.get());
  }

  @Test
  public void testSizeIsBasedOnDimensPreKitKat() {
    Util.setSdkVersionInt(18);
    assertEquals(
        harness.bitmap.getWidth() * harness.bitmap.getHeight() * 4, harness.resource.getSize());
  }

  @Test
  public void testPutsBitmapInPoolOnRecycle() {
    harness.resource.recycle();

    verify(harness.bitmapPool).put(eq(harness.bitmap));
  }

  @Test(expected = NullPointerException.class)
  public void testThrowsIfBitmapIsNull() {
    new BitmapResource(null, mock(BitmapPool.class));
  }

  @Test(expected = NullPointerException.class)
  public void testThrowsIfBitmapPoolIsNull() {
    new BitmapResource(Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565), null);
  }

  @Test(expected = NullPointerException.class)
  public void testThrowsIfBitmapAndBitmapPoolAreNull() {
    new BitmapResource(null, null);
  }

  private static class BitmapResourceHarness {
    final Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    final BitmapPool bitmapPool = mock(BitmapPool.class);
    final BitmapResource resource = new BitmapResource(bitmap, bitmapPool);
  }
}