Back to Repositories

Testing View Property Animation Transitions in Glide Library

This test suite validates the ViewPropertyViewTransitionAnimationFactory class in Glide, focusing on animation transitions based on data source and resource state. It ensures proper animation handling for memory cache, disk cache, and first-time resource loading scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the ViewPropertyAnimationFactory behavior across different data source scenarios.

Key areas tested include:
  • Memory cache animation handling
  • First resource loading behavior
  • Disk cache transition management
  • No-animation conditions verification

Implementation Analysis

The testing approach utilizes JUnit4 with Mockito for mocking dependencies. The implementation follows AAA (Arrange-Act-Assert) pattern, with a clear setup phase using @Before and individual test cases verifying specific transition scenarios.

The tests leverage assertEquals and assertNotEquals assertions to validate transition behaviors against NoTransition instances.

Technical Details

Testing tools and configuration:
  • JUnit4 test runner
  • Mockito mocking framework
  • ViewPropertyTransition.Animator mock object
  • DataSource enumeration testing
  • Custom ViewPropertyAnimationFactory implementation

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, clear test naming conventions, and focused test cases. Each test method verifies a single specific behavior, with descriptive names that clearly indicate the scenario being tested.

The setup method effectively isolates common initialization code, reducing duplication and improving maintainability.

bumptech/glide

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

            
package com.bumptech.glide.request.transition;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock;

import com.bumptech.glide.load.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ViewPropertyViewTransitionAnimationFactoryTest {

  private ViewPropertyAnimationFactory<Object> factory;

  @Before
  public void setUp() {
    ViewPropertyTransition.Animator animator = mock(ViewPropertyTransition.Animator.class);
    factory = new ViewPropertyAnimationFactory<>(animator);
  }

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

  @Test
  public void testReturnsNoAnimationIfNotFirstResource() {
    assertEquals(
        NoTransition.get(), factory.build(DataSource.DATA_DISK_CACHE, false /*isFirstResource*/));
  }

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