Skip to content

Commit

Permalink
Remove custom Bitmap shadows from Glide Robolectric tests
Browse files Browse the repository at this point in the history
These custom shadows are not needed any more.

PiperOrigin-RevId: 482877761
  • Loading branch information
hoisie authored and glide-copybara-robot committed Oct 21, 2022
1 parent e1366e5 commit 4298bb7
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 76 deletions.
26 changes: 0 additions & 26 deletions library/test/src/test/java/com/bumptech/glide/GlideTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
import android.view.ViewGroup;
import android.widget.ImageView;
Expand Down Expand Up @@ -90,7 +89,6 @@
import org.robolectric.annotation.LooperMode;
import org.robolectric.annotation.Resetter;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowBitmap;

/** Tests for the {@link Glide} interface and singleton. */
@LooperMode(LEGACY)
Expand All @@ -100,7 +98,6 @@
shadows = {
GlideTest.ShadowFileDescriptorContentResolver.class,
GlideTest.ShadowMediaMetadataRetriever.class,
GlideTest.MutableShadowBitmap.class
})
@SuppressWarnings("unchecked")
public class GlideTest {
Expand All @@ -116,7 +113,6 @@ public class GlideTest {
@Mock private DiskCache.Factory diskCacheFactory;
@Mock private DiskCache diskCache;
@Mock private MemoryCache memoryCache;
@Mock private Handler bgHandler;
@Mock private Lifecycle lifecycle;
@Mock private RequestManagerTreeNode treeNode;
@Mock private BitmapPool bitmapPool;
Expand Down Expand Up @@ -155,17 +151,6 @@ public void setUp() {
imageView.layout(0, 0, 100, 100);
doAnswer(new CallSizeReady()).when(target).getSize(isA(SizeReadyCallback.class));

when(bgHandler.post(isA(Runnable.class)))
.thenAnswer(
new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) {
Runnable runnable = (Runnable) invocation.getArguments()[0];
runnable.run();
return true;
}
});

requestManager = new RequestManager(Glide.get(context), lifecycle, treeNode, context);
requestManager.resumeRequests();
}
Expand Down Expand Up @@ -878,17 +863,6 @@ public AssetFileDescriptor openAssetFileDescriptor(Uri uri, String type) {
}
}

@Implements(Bitmap.class)
public static class MutableShadowBitmap extends ShadowBitmap {

@Implementation
public static Bitmap createBitmap(int width, int height, Bitmap.Config config) {
Bitmap bitmap = ShadowBitmap.createBitmap(width, height, config);
Shadows.shadowOf(bitmap).setMutable(true);
return bitmap;
}
}

@Implements(MediaMetadataRetriever.class)
public static class ShadowMediaMetadataRetriever {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import androidx.exifinterface.media.ExifInterface;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.tests.Util;
import com.bumptech.glide.util.Preconditions;
import com.google.common.collect.Range;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -27,14 +26,9 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowBitmap;

@RunWith(RobolectricTestRunner.class)
@Config(
sdk = 28,
shadows = {TransformationUtilsTest.AlphaShadowBitmap.class})
@Config(sdk = 28)
public class TransformationUtilsTest {

@Mock private BitmapPool bitmapPool;
Expand Down Expand Up @@ -449,16 +443,4 @@ public void testInitializeMatrixSetsRotateOnRotation() {
TransformationUtils.initializeMatrixForRotation(ExifInterface.ORIENTATION_ROTATE_270, matrix);
verify(matrix).setRotate(-90);
}

@Implements(Bitmap.class)
public static class AlphaShadowBitmap extends ShadowBitmap {

@Implementation
public static Bitmap createBitmap(int width, int height, Bitmap.Config config) {
// Robolectric doesn't match the framework behavior with null configs, so we have to do so
// here.
Preconditions.checkNotNull("Config must not be null");
return ShadowBitmap.createBitmap(width, height, config);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.robolectric.Shadows.shadowOf;

import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import com.bumptech.glide.testutil.TestUtil;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowBitmap;

/** Tests for {@link com.bumptech.glide.gifdecoder.GifDecoder}. */
@RunWith(RobolectricTestRunner.class)
Expand Down Expand Up @@ -141,7 +136,6 @@ public void testSettingDataResetsFramePointer() {
}

@Test
@Config(shadows = {CustomShadowBitmap.class})
public void testFirstFrameMustClearBeforeDrawingWhenLastFrameIsDisposalBackground()
throws IOException {
byte[] data = TestUtil.resourceToBytes(getClass(), "transparent_disposal_background.gif");
Expand All @@ -156,12 +150,10 @@ public void testFirstFrameMustClearBeforeDrawingWhenLastFrameIsDisposalBackgroun
decoder.getNextFrame();
decoder.advance();
Bitmap firstFrameTwice = decoder.getNextFrame();
assertTrue(Arrays.equals((((CustomShadowBitmap) shadowOf(firstFrame))).getPixels(),
(((CustomShadowBitmap) shadowOf(firstFrameTwice))).getPixels()));
assertTrue(firstFrame.sameAs(firstFrameTwice));
}

@Test
@Config(shadows = {CustomShadowBitmap.class})
public void testFirstFrameMustClearBeforeDrawingWhenLastFrameIsDisposalNone() throws IOException {
byte[] data = TestUtil.resourceToBytes(getClass(), "transparent_disposal_none.gif");
GifHeaderParser headerParser = new GifHeaderParser();
Expand All @@ -175,28 +167,7 @@ public void testFirstFrameMustClearBeforeDrawingWhenLastFrameIsDisposalNone() th
decoder.getNextFrame();
decoder.advance();
Bitmap firstFrameTwice = decoder.getNextFrame();
assertTrue(Arrays.equals((((CustomShadowBitmap) shadowOf(firstFrame))).getPixels(),
(((CustomShadowBitmap) shadowOf(firstFrameTwice))).getPixels()));
}

/**
* Preserve generated bitmap data for checking.
*/
@Implements(Bitmap.class)
public static class CustomShadowBitmap extends ShadowBitmap {

private int[] pixels;

@Implementation
public void setPixels(int[] pixels, int offset, int stride,
int x, int y, int width, int height) {
this.pixels = new int[pixels.length];
System.arraycopy(pixels, 0, this.pixels, 0, this.pixels.length);
}

public int[] getPixels() {
return pixels;
}
assertTrue(firstFrame.sameAs(firstFrameTwice));
}

private static class MockProvider implements GifDecoder.BitmapProvider {
Expand Down

0 comments on commit 4298bb7

Please sign in to comment.