Back to Repositories

Testing URI Loading Implementation in Glide Image Library

This test suite validates the UriLoader class in Glide, an image loading library for Android. It focuses on verifying proper handling of file and content URIs when loading images through the UriLoader component.

Test Coverage Overview

The test suite provides comprehensive coverage of URI handling functionality in Glide’s UriLoader class.

Key areas tested include:
  • File URI handling and validation
  • Content URI processing and verification
  • Integration with LocalUriFetcherFactory
  • Proper data fetcher construction for different URI types

Implementation Analysis

The testing approach utilizes JUnit and Robolectric frameworks for Android-specific URI testing. The implementation employs Mockito for dependency mocking and verification.

Notable patterns include:
  • Mock injection for LocalUriFetcherFactory
  • URI type-specific test cases
  • Verification of fetcher construction and handling

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • Robolectric for Android environment simulation
  • Mockito for mock object creation
  • Custom Options configuration
  • Fixed image dimensions (120×120) for consistent testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test setup and initialization using @Before
  • Clear separation of concerns between file and content URI tests
  • Effective use of mocking to isolate components
  • Consistent verification patterns
  • Clear test method naming conventions

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/model/UriLoaderTest.java

            
package com.bumptech.glide.load.model;

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import android.net.Uri;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.util.Preconditions;
import java.io.File;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

/** Tests for the {@link UriLoader} class. */
@RunWith(RobolectricTestRunner.class)
@Config(sdk = ROBOLECTRIC_SDK)
public class UriLoaderTest {
  // Not a magic number, just arbitrary non zero.
  private static final int IMAGE_SIDE = 120;

  @Mock private DataFetcher<Object> localUriFetcher;
  @Mock private UriLoader.LocalUriFetcherFactory<Object> factory;
  private UriLoader<Object> loader;
  private Options options;

  @Before
  public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);

    options = new Options();
    loader = new UriLoader<>(factory);
  }

  @Test
  public void testHandlesFileUris() throws IOException {
    Uri fileUri = Uri.fromFile(new File("f"));
    when(factory.build(eq(fileUri))).thenReturn(localUriFetcher);

    assertTrue(loader.handles(fileUri));
    assertEquals(
        localUriFetcher,
        Preconditions.checkNotNull(loader.buildLoadData(fileUri, IMAGE_SIDE, IMAGE_SIDE, options))
            .fetcher);
  }

  @Test
  public void testHandlesContentUris() {
    Uri contentUri = Uri.parse("content://com.bumptech.glide");
    when(factory.build(eq(contentUri))).thenReturn(localUriFetcher);

    assertTrue(loader.handles(contentUri));
    assertEquals(
        localUriFetcher,
        Preconditions.checkNotNull(
                loader.buildLoadData(contentUri, IMAGE_SIDE, IMAGE_SIDE, options))
            .fetcher);
  }
}