Back to Repositories

Testing Performance Tracking Implementation in Lottie Android

This test suite validates the PerformanceTracker functionality in Lottie Android, focusing on accurate frame rendering time measurements and layer-specific performance monitoring. The tests ensure reliable performance tracking across different animation scenarios and states.

Test Coverage Overview

The test suite provides comprehensive coverage of the PerformanceTracker class functionality.

  • Tests disabled state behavior
  • Validates single frame rendering time tracking
  • Verifies multiple frame averaging
  • Tests multi-layer performance tracking
  • Ensures accurate averaging across alternating frame sequences

Implementation Analysis

The testing approach utilizes JUnit 4 framework with a systematic verification of performance tracking scenarios.

Key implementation patterns include:
  • Setup method initializing tracker state
  • Assertion combinations using both JUnit and Hamcrest matchers
  • Progressive complexity testing from single to multiple layers
  • Float precision handling for render time calculations

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • Hamcrest matchers for assertions
  • AndroidX Core Pair utility for data structure testing
  • Setup method with @Before annotation
  • Floating-point precision handling for render times

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

  • Clear test method naming reflecting functionality
  • Isolated test cases with specific assertions
  • Proper test setup and initialization
  • Progressive complexity in test scenarios
  • Comprehensive edge case coverage

airbnb/lottie-android

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

            
package com.airbnb.lottie;

import androidx.core.util.Pair;

import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

public class PerformanceTrackerTest {

  private PerformanceTracker performanceTracker;

  @Before
  public void setup() {
    performanceTracker = new PerformanceTracker();
    performanceTracker.setEnabled(true);
  }

  @Test
  public void testDisabled() {
    performanceTracker.setEnabled(false);
    performanceTracker.recordRenderTime("Hello", 16f);
    assertTrue(performanceTracker.getSortedRenderTimes().isEmpty());
  }

  @Test
  public void testOneFrame() {
    performanceTracker.recordRenderTime("Hello", 16f);
    List<Pair<String, Float>> sortedRenderTimes = performanceTracker.getSortedRenderTimes();
    assertThat(sortedRenderTimes.size(), equalTo(1));
    assertThat(sortedRenderTimes.get(0).first, equalTo("Hello"));
    assertThat(sortedRenderTimes.get(0).second, equalTo(16f));
  }

  @Test
  public void testTwoFrames() {
    performanceTracker.recordRenderTime("Hello", 16f);
    performanceTracker.recordRenderTime("Hello", 8f);
    List<Pair<String, Float>> sortedRenderTimes = performanceTracker.getSortedRenderTimes();
    assertThat(sortedRenderTimes.size(), equalTo(1));
    assertThat(sortedRenderTimes.get(0).first, equalTo("Hello"));
    assertThat(sortedRenderTimes.get(0).second, equalTo(12f));
  }

  @Test
  public void testTwoLayers() {
    performanceTracker.recordRenderTime("Hello", 16f);
    performanceTracker.recordRenderTime("World", 8f);
    List<Pair<String, Float>> sortedRenderTimes = performanceTracker.getSortedRenderTimes();
    assertThat(sortedRenderTimes.size(), equalTo(2));
    assertThat(sortedRenderTimes.get(0).first, equalTo("Hello"));
    assertThat(sortedRenderTimes.get(0).second, equalTo(16f));
    assertThat(sortedRenderTimes.get(1).first, equalTo("World"));
    assertThat(sortedRenderTimes.get(1).second, equalTo(8f));
  }

  @Test
  public void testTwoLayersAlternatingFrames() {
    performanceTracker.recordRenderTime("Hello", 16f);
    performanceTracker.recordRenderTime("World", 8f);
    performanceTracker.recordRenderTime("Hello", 32f);
    performanceTracker.recordRenderTime("World", 4f);
    List<Pair<String, Float>> sortedRenderTimes = performanceTracker.getSortedRenderTimes();
    assertThat(sortedRenderTimes.size(), equalTo(2));
    assertThat(sortedRenderTimes.get(0).first, equalTo("Hello"));
    assertThat(sortedRenderTimes.get(0).second, equalTo(24f));
    assertThat(sortedRenderTimes.get(1).first, equalTo("World"));
    assertThat(sortedRenderTimes.get(1).second, equalTo(6f));
  }
}