Back to Repositories

Testing SizeStrategyKey Implementation in Glide's Bitmap Recycling System

This test suite validates the functionality of SizeStrategyKey in Glide’s bitmap recycling system, focusing on key initialization, equality comparison, and pool management. The tests ensure proper memory management and object reuse for bitmap size-based recycling strategy.

Test Coverage Overview

The test suite provides comprehensive coverage of the SizeStrategyKey component, focusing on key equality comparison, size initialization, and pool management.

  • Tests key equality based on size values
  • Verifies proper initialization of size values
  • Validates pool offering mechanism
  • Covers size modification scenarios

Implementation Analysis

The implementation employs JUnit 4 framework with Mockito for mocking dependencies. The testing approach uses a combination of equality testing and behavior verification.

  • Uses EqualsTester for thorough equality comparison
  • Implements mock verification for pool interactions
  • Employs state-based testing for size initialization

Technical Details

Test implementation utilizes several technical components:

  • JUnit 4 test runner and annotations
  • Mockito framework for mocking
  • Google’s EqualsTester utility
  • Key pool management through mocked KeyPool
  • Assert statements for equality verification

Best Practices Demonstrated

The test suite demonstrates several testing best practices and patterns.

  • Proper test isolation using @Before setup
  • Clear test method naming conventions
  • Single responsibility per test method
  • Effective use of mock objects
  • Comprehensive equality testing

bumptech/glide

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

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

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 com.bumptech.glide.load.engine.bitmap_recycle.SizeStrategy.Key;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SizeStrategyKeyTest {

  private SizeStrategy.KeyPool keyPool;

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

  @Test
  public void testEquality() {
    Key first = new Key(keyPool);
    first.init(100);
    Key second = new Key(keyPool);
    second.init(100);
    Key third = new Key(keyPool);
    third.init(50);

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

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

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

  @Test
  public void testInitSetsSize() {
    Key key = new Key(keyPool);
    key.init(100);

    Key other = new Key(keyPool);
    other.init(200);

    assertNotEquals(key, other);

    key.init(200);

    assertEquals(key, other);
  }
}