Back to Repositories

Testing UnitTransformation Resource Handling in Glide

This test suite validates the UnitTransformation class in Glide, focusing on resource transformation and equality operations. It ensures the transformation correctly handles resource passing and maintains proper object equality semantics.

Test Coverage Overview

The test suite covers core functionality of UnitTransformation with specific focus on resource handling and object equality verification.

  • Tests resource transformation integrity
  • Validates equals/hashCode contract
  • Verifies disk cache key generation
  • Tests against mock resources and transformations

Implementation Analysis

The testing approach utilizes JUnit4 with AndroidJUnit4 runner for Android context integration. The implementation leverages Mockito for mocking dependencies and custom test utilities for verification.

Key patterns include:
  • Rule-based testing with KeyTester
  • Mock resource creation and verification
  • Equivalence group testing for equality operations

Technical Details

Testing infrastructure includes:

  • JUnit4 test framework
  • AndroidJUnit4 test runner
  • Mockito mocking framework
  • Custom KeyTester utility
  • ApplicationProvider for Android context
  • MessageDigest for cache key testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test setup with @Before annotation
  • Isolated test cases with clear purpose
  • Comprehensive equality testing
  • Proper resource cleanup
  • Clear test method naming
  • Effective use of test utilities and helpers

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/resource/UnitTransformationTest.java

            
package com.bumptech.glide.load.resource;

import static com.bumptech.glide.tests.Util.mockResource;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

import android.app.Application;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.tests.KeyTester;
import com.bumptech.glide.tests.Util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class UnitTransformationTest {
  @Rule public final KeyTester keyTester = new KeyTester();

  private Application app;

  @Before
  public void setUp() {
    app = ApplicationProvider.getApplicationContext();
  }

  @Test
  public void testReturnsGivenResource() {
    Resource<Object> resource = mockResource();
    UnitTransformation<Object> transformation = UnitTransformation.get();
    assertEquals(resource, transformation.transform(app, resource, 10, 10));
  }

  @Test
  public void testEqualsHashCodeDigest() throws NoSuchAlgorithmException {
    @SuppressWarnings("unchecked")
    Transformation<Object> other = mock(Transformation.class);
    doAnswer(new Util.WriteDigest("other"))
        .when(other)
        .updateDiskCacheKey(any(MessageDigest.class));

    keyTester
        .addEquivalenceGroup(UnitTransformation.get(), UnitTransformation.get())
        .addEquivalenceGroup(other)
        .addEmptyDigestRegressionTest(UnitTransformation.get())
        .test();
  }
}