Back to Repositories

Testing Lottie Animation Composition Cache Implementation in lottie-android

This test suite validates the caching functionality of Lottie animations in the Android implementation. It focuses on testing the LottieCompositionCache class which manages caching of animation compositions for improved performance and resource management.

Test Coverage Overview

The test suite provides comprehensive coverage of the LottieCompositionCache functionality.

Key areas tested include:
  • Empty cache retrieval behavior
  • Strong reference caching mechanism
  • Weak reference handling for cached assets
The tests verify both positive and negative scenarios for cache operations, ensuring reliable animation asset management.

Implementation Analysis

The testing approach utilizes JUnit 4 framework with Mockito for mock object creation. The implementation follows the AAA (Arrange-Act-Assert) pattern with a clear setup phase using @Before annotation and individual test methods for specific scenarios.

Technical patterns include:
  • Mock composition objects for controlled testing
  • Cache initialization in setup phase
  • Assertion-based verification of cache operations

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Mockito mocking framework
  • BaseTest extension for common functionality
  • Custom cache implementation testing
  • Assert methods for validation

Best Practices Demonstrated

The test suite demonstrates several testing best practices for Android component testing.

Notable practices include:
  • Proper test isolation through @Before setup
  • Clear test method naming conventions
  • Focused test cases with single responsibility
  • Effective use of mocking for dependencies
  • Consistent assertion patterns

airbnb/lottie-android

lottie/src/test/java/com/airbnb/lottie/model/LottieCompositionCacheTest.java

            
package com.airbnb.lottie.model;

import com.airbnb.lottie.BaseTest;
import com.airbnb.lottie.LottieComposition;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class LottieCompositionCacheTest extends BaseTest  {

  private LottieComposition composition;
  private LottieCompositionCache cache;

  @Before
  public void setup() {
    composition = Mockito.mock(LottieComposition.class);
    cache = new LottieCompositionCache();
  }

  @Test
  public void testEmpty() {
    assertNull(cache.get("foo"));
  }

  @Test
  public void testStrongAsset() {
    cache.put("foo", composition);
    assertEquals(composition, cache.get("foo"));
  }

  @Test
  public void testWeakAsset() {
    cache.put("foo", composition);
    assertEquals(composition, cache.get("foo"));
  }
}