提交 91f2e944 编写于 作者: S Sam Judd

Get Emulator tests compiling in a cross build system way.

Google’s internal build system doesn’t exactly match gradle/android studio’s behavior. The biggest compromise seems to be resource ids, which aren’t available (or at least I haven’t figured out how to get them) internally.
上级 5fd2aaf9
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
<suppressions> <suppressions>
<suppress files=".*[/\\]library[/\\]src[/\\]test[/\\].*" checks="Javadoc.*"/> <suppress files=".*[/\\]library[/\\]src[/\\]test[/\\].*" checks="Javadoc.*"/>
<suppress files=".*[/\\]library[/\\]src[/\\]androidTest[/\\].*" checks="Javadoc.*"/> <suppress files=".*[/\\]library[/\\]src[/\\]androidTest[/\\].*" checks="Javadoc.*"/>
<suppress files=".*[/\\]library[/\\]src[/\\]androidTest[/\\].*[/\\]ResourceIds" checks=".*"/>
<suppress files=".*[/\\]gif_encoder[/\\].*" checks=".*"/> <suppress files=".*[/\\]gif_encoder[/\\].*" checks=".*"/>
<suppress files=".*RequestBuilder.java|ChildLoadProvider.java|TransitionOptions.java|BaseDecodeOptions.java|RequestOptions.java" checks="NoClone" /> <suppress files=".*RequestBuilder.java|ChildLoadProvider.java|TransitionOptions.java|BaseDecodeOptions.java|RequestOptions.java" checks="NoClone" />
</suppressions> </suppressions>
......
...@@ -23,7 +23,9 @@ DAGGER_VERSION=2.11 ...@@ -23,7 +23,9 @@ DAGGER_VERSION=2.11
COVERALLS_GRADLE_VERSION=2.4.0 COVERALLS_GRADLE_VERSION=2.4.0
JUNIT_VERSION=4.12 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 ROBOLECTRIC_VERSION=3.3.2
MOCKWEBSERVER_VERSION=3.0.0-RC1 MOCKWEBSERVER_VERSION=3.0.0-RC1
TRUTH_VERSION=0.26 TRUTH_VERSION=0.26
......
...@@ -6,7 +6,7 @@ dependencies { ...@@ -6,7 +6,7 @@ dependencies {
testCompile project(":testutil") testCompile project(":testutil")
testCompile "com.google.truth:truth:${TRUTH_VERSION}" testCompile "com.google.truth:truth:${TRUTH_VERSION}"
testCompile "junit:junit:${JUNIT_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 "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}"
testCompile "com.android.support:support-v4:${ANDROID_SUPPORT_VERSION}" testCompile "com.android.support:support-v4:${ANDROID_SUPPORT_VERSION}"
} }
......
...@@ -70,6 +70,7 @@ public class ReEncodingGifResourceEncoderTest { ...@@ -70,6 +70,7 @@ public class ReEncodingGifResourceEncoderTest {
Application context = RuntimeEnvironment.application; Application context = RuntimeEnvironment.application;
ReEncodingGifResourceEncoder.Factory factory = mock(ReEncodingGifResourceEncoder.Factory.class); 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.buildDecoder(any(GifDecoder.BitmapProvider.class))).thenReturn(decoder);
when(factory.buildParser()).thenReturn(parser); when(factory.buildParser()).thenReturn(parser);
when(factory.buildEncoder()).thenReturn(gifEncoder); when(factory.buildEncoder()).thenReturn(gifEncoder);
......
...@@ -19,6 +19,7 @@ dependencies { ...@@ -19,6 +19,7 @@ dependencies {
testCompile "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}" testCompile "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}"
testCompile "com.squareup.okhttp3:mockwebserver:${MOCKWEBSERVER_VERSION}" testCompile "com.squareup.okhttp3:mockwebserver:${MOCKWEBSERVER_VERSION}"
testCompile "com.android.support:support-v4:${ANDROID_SUPPORT_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:support-annotations:${ANDROID_SUPPORT_VERSION}"
androidTestCompile 'com.android.support.test:runner:0.5' androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5' androidTestCompile 'com.android.support.test:rules:0.5'
......
package com.bumptech.glide.test; package com.bumptech.glide;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
...@@ -15,8 +15,7 @@ import android.graphics.drawable.Drawable; ...@@ -15,8 +15,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import com.bumptech.glide.Glide; import com.bumptech.glide.test.ResourceIds;
import com.google.common.collect.ImmutableList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
...@@ -27,11 +26,7 @@ import org.junit.Test; ...@@ -27,11 +26,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class TestNonBitmapResources { public class NonBitmapDrawableResourcesTest {
private static final ImmutableList<String> PACKAGES = ImmutableList.of(
"com.android.gallery3d",
"com.android.contacts");
private Context context; private Context context;
@Before @Before
...@@ -69,7 +64,7 @@ public class TestNonBitmapResources { ...@@ -69,7 +64,7 @@ public class TestNonBitmapResources {
public void load_withBitmapAliasResourceId_asDrawable_producesNonNullDrawable() public void load_withBitmapAliasResourceId_asDrawable_producesNonNullDrawable()
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
Drawable drawable = Glide.with(context) Drawable drawable = Glide.with(context)
.load(R.drawable.bitmap_alias) .load(ResourceIds.drawable.bitmap_alias)
.submit() .submit()
.get(); .get();
assertThat(drawable).isNotNull(); assertThat(drawable).isNotNull();
...@@ -79,7 +74,7 @@ public class TestNonBitmapResources { ...@@ -79,7 +74,7 @@ public class TestNonBitmapResources {
public void load_withShapeDrawableResourceId_asDrawable_producesNonNullDrawable() public void load_withShapeDrawableResourceId_asDrawable_producesNonNullDrawable()
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
Drawable drawable = Glide.with(context) Drawable drawable = Glide.with(context)
.load(R.drawable.shape_drawable) .load(ResourceIds.drawable.shape_drawable)
.submit() .submit()
.get(); .get();
assertThat(drawable).isNotNull(); assertThat(drawable).isNotNull();
...@@ -89,7 +84,7 @@ public class TestNonBitmapResources { ...@@ -89,7 +84,7 @@ public class TestNonBitmapResources {
public void load_withStateListDrawableResourceId_asDrawable_producesNonNullDrawable() public void load_withStateListDrawableResourceId_asDrawable_producesNonNullDrawable()
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
Drawable drawable = Glide.with(context) Drawable drawable = Glide.with(context)
.load(R.drawable.state_list_drawable) .load(ResourceIds.drawable.state_list_drawable)
.submit() .submit()
.get(); .get();
assertThat(drawable).isNotNull(); assertThat(drawable).isNotNull();
...@@ -99,7 +94,7 @@ public class TestNonBitmapResources { ...@@ -99,7 +94,7 @@ public class TestNonBitmapResources {
public void load_withVectorDrawableResourceId_asDrawable_producesNonNullDrawable() public void load_withVectorDrawableResourceId_asDrawable_producesNonNullDrawable()
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
Drawable drawable = Glide.with(context) Drawable drawable = Glide.with(context)
.load(R.drawable.vector_drawable) .load(ResourceIds.drawable.vector_drawable)
.submit() .submit()
.get(); .get();
assertThat(drawable).isNotNull(); assertThat(drawable).isNotNull();
...@@ -109,7 +104,7 @@ public class TestNonBitmapResources { ...@@ -109,7 +104,7 @@ public class TestNonBitmapResources {
public void load_withNinePatchResourceId_asDrawable_producesNonNullDrawable() public void load_withNinePatchResourceId_asDrawable_producesNonNullDrawable()
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
Drawable drawable = Glide.with(context) Drawable drawable = Glide.with(context)
.load(R.drawable.googlelogo_color_120x44dp) .load(ResourceIds.drawable.googlelogo_color_120x44dp)
.submit() .submit()
.get(); .get();
......
...@@ -400,7 +400,8 @@ public class DownsamplerEmulatorTest { ...@@ -400,7 +400,8 @@ public class DownsamplerEmulatorTest {
} }
private static Downsampler buildDownsampler() { private static Downsampler buildDownsampler() {
List<ImageHeaderParser> parsers = Collections.singletonList(new DefaultImageHeaderParser()); List<ImageHeaderParser> parsers =
Collections.<ImageHeaderParser>singletonList(new DefaultImageHeaderParser());
DisplayMetrics displayMetrics = new DisplayMetrics(); DisplayMetrics displayMetrics = new DisplayMetrics();
// XHDPI. // XHDPI.
displayMetrics.densityDpi = 320; displayMetrics.densityDpi = 320;
......
package com.bumptech.glide.load.resource.bitmap; 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 static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color; 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.BitmapPool;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolAdapter; 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. * Emulator tests for Glide transformation utilities.
*/ */
public class TransformationUtilsTest extends AndroidTestCase { @RunWith(AndroidJUnit4.class)
public class TransformationUtilsTest {
private BitmapPool bitmapPool; private BitmapPool bitmapPool;
private Context context;
@Override @Before
public void setUp() throws Exception { public void setUp() throws Exception {
bitmapPool = new BitmapPoolAdapter(); 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() { public void testRoundedCorners() {
int width = 20; int width = 20;
int height = 30; int height = 30;
Bitmap blueRect = createRect(Color.BLUE, width, height, Bitmap.Config.ARGB_8888); Bitmap blueRect = createRect(Color.BLUE, width, height, Bitmap.Config.ARGB_8888);
Bitmap roundedBlueRect = Bitmap roundedBlueRect =
TransformationUtils.roundedCorners(bitmapPool, blueRect, width, height, 5); TransformationUtils.roundedCorners(bitmapPool, blueRect, 5);
assertBitmapMatches("blue_rect_rounded", roundedBlueRect); assertBitmapMatches(ResourceIds.raw.blue_rect_rounded, roundedBlueRect);
} }
@Test
public void testRoundedCorners_usePool() { public void testRoundedCorners_usePool() {
int width = 20; int width = 20;
int height = 30; int height = 30;
Bitmap blueRect = createRect(Color.BLUE, width, height, Bitmap.Config.ARGB_8888); Bitmap blueRect = createRect(Color.BLUE, width, height, Bitmap.Config.ARGB_8888);
Bitmap redRect = createRect(Color.RED, width, height, Bitmap.Config.ARGB_8888); Bitmap redRect = createRect(Color.RED, width, height, Bitmap.Config.ARGB_8888);
BitmapPool mockBitmapPool = Mockito.mock(BitmapPool.class); BitmapPool mockBitmapPool = mock(BitmapPool.class);
when(mockBitmapPool.get(width, height, Bitmap.Config.ARGB_8888)).thenReturn(redRect); when(mockBitmapPool.get(width, height, Config.ARGB_8888)).thenReturn(redRect);
Bitmap roundedBlueRect = Bitmap roundedBlueRect =
TransformationUtils.roundedCorners(mockBitmapPool, blueRect, width, height, 5); TransformationUtils.roundedCorners(mockBitmapPool, blueRect, 5);
assertBitmapMatches("blue_rect_rounded", roundedBlueRect); assertBitmapMatches(ResourceIds.raw.blue_rect_rounded, roundedBlueRect);
assertSame("Did not reuse provided Bitmap.", redRect, roundedBlueRect); assertThat(roundedBlueRect).isEqualTo(redRect);
} }
@Test
public void testRoundedCorners_overRounded() { public void testRoundedCorners_overRounded() {
int width = 40; int width = 40;
int height = 20; int height = 20;
Bitmap blueRect = createRect(Color.BLUE, width, height, Bitmap.Config.RGB_565); Bitmap blueRect = createRect(Color.BLUE, width, height, Bitmap.Config.RGB_565);
Bitmap roundedBlueRect = Bitmap roundedBlueRect =
TransformationUtils.roundedCorners(bitmapPool, blueRect, width, height, 20); TransformationUtils.roundedCorners(bitmapPool, blueRect, 20);
assertBitmapMatches("blue_rect_over_rounded", roundedBlueRect); assertBitmapMatches(ResourceIds.raw.blue_rect_over_rounded, roundedBlueRect);
} }
private Bitmap createRect(int color, int width, int height, Bitmap.Config config) { private Bitmap createRect(int color, int width, int height, Bitmap.Config config) {
...@@ -63,12 +83,12 @@ public class TransformationUtilsTest extends AndroidTestCase { ...@@ -63,12 +83,12 @@ public class TransformationUtilsTest extends AndroidTestCase {
return result; return result;
} }
private void assertBitmapMatches(String resourceName, Bitmap actual) { private void assertBitmapMatches(int resId, Bitmap actual) {
Resources res = getContext().getResources(); Resources res = context.getResources();
int resId = res.getIdentifier(resourceName, "drawable", "com.bumptech.glide"); // Avoid default density scaling when decoding the expected Bitmap.
assertTrue("Cannot find drawable for resource name: " + resourceName, resId > 0); BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap expected = BitmapFactory.decodeResource(res, resId); Bitmap expected = BitmapFactory.decodeResource(res, resId, options);
assertPixelDataMatches(expected, actual); assertPixelDataMatches(expected, actual);
} }
......
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());
}
}
...@@ -58,7 +58,7 @@ public class EngineTest { ...@@ -58,7 +58,7 @@ public class EngineTest {
public void testNewRunnerIsCreatedAndPostedWithNoExistingLoad() { public void testNewRunnerIsCreatedAndPostedWithNoExistingLoad() {
harness.doLoad(); harness.doLoad();
verify(harness.job).start(any(DecodeJob.class)); verify(harness.job).start((DecodeJob) any());
} }
@Test @Test
...@@ -93,7 +93,7 @@ public class EngineTest { ...@@ -93,7 +93,7 @@ public class EngineTest {
harness.doLoad(); harness.doLoad();
harness.doLoad(); harness.doLoad();
verify(harness.job, times(1)).start(any(DecodeJob.class)); verify(harness.job, times(1)).start((DecodeJob) any());
} }
@Test @Test
...@@ -192,7 +192,7 @@ public class EngineTest { ...@@ -192,7 +192,7 @@ public class EngineTest {
harness.doLoad(); harness.doLoad();
verify(harness.resource, never()).acquire(); verify(harness.resource, never()).acquire();
verify(harness.job).start(any(DecodeJob.class)); verify(harness.job).start((DecodeJob) any());
} }
@Test @Test
...@@ -211,7 +211,7 @@ public class EngineTest { ...@@ -211,7 +211,7 @@ public class EngineTest {
harness.isMemoryCacheable = false; harness.isMemoryCacheable = false;
harness.doLoad(); harness.doLoad();
verify(harness.job).start(any(DecodeJob.class)); verify(harness.job).start((DecodeJob) any());
} }
@Test @Test
......
...@@ -46,7 +46,7 @@ public class Util { ...@@ -46,7 +46,7 @@ public class Util {
} }
public static Context anyContext() { public static Context anyContext() {
return any(Context.class); return any();
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册