Back to Repositories

Testing Fixed Preload Size Provider Implementation in Glide

This test suite validates the FixedPreloadSizeProvider class in the Glide image loading library. It focuses on verifying the provider’s ability to consistently return predefined width and height dimensions for preloading images.

Test Coverage Overview

The test coverage focuses on the core functionality of FixedPreloadSizeProvider, ensuring it correctly returns predefined dimensions.

Key areas tested include:
  • Dimension integrity verification
  • Fixed size value preservation
  • Generic type parameter handling

Implementation Analysis

The testing approach utilizes JUnit with Robolectric for Android environment simulation. The implementation employs a straightforward unit test pattern with explicit size parameters and assertion verification.

Technical implementation features:
  • Robolectric test runner configuration
  • Google Truth assertion library usage
  • Generic type parameter testing

Technical Details

Testing infrastructure includes:
  • JUnit 4 framework
  • Robolectric for Android runtime simulation
  • Google Truth assertion library
  • SDK version configuration via ROBOLECTRIC_SDK constant
  • SuppressWarnings annotation for intentional result ignoring

Best Practices Demonstrated

The test demonstrates several quality testing practices in Java unit testing.

Notable practices include:
  • Clear test method naming
  • Explicit test setup with defined variables
  • Proper assertion library usage
  • Appropriate annotation usage
  • Concise and focused test scope

bumptech/glide

library/test/src/test/java/com/bumptech/glide/util/FixedPreloadSizeProviderTest.java

            
package com.bumptech.glide.util;

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = ROBOLECTRIC_SDK)
public class FixedPreloadSizeProviderTest {

  // containsExactly doesn't need a return value check.
  @SuppressWarnings("ResultOfMethodCallIgnored")
  @Test
  public void testReturnsGivenSize() {
    int width = 500;
    int height = 1234;
    FixedPreloadSizeProvider<Object> provider = new FixedPreloadSizeProvider<>(width, height);

    int[] size = provider.getPreloadSize(new Object(), 0, 0);

    assertThat(size).asList().containsExactly(width, height);
  }
}