Back to Repositories

Testing Bitmap Recycling Strategy Key Implementation in Glide

This test suite validates the functionality of AttributeStrategyKey in Glide’s bitmap recycling system, focusing on key equality, pooling, and attribute initialization. The tests ensure proper handling of bitmap configurations and dimensions in the recycling strategy.

Test Coverage Overview

The test suite provides comprehensive coverage of AttributeStrategyKey functionality, including:

  • Key equality testing with various bitmap configurations and dimensions
  • Key pooling behavior verification
  • Attribute initialization and comparison testing
  • Edge cases for different bitmap configurations (ARGB_4444, RGB_565)

Implementation Analysis

The testing approach utilizes JUnit and Robolectric frameworks for Android bitmap handling verification. The implementation employs Mockito for mocking dependencies and Google’s EqualsTester for thorough equality testing.

Key patterns include setup isolation through @Before annotation and systematic attribute verification through multiple test cases.

Technical Details

Testing tools and configuration:

  • Robolectric test runner with SDK configuration
  • Mockito for KeyPool interaction verification
  • EqualsTester for comprehensive equality testing
  • JUnit 4 testing framework
  • Android Bitmap configuration testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation using @Before setup
  • Comprehensive equality testing using dedicated testing utilities
  • Mock verification for dependency interactions
  • Clear test method naming conventions
  • Systematic attribute verification

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/AttributeStrategyKeyTest.java

            
package com.bumptech.glide.load.engine.bitmap_recycle;

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

import android.graphics.Bitmap;
import com.bumptech.glide.load.engine.bitmap_recycle.AttributeStrategy.Key;
import com.google.common.testing.EqualsTester;
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 AttributeStrategyKeyTest {

  private AttributeStrategy.KeyPool keyPool;

  @Before
  public void setUp() {
    keyPool = mock(AttributeStrategy.KeyPool.class);
  }

  @Test
  public void testEquality() {
    Key first = new Key(keyPool);
    first.init(100, 100, Bitmap.Config.ARGB_4444);
    Key second = new Key(keyPool);
    second.init(100, 100, Bitmap.Config.ARGB_4444);

    Key third = new Key(keyPool);
    third.init(200, 100, Bitmap.Config.ARGB_4444);

    Key fourth = new Key(keyPool);
    fourth.init(100, 200, Bitmap.Config.ARGB_4444);

    Key fifth = new Key(keyPool);
    fifth.init(100, 100, Bitmap.Config.RGB_565);

    new EqualsTester()
        .addEqualityGroup(first, second)
        .addEqualityGroup(third)
        .addEqualityGroup(fourth)
        .addEqualityGroup(fifth)
        .testEquals();
  }

  @Test
  public void testReturnsSelfToPoolOnOffer() {
    Key key = new Key(keyPool);
    key.offer();

    verify(keyPool).offer(eq(key));
  }

  @Test
  public void testInitSetsAttributes() {
    Key key = new Key(keyPool);
    key.init(100, 100, Bitmap.Config.ARGB_4444);

    Key other = new Key(keyPool);
    other.init(200, 200, Bitmap.Config.RGB_565);

    assertNotEquals(key, other);

    key.init(200, 200, Bitmap.Config.RGB_565);

    assertEquals(key, other);
  }
}