Back to Repositories

Testing Gradient Color Array Merging Implementation in Lottie Android

This test suite validates the GradientColorParser functionality in Lottie Android, focusing on array merging operations for gradient color processing. The tests ensure proper handling of float arrays with various scenarios including distinct and non-distinct elements.

Test Coverage Overview

The test suite provides comprehensive coverage for array merging operations in gradient color parsing:
  • Basic array merging without distinct elements
  • Complex merging with overlapping values
  • Edge cases with identical arrays
  • Interleaving value scenarios

Implementation Analysis

The testing approach utilizes JUnit’s assertion framework to validate array merging logic. Each test case focuses on specific scenarios using float arrays, implementing a helper method ‘assertMerged’ to streamline test execution and improve code readability.

Technical Details

Testing tools and configuration:
  • JUnit test framework
  • Static assertions for array comparison
  • Custom helper method for validation
  • Float-based array comparison with delta tolerance

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Single responsibility principle in test methods
  • Clear test naming conventions
  • Helper method abstraction for repeated operations
  • Comprehensive edge case coverage

airbnb/lottie-android

lottie/src/test/java/com/airbnb/lottie/parser/GradientColorParserTest.java

            
package com.airbnb.lottie.parser;

import static org.junit.Assert.assertArrayEquals;

import org.junit.Test;

public class GradientColorParserTest {

  @Test public void testNoDistinctShort() {
    assertMerged(new float[]{1}, new float[]{2}, new float[]{1, 2});
  }

  @Test public void testNoDistinct() {
    assertMerged(new float[]{1, 2, 3}, new float[]{4, 5, 6}, new float[]{1, 2, 3, 4, 5, 6});
  }

  @Test public void testWithDistinct() {
    assertMerged(new float[]{1, 2, 3, 5}, new float[]{4, 5, 6}, new float[]{1, 2, 3, 4, 5, 6});
  }

  @Test public void testWithDistinctInterleavingValues() {
    assertMerged(new float[]{2, 4, 5, 6, 8, 10}, new float[]{1, 3, 4, 5, 7, 9}, new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
  }

  @Test public void testIdentical() {
    assertMerged(new float[]{2, 3}, new float[]{2, 3}, new float[]{2, 3});
  }

  private void assertMerged(float[] arrayA, float[] arrayB, float[] merged) {
    assertArrayEquals(merged, GradientColorParser.mergeUniqueElements(arrayA, arrayB), 0f);
  }
}