From 5aac7f82c273daac1a5b812203fa1064a9033f58 Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Wed, 16 Oct 2024 11:02:53 -0700 Subject: [PATCH] Test binary XML image formats Reviewed By: oprisnik Differential Revision: D64345016 --- .../imageformat/ImageFormatCheckerTest.kt | 21 ++++++++ .../imageformat/xmls/AndroidManifest.xml | 5 ++ .../com/facebook/imageformat/xmls/README.md | 14 +++++ .../imageformat/xmls/compiled/layer_list.xml | Bin 0 -> 692 bytes .../imageformat/xmls/compiled/level_list.xml | Bin 0 -> 624 bytes .../imageformat/xmls/compiled/state_list.xml | Bin 0 -> 660 bytes .../xmls/compiled/vector_drawable.xml | Bin 0 -> 496 bytes .../com/facebook/imageformat/xmls/convert.sh | 51 ++++++++++++++++++ .../xmls/raw/drawable/layer_list.xml | 15 ++++++ .../xmls/raw/drawable/level_list.xml | 12 +++++ .../xmls/raw/drawable/state_list.xml | 5 ++ .../xmls/raw/drawable/vector_drawable.xml | 13 +++++ 12 files changed, 136 insertions(+) create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/AndroidManifest.xml create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/README.md create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/compiled/layer_list.xml create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/compiled/level_list.xml create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/compiled/state_list.xml create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/compiled/vector_drawable.xml create mode 100755 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/convert.sh create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/layer_list.xml create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/level_list.xml create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/state_list.xml create mode 100644 imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/vector_drawable.xml diff --git a/imagepipeline-base/src/test/java/com/facebook/imageformat/ImageFormatCheckerTest.kt b/imagepipeline-base/src/test/java/com/facebook/imageformat/ImageFormatCheckerTest.kt index efc637c82a..ea3509f540 100644 --- a/imagepipeline-base/src/test/java/com/facebook/imageformat/ImageFormatCheckerTest.kt +++ b/imagepipeline-base/src/test/java/com/facebook/imageformat/ImageFormatCheckerTest.kt @@ -70,6 +70,27 @@ class ImageFormatCheckerTest constructor() { singleImageTypeTest(getName("heifs/1.heif"), DefaultImageFormats.HEIF) } + @Test + fun testXmlVectorDrawable() { + singleImageTypeTest( + getName("xmls/compiled/vector_drawable.xml"), DefaultImageFormats.BINARY_XML) + } + + @Test + fun testXmlLayerListDrawable() { + singleImageTypeTest(getName("xmls/compiled/layer_list.xml"), DefaultImageFormats.BINARY_XML) + } + + @Test + fun testXmlLevelListDrawable() { + singleImageTypeTest(getName("xmls/compiled/level_list.xml"), DefaultImageFormats.BINARY_XML) + } + + @Test + fun testXmlStateListDrawable() { + singleImageTypeTest(getName("xmls/compiled/state_list.xml"), DefaultImageFormats.BINARY_XML) + } + private fun singleImageTypeTest(resourceNames: List, expectedImageType: ImageFormat) { for (name: String in resourceNames) { val resourceStream = getResourceStream(name) diff --git a/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/AndroidManifest.xml b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/AndroidManifest.xml new file mode 100644 index 0000000000..2cfa7b7e5f --- /dev/null +++ b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + diff --git a/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/README.md b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/README.md new file mode 100644 index 0000000000..ed62941db3 --- /dev/null +++ b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/README.md @@ -0,0 +1,14 @@ +# XMLs + +## Context + +Android compiles raw XML to binary XML during app build as layout inflation at runtime does not support parsing raw XML files. As a result, Fresco only supports loading binary XML files since it relies on the Android to perform layout inflation. Therefore, we need to mock the build step that performs this compilation with the Android packaging tool. This directory houses raw, uncompiled files and their compiled counterparts in respective directories so we can test against them. + +## How to add/update assets + +These instructions are for POSIX devices. If you are on Windows, you will have to manually run these steps or use Android Studio to generate an APK + +1. Install Android's command-line tools by [following these instructions](https://developer.android.com/tools). Make sure you have `$ANDROID_HOME` set in your PATH by running `echo $ANDROID_HOME` +1. Install the latest packages of Android's build tools and platform. For example, you can run `sdkmanager "build-tools;34.0.0"` or `sdkmanager "platforms;android-33"`. [More instructions for sdkmanager can be found here](https://developer.android.com/tools/sdkmanager) +1. Run `./convert.sh` to automatically compile and extract resources into the `compiled` folder. It will use the latest available version of your build-tools and platforms. +1. That's it! diff --git a/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/compiled/layer_list.xml b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/compiled/layer_list.xml new file mode 100644 index 0000000000000000000000000000000000000000..e50b6cdff81441f65353a0ccdb3ea4b58f011487 GIT binary patch literal 692 zcmb`EJx;?=5QJxKM=>cNky20~Dnx-qLX9Xm0e1+*K*W!uI08|@HMjr;63}o0E(YJe z_{kxnWUbMzXUDsvb*fySiDcSR*Q7Nyaom*ZQ}2QUa0E`k3wQ-zAlFzkjdTxAb*%!f zt&70jX`;CvU{Z3aDODCU=e)aaq}%X)iE<3Hr%O6>l!qGfR62txbc3?eU`0QmThbfC zPjy9}p{>IE@41Xths`706sY#8Cn()T7F_xck0;BuNq#o751GA_-+-(ecuuBoA|tmz z0>9(A=DpXZ?uW}!5U%k}De%5WPVE|>!)>IvLxsd?m9T4=2a z(IZ&a+*HpkuE&{mJikFdLv^luRjfjn8na5>!OXQnS!(o0KO(y!H-<0tz`jLW#<*RV z(mIUO?dG|_U!bhuyLeUXVbyEM<9**k2%q2!C=7H=j;GQOma%so!=E;I=MIR6u>uZe z&4k@&oVWGxt#Lh@YuTLR4??ESi#fg|aRd8VJ%@W(YjZ94xM8=yWA{DW*Y=+tNtf=) YEUmsr2%7cNJpBgm*+M;M;?sNm0xsToumAu6 literal 0 HcmV?d00001 diff --git a/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/compiled/state_list.xml b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/compiled/state_list.xml new file mode 100644 index 0000000000000000000000000000000000000000..58bb851b20e549224b5e0e5b4d39c7e6d1d23938 GIT binary patch literal 660 zcmaixF;2rk5JmsSfw)0JChT zint=WR;F9_Ol!W2Kh-VWOYmh~9nrUmoP|~qe@cJF+Ui1Qtg>dzL6(~K@KdUVa&R*` z*6iQ>j(_tq3e#KHQ)eg?V5K3to;)`|k0Fi!{~SViQ3$WN!l90-adrB{6852E@+Y0V zYx~6G$iM+J=7!xpuG>bOjd9LqE}kRSd6T0i*XVU~u9<@zErW-oo~_T{=$hMj+kd*o f#Q$nFerD0e(H+ z5uYB_Om|InZSN$i>1dLvsiQ=7j{Z5-6L}lF0p}0k3;ckx_F8D7Tyv@l`;>adnNye8 z7g~qBsu9^lTcx4H{alr6qn^5~OykhMLYb*^qwjE=YK3k@$C7i)x3W-Q`|@EX*>O9@ zJw@k}Z0P%+^3!Un!wt=B0A7IXSq->5a3Ye>Lk<6N!TVi>y3TTqM+w9}-mvxl`+ahb trwv|kWy9P#5BG?i`@h277rft9$=yFjQq1;nqlx_E1@E(2p^tq0mp>v!T!{bx literal 0 HcmV?d00001 diff --git a/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/convert.sh b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/convert.sh new file mode 100755 index 0000000000..a888ed4062 --- /dev/null +++ b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/convert.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# 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. + +if [ -z "$ANDROID_HOME" ]; then + echo "Environment variable ANDROID_HOME is not set" + exit 1 +fi + +SCRIPT_DIR="$(readlink -f "$(dirname "$0")")" +cd "$SCRIPT_DIR" || exit + +# Use the latest available AAPT binary +BUILD_TOOLS_DIR="$ANDROID_HOME/build-tools" +LATEST_TOOLS_DIR=$(find "$BUILD_TOOLS_DIR" -maxdepth 1 -type d -print | sort -rn --key=4.1 | head -1) + +if [ ! -d "$LATEST_TOOLS_DIR" ]; then + echo "Could not find build tools in $BUILD_TOOLS_DIR" + exit 1 +fi + +# Use the latest available Android version +ANDROID_PLATFORMS_DIR="$ANDROID_HOME/platforms" +LATEST_PLATFORM_DIR=$(find "$ANDROID_PLATFORMS_DIR" -maxdepth 1 -type d -print | sort -rn --key=4.1 | head -1) + +if [ ! -d "$LATEST_PLATFORM_DIR" ]; then + echo "Could not find any platforms in $ANDROID_PLATFORMS_DIR" + exit 1 +fi + +# Define a temporary directory for the APK +TMP_DIR=$(mktemp -d) +APK_OUTPUT="$TMP_DIR/app.apk" + +# Build an APK with our raw resources +"$LATEST_TOOLS_DIR/aapt" package -f -m -M AndroidManifest.xml -S raw -0 "" -I "$LATEST_PLATFORM_DIR/android.jar" -F "$APK_OUTPUT" || exit 1 + +# Unzip all APK artifacts +cd "$TMP_DIR" || exit +unzip -q "app.apk" + +# Copy all compiled resources from the base drawable folder +cd "$SCRIPT_DIR" || exit +rm -rf compiled +mkdir compiled +cp -R "$TMP_DIR/res/drawable/" ./compiled + +# Remove the temporary folder +rm -rf "$TMP_DIR" diff --git a/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/layer_list.xml b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/layer_list.xml new file mode 100644 index 0000000000..387e8f1903 --- /dev/null +++ b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/layer_list.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/level_list.xml b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/level_list.xml new file mode 100644 index 0000000000..8a658c9952 --- /dev/null +++ b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/level_list.xml @@ -0,0 +1,12 @@ + + + + + diff --git a/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/state_list.xml b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/state_list.xml new file mode 100644 index 0000000000..159e77f8fe --- /dev/null +++ b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/state_list.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/vector_drawable.xml b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/vector_drawable.xml new file mode 100644 index 0000000000..b84f8093b6 --- /dev/null +++ b/imagepipeline-base/src/test/resources/com/facebook/imageformat/xmls/raw/drawable/vector_drawable.xml @@ -0,0 +1,13 @@ + + + +