Back to Repositories

Testing ViewPreloadSizeProvider Component Behavior in Glide

This test suite validates the ViewPreloadSizeProvider component in Glide, focusing on size calculation and view management functionality. It ensures proper handling of view dimensions and preloading behavior for image loading optimizations.

Test Coverage Overview

The test suite provides comprehensive coverage of ViewPreloadSizeProvider functionality.

Key areas tested include:
  • Size initialization and retrieval behavior
  • View dimension handling
  • Multiple view management
  • Size caching mechanisms
Edge cases covered include null states, view switching, and dimension validation.

Implementation Analysis

The testing approach utilizes JUnit and Robolectric for Android view testing. The implementation follows a systematic pattern of setup and verification, using RobolectricTestRunner for Android component simulation.

Framework features leveraged include:
  • @Before setup methods for test initialization
  • Robolectric view creation and manipulation
  • Truth assertion library for precise validation

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Robolectric for Android environment simulation
  • ApplicationProvider for context management
  • Custom SDK configuration for Robolectric tests
  • Truth assertion library for enhanced verification

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices with clear test method organization and thorough validation.

Notable practices include:
  • Isolated test methods with single responsibility
  • Comprehensive setup and teardown management
  • Clear naming conventions for test methods
  • Explicit state verification
  • Proper resource management

bumptech/glide

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

            
package com.bumptech.glide.util;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNull;

import android.view.View;
import android.view.ViewGroup;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK)
public class ViewPreloadSizeProviderTest {

  private View view;
  private ViewPreloadSizeProvider<Object> provider;

  @Before
  public void setUp() {
    view = new View(ApplicationProvider.getApplicationContext());
    provider = new ViewPreloadSizeProvider<>();
  }

  @Test
  public void testReturnsNullFromGetPreloadSizeBeforeHasSize() {
    assertNull(provider.getPreloadSize(new Object(), 0, 0));
  }

  @Test
  public void testReturnsValidSizeFromGetPreloadSizeAfterHasSize() {
    int width = 4123;
    int height = 342;
    provider.onSizeReady(width, height);

    int[] size = provider.getPreloadSize(new Object(), 0, 0);
    assertThat(size).asList().containsExactly(width, height).inOrder();
  }

  @Test
  public void testDoesNotObtainSizeFromViewOnceSizeIsSet() {
    int width = 123;
    int height = 456;
    provider.onSizeReady(width, height);
    view.setLayoutParams(new ViewGroup.LayoutParams(1, 1));
    view.layout(0, 0, 1, 1);

    provider.setView(view);

    int[] size = provider.getPreloadSize(new Object(), 0, 0);
    assertThat(size).asList().containsExactly(width, height).inOrder();
  }

  @Test
  public void testCanObtainFixedSizeFromView() {
    int width = 123;
    int height = 456;
    view.setLayoutParams(new ViewGroup.LayoutParams(width, height));
    view.layout(0, 0, width, height);

    provider.setView(view);

    int[] size = provider.getPreloadSize(new Object(), 0, 0);
    assertThat(size).asList().containsExactly(width, height).inOrder();
  }

  @Test
  public void testIgnoresNewViewIfAlreadyWaitingOnSizeOfAnotherView() {
    provider.setView(view);

    View newView = new View(ApplicationProvider.getApplicationContext());
    newView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
    provider.setView(newView);

    assertNull(provider.getPreloadSize(new Object(), 0, 0));
  }

  @Test
  public void testCanObtainSizeFromViewWhenGivenViewInConstructor() {
    int width = 100;
    int height = 200;
    view.setLayoutParams(new ViewGroup.LayoutParams(width, height));
    view.layout(0, 0, width, height);

    provider = new ViewPreloadSizeProvider<>(view);

    int[] size = provider.getPreloadSize(new Object(), 0, 0);
    assertThat(size).asList().containsExactly(width, height).inOrder();
  }
}