Skip to content

Commit

Permalink
Fix warnings in integration_tests:nativegraphics
Browse files Browse the repository at this point in the history
This addresses the main warnings in `integration_tests:nativegraphics`
  • Loading branch information
MGaetan89 committed Dec 1, 2024
1 parent 6946b1d commit 246c0af
Show file tree
Hide file tree
Showing 42 changed files with 290 additions and 445 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static Boolean compareBasicBitmapsInfo(Bitmap bmp1, Bitmap bmp2) {
public static boolean compareBitmaps(Bitmap bmp1, Bitmap bmp2) {
final Boolean basicComparison = compareBasicBitmapsInfo(bmp1, bmp2);
if (basicComparison != null) {
return basicComparison.booleanValue();
return basicComparison;
}

for (int i = 0; i < bmp1.getWidth(); i++) {
Expand All @@ -107,7 +107,7 @@ public static boolean compareBitmaps(Bitmap bmp1, Bitmap bmp2) {
public static boolean compareBitmaps(Bitmap bmp1, Bitmap bmp2, double minimumPrecision) {
final Boolean basicComparison = compareBasicBitmapsInfo(bmp1, bmp2);
if (basicComparison != null) {
return basicComparison.booleanValue();
return basicComparison;
}

final int width = bmp1.getWidth();
Expand Down Expand Up @@ -210,7 +210,7 @@ public static boolean compareBitmapsMse(
boolean isPremultiplied) {
final Boolean basicComparison = compareBasicBitmapsInfo(expected, actual);
if (basicComparison != null) {
return basicComparison.booleanValue();
return basicComparison;
}

double mse = 0;
Expand Down Expand Up @@ -238,14 +238,13 @@ public static boolean compareBitmapsMse(
Log.d(TAG, "MSE too large for normal case: " + mse);
return false;
}
return true;
} else {
if (mse <= mseMargin) {
Log.d(TAG, "MSE too small for abnormal case: " + mse);
return false;
}
return true;
}
return true;
}

// Same as above, but asserts compareBitmapsMse's return value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public static void verifyColor(@Nonnull String s, int expected, int observed, in
+ Integer.toHexString(observed)
+ ", tolerated channel error 0x"
+ tolerance;
String red = verifyChannel("red", expected, observed, tolerance, (i) -> Color.red(i));
String green = verifyChannel("green", expected, observed, tolerance, (i) -> Color.green(i));
String blue = verifyChannel("blue", expected, observed, tolerance, (i) -> Color.blue(i));
String alpha = verifyChannel("alpha", expected, observed, tolerance, (i) -> Color.alpha(i));
String red = verifyChannel("red", expected, observed, tolerance, Color::red);
String green = verifyChannel("green", expected, observed, tolerance, Color::green);
String blue = verifyChannel("blue", expected, observed, tolerance, Color::blue);
String alpha = verifyChannel("alpha", expected, observed, tolerance, Color::alpha);

buildErrorString(s, red, green, blue, alpha);
}
Expand Down Expand Up @@ -81,10 +81,10 @@ public static void verifyColor(
+ observed
+ ", tolerated channel error "
+ tolerance;
String red = verifyChannel("red", expected, observed, tolerance, (c) -> c.red());
String green = verifyChannel("green", expected, observed, tolerance, (c) -> c.green());
String blue = verifyChannel("blue", expected, observed, tolerance, (c) -> c.blue());
String alpha = verifyChannel("alpha", expected, observed, tolerance, (c) -> c.alpha());
String red = verifyChannel("red", expected, observed, tolerance, Color::red);
String green = verifyChannel("green", expected, observed, tolerance, Color::green);
String blue = verifyChannel("blue", expected, observed, tolerance, Color::blue);
String alpha = verifyChannel("alpha", expected, observed, tolerance, Color::alpha);

buildErrorString(msg, red, green, blue, alpha);
}
Expand All @@ -95,18 +95,18 @@ private static void buildErrorString(
@Nullable String green,
@Nullable String blue,
@Nullable String alpha) {
String err = null;
StringBuilder error = null;
for (String channel : new String[] {red, green, blue, alpha}) {
if (channel == null) {
continue;
}
if (err == null) {
err = s;
if (error == null) {
error = new StringBuilder(s);
}
err += "\n\t\t" + channel;
error.append("\n\t\t").append(channel);
}
if (err != null) {
fail(err);
if (error != null) {
fail(error.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@Config(minSdk = Q)
public class HardwareAcceleratedActivityRenderTest {
@Test
public void hardwareAcceleratedActivity_setup() throws Exception {
public void hardwareAcceleratedActivity_setup() {
// Setting up an Activity is a smoke test that exercises much of the HardwareRenderer /
// RenderNode / RecordingCanvas native code.
Robolectric.setupActivity(HardwareAcceleratedActivity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class ShadowNativeAllocationRegistryTest {
// TODO(hoisie): choose a different free function to test in V and above.
@Config(maxSdk = U.SDK_INT)
@Test
public void applyFreeFunction_matrix() throws Exception {
public void applyFreeFunction_matrix() {
WeakReference<Matrix> weakMatrix = new WeakReference<>(newMatrix());
// Invokes 'applyFreeFunction' when the matrix is GC'd.
GcFinalization.awaitClear(weakMatrix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void setup() {
}

@Test
public void testInflate() throws Exception {
public void testInflate() {
AnimatedImageDrawable aid = (AnimatedImageDrawable) resources.getDrawable(R.drawable.animated);
assertThat(aid).isNotNull();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static android.os.Build.VERSION_CODES.O;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

import android.content.res.Resources;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -92,7 +94,7 @@ public void start_isRunning_returnsTrue() throws Exception {

drawable.start();

assertEquals(true, Shadows.shadowOf(drawable).isStartInitiated());
assertTrue(Shadows.shadowOf(drawable).isStartInitiated());
}

@Test
Expand All @@ -116,6 +118,6 @@ public void stop_returnsFalse() throws Exception {
drawable.start();
drawable.stop();

assertEquals(false, Shadows.shadowOf(drawable).isStartInitiated());
assertFalse(Shadows.shadowOf(drawable).isStartInitiated());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -278,9 +279,7 @@ public void testDecodeReuseRecycled() {

assertThrows(
IllegalArgumentException.class,
() -> {
BitmapFactory.decodeResource(res, R.drawable.pass, options);
});
() -> BitmapFactory.decodeResource(res, R.drawable.pass, options));
}

/** Create bitmap sized to load unscaled resources: start, pass, and alpha */
Expand Down Expand Up @@ -345,9 +344,7 @@ public void testDecodeReuseFailure() {
options.inMutable = true;
options.inScaled = false;
options.inSampleSize = 4;
Bitmap reduced = BitmapFactory.decodeResource(res, R.drawable.robot, options);

options.inBitmap = reduced;
options.inBitmap = BitmapFactory.decodeResource(res, R.drawable.robot, options);
options.inSampleSize = 1;
assertThrows(
"should throw exception due to lack of space",
Expand Down Expand Up @@ -410,8 +407,8 @@ public void testDecodeReuseEquivalentScaling() {
options.inSampleSize = 2;
Bitmap scaleReduced = BitmapFactory.decodeResource(res, R.drawable.robot, options);
// verify that density isn't incorrectly carried over during bitmap reuse
assertFalse(densityReduced.getDensity() == 2);
assertFalse(densityReduced.getDensity() == 0);
assertNotEquals(2, densityReduced.getDensity());
assertNotEquals(0, densityReduced.getDensity());
assertSame(densityReduced, scaleReduced);
}

Expand All @@ -430,17 +427,19 @@ public void testDecodeInPurgeableAllocationCount() {
options.inInputShareable = false;
byte[] array = obtainArray();
Bitmap purgeableBitmap = BitmapFactory.decodeByteArray(array, 0, array.length, options);
assertFalse(purgeableBitmap.getAllocationByteCount() == 0);
assertNotEquals(0, purgeableBitmap.getAllocationByteCount());
}

private int defaultCreationDensity;

private void verifyScaled(Bitmap b) {
private void verifyScaled(@Nullable Bitmap b) {
assertNotNull(b);
assertEquals(START_WIDTH * 2, b.getWidth());
assertEquals(2, b.getDensity());
}

private void verifyUnscaled(Bitmap b) {
private void verifyUnscaled(@Nullable Bitmap b) {
assertNotNull(b);
assertEquals(START_WIDTH, b.getWidth());
assertEquals(b.getDensity(), defaultCreationDensity);
}
Expand Down Expand Up @@ -478,28 +477,30 @@ public void testDecodeScaling() {

@Test
public void testDecodeFileDescriptor1() throws IOException {
ParcelFileDescriptor pfd = obtainParcelDescriptor(obtainPath());
FileDescriptor input = pfd.getFileDescriptor();
Rect r = new Rect(1, 1, 1, 1);
Bitmap b = BitmapFactory.decodeFileDescriptor(input, r, opt1);
assertNotNull(b);
// Test the bitmap size
assertEquals(START_HEIGHT, b.getHeight());
assertEquals(START_WIDTH, b.getWidth());
// Test if no bitmap
assertNull(BitmapFactory.decodeFileDescriptor(input, r, opt2));
try (ParcelFileDescriptor pfd = obtainParcelDescriptor(obtainPath())) {
FileDescriptor input = pfd.getFileDescriptor();
Rect r = new Rect(1, 1, 1, 1);
Bitmap b = BitmapFactory.decodeFileDescriptor(input, r, opt1);
assertNotNull(b);
// Test the bitmap size
assertEquals(START_HEIGHT, b.getHeight());
assertEquals(START_WIDTH, b.getWidth());
// Test if no bitmap
assertNull(BitmapFactory.decodeFileDescriptor(input, r, opt2));
}
}

@Test
public void testDecodeFileDescriptor2() throws IOException {
ParcelFileDescriptor pfd = obtainParcelDescriptor(obtainPath());
FileDescriptor input = pfd.getFileDescriptor();
Bitmap b = BitmapFactory.decodeFileDescriptor(input);
try (ParcelFileDescriptor pfd = obtainParcelDescriptor(obtainPath())) {
FileDescriptor input = pfd.getFileDescriptor();
Bitmap b = BitmapFactory.decodeFileDescriptor(input);

assertNotNull(b);
// Test the bitmap size
assertEquals(START_HEIGHT, b.getHeight());
assertEquals(START_WIDTH, b.getWidth());
assertNotNull(b);
// Test the bitmap size
assertEquals(START_HEIGHT, b.getHeight());
assertEquals(START_WIDTH, b.getWidth());
}
}

@Test
Expand All @@ -511,30 +512,32 @@ public void testDecodeFileDescriptor3() throws IOException {
for (int j = 0; j < actualOffsets.length; ++j) {
long actualOffset = actualOffsets[j];
String path = obtainPath(testImage.id, actualOffset);
RandomAccessFile file = new RandomAccessFile(path, "r");
FileDescriptor fd = file.getFD();
assertTrue(fd.valid());

// Set the offset to ACTUAL_OFFSET
file.seek(actualOffset);
assertEquals(file.getFilePointer(), actualOffset);

// Now decode. This should be successful and leave the offset
// unchanged.
Bitmap b = BitmapFactory.decodeFileDescriptor(fd);
assertNotNull(b);
assertEquals(file.getFilePointer(), actualOffset);

// Now use the other offset. It should fail to decode, and
// the offset should remain unchanged.
long otherOffset = actualOffsets[(j + 1) % actualOffsets.length];
assertFalse(otherOffset == actualOffset);
file.seek(otherOffset);
assertEquals(file.getFilePointer(), otherOffset);

b = BitmapFactory.decodeFileDescriptor(fd);
assertNull(b);
assertEquals(file.getFilePointer(), otherOffset);

try (RandomAccessFile file = new RandomAccessFile(path, "r")) {
FileDescriptor fd = file.getFD();
assertTrue(fd.valid());

// Set the offset to ACTUAL_OFFSET
file.seek(actualOffset);
assertEquals(file.getFilePointer(), actualOffset);

// Now decode. This should be successful and leave the offset
// unchanged.
Bitmap b = BitmapFactory.decodeFileDescriptor(fd);
assertNotNull(b);
assertEquals(file.getFilePointer(), actualOffset);

// Now use the other offset. It should fail to decode, and
// the offset should remain unchanged.
long otherOffset = actualOffsets[(j + 1) % actualOffsets.length];
assertNotEquals(otherOffset, actualOffset);
file.seek(otherOffset);
assertEquals(file.getFilePointer(), otherOffset);

b = BitmapFactory.decodeFileDescriptor(fd);
assertNull(b);
assertEquals(file.getFilePointer(), otherOffset);
}
}
}
}
Expand Down Expand Up @@ -604,10 +607,9 @@ static File obtainFile(int resId, long offset) throws IOException {
File dir = RuntimeEnvironment.getApplication().getFilesDir();
dir.mkdirs();

String name =
RuntimeEnvironment.getApplication().getResources().getResourceEntryName(resId).toString();
String name = RuntimeEnvironment.getApplication().getResources().getResourceEntryName(resId);
if (offset > 0) {
name = name + "_" + String.valueOf(offset);
name = name + "_" + offset;
}

File file = new File(dir, name);
Expand All @@ -624,7 +626,7 @@ static File obtainFile(int resId, long offset) throws IOException {
// Write a bunch of zeroes before the image.
assertThat(offset).isAtMost(1024);
fOutput.write(dataBuffer, 0, (int) offset);
int readLength = 0;
int readLength;
while ((readLength = is.read(dataBuffer)) != -1) {
fOutput.write(dataBuffer, 0, readLength);
}
Expand Down
Loading

0 comments on commit 246c0af

Please sign in to comment.