Back to Repositories

Testing View Animation Transitions in Glide Library

This test suite validates the ViewAnimation functionality in Glide’s transition system, focusing on animation behaviors for image loading transitions. It verifies proper animation clearing, starting, and state management for view transitions in Android ImageViews.

Test Coverage Overview

The test suite provides comprehensive coverage of ViewAnimation transition behaviors.

Key areas tested include:
  • Animation clearing functionality
  • Animation state management
  • Transition return value verification
  • Animation initialization and starting sequence
Integration points cover Android ImageView animations and Glide’s transition system.

Implementation Analysis

The testing approach uses Robolectric for Android environment simulation and Mockito for dependency mocking. The implementation follows AAA (Arrange-Act-Assert) pattern with clean test isolation through @Before setup. Each test focuses on a specific animation behavior aspect using mock verifications and assertions.

Technical Details

Testing tools and configuration:
  • Robolectric TestRunner with SDK configuration
  • JUnit 4 test framework
  • Mockito mocking framework
  • Custom ViewTransition implementation
  • Android Animation and ImageView components

Best Practices Demonstrated

The test suite exhibits several testing best practices:

  • Proper test isolation through setup methods
  • Clear test naming conventions
  • Focused test cases with single responsibilities
  • Effective use of mocking for external dependencies
  • Consistent verification patterns

bumptech/glide

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

            
package com.bumptech.glide.request.transition;

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.view.animation.Animation;
import android.widget.ImageView;
import com.bumptech.glide.request.transition.Transition.ViewAdapter;
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 ViewAnimationTest {
  private ViewTransition<Object> viewAnimation;
  private ViewAdapter adapter;
  private ImageView view;
  private ViewTransition.ViewTransitionAnimationFactory viewTransitionAnimationFactory;

  @SuppressWarnings("unchecked")
  @Before
  public void setUp() {
    viewTransitionAnimationFactory = mock(ViewTransition.ViewTransitionAnimationFactory.class);
    view = mock(ImageView.class);
    adapter = mock(ViewAdapter.class);
    when(adapter.getView()).thenReturn(view);
    viewAnimation = new ViewTransition<>(viewTransitionAnimationFactory);
  }

  @Test
  public void testClearsAnimationOnAnimate() {
    viewAnimation.transition(null, adapter);

    verify(view).clearAnimation();
  }

  @Test
  public void testAlwaysReturnsFalse() {
    assertFalse(viewAnimation.transition(null, adapter));
  }

  @Test
  public void testStartsAnimationOnAnimate() {
    Animation animation = mock(Animation.class);
    when(viewTransitionAnimationFactory.build(anyContextOrNull())).thenReturn(animation);
    viewAnimation.transition(null, adapter);
    verify(view).clearAnimation();
    verify(view).startAnimation(eq(animation));
  }

  private static Context anyContextOrNull() {
    return any();
  }
}