Back to Repositories

Testing Bitmap Bytes Transcoding Implementation in Glide

This test suite validates the BitmapBytesTranscoder class in Glide, which handles the conversion of Bitmap resources to byte arrays. It ensures proper bitmap compression, quality settings, and resource management during transcoding operations.

Test Coverage Overview

The test suite provides comprehensive coverage of bitmap transcoding functionality:
  • Bitmap to bytes conversion validation
  • Quality parameter handling
  • Support for different compression formats
  • Resource cleanup verification
Tests cover both standard operations and edge cases across various compression formats.

Implementation Analysis

The testing approach utilizes JUnit with Robolectric for Android environment simulation. The implementation employs a test harness pattern for setup consistency and reusability.

Key patterns include:
  • Mock resource management with Mockito
  • Parameterized format testing
  • Harness-based test isolation

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • Robolectric for Android SDK simulation
  • Mockito for resource mocking
  • Google Truth for assertions
  • Custom BitmapBytesTranscoderHarness for test setup

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test cases with clear single responsibilities
  • Proper resource management and cleanup verification
  • Consistent test structure using @Before setup
  • Comprehensive format testing across all possible values
  • Clear separation of test setup and execution logic

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/resource/transcode/BitmapBytesTranscoderTest.java

            
package com.bumptech.glide.load.resource.transcode;

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static com.bumptech.glide.tests.Util.mockResource;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.graphics.Bitmap;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.util.Preconditions;
import java.io.ByteArrayOutputStream;
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 = ROBOLECTRIC_SDK)
public class BitmapBytesTranscoderTest {
  private BitmapBytesTranscoderHarness harness;

  @Before()
  public void setUp() {
    harness = new BitmapBytesTranscoderHarness();
  }

  @Test
  public void testReturnsBytesOfGivenBitmap() {
    assertThat(harness.getTranscodeResult()).isEqualTo(harness.getExpectedData());
  }

  @Test
  public void testUsesGivenQuality() {
    harness.quality = 66;
    assertThat(harness.getTranscodeResult()).isEqualTo(harness.getExpectedData());
  }

  @Test
  public void testUsesGivenFormat() {
    for (Bitmap.CompressFormat format : Bitmap.CompressFormat.values()) {
      harness.compressFormat = format;
      assertThat(harness.getTranscodeResult()).isEqualTo(harness.getExpectedData());
    }
  }

  @Test
  public void testBitmapResourceIsRecycled() {
    harness.getTranscodeResult();

    verify(harness.bitmapResource).recycle();
  }

  private static class BitmapBytesTranscoderHarness {
    Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
    int quality = 100;
    final Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8);
    final Resource<Bitmap> bitmapResource = mockResource();
    final Options options = new Options();

    BitmapBytesTranscoderHarness() {
      when(bitmapResource.get()).thenReturn(bitmap);
    }

    byte[] getTranscodeResult() {
      BitmapBytesTranscoder transcoder = new BitmapBytesTranscoder(compressFormat, quality);
      Resource<byte[]> bytesResource =
          Preconditions.checkNotNull(transcoder.transcode(bitmapResource, options));

      return bytesResource.get();
    }

    byte[] getExpectedData() {
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      bitmap.compress(compressFormat, quality, os);
      return os.toByteArray();
    }
  }
}