Back to Repositories

Testing Gradient Color Interpolation in Lottie Android

This test suite validates the gradient color interpolation functionality in Lottie Android’s animation system. It ensures proper color transitions and edge case handling for gradient animations, which are critical for smooth visual effects.

Test Coverage Overview

The test suite provides comprehensive coverage of GradientColor interpolation scenarios.

Key areas tested include:
  • Boundary condition handling for progress values (negative, zero, positive)
  • Linear interpolation accuracy at key progress points (0.0, 0.5, 1.0)
  • Color transition validation between start and end states
  • Edge case handling for out-of-bounds progress values

Implementation Analysis

The testing approach uses JUnit framework with TestCase extension, implementing systematic validation of the lerp (linear interpolation) method. Each test method focuses on a specific interpolation scenario, using precise color values and progress parameters to verify accurate gradient transitions.

The implementation leverages assertEquals for comparing GradientColor objects, ensuring exact matches in both color values and positions.

Technical Details

Testing infrastructure includes:
  • JUnit test framework integration
  • Custom GradientColor class implementation
  • Float array position mapping
  • Integer array color value storage
  • Linear interpolation calculation system

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for each scenario
  • Clear test method naming conventions
  • Comprehensive edge case coverage
  • Consistent test structure and organization
  • Proper setup of test fixtures
  • Explicit expected vs actual value comparisons

airbnb/lottie-android

lottie/src/test/java/com/airbnb/lottie/model/content/GradientColorTest.java

            
package com.airbnb.lottie.model.content;

import junit.framework.TestCase;
import org.junit.Test;

import java.util.Arrays;

public class GradientColorTest extends TestCase {

  private final GradientColor start = new GradientColor(new float[]{0f, 1f}, new int[]{0xFF000000, 0xFF020202});

  private final GradientColor end = new GradientColor(new float[]{0f, 1f}, new int[]{0xFF020202, 0xFF040404});

  private final GradientColor gradient = new GradientColor(new float[2], new int[2]);

  @Test
  public void testLerpWithOutOfBoundsNegativeProgress() {
    gradient.lerp(start, end, -42f);
    assertEquals(start, gradient);
  }

  @Test
  public void testLerpWithZeroProgress() {
    gradient.lerp(start, end, 0f);
    assertEquals(start, gradient);
  }

  @Test
  public void testLerpWithHalfProgress() {
    gradient.lerp(start, end, 0.5f);
    GradientColor half = new GradientColor(new float[]{0f, 1f}, new int[]{0xFF010101, 0xFF030303});
    assertEquals(half, gradient);
  }

  @Test
  public void testLerpWithOneProgress() {
    gradient.lerp(start, end, 1f);
    assertEquals(end, gradient);
  }

  @Test
  public void testLerpWithOutOfBoundsPositiveProgress() {
    gradient.lerp(start, end, 42f);
    assertEquals(end, gradient);
  }
}