Skip to content

Commit

Permalink
Remove custom Canvas shadows from Glide Robolectric tests
Browse files Browse the repository at this point in the history
The built-in ShadowCanvas can be used.

PiperOrigin-RevId: 482519852
  • Loading branch information
sjudd authored and glide-copybara-robot committed Oct 20, 2022
1 parent ef64c38 commit 26a393b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import androidx.test.core.app.ApplicationProvider;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
Expand All @@ -33,14 +30,9 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowCanvas;

@RunWith(RobolectricTestRunner.class)
@Config(
sdk = 18,
shadows = {CenterInsideTest.DrawNothingCanvas.class})
@Config(sdk = 18)
public class CenterInsideTest {
@Rule public final KeyTester keyTester = new KeyTester();

Expand Down Expand Up @@ -119,14 +111,4 @@ public void testEquals() throws NoSuchAlgorithmException {
new CenterInside(), "acf83850a2e8e9e809c8bfb999e2aede9e932cb897a15367fac9856b96f3ba33")
.test();
}

@Implements(Canvas.class)
public static final class DrawNothingCanvas extends ShadowCanvas {

@Implementation
@Override
public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) {
// Do nothing.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import androidx.test.core.app.ApplicationProvider;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
Expand All @@ -32,14 +29,9 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowCanvas;

@RunWith(RobolectricTestRunner.class)
@Config(
sdk = 18,
shadows = {FitCenterTest.DrawNothingCanvas.class})
@Config(sdk = 18)
public class FitCenterTest {
@Rule public final KeyTester keyTester = new KeyTester();

Expand Down Expand Up @@ -103,14 +95,4 @@ public void testEquals() throws NoSuchAlgorithmException {
new FitCenter(), "eda03bc6969032145110add4bfe399915897406f4ca3a1a7512d07750e60f90d")
.test();
}

@Implements(Canvas.class)
public static final class DrawNothingCanvas extends ShadowCanvas {

@Implementation
@Override
public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) {
// Do nothing.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@
import androidx.test.core.app.ApplicationProvider;
import com.bumptech.glide.gifdecoder.GifDecoder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.gif.GifDrawableTest.BitmapTrackingShadowCanvas;
import com.bumptech.glide.tests.TearDownGlide;
import com.bumptech.glide.tests.Util;
import com.bumptech.glide.util.Preconditions;
import java.util.HashSet;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -47,15 +44,11 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCanvas;

@RunWith(RobolectricTestRunner.class)
@Config(
sdk = 18,
shadows = {BitmapTrackingShadowCanvas.class})
@Config(sdk = 18)
public class GifDrawableTest {
@Rule public final TearDownGlide tearDownGlide = new TearDownGlide();

Expand Down Expand Up @@ -100,15 +93,19 @@ public void tearDown() {
Util.setSdkVersionInt(initialSdkVersion);
}

// containsExactly doesn't need its return value checked.
@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void testShouldDrawFirstFrameBeforeAnyFrameRead() {
Canvas canvas = new Canvas();
drawable.draw(canvas);

BitmapTrackingShadowCanvas shadowCanvas = Shadow.extract(canvas);
assertThat(shadowCanvas.getDrawnBitmaps()).containsExactly(firstFrame);
ShadowCanvas shadowCanvas = Shadow.extract(canvas);
assertThat(shadowCanvas.getDescription())
.isEqualTo(
"Bitmap ("
+ firstFrame.getWidth()
+ " x "
+ firstFrame.getHeight()
+ ") at (0,0) with height=0 and width=0");
}

@Test
Expand Down Expand Up @@ -633,20 +630,4 @@ private void runLoops(int loopCount, int frameCount) {
}
}
}

/** Keeps track of the set of Bitmaps drawn to the canvas. */
@Implements(Canvas.class)
public static final class BitmapTrackingShadowCanvas extends ShadowCanvas {
private final Set<Bitmap> drawnBitmaps = new HashSet<>();

@Implementation
@Override
public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) {
drawnBitmaps.add(bitmap);
}

private Iterable<Bitmap> getDrawnBitmaps() {
return drawnBitmaps;
}
}
}

0 comments on commit 26a393b

Please sign in to comment.