|
| 1 | +package com.bumptech.glide; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | +import android.graphics.Bitmap; |
| 7 | +import android.support.test.InstrumentationRegistry; |
| 8 | +import android.support.test.runner.AndroidJUnit4; |
| 9 | +import com.bumptech.glide.test.BitmapRegressionTester; |
| 10 | +import com.bumptech.glide.test.CanonicalBitmap; |
| 11 | +import com.bumptech.glide.test.GlideApp; |
| 12 | +import com.bumptech.glide.test.RegressionTest; |
| 13 | +import com.bumptech.glide.test.SplitByCpu; |
| 14 | +import com.bumptech.glide.test.SplitBySdk; |
| 15 | +import com.bumptech.glide.test.TearDownGlide; |
| 16 | +import java.util.concurrent.ExecutionException; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.rules.ExpectedException; |
| 21 | +import org.junit.rules.TestName; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | + |
| 24 | +@RunWith(AndroidJUnit4.class) |
| 25 | +@SplitByCpu |
| 26 | +@SplitBySdk({26, 24, 23, 21, 18, 16}) |
| 27 | +@RegressionTest |
| 28 | +public class CircleCropRegressionTest { |
| 29 | + @Rule public TestName testName = new TestName(); |
| 30 | + @Rule public ExpectedException expectedException = ExpectedException.none(); |
| 31 | + @Rule public TearDownGlide tearDownGlide = new TearDownGlide(); |
| 32 | + private BitmapRegressionTester bitmapRegressionTester; |
| 33 | + private Context context; |
| 34 | + private CanonicalBitmap canonical; |
| 35 | + |
| 36 | + @Before |
| 37 | + public void setUp() { |
| 38 | + context = InstrumentationRegistry.getTargetContext(); |
| 39 | + bitmapRegressionTester = new BitmapRegressionTester(getClass(), testName); |
| 40 | + canonical = new CanonicalBitmap(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void circleCrop_withSquareSmallerThanImage_returnsSquaredImage() |
| 45 | + throws ExecutionException, InterruptedException { |
| 46 | + Bitmap result = |
| 47 | + bitmapRegressionTester.test( |
| 48 | + GlideApp.with(context) |
| 49 | + .asBitmap() |
| 50 | + .load(canonical.getBitmap()) |
| 51 | + .circleCrop() |
| 52 | + .override(50)); |
| 53 | + |
| 54 | + assertThat(result.getWidth()).isEqualTo(50); |
| 55 | + assertThat(result.getHeight()).isEqualTo(50); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void circleCrop_withSquareLargerThanImage_returnsUpscaledFitImage() |
| 60 | + throws ExecutionException, InterruptedException { |
| 61 | + float multiplier = 1.1f; |
| 62 | + int multipliedWidth = (int) (canonical.getWidth() * multiplier); |
| 63 | + int multipliedHeight = (int) (canonical.getHeight() * multiplier); |
| 64 | + Bitmap result = |
| 65 | + bitmapRegressionTester.test( |
| 66 | + GlideApp.with(context) |
| 67 | + .asBitmap() |
| 68 | + .load(canonical.getBitmap()) |
| 69 | + .circleCrop() |
| 70 | + .override(multipliedWidth)); |
| 71 | + |
| 72 | + assertThat(result.getWidth()).isEqualTo(multipliedWidth); |
| 73 | + assertThat(result.getHeight()).isEqualTo(multipliedWidth); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void circleCrop_withNarrowRectangle_cropsWithin() |
| 78 | + throws ExecutionException, InterruptedException { |
| 79 | + Bitmap result = |
| 80 | + bitmapRegressionTester.test( |
| 81 | + GlideApp.with(context) |
| 82 | + .asBitmap() |
| 83 | + .load(canonical.getBitmap()) |
| 84 | + .circleCrop() |
| 85 | + .override(canonical.getWidth() / 10, canonical.getHeight())); |
| 86 | + |
| 87 | + assertThat(result.getWidth()).isEqualTo(canonical.getWidth() / 10); |
| 88 | + assertThat(result.getHeight()).isEqualTo(canonical.getWidth() / 10); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void circleCrop_withShortRectangle_fitsWithinMaintainingAspectRatio() |
| 93 | + throws ExecutionException, InterruptedException { |
| 94 | + Bitmap result = |
| 95 | + bitmapRegressionTester.test( |
| 96 | + GlideApp.with(context) |
| 97 | + .asBitmap() |
| 98 | + .load(canonical.getBitmap()) |
| 99 | + .circleCrop() |
| 100 | + .override(canonical.getWidth(), canonical.getHeight() / 2)); |
| 101 | + |
| 102 | + assertThat(result.getWidth()).isEqualTo(canonical.getHeight() / 2); |
| 103 | + assertThat(result.getHeight()).isEqualTo(canonical.getHeight() / 2); |
| 104 | + } |
| 105 | +} |
0 commit comments