Skip to content

Commit

Permalink
fbandroid/libraries/fresco/static-webp/src/androidTest/java/com/faceb…
Browse files Browse the repository at this point in the history
…ook/webpsupport/WebpBitmapFactoryTest.java

Reviewed By: jocelynluizzi13

Differential Revision: D68763198

fbshipit-source-id: 87ff05a4a98087efb00e0e7fc3af13a18d27dbaa
  • Loading branch information
generatedunixname89002005287564 authored and facebook-github-bot committed Jan 28, 2025
1 parent 044bbe3 commit 1e54801
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.facebook.common.internal.Throwables;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.core.ImagePipelineFactory;
import com.facebook.infer.annotation.Nullsafe;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
Expand All @@ -26,6 +27,7 @@
import org.junit.Before;
import org.junit.Test;

@Nullsafe(Nullsafe.Mode.LOCAL)
public class WebpBitmapFactoryTest extends TestCase {
private Instrumentation mInstrumentation;

Expand All @@ -37,6 +39,7 @@ public void setUp() {
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mWebpBitmapFactory = new WebpBitmapFactoryImpl();
ImagePipelineConfig.Builder configBuilder =
// NULLSAFE_FIXME[Not Vetted Third-Party]
ImagePipelineConfig.newBuilder(mInstrumentation.getContext());
configBuilder.experiment().setWebpBitmapFactory(mWebpBitmapFactory);
ImagePipelineFactory.initialize(configBuilder.build());
Expand All @@ -57,6 +60,7 @@ private FileDescriptor getImageFileDescriptor(String path) {

private InputStream getTestImageInputStream(String path) {
try {
// NULLSAFE_FIXME[Not Vetted Third-Party]
return mInstrumentation.getContext().getResources().getAssets().open(path);
} catch (IOException e) {
throw Throwables.propagate(e);
Expand Down Expand Up @@ -85,21 +89,29 @@ private InputStream getTestWebpInputStream() {
public void testJpegFallback() throws Throwable {
final Bitmap bitmap = mWebpBitmapFactory.decodeStream(getTestJpegInputStream(), null, null);

// NULLSAFE_FIXME[Parameter Not Nullable]
assertNotNull("Bitmap should not be null", bitmap);
// NULLSAFE_FIXME[Nullable Dereference]
assertEquals("Width should be decoded properly", 20, bitmap.getWidth());
// NULLSAFE_FIXME[Nullable Dereference]
assertEquals("Height should be decoded properly", 20, bitmap.getHeight());

// NULLSAFE_FIXME[Nullable Dereference]
assertEquals("Bitmap pixels should be red", 0xFFFF0100, bitmap.getPixel(5, 8));
}

@Test
public void testWebpDecodeStream() throws Throwable {
final Bitmap bitmap = mWebpBitmapFactory.decodeStream(getTestWebpInputStream(), null, null);

// NULLSAFE_FIXME[Parameter Not Nullable]
assertNotNull("Bitmap should not be null", bitmap);
// NULLSAFE_FIXME[Nullable Dereference]
assertEquals("Width should be decoded properly", 20, bitmap.getWidth());
// NULLSAFE_FIXME[Nullable Dereference]
assertEquals("Height should be decoded properly", 20, bitmap.getHeight());

// NULLSAFE_FIXME[Nullable Dereference]
assertEquals("Bitmap pixels should be red", 0xFFFF0100, bitmap.getPixel(5, 8));
// Alternatively, load image manually adb pull /mnt/sdcard/resulthooked.jpg
// bitmap.compress(
Expand All @@ -116,6 +128,7 @@ public void testWebpJustDecodeBounds() throws Throwable {

final Bitmap bitmap = mWebpBitmapFactory.decodeStream(getTestWebpInputStream(), null, options);

// NULLSAFE_FIXME[Parameter Not Nullable]
assertNull("Bitmap should be null", bitmap);
assertEquals("Width should be decoded properly", 20, options.outWidth);
assertEquals("Height should be decoded properly", 20, options.outHeight);
Expand All @@ -131,8 +144,11 @@ public void testInBitmap() throws Throwable {
final Bitmap outBitmap =
mWebpBitmapFactory.decodeStream(getTestWebpInputStream(), null, options);

// NULLSAFE_FIXME[Parameter Not Nullable]
assertNotNull("Bitmap should not be null", outBitmap);
// NULLSAFE_FIXME[Parameter Not Nullable]
assertSame("Output bitmap shuold be the same as input bitmap", inBitmap, outBitmap);
// NULLSAFE_FIXME[Nullable Dereference]
assertEquals("Bitmap pixels should be red", 0xFFFF0100, outBitmap.getPixel(5, 8));
}

Expand All @@ -141,13 +157,15 @@ public void testByteArrayDecode() throws Throwable {
byte[] data = ByteStreams.toByteArray(getTestWebpInputStream());
final Bitmap bitmap = mWebpBitmapFactory.decodeByteArray(data, 0, data.length, null);

// NULLSAFE_FIXME[Parameter Not Nullable]
testBitmapDefault(bitmap, 20, 20);
}

@Test
public void testOutMimeType() throws Throwable {
BitmapFactory.Options options = new BitmapFactory.Options();

// NULLSAFE_FIXME[Not Vetted Third-Party]
if (options.outMimeType != null) {
// Not all devices are able to get this info from the image
assertEquals("Mime type should be detected properly", "image/webp", options.outMimeType);
Expand All @@ -163,6 +181,7 @@ public void testInTempStorage() throws Throwable {
options.inTempStorage = new byte[128 * 1024];

Bitmap bitmap = mWebpBitmapFactory.decodeStream(getTestWebpInputStream(), null, options);
// NULLSAFE_FIXME[Parameter Not Nullable]
testBitmapDefault(bitmap, 20, 20);
}

Expand All @@ -173,6 +192,7 @@ public void testInSampleSize() throws Throwable {

final Bitmap bitmap = mWebpBitmapFactory.decodeStream(getTestWebpInputStream(), null, options);

// NULLSAFE_FIXME[Parameter Not Nullable]
testBitmapDefault(bitmap, 10, 10);
}

Expand All @@ -182,6 +202,7 @@ public void testOutWidthHeight() throws Throwable {

final Bitmap bitmap = mWebpBitmapFactory.decodeStream(getTestWebpInputStream(), null, options);

// NULLSAFE_FIXME[Parameter Not Nullable]
assertNotNull("Bitmap should not be null", bitmap);
assertEquals("Width should be scaled", 20, options.outWidth);
assertEquals("Height should be scaled", 20, options.outHeight);
Expand All @@ -205,6 +226,7 @@ public void testWebpFileDescriptorDecode() throws Throwable {
FileDescriptor fd = getImageFileDescriptor("redsquare.webp");
final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(fd, null, null);

// NULLSAFE_FIXME[Parameter Not Nullable]
testBitmapDefault(bitmap, 20, 20);
}

Expand All @@ -213,6 +235,7 @@ public void testJpegFileDescriptorDecode() throws Throwable {
FileDescriptor fd = getImageFileDescriptor("redsquare.jpg");
final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(fd, null, null);

// NULLSAFE_FIXME[Parameter Not Nullable]
testBitmapDefault(bitmap, 20, 20);
}

Expand All @@ -226,6 +249,7 @@ public void testInScaled() throws Throwable {

final Bitmap bitmap = mWebpBitmapFactory.decodeStream(getTestWebpInputStream(), null, options);

// NULLSAFE_FIXME[Parameter Not Nullable]
testBitmapDefault(bitmap, 10, 10);
}

Expand All @@ -239,6 +263,7 @@ public void testInScaled2() throws Throwable {

final Bitmap bitmap = mWebpBitmapFactory.decodeStream(getTestWebpInputStream(), null, options);

// NULLSAFE_FIXME[Parameter Not Nullable]
testBitmapDefault(bitmap, 20, 20);
}

Expand All @@ -252,6 +277,7 @@ public void testInScaled3() throws Throwable {

final Bitmap bitmap = mWebpBitmapFactory.decodeStream(getTestWebpInputStream(), null, options);

// NULLSAFE_FIXME[Parameter Not Nullable]
testBitmapDefault(bitmap, 20, 20);
}

Expand Down

0 comments on commit 1e54801

Please sign in to comment.