Back to Repositories

Validating MediaStore URI Processing in Glide

This test suite validates the MediaStoreUtil functionality in Glide’s media store handling, specifically focusing on URI validation for Android’s media picker. It ensures proper identification and handling of different types of media URIs in the Android environment.

Test Coverage Overview

The test suite provides comprehensive coverage of MediaStoreUtil’s URI validation capabilities.

Key areas tested include:
  • Validation of standard MediaStore URIs
  • Identification of Android Picker-specific URIs
  • Edge case handling for different URI formats
  • Integration with Android’s MediaStore content provider system

Implementation Analysis

The testing approach utilizes JUnit with Robolectric for Android environment simulation. The implementation follows a clear pattern of positive and negative test cases, leveraging Truth assertions for readable test conditions.

Framework-specific features include:
  • Robolectric test runner configuration
  • SDK version specification
  • Uri manipulation utilities

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Robolectric for Android runtime simulation
  • Google Truth assertion library
  • Custom SDK configuration via @Config annotation
  • MediaStore content URI handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Android development.

Notable practices include:
  • Clear test method naming convention
  • Isolated test cases for specific functionality
  • Proper use of assertion libraries
  • Appropriate use of Android testing annotations
  • Comprehensive coverage of both positive and negative scenarios

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtilTest.java

            
package com.bumptech.glide.load.data.mediastore;

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static com.google.common.truth.Truth.assertThat;

import android.net.Uri;
import android.provider.MediaStore;
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 MediaStoreUtilTest {

  @Test
  public void isAndroidPickerUri_notAndroidPickerUri_returnsFalse() {
    Uri mediaStoreUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "123");

    assertThat(MediaStoreUtil.isAndroidPickerUri(mediaStoreUri)).isFalse();
  }

  @Test
  public void isAndroidPickerUri_identifiesAndroidPickerUri_returnsTrue() {
    Uri androidPickerUri =
        Uri.parse("content://media/picker/0/com.android.providers.media.photopicker/media/123");

    assertThat(MediaStoreUtil.isAndroidPickerUri(androidPickerUri)).isTrue();
  }
}