diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/fresco/HeifExifOrientationTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/fresco/HeifExifOrientationTest.kt new file mode 100644 index 000000000000..849f42d862b1 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/fresco/HeifExifOrientationTest.kt @@ -0,0 +1,84 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.modules.fresco + +import com.facebook.imageutils.HeifExifUtil +import java.io.ByteArrayInputStream +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +/** + * Fresco reads a HEIF image's EXIF orientation with `androidx.exifinterface`, which it does not + * declare as a dependency. ReactAndroid declares it, and this test fails with a + * `NoClassDefFoundError` if that dependency is removed. + */ +@RunWith(RobolectricTestRunner::class) +class HeifExifOrientationTest { + + @Test + fun heifExifUtil_readsTheOrientationWithExifInterface() { + assertThat(HeifExifUtil.getOrientation(ByteArrayInputStream(EXIF_ROTATED_90))).isEqualTo(6) + } + + private companion object { + // An EXIF block whose only tag is Orientation (0x0112) = 6, "rotate 90 CW", in the smallest + // file ExifInterface reads: a JPEG with no image data. HeifExifUtil passes the stream to + // ExifInterface without checking its format. + private val EXIF_ROTATED_90 = + byteArrayOf( + // SOI + 0xFF.toByte(), + 0xD8.toByte(), + // APP1, 34 bytes, "Exif\0\0" + 0xFF.toByte(), + 0xE1.toByte(), + 0x00, + 0x22, + 0x45, + 0x78, + 0x69, + 0x66, + 0x00, + 0x00, + // TIFF header: big-endian, first IFD at offset 8 + 0x4D, + 0x4D, + 0x00, + 0x2A, + 0x00, + 0x00, + 0x00, + 0x08, + // IFD with one entry: Orientation, SHORT, count 1, value 6 + 0x00, + 0x01, + 0x01, + 0x12, + 0x00, + 0x03, + 0x00, + 0x00, + 0x00, + 0x01, + 0x00, + 0x06, + 0x00, + 0x00, + // No next IFD + 0x00, + 0x00, + 0x00, + 0x00, + // EOI + 0xFF.toByte(), + 0xD9.toByte(), + ) + } +}