Skip to content

Commit

Permalink
Adds tests for accurately scaling bitmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 committed Mar 14, 2018
1 parent 00470b4 commit 7734324
Showing 1 changed file with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,114 @@ public class BitmapScaledToDisplayTest {

@Test
public void scaleDownBitmapWhenPossible() {
new DownScaleImageTest()
new ScaleImageTest()
.createBitmap(1000, 1000)
.scaleBitmapToDisplay(500, 500)
.assertScaledBitmapDimensions(500, 500);

new DownScaleImageTest()
new ScaleImageTest()
.createBitmap(600, 800)
.scaleBitmapToDisplay(600, 200)
.assertScaledBitmapDimensions(150, 200);

new DownScaleImageTest()
new ScaleImageTest()
.createBitmap(500, 400)
.scaleBitmapToDisplay(250, 200)
.assertScaledBitmapDimensions(250, 200);

new DownScaleImageTest()
new ScaleImageTest()
.createBitmap(2000, 800)
.scaleBitmapToDisplay(300, 400)
.assertScaledBitmapDimensions(333, 133);
}

@Test
public void doNotScaleDownBitmapWhenNotPossible() {
new DownScaleImageTest()
new ScaleImageTest()
.createBitmap(1000, 1000)
.scaleBitmapToDisplay(2000, 2000)
.assertScaledBitmapDimensions(1000, 1000);

new DownScaleImageTest()
new ScaleImageTest()
.createBitmap(600, 800)
.scaleBitmapToDisplay(600, 800)
.assertScaledBitmapDimensions(600, 800);

new DownScaleImageTest()
new ScaleImageTest()
.createBitmap(500, 400)
.scaleBitmapToDisplay(600, 600)
.assertScaledBitmapDimensions(500, 400);

new DownScaleImageTest()
new ScaleImageTest()
.createBitmap(2000, 800)
.scaleBitmapToDisplay(4000, 2000)
.assertScaledBitmapDimensions(2000, 800);
}

class DownScaleImageTest {
@Test
public void accuratelyScaleBitmapToDisplay() {
new ScaleImageTest()
.createBitmap(1000, 1000)
.accuratelyScaleBitmapToDisplay(500, 500)
.assertScaledBitmapDimensions(500, 500);

new ScaleImageTest()
.createBitmap(600, 800)
.accuratelyScaleBitmapToDisplay(600, 200)
.assertScaledBitmapDimensions(150, 200);

new ScaleImageTest()
.createBitmap(500, 400)
.accuratelyScaleBitmapToDisplay(250, 200)
.assertScaledBitmapDimensions(250, 200);

new ScaleImageTest()
.createBitmap(2000, 800)
.accuratelyScaleBitmapToDisplay(300, 400)
.assertScaledBitmapDimensions(300, 120);

new ScaleImageTest()
.createBitmap(1000, 1000)
.accuratelyScaleBitmapToDisplay(2000, 2000)
.assertScaledBitmapDimensions(2000, 2000);

new ScaleImageTest()
.createBitmap(600, 800)
.accuratelyScaleBitmapToDisplay(600, 800)
.assertScaledBitmapDimensions(600, 800);

new ScaleImageTest()
.createBitmap(500, 400)
.accuratelyScaleBitmapToDisplay(600, 600)
.assertScaledBitmapDimensions(600, 480);

new ScaleImageTest()
.createBitmap(2000, 800)
.accuratelyScaleBitmapToDisplay(4000, 2000)
.assertScaledBitmapDimensions(4000, 1600);
}

class ScaleImageTest {
private final File cache = Collect.getInstance().getApplicationContext().getExternalCacheDir();
private final File imageFile = new File(cache, "testImage.jpeg");
private Bitmap scaledBitmap;

DownScaleImageTest createBitmap(int imageHeight, int imageWidth) {
ScaleImageTest createBitmap(int imageHeight, int imageWidth) {
Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
FileUtils.saveBitmapToFile(bitmap, imageFile.getAbsolutePath());
return this;
}

DownScaleImageTest scaleBitmapToDisplay(int windowHeight, int windowWidth) {
ScaleImageTest scaleBitmapToDisplay(int windowHeight, int windowWidth) {
scaledBitmap = FileUtils.getBitmapScaledToDisplay(imageFile, windowHeight, windowWidth);
return this;
}

ScaleImageTest accuratelyScaleBitmapToDisplay(int windowHeight, int windowWidth) {
scaledBitmap = FileUtils.getBitmapAccuratelyScaledToDisplay(imageFile, windowHeight, windowWidth);
return this;
}

void assertScaledBitmapDimensions(int expectedHeight, int expectedWidth) {
assertEquals(expectedHeight, scaledBitmap.getHeight());
assertEquals(expectedWidth, scaledBitmap.getWidth());
Expand Down

0 comments on commit 7734324

Please sign in to comment.