Back to Repositories

Validating PreFillType Bitmap Configuration Management in Glide

This test suite validates the PreFillType class in Glide’s bitmap pre-filling functionality. It ensures proper handling of bitmap configurations, dimensions, and weights while enforcing input validation for the PreFillType builder and constructor.

Test Coverage Overview

The test suite provides comprehensive coverage of PreFillType initialization and validation.

  • Input validation for dimensions (width, height) and weight parameters
  • Null configuration handling
  • Getter method verification for width, height, config and weight
  • Object equality testing across different parameter combinations

Implementation Analysis

The testing approach uses JUnit with Robolectric for Android environment simulation.

Tests utilize expected exceptions for validation cases and equality testing patterns through Google’s EqualsTester. The implementation follows a systematic verification of all PreFillType builder and constructor parameters.

Technical Details

  • Testing Framework: JUnit with RobolectricTestRunner
  • Android SDK Configuration: Uses ROBOLECTRIC_SDK
  • Additional Tools: Google Common Testing Library (EqualsTester)
  • Test Environment: Android Bitmap configurations (ARGB_4444, ARGB_8888)

Best Practices Demonstrated

The test suite exemplifies robust unit testing practices.

  • Comprehensive input validation testing
  • Systematic property verification
  • Thorough equality testing across different parameter combinations
  • Clear test method naming conventions
  • Proper exception testing methodology

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/engine/prefill/PreFillTypeTest.java

            
package com.bumptech.glide.load.engine.prefill;

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static org.junit.Assert.assertEquals;

import android.graphics.Bitmap;
import com.google.common.testing.EqualsTester;
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 PreFillTypeTest {

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsIfSizeIsZero() {
    new PreFillType.Builder(0);
  }

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsIfWidthIsZero() {
    new PreFillType.Builder(0, 100);
  }

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsIfHeightIsZero() {
    new PreFillType.Builder(100, 0);
  }

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsIfWeightIsZero() {
    new PreFillType.Builder(100).setWeight(0);
  }

  @Test(expected = NullPointerException.class)
  public void testConstructorThrowsIfConfigIsNull() {
    new PreFillType(100, 100, null, 1);
  }

  @Test
  public void testGetWidthReturnsGivenWidth() {
    int width = 500;
    assertEquals(width, new PreFillType(width, 100, Bitmap.Config.ARGB_4444, 1).getWidth());
  }

  @Test
  public void testGetHeightReturnsGivenHeight() {
    int height = 123;
    assertEquals(height, new PreFillType(100, height, Bitmap.Config.ARGB_4444, 1).getHeight());
  }

  @Test
  public void testGetConfigReturnsGivenConfig() {
    Bitmap.Config config = Bitmap.Config.ARGB_8888;
    assertEquals(config, new PreFillType(100, 100, config, 1).getConfig());
  }

  @Test
  public void testGetWeightReturnsGivenWeight() {
    int weight = 400;
    assertEquals(weight, new PreFillType(100, 100, Bitmap.Config.ARGB_4444, weight).getWeight());
  }

  @Test
  public void testEquality() {
    new EqualsTester()
        .addEqualityGroup(
            new PreFillType(100, 100, Bitmap.Config.ARGB_4444, 1),
            new PreFillType(100, 100, Bitmap.Config.ARGB_4444, 1))
        .addEqualityGroup(new PreFillType(200, 100, Bitmap.Config.ARGB_4444, 1))
        .addEqualityGroup(new PreFillType(100, 200, Bitmap.Config.ARGB_4444, 1))
        .addEqualityGroup(new PreFillType(100, 100, Bitmap.Config.ARGB_8888, 1))
        .addEqualityGroup(new PreFillType(100, 100, Bitmap.Config.ARGB_4444, 2))
        .testEquals();
  }
}