Back to Repositories

Validating GlideContext Transition Management in bumptech/glide

This test suite validates the GlideContext class functionality in the Glide image loading library, focusing on transition options handling and default behaviors. The tests ensure proper management of image loading transitions across different drawable types and inheritance scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of GlideContext’s transition options management.

Key areas tested include:
  • Default transition options behavior when no options are registered
  • Handling of non-matching registered options
  • Matching option resolution
  • Inheritance-based option resolution for drawable subtypes

Implementation Analysis

The testing approach uses JUnit with Robolectric for Android environment simulation. The implementation leverages mocking with Mockito and Truth assertions for verification.

Notable patterns include:
  • Consistent test setup using @Before annotation
  • Isolated test cases for each transition scenario
  • Mock injection for external dependencies

Technical Details

Testing tools and configuration:
  • JUnit 4 test runner
  • Robolectric for Android runtime simulation
  • Mockito for dependency mocking
  • Truth assertion library for readable verifications
  • ApplicationProvider for Android context access

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear organization and thorough coverage.

Notable practices include:
  • Proper test isolation and setup
  • Comprehensive edge case coverage
  • Clear test method naming conventions
  • Effective use of testing frameworks and assertions

bumptech/glide

library/test/src/test/java/com/bumptech/glide/GlideContextTest.java

            
package com.bumptech.glide;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.test.core.app.ApplicationProvider;
import com.bumptech.glide.Glide.RequestOptionsFactory;
import com.bumptech.glide.load.engine.Engine;
import com.bumptech.glide.load.engine.bitmap_recycle.LruArrayPool;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTargetFactory;
import com.bumptech.glide.util.GlideSuppliers.GlideSupplier;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public final class GlideContextTest {
  private Map<Class<?>, TransitionOptions<?, ?>> transitionOptions;
  private GlideContext context;

  @Before
  public void setUp() {
    Application app = ApplicationProvider.getApplicationContext();

    transitionOptions = new HashMap<>();
    context =
        new GlideContext(
            app,
            new LruArrayPool(),
            new GlideSupplier<Registry>() {
              @Override
              public Registry get() {
                return new Registry();
              }
            },
            new ImageViewTargetFactory(),
            new RequestOptionsFactory() {
              @NonNull
              @Override
              public RequestOptions build() {
                return new RequestOptions();
              }
            },
            transitionOptions,
            /* defaultRequestListeners= */ Collections.<RequestListener<Object>>emptyList(),
            mock(Engine.class),
            mock(GlideExperiments.class),
            Log.DEBUG);
  }

  @Test
  public void getDefaultTransitionOptions_withNoOptionsRegistered_returnsDefaultOptions() {
    assertThat(context.getDefaultTransitionOptions(Object.class))
        .isEqualTo(GlideContext.DEFAULT_TRANSITION_OPTIONS);
  }

  @Test
  public void getDefaultTransitionOptions_withNonMatchingOptionRegistered_returnsDefaultOptions() {
    transitionOptions.put(Bitmap.class, new GenericTransitionOptions<>());
    assertThat(context.getDefaultTransitionOptions(Drawable.class))
        .isEqualTo(GlideContext.DEFAULT_TRANSITION_OPTIONS);
  }

  @Test
  public void getDefaultTransitionOptions_withMatchingOptionsRegistered_returnsMatchingOptions() {
    GenericTransitionOptions<Object> expected = new GenericTransitionOptions<>();
    transitionOptions.put(Bitmap.class, expected);
    assertThat(context.getDefaultTransitionOptions(Bitmap.class)).isEqualTo(expected);
  }

  @Test
  public void getDefaultTransitionOptions_withSuperClassRegistered_returnsSuperClassOptions() {
    DrawableTransitionOptions expected = new DrawableTransitionOptions();
    transitionOptions.put(Drawable.class, expected);
    assertThat(context.getDefaultTransitionOptions(BitmapDrawable.class)).isEqualTo(expected);
    assertThat(context.getDefaultTransitionOptions(GifDrawable.class)).isEqualTo(expected);
  }
}