Skip to content

Commit

Permalink
Avoid static references to android classes in test
Browse files Browse the repository at this point in the history
Depending on the order tests are run in, this
seems to be able to cause errors.
  • Loading branch information
sjudd committed Nov 3, 2014
1 parent 8588abe commit 57de21f
Showing 1 changed file with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@
public class BitmapPreFillerTest {
private static final int DEFAULT_BITMAP_WIDTH = 100;
private static final int DEFAULT_BITMAP_HEIGHT = 50;
private static final Bitmap.Config DEFAULT_BITMAP_CONFIG = PreFillType.DEFAULT_CONFIG;
private static final Bitmap DEFAULT_BITMAP =
Bitmap.createBitmap(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT, DEFAULT_BITMAP_CONFIG);
private static final int DEFAULT_BITMAP_SIZE = Util.getBitmapByteSize(DEFAULT_BITMAP);

private static final int DEFAULT_BITMAPS_IN_POOL = 10;
private static final int DEFAULT_BITMAPS_IN_CACHE = 10;
private static final int POOL_SIZE = DEFAULT_BITMAPS_IN_POOL * DEFAULT_BITMAP_SIZE;
private static final int CACHE_SIZE = DEFAULT_BITMAPS_IN_CACHE * DEFAULT_BITMAP_SIZE;
private static final int BITMAPS_IN_POOL = 10;
private static final int BITMAPS_IN_CACHE = 10;

private final Bitmap.Config defaultBitmapConfig = PreFillType.DEFAULT_CONFIG;
private final Bitmap defaultBitmap =
Bitmap.createBitmap(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT, defaultBitmapConfig);
private final int defaultBitmapSize = Util.getBitmapByteSize(defaultBitmap);
private final int poolSize = BITMAPS_IN_CACHE * defaultBitmapSize;
private final int cacheSize = BITMAPS_IN_POOL * defaultBitmapSize;

private BitmapPool pool;
private BitmapPreFiller bitmapPreFiller;
Expand All @@ -48,9 +49,9 @@ public class BitmapPreFillerTest {
@Before
public void setUp() {
pool = mock(BitmapPool.class);
when(pool.getMaxSize()).thenReturn(POOL_SIZE);
when(pool.getMaxSize()).thenReturn(poolSize);
cache = mock(MemoryCache.class);
when(cache.getMaxSize()).thenReturn(CACHE_SIZE);
when(cache.getMaxSize()).thenReturn(cacheSize);

bitmapPreFiller = new BitmapPreFiller(cache, pool, DecodeFormat.DEFAULT);
}
Expand All @@ -60,25 +61,25 @@ public void testAllocationOrderContainsEnoughSizesToFillPoolAndMemoryCache() {
PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(
new PreFillType[] {
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build()
}
);

assertEquals(DEFAULT_BITMAPS_IN_POOL + DEFAULT_BITMAPS_IN_CACHE, allocationOrder.getSize());
assertEquals(BITMAPS_IN_POOL + BITMAPS_IN_CACHE, allocationOrder.getSize());
}

@Test
public void testAllocationOrderThatDoesNotFitExactlyIntoGivenSizeRoundsDown() {
PreFillType[] sizes = new PreFillType[] {
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build(),
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build(),
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build(),
};
PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(sizes);
Expand All @@ -90,7 +91,7 @@ public void testAllocationOrderThatDoesNotFitExactlyIntoGivenSizeRoundsDown() {
}

int expectedSize = 0;
int maxSize = POOL_SIZE + CACHE_SIZE;
int maxSize = poolSize + cacheSize;
for (PreFillType current : sizes) {
int currentSize = Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig());
expectedSize += currentSize * (maxSize / (3 * currentSize));
Expand All @@ -102,15 +103,15 @@ public void testAllocationOrderThatDoesNotFitExactlyIntoGivenSizeRoundsDown() {
@Test
public void testAllocationOrderDoesNotOverFillWithMultipleSizes() {
PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(
new PreFillType[] {
new PreFillType[]{
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build(),
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build(),
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build()
}
);
Expand All @@ -121,22 +122,22 @@ public void testAllocationOrderDoesNotOverFillWithMultipleSizes() {
byteSize += Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig());
}

assertThat(byteSize).isIn(Range.atMost(POOL_SIZE + CACHE_SIZE));
assertThat(byteSize).isIn(Range.atMost(poolSize + cacheSize));
}

@Test
public void testAllocationOrderDoesNotOverFillWithMultipleSizesAndWeights() {
PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(
new PreFillType[]{
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.setWeight(4)
.build(),
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build(),
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 3)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.setWeight(3)
.build()
}
Expand All @@ -148,15 +149,15 @@ public void testAllocationOrderDoesNotOverFillWithMultipleSizesAndWeights() {
byteSize += Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig());
}

assertThat(byteSize).isIn(Range.atMost(POOL_SIZE + CACHE_SIZE));
assertThat(byteSize).isIn(Range.atMost(poolSize + cacheSize));
}

@Test
public void testAllocationOrderContainsSingleSizeIfSingleSizeIsProvided() {
PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(
new PreFillType[] {
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build()
}
);
Expand All @@ -165,18 +166,18 @@ public void testAllocationOrderContainsSingleSizeIfSingleSizeIsProvided() {
PreFillType size = allocationOrder.remove();
assertEquals(DEFAULT_BITMAP_WIDTH, size.getWidth());
assertEquals(DEFAULT_BITMAP_HEIGHT, size.getHeight());
assertEquals(DEFAULT_BITMAP_CONFIG, size.getConfig());
assertEquals(defaultBitmapConfig, size.getConfig());
}
}

@Test
public void testAllocationOrderSplitsEvenlyBetweenEqualSizesWithEqualWeights() {
PreFillType smallWidth = new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build();
PreFillType smallHeight =
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build();
PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(
new PreFillType[] { smallWidth, smallHeight, }
Expand All @@ -201,11 +202,11 @@ public void testAllocationOrderSplitsEvenlyBetweenEqualSizesWithEqualWeights() {
public void testAllocationOrderSplitsByteSizeEvenlyBetweenUnEqualSizesWithEqualWeights() {
PreFillType smallWidth =
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build();
PreFillType normal =
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build();
PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(
new PreFillType[] { smallWidth, normal }
Expand All @@ -230,11 +231,11 @@ public void testAllocationOrderSplitsByteSizeEvenlyBetweenUnEqualSizesWithEqualW
public void testAllocationOrderSplitsByteSizeUnevenlyBetweenEqualSizesWithUnequalWeights() {
PreFillType doubleWeight =
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.setWeight(2)
.build();
PreFillType normal = new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build();
PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(
new PreFillType[] { doubleWeight, normal }
Expand All @@ -257,15 +258,15 @@ public void testAllocationOrderSplitsByteSizeUnevenlyBetweenEqualSizesWithUnequa

@Test
public void testAllocationOrderRoundRobinsDifferentSizes() {
when(pool.getMaxSize()).thenReturn(DEFAULT_BITMAP_SIZE);
when(cache.getMaxSize()).thenReturn(DEFAULT_BITMAP_SIZE);
when(pool.getMaxSize()).thenReturn(defaultBitmapSize);
when(cache.getMaxSize()).thenReturn(defaultBitmapSize);
PreFillType smallWidth =
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build();
PreFillType smallHeight =
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2)
.setConfig(DEFAULT_BITMAP_CONFIG)
.setConfig(defaultBitmapConfig)
.build();

PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(
Expand Down

0 comments on commit 57de21f

Please sign in to comment.