Testing ModelCache Dimension Handling Implementation in Glide
This test suite validates the ModelCache class functionality in Glide, focusing on caching behavior for model objects with different dimensions. The tests ensure proper key equivalence and cache operations for storing and retrieving model objects.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
bumptech/glide
library/test/src/test/java/com/bumptech/glide/load/model/ModelCacheTest.java
package com.bumptech.glide.load.model;
import static org.junit.Assert.assertEquals;
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 ModelCacheTest {
private ModelCache<Object, Object> cache;
@Before
public void setUp() {
cache = new ModelCache<>(10);
}
@Test
public void testModelKeyEquivalence() {
new EqualsTester()
.addEqualityGroup(
ModelCache.ModelKey.get(14f, 100, 200), ModelCache.ModelKey.get(14f, 100, 200))
.addEqualityGroup(ModelCache.ModelKey.get(13f, 100, 200))
.addEqualityGroup(ModelCache.ModelKey.get(14f, 200, 200))
.addEqualityGroup(ModelCache.ModelKey.get(14f, 100, 300))
.testEquals();
}
@Test
public void testCanSetAndGetModel() {
Object model = new Object();
int width = 10;
int height = 20;
Object result = new Object();
cache.put(model, width, height, result);
assertEquals(result, cache.get(model, width, height));
}
@Test
public void testCanSetAndGetMultipleResultsWithDifferentDimensionsForSameObject() {
Object model = new Object();
int firstWidth = 10;
int firstHeight = 20;
Object firstResult = new Object();
int secondWidth = 30;
int secondHeight = 40;
Object secondResult = new Object();
cache.put(model, firstWidth, firstHeight, firstResult);
cache.put(model, secondWidth, secondHeight, secondResult);
assertEquals(firstResult, cache.get(model, firstWidth, firstHeight));
assertEquals(secondResult, cache.get(model, secondWidth, secondHeight));
}
}