Back to Repositories

Validating SimpleTarget Dimension Handling in Glide

This test suite validates the SimpleTarget class functionality in the Glide image loading library, focusing on dimension validation and target initialization. The tests ensure proper handling of target dimensions and verify constructor behavior for image loading targets.

Test Coverage Overview

The test suite provides comprehensive coverage of SimpleTarget’s dimension validation logic and initialization scenarios.

Key areas tested include:
  • Validation of negative and zero width/height values
  • Support for SIZE_ORIGINAL dimension parameter
  • Constructor behavior with different dimension combinations
  • Edge cases for target initialization without dimensions

Implementation Analysis

The testing approach utilizes JUnit 4 with Mockito for mock objects, implementing systematic validation of target dimension parameters.

The tests follow a clear pattern of validating constructor parameters and size callback behavior, using expected exceptions to verify proper error handling. Implementation leverages anonymous inner classes to test abstract class functionality.

Technical Details

Testing tools and configuration:
  • JUnit 4 test runner (@RunWith(JUnit4.class))
  • Mockito for SizeReadyCallback mocking
  • AndroidX annotations (@NonNull, @Nullable)
  • Custom helper method getTarget() for test instance creation

Best Practices Demonstrated

The test suite demonstrates excellent testing practices through focused test methods and clear naming conventions.

Notable practices include:
  • Systematic validation of boundary conditions
  • Explicit exception testing using @Test(expected)
  • Consistent test method naming pattern
  • Proper separation of test setup through helper methods

bumptech/glide

library/test/src/test/java/com/bumptech/glide/request/target/SimpleTargetTest.java

            
package com.bumptech.glide.request.target;

import static org.mockito.Mockito.mock;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.request.transition.Transition;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SimpleTargetTest {

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsOnGetSizeIfGivenWidthIsLessThanZero() {
    getTarget(-1, 1).getSize(mock(SizeReadyCallback.class));
  }

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsOnGetSizeIfGivenWidthIsEqualToZero() {
    getTarget(0, 1).getSize(mock(SizeReadyCallback.class));
  }

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsOnGetSizeIfGivenHeightIsLessThanZero() {
    getTarget(1, -1).getSize(mock(SizeReadyCallback.class));
  }

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsOnGetSizeIfGivenHeightIsEqualToZero() {
    getTarget(1, 0).getSize(mock(SizeReadyCallback.class));
  }

  @Test
  public void testCanBeConstructedWithoutDimensions() {
    new SimpleTarget<Object>() {
      @Override
      public void onResourceReady(
          @NonNull Object resource, @Nullable Transition<? super Object> transition) {
        // Do nothing.
      }
    };
  }

  @Test
  public void testConstructorDoesNotThrowWithSizeOriginal() {
    getTarget(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
  }

  @Test
  public void testGetSizeDoesNotThrowWithSizeOriginal() {
    getTarget(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).getSize(mock(SizeReadyCallback.class));
  }

  private SimpleTarget<Object> getTarget(int width, int height) {
    return new SimpleTarget<Object>(width, height) {
      @Override
      public void onResourceReady(
          @NonNull Object resource, @Nullable Transition<? super Object> transition) {
        // Do nothing.
      }
    };
  }
}