Skip to content

Commit 2df16e5

Browse files
authored
Test that ReactAndroid ships the ExifInterface Fresco needs
## Summary: #60371 made ReactAndroid depend on `androidx.exifinterface`, which Fresco 3.9.0 uses to read a HEIF image's EXIF orientation but does not declare. No test failed if that dependency was removed. The new `HeifExifOrientationTest` calls Fresco's `HeifExifUtil.getOrientation`, the method that crashed, on a stream that holds an EXIF orientation tag, and expects that orientation back. It goes through Fresco instead of loading the class by name, so it checks what an app needs: if a later Fresco stops using ExifInterface, the test passes without the dependency. The stream is a JPEG with no image data, the smallest file ExifInterface reads: `HeifExifUtil` passes the stream to `ExifInterface` without checking its format, and parsing a real HEIF goes through `MediaMetadataRetriever`, which Robolectric only stubs. ## Changelog: [INTERNAL] [ADDED] - Test that ReactAndroid provides the ExifInterface class Fresco uses for HEIF images ## Test Plan: ```sh ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests com.facebook.react.modules.fresco.HeifExifOrientationTest -Preact.internal.useHermesStable=true --console=plain ``` The test passes. With `implementation(libs.androidx.exifinterface)` removed from `ReactAndroid/build.gradle.kts` it fails as an app did before #60371: ``` java.lang.NoClassDefFoundError: androidx/exifinterface/media/ExifInterface at com.facebook.imageutils.HeifExifUtil.getOrientation(HeifExifUtil.kt:31) ``` ktfmt is clean. Differential Revision: D135308452 Pull Request resolved: #60420
1 parent e83162a commit 2df16e5

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.modules.fresco
9+
10+
import com.facebook.imageutils.HeifExifUtil
11+
import java.io.ByteArrayInputStream
12+
import org.assertj.core.api.Assertions.assertThat
13+
import org.junit.Test
14+
import org.junit.runner.RunWith
15+
import org.robolectric.RobolectricTestRunner
16+
17+
/**
18+
* Fresco reads a HEIF image's EXIF orientation with `androidx.exifinterface`, which it does not
19+
* declare as a dependency. ReactAndroid declares it, and this test fails with a
20+
* `NoClassDefFoundError` if that dependency is removed.
21+
*/
22+
@RunWith(RobolectricTestRunner::class)
23+
class HeifExifOrientationTest {
24+
25+
@Test
26+
fun heifExifUtil_readsTheOrientationWithExifInterface() {
27+
assertThat(HeifExifUtil.getOrientation(ByteArrayInputStream(EXIF_ROTATED_90))).isEqualTo(6)
28+
}
29+
30+
private companion object {
31+
// An EXIF block whose only tag is Orientation (0x0112) = 6, "rotate 90 CW", in the smallest
32+
// file ExifInterface reads: a JPEG with no image data. HeifExifUtil passes the stream to
33+
// ExifInterface without checking its format.
34+
private val EXIF_ROTATED_90 =
35+
byteArrayOf(
36+
// SOI
37+
0xFF.toByte(),
38+
0xD8.toByte(),
39+
// APP1, 34 bytes, "Exif\0\0"
40+
0xFF.toByte(),
41+
0xE1.toByte(),
42+
0x00,
43+
0x22,
44+
0x45,
45+
0x78,
46+
0x69,
47+
0x66,
48+
0x00,
49+
0x00,
50+
// TIFF header: big-endian, first IFD at offset 8
51+
0x4D,
52+
0x4D,
53+
0x00,
54+
0x2A,
55+
0x00,
56+
0x00,
57+
0x00,
58+
0x08,
59+
// IFD with one entry: Orientation, SHORT, count 1, value 6
60+
0x00,
61+
0x01,
62+
0x01,
63+
0x12,
64+
0x00,
65+
0x03,
66+
0x00,
67+
0x00,
68+
0x00,
69+
0x01,
70+
0x00,
71+
0x06,
72+
0x00,
73+
0x00,
74+
// No next IFD
75+
0x00,
76+
0x00,
77+
0x00,
78+
0x00,
79+
// EOI
80+
0xFF.toByte(),
81+
0xD9.toByte(),
82+
)
83+
}
84+
}

0 commit comments

Comments
 (0)