Back to Repositories

Testing Drawable Transition Animations in Glide Image Loading Library

This test suite validates the functionality of DrawableCrossFadeFactory in the Glide image loading library, focusing on transition animations between drawables. It verifies the factory’s behavior for different data sources and resource states.

Test Coverage Overview

The test suite provides comprehensive coverage of the DrawableCrossFadeFactory’s transition handling logic.

Key areas tested include:
  • Memory cache transition behavior
  • Disk cache transition animations
  • First resource loading scenarios
  • Cross-fade animation enabling conditions

Implementation Analysis

The testing approach uses JUnit with Robolectric for Android environment simulation. The tests employ a systematic pattern of verifying transition behaviors across different data sources and resource states.

The implementation leverages:
  • Robolectric test runner configuration
  • JUnit assertions for transition validation
  • Factory pattern testing methodology

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Robolectric for Android SDK simulation
  • Custom SDK version configuration
  • DrawableCrossFadeFactory with 100ms duration setting

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming conventions
  • Proper test setup and initialization
  • Isolated test cases for specific behaviors
  • Comprehensive assertion coverage
  • Efficient use of @Before setup methods

bumptech/glide

library/test/src/test/java/com/bumptech/glide/request/transition/DrawableCrossFadeFactoryTest.java

            
package com.bumptech.glide.request.transition;

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import android.graphics.drawable.Drawable;
import com.bumptech.glide.load.DataSource;
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 DrawableCrossFadeFactoryTest {

  private DrawableCrossFadeFactory factory;

  @SuppressWarnings("unchecked")
  @Before
  public void setUp() {
    factory = new DrawableCrossFadeFactory(100 /*duration*/, false /*isCrossFadeEnabled*/);
  }

  @Test
  public void testReturnsNoAnimationIfFromMemoryCache() {
    assertEquals(
        NoTransition.<Drawable>get(),
        factory.build(DataSource.MEMORY_CACHE, true /*isFirstResource*/));
  }

  @Test
  public void testReturnsReturnsAnimationIfNotFromMemoryCacheAndIsFirstResource() {
    assertNotEquals(
        NoTransition.<Drawable>get(),
        factory.build(DataSource.DATA_DISK_CACHE, true /*isFirstResource*/));
  }

  @Test
  public void testReturnsAnimationIfNotFromMemoryCacheAndNotIsFirstResource() {
    assertNotEquals(
        NoTransition.<Drawable>get(),
        factory.build(DataSource.DATA_DISK_CACHE, false /*isFirstResource*/));
  }
}