Back to Repositories

Validating URL URI Loading Mechanisms in Glide

This test suite validates the UrlUriLoader component in Glide, focusing on handling different types of URI schemes and edge cases for URL loading. The tests ensure proper URL parsing and handling of both standard and malformed HTTP/HTTPS URIs.

Test Coverage Overview

The test suite provides comprehensive coverage of URI handling in Glide’s URL loading system.

Key areas tested include:
  • HTTP URI handling and validation
  • HTTPS URI handling and validation
  • Edge case handling of malformed URLs
  • Integration with GlideUrl wrapper class

Implementation Analysis

The testing approach utilizes JUnit and Robolectric for Android environment simulation, with Mockito for dependency mocking. The tests follow a structured pattern of URI creation, loader handling verification, and load data building validation.

Framework features leveraged include:
  • Mockito verification for precise interaction testing
  • RobolectricTestRunner for Android SDK simulation
  • JUnit annotations for test organization

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Robolectric for Android SDK simulation
  • Mockito for mocking dependencies
  • Custom SDK version configuration
  • MockitoAnnotations for automatic mock initialization

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java unit testing.

Notable practices include:
  • Proper test isolation using @Before setup
  • Clear test method naming conventions
  • Specific test cases for each URI type
  • Documentation of bug-related test cases
  • Effective use of mocking for external dependencies

bumptech/glide

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

            
package com.bumptech.glide.load.model;

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

import android.net.Uri;
import com.bumptech.glide.load.Options;
import java.io.InputStream;
import java.net.MalformedURLException;
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;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = ROBOLECTRIC_SDK)
public class UrlUriLoaderTest {
  private static final int IMAGE_SIDE = 100;
  private static final Options OPTIONS = new Options();

  @Mock private ModelLoader<GlideUrl, InputStream> urlLoader;
  private UrlUriLoader<InputStream> loader;

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

    loader = new UrlUriLoader<>(urlLoader);
  }

  @Test
  public void testHandlesHttpUris() throws MalformedURLException {
    Uri httpUri = Uri.parse("http://www.google.com");
    loader.buildLoadData(httpUri, IMAGE_SIDE, IMAGE_SIDE, OPTIONS);

    assertTrue(loader.handles(httpUri));
    verify(urlLoader)
        .buildLoadData(
            eq(new GlideUrl(httpUri.toString())), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(OPTIONS));
  }

  @Test
  public void testHandlesHttpsUris() throws MalformedURLException {
    Uri httpsUri = Uri.parse("https://www.google.com");
    loader.buildLoadData(httpsUri, IMAGE_SIDE, IMAGE_SIDE, OPTIONS);

    assertTrue(loader.handles(httpsUri));
    verify(urlLoader)
        .buildLoadData(
            eq(new GlideUrl(httpsUri.toString())), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(OPTIONS));
  }

  // Test for https://github.com/bumptech/glide/issues/71.
  @Test
  public void testHandlesMostlyInvalidHttpUris() {
    Uri mostlyInvalidHttpUri =
        Uri.parse(
            "http://myserver_url.com:80http://myserver_url.com/webapp/images/no_image.png"
                + "?size=100");

    assertTrue(loader.handles(mostlyInvalidHttpUri));
    loader.buildLoadData(mostlyInvalidHttpUri, IMAGE_SIDE, IMAGE_SIDE, OPTIONS);
    verify(urlLoader)
        .buildLoadData(
            eq(new GlideUrl(mostlyInvalidHttpUri.toString())),
            eq(IMAGE_SIDE),
            eq(IMAGE_SIDE),
            eq(OPTIONS));
  }
}