diff --git a/checkstyle_suppressions.xml b/checkstyle_suppressions.xml index cda0ec42597bbea93a70ecf64497a5ae50b882a6..490a57ec93b1b9b9db76319a547a255c3ef974b6 100644 --- a/checkstyle_suppressions.xml +++ b/checkstyle_suppressions.xml @@ -7,6 +7,7 @@ + diff --git a/gradle.properties b/gradle.properties index bf785e9f691e88479cbdc10e8fa1bcdc1ace06ce..97bb544a92ab7a9ec757578b3ca0fa7afe9db9eb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -23,7 +23,9 @@ DAGGER_VERSION=2.11 COVERALLS_GRADLE_VERSION=2.4.0 JUNIT_VERSION=4.12 -MOCKITO_VERSION=1.10.19 +# Matches the version in Google. +MOCKITO_VERSION=1.9.5 +MOCKITO_ANDROID_VERSION=2.11.0 ROBOLECTRIC_VERSION=3.3.2 MOCKWEBSERVER_VERSION=3.0.0-RC1 TRUTH_VERSION=0.26 diff --git a/integration/gifencoder/build.gradle b/integration/gifencoder/build.gradle index 3ef29e0f6f674c59ecd699e84c556d64dc8942b5..a5defa84161f083bda670379a3bf5c69fd06f0e7 100644 --- a/integration/gifencoder/build.gradle +++ b/integration/gifencoder/build.gradle @@ -6,7 +6,7 @@ dependencies { testCompile project(":testutil") testCompile "com.google.truth:truth:${TRUTH_VERSION}" testCompile "junit:junit:${JUNIT_VERSION}" - testCompile "org.mockito:mockito-all:${MOCKITO_VERSION}" + testCompile "org.mockito:mockito-core:${MOCKITO_VERSION}" testCompile "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}" testCompile "com.android.support:support-v4:${ANDROID_SUPPORT_VERSION}" } diff --git a/integration/gifencoder/src/test/java/com/bumptech/glide/integration/gifencoder/ReEncodingGifResourceEncoderTest.java b/integration/gifencoder/src/test/java/com/bumptech/glide/integration/gifencoder/ReEncodingGifResourceEncoderTest.java index dde1d02435a12057bb66b1bb567d7224952c75c5..9d449e805f83520837e601ae346c9c1467e18e27 100644 --- a/integration/gifencoder/src/test/java/com/bumptech/glide/integration/gifencoder/ReEncodingGifResourceEncoderTest.java +++ b/integration/gifencoder/src/test/java/com/bumptech/glide/integration/gifencoder/ReEncodingGifResourceEncoderTest.java @@ -70,6 +70,7 @@ public class ReEncodingGifResourceEncoderTest { Application context = RuntimeEnvironment.application; ReEncodingGifResourceEncoder.Factory factory = mock(ReEncodingGifResourceEncoder.Factory.class); + when(decoder.getNextFrame()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); when(factory.buildDecoder(any(GifDecoder.BitmapProvider.class))).thenReturn(decoder); when(factory.buildParser()).thenReturn(parser); when(factory.buildEncoder()).thenReturn(gifEncoder); diff --git a/library/build.gradle b/library/build.gradle index 5ac346e41fe3c48c97f5e2e3d1bf3a511e52b4e2..c9e7010186883b4ddd3e91599cdf381f4792848a 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -19,6 +19,7 @@ dependencies { testCompile "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}" testCompile "com.squareup.okhttp3:mockwebserver:${MOCKWEBSERVER_VERSION}" testCompile "com.android.support:support-v4:${ANDROID_SUPPORT_VERSION}" + androidTestCompile "org.mockito:mockito-android:${MOCKITO_ANDROID_VERSION}" androidTestCompile "com.android.support:support-annotations:${ANDROID_SUPPORT_VERSION}" androidTestCompile 'com.android.support.test:runner:0.5' androidTestCompile 'com.android.support.test:rules:0.5' diff --git a/library/src/androidTest/java/com/bumptech/glide/test/TestNonBitmapResources.java b/library/src/androidTest/java/com/bumptech/glide/NonBitmapDrawableResourcesTest.java similarity index 91% rename from library/src/androidTest/java/com/bumptech/glide/test/TestNonBitmapResources.java rename to library/src/androidTest/java/com/bumptech/glide/NonBitmapDrawableResourcesTest.java index 4dd43d7cd91043b9ed5b7b8b1262a61686f84f03..03e7d5f9aab49996b03c3146fac3b9d0c738425e 100644 --- a/library/src/androidTest/java/com/bumptech/glide/test/TestNonBitmapResources.java +++ b/library/src/androidTest/java/com/bumptech/glide/NonBitmapDrawableResourcesTest.java @@ -1,4 +1,4 @@ -package com.bumptech.glide.test; +package com.bumptech.glide; import static com.google.common.truth.Truth.assertThat; @@ -15,8 +15,7 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; -import com.bumptech.glide.Glide; -import com.google.common.collect.ImmutableList; +import com.bumptech.glide.test.ResourceIds; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -27,11 +26,7 @@ import org.junit.Test; import org.junit.runner.RunWith; @RunWith(AndroidJUnit4.class) -public class TestNonBitmapResources { - private static final ImmutableList PACKAGES = ImmutableList.of( - "com.android.gallery3d", - "com.android.contacts"); - +public class NonBitmapDrawableResourcesTest { private Context context; @Before @@ -69,7 +64,7 @@ public class TestNonBitmapResources { public void load_withBitmapAliasResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) - .load(R.drawable.bitmap_alias) + .load(ResourceIds.drawable.bitmap_alias) .submit() .get(); assertThat(drawable).isNotNull(); @@ -79,7 +74,7 @@ public class TestNonBitmapResources { public void load_withShapeDrawableResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) - .load(R.drawable.shape_drawable) + .load(ResourceIds.drawable.shape_drawable) .submit() .get(); assertThat(drawable).isNotNull(); @@ -89,7 +84,7 @@ public class TestNonBitmapResources { public void load_withStateListDrawableResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) - .load(R.drawable.state_list_drawable) + .load(ResourceIds.drawable.state_list_drawable) .submit() .get(); assertThat(drawable).isNotNull(); @@ -99,7 +94,7 @@ public class TestNonBitmapResources { public void load_withVectorDrawableResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) - .load(R.drawable.vector_drawable) + .load(ResourceIds.drawable.vector_drawable) .submit() .get(); assertThat(drawable).isNotNull(); @@ -109,7 +104,7 @@ public class TestNonBitmapResources { public void load_withNinePatchResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) - .load(R.drawable.googlelogo_color_120x44dp) + .load(ResourceIds.drawable.googlelogo_color_120x44dp) .submit() .get(); diff --git a/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/DownsamplerEmulatorTest.java b/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/DownsamplerEmulatorTest.java index 460d9f92f756c7a44a33c6f11b56eb86fdc813b1..1447ed9bd46c0fcfec85a267753e624a9b30ea4c 100644 --- a/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/DownsamplerEmulatorTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/DownsamplerEmulatorTest.java @@ -400,7 +400,8 @@ public class DownsamplerEmulatorTest { } private static Downsampler buildDownsampler() { - List parsers = Collections.singletonList(new DefaultImageHeaderParser()); + List parsers = + Collections.singletonList(new DefaultImageHeaderParser()); DisplayMetrics displayMetrics = new DisplayMetrics(); // XHDPI. displayMetrics.densityDpi = 320; diff --git a/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/TransformationUtilsTest.java b/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/TransformationUtilsTest.java index 220bae31097af4b542df5a9e5cc9673becf2f2cc..3c23d09da55589d0aed544268442789e70b00b9c 100644 --- a/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/TransformationUtilsTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/TransformationUtilsTest.java @@ -1,59 +1,79 @@ package com.bumptech.glide.load.resource.bitmap; +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assume.assumeTrue; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; +import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; -import android.test.AndroidTestCase; +import android.os.Build; +import android.os.Build.VERSION_CODES; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolAdapter; -import org.mockito.Mockito; +import com.bumptech.glide.test.ResourceIds; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; /** * Emulator tests for Glide transformation utilities. */ -public class TransformationUtilsTest extends AndroidTestCase { +@RunWith(AndroidJUnit4.class) +public class TransformationUtilsTest { private BitmapPool bitmapPool; + private Context context; - @Override + @Before public void setUp() throws Exception { bitmapPool = new BitmapPoolAdapter(); + context = InstrumentationRegistry.getContext(); + // TODO: Add Android API specific resources that work > API 16. + assumeTrue(Build.VERSION.SDK_INT <= VERSION_CODES.JELLY_BEAN); } + @Test public void testRoundedCorners() { int width = 20; int height = 30; Bitmap blueRect = createRect(Color.BLUE, width, height, Bitmap.Config.ARGB_8888); Bitmap roundedBlueRect = - TransformationUtils.roundedCorners(bitmapPool, blueRect, width, height, 5); - assertBitmapMatches("blue_rect_rounded", roundedBlueRect); + TransformationUtils.roundedCorners(bitmapPool, blueRect, 5); + assertBitmapMatches(ResourceIds.raw.blue_rect_rounded, roundedBlueRect); } + @Test public void testRoundedCorners_usePool() { int width = 20; int height = 30; Bitmap blueRect = createRect(Color.BLUE, width, height, Bitmap.Config.ARGB_8888); Bitmap redRect = createRect(Color.RED, width, height, Bitmap.Config.ARGB_8888); - BitmapPool mockBitmapPool = Mockito.mock(BitmapPool.class); - when(mockBitmapPool.get(width, height, Bitmap.Config.ARGB_8888)).thenReturn(redRect); + BitmapPool mockBitmapPool = mock(BitmapPool.class); + when(mockBitmapPool.get(width, height, Config.ARGB_8888)).thenReturn(redRect); Bitmap roundedBlueRect = - TransformationUtils.roundedCorners(mockBitmapPool, blueRect, width, height, 5); - assertBitmapMatches("blue_rect_rounded", roundedBlueRect); - assertSame("Did not reuse provided Bitmap.", redRect, roundedBlueRect); + TransformationUtils.roundedCorners(mockBitmapPool, blueRect, 5); + assertBitmapMatches(ResourceIds.raw.blue_rect_rounded, roundedBlueRect); + assertThat(roundedBlueRect).isEqualTo(redRect); } + @Test public void testRoundedCorners_overRounded() { int width = 40; int height = 20; Bitmap blueRect = createRect(Color.BLUE, width, height, Bitmap.Config.RGB_565); Bitmap roundedBlueRect = - TransformationUtils.roundedCorners(bitmapPool, blueRect, width, height, 20); - assertBitmapMatches("blue_rect_over_rounded", roundedBlueRect); + TransformationUtils.roundedCorners(bitmapPool, blueRect, 20); + assertBitmapMatches(ResourceIds.raw.blue_rect_over_rounded, roundedBlueRect); } private Bitmap createRect(int color, int width, int height, Bitmap.Config config) { @@ -63,12 +83,12 @@ public class TransformationUtilsTest extends AndroidTestCase { return result; } - private void assertBitmapMatches(String resourceName, Bitmap actual) { - Resources res = getContext().getResources(); - int resId = res.getIdentifier(resourceName, "drawable", "com.bumptech.glide"); - assertTrue("Cannot find drawable for resource name: " + resourceName, resId > 0); - - Bitmap expected = BitmapFactory.decodeResource(res, resId); + private void assertBitmapMatches(int resId, Bitmap actual) { + Resources res = context.getResources(); + // Avoid default density scaling when decoding the expected Bitmap. + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inScaled = false; + Bitmap expected = BitmapFactory.decodeResource(res, resId, options); assertPixelDataMatches(expected, actual); } diff --git a/library/src/androidTest/java/com/bumptech/glide/test/ResourceIds.java b/library/src/androidTest/java/com/bumptech/glide/test/ResourceIds.java new file mode 100644 index 0000000000000000000000000000000000000000..b7853060f5567be0d317368c2dab8109f2f8aa48 --- /dev/null +++ b/library/src/androidTest/java/com/bumptech/glide/test/ResourceIds.java @@ -0,0 +1,34 @@ +package com.bumptech.glide.test; + +import android.content.Context; +import android.content.res.Resources; +import android.support.test.InstrumentationRegistry; + +/** + * Internally in google we don't appear to be able to reference resource ids directly, this class is + * a hack around that until we figure out what's going wrong. + */ +public final class ResourceIds { + private ResourceIds() { + // Utility class. + } + + public interface raw { + int blue_rect_over_rounded = getResourceId("raw", "blue_rect_over_rounded"); + int blue_rect_rounded = getResourceId("raw", "blue_rect_rounded"); + } + + public interface drawable { + int bitmap_alias = getResourceId("drawable", "bitmap_alias"); + int googlelogo_color_120x44dp= getResourceId("drawable", "googlelogo_color_120x44dp"); + int shape_drawable = getResourceId("drawable", "shape_drawable"); + int state_list_drawable = getResourceId("drawable", "state_list_drawable"); + int vector_drawable = getResourceId("drawable", "vector_drawable"); + } + + private static int getResourceId(String type, String resourceName) { + Context context = InstrumentationRegistry.getContext(); + Resources res = context.getResources(); + return res.getIdentifier(resourceName, type, context.getPackageName()); + } +} diff --git a/library/src/androidTest/res/drawable-hdpi/blue_rect_over_rounded.png b/library/src/androidTest/res/raw/blue_rect_over_rounded.png similarity index 100% rename from library/src/androidTest/res/drawable-hdpi/blue_rect_over_rounded.png rename to library/src/androidTest/res/raw/blue_rect_over_rounded.png diff --git a/library/src/androidTest/res/drawable-hdpi/blue_rect_rounded.png b/library/src/androidTest/res/raw/blue_rect_rounded.png similarity index 100% rename from library/src/androidTest/res/drawable-hdpi/blue_rect_rounded.png rename to library/src/androidTest/res/raw/blue_rect_rounded.png diff --git a/library/src/test/java/com/bumptech/glide/load/engine/EngineTest.java b/library/src/test/java/com/bumptech/glide/load/engine/EngineTest.java index caa85b86c37ed24217231f390a492ca7d02fb636..9f17c6768de4815b9e8ad19752fd855b53ba563a 100644 --- a/library/src/test/java/com/bumptech/glide/load/engine/EngineTest.java +++ b/library/src/test/java/com/bumptech/glide/load/engine/EngineTest.java @@ -58,7 +58,7 @@ public class EngineTest { public void testNewRunnerIsCreatedAndPostedWithNoExistingLoad() { harness.doLoad(); - verify(harness.job).start(any(DecodeJob.class)); + verify(harness.job).start((DecodeJob) any()); } @Test @@ -93,7 +93,7 @@ public class EngineTest { harness.doLoad(); harness.doLoad(); - verify(harness.job, times(1)).start(any(DecodeJob.class)); + verify(harness.job, times(1)).start((DecodeJob) any()); } @Test @@ -192,7 +192,7 @@ public class EngineTest { harness.doLoad(); verify(harness.resource, never()).acquire(); - verify(harness.job).start(any(DecodeJob.class)); + verify(harness.job).start((DecodeJob) any()); } @Test @@ -211,7 +211,7 @@ public class EngineTest { harness.isMemoryCacheable = false; harness.doLoad(); - verify(harness.job).start(any(DecodeJob.class)); + verify(harness.job).start((DecodeJob) any()); } @Test diff --git a/library/src/test/java/com/bumptech/glide/tests/Util.java b/library/src/test/java/com/bumptech/glide/tests/Util.java index e80796ba28e591f81edfe850e38587483a17c27e..8f80b6049c8745d305a58755ef33e208acd2edde 100644 --- a/library/src/test/java/com/bumptech/glide/tests/Util.java +++ b/library/src/test/java/com/bumptech/glide/tests/Util.java @@ -46,7 +46,7 @@ public class Util { } public static Context anyContext() { - return any(Context.class); + return any(); } /**