Back to Repositories

Testing BitmapDrawable Resource Management in Glide Library

This test suite validates the functionality of BitmapDrawableResource class in the Glide image loading library. It focuses on bitmap handling, resource management, and memory recycling operations using Robolectric for Android environment simulation.

Test Coverage Overview

The test suite provides comprehensive coverage of BitmapDrawable resource handling and lifecycle management.

Key areas tested include:
  • Bitmap retrieval and verification
  • Drawable instance management
  • Resource size calculation
  • Memory recycling operations
Integration points cover BitmapPool interactions and Android resource system compatibility.

Implementation Analysis

The testing approach utilizes JUnit with Robolectric for Android environment simulation. The implementation employs a test harness pattern for consistent setup and resource management.

Technical patterns include:
  • Mock object usage for BitmapPool
  • Bitmap creation with specific configurations
  • Resource wrapper validation
  • Memory management verification

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Robolectric TestRunner with SDK configuration
  • Mockito for dependency mocking
  • ApplicationProvider for Android context
  • Custom BitmapDrawableResourceHarness for test setup

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Android resource management testing.

Notable practices include:
  • Isolated test setup using @Before annotation
  • Clear test method naming conventions
  • Proper resource cleanup verification
  • Efficient test harness implementation
  • Comprehensive assertion coverage

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/resource/bitmap/BitmapDrawableResourceTest.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.junit.Assert.assertNotSame;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import androidx.test.core.app.ApplicationProvider;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
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 BitmapDrawableResourceTest {
  private BitmapDrawableResourceHarness harness;

  @Before
  public void setUp() {
    harness = new BitmapDrawableResourceHarness();
  }

  @Test
  public void testReturnsGivenBitmapFromGet() {
    assertEquals(harness.bitmap, harness.create().get().getBitmap());
  }

  @Test
  public void testReturnsDifferentDrawableEachTime() {
    BitmapDrawableResource resource = harness.create();
    BitmapDrawable first = resource.get();
    BitmapDrawable second = resource.get();

    assertNotSame(first, second);
  }

  @Test
  public void testReturnsSizeFromGivenBitmap() {
    assertEquals(
        harness.bitmap.getHeight() * harness.bitmap.getRowBytes(), harness.create().getSize());
  }

  @Test
  public void testBitmapIsReturnedToPoolOnRecycle() {
    harness.create().recycle();

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

  private static class BitmapDrawableResourceHarness {
    final BitmapPool bitmapPool = mock(BitmapPool.class);
    final Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

    BitmapDrawableResource create() {
      return new BitmapDrawableResource(
          new BitmapDrawable(ApplicationProvider.getApplicationContext().getResources(), bitmap),
          bitmapPool);
    }
  }
}