Back to Repositories

Validating Animation Frame Calculations in lottie-android

This test suite validates core frame calculation functionality in the Lottie animation library for Android. It focuses on accurate conversion between animation progress percentages and frame numbers, ensuring precise animation timing and synchronization.

Test Coverage Overview

The test suite provides targeted coverage for frame-progress conversion methods in LottieComposition:
  • Frame calculation from progress percentage
  • Progress calculation from frame number
  • Validation of animation timing parameters
  • Handling of frame boundaries and precision

Implementation Analysis

The testing approach uses JUnit to verify mathematical accuracy in frame calculations. It employs precise floating-point comparisons with defined tolerance levels for handling decimal precision. The tests use a sample JSON animation configuration to validate real-world scenarios.

Technical Details

Testing infrastructure includes:
  • JUnit test framework
  • Mock animation JSON data
  • LottieCompositionFactory for composition parsing
  • Float comparison with delta tolerance of 0.01
  • Base test class extension for common functionality

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test methods with single responsibility
  • Precise floating-point comparisons
  • Representative test data
  • Clear test method naming
  • Proper test setup and organization

airbnb/lottie-android

lottie/src/test/java/com/airbnb/lottie/LottieCompositionTest.java

            
package com.airbnb.lottie;

import static junit.framework.TestCase.assertEquals;

import org.junit.Test;

public class LottieCompositionTest extends BaseTest {
  private static final String JSON = "{\"v\":\"4.11.1\",\"fr\":60,\"ip\":0,\"op\":180,\"w\":300,\"h\":300,\"nm\":\"Comp 1\",\"ddd\":0,\"assets\":[]," +
      "\"layers\":[{\"ddd\":0,\"ind\":1,\"ty\":4,\"nm\":\"Shape Layer 1\",\"sr\":1,\"ks\":{\"o\":{\"a\":0,\"k\":100,\"ix\":11},\"r\":{\"a\":0," +
      "\"k\":0,\"ix\":10},\"p\":{\"a\":0,\"k\":[150,150,0],\"ix\":2},\"a\":{\"a\":0,\"k\":[0,0,0],\"ix\":1},\"s\":{\"a\":0,\"k\":[100,100,100]," +
      "\"ix\":6}},\"ao\":0,\"shapes\":[{\"ty\":\"rc\",\"d\":1,\"s\":{\"a\":0,\"k\":[100,100],\"ix\":2},\"p\":{\"a\":0,\"k\":[0,0],\"ix\":3}," +
      "\"r\":{\"a\":0,\"k\":0,\"ix\":4},\"nm\":\"Rectangle Path 1\",\"mn\":\"ADBE Vector Shape - Rect\",\"hd\":false},{\"ty\":\"fl\"," +
      "\"c\":{\"a\":0,\"k\":[0.928262987324,0,0,1],\"ix\":4},\"o\":{\"a\":0,\"k\":100,\"ix\":5},\"r\":1,\"nm\":\"Fill 1\",\"mn\":\"ADBE Vector " +
      "Graphic - Fill\",\"hd\":false}],\"ip\":0,\"op\":180,\"st\":0,\"bm\":0}]}";

  @Test
  public void testGetFrameForProgress() {
    LottieResult<LottieComposition> result = LottieCompositionFactory.fromJsonStringSync(JSON, null);
    //noinspection ConstantConditions
    assertEquals(66.59f, result.getValue().getFrameForProgress(0.37f), 0.01f);
  }

  @Test
  public void testGetProgressForFrame() {
    LottieResult<LottieComposition> result = LottieCompositionFactory.fromJsonStringSync(JSON, null);
    //noinspection ConstantConditions
    assertEquals(0.5f, result.getValue().getProgressForFrame(90), 0.01f);
  }
}