1#!/bin/bash
2
3readonly OUT_DIR="$1"
4readonly DIST_DIR="$2"
5readonly BUILD_NUMBER="$3"
6
7readonly SCRIPT_DIR="$(dirname "$0")"
8readonly SCRIPT_TO_TOP="../../../../../.."
9
10readonly FAILURE_DIR=layoutlib-test-failures
11readonly FAILURE_ZIP=layoutlib-test-failures.zip
12
13STUDIO_JDK=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/prebuilts/jdk/jdk11/linux-x86"
14MISC_COMMON=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/prebuilts/misc/common"
15OUT_INTERMEDIATES=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/out/soong/.intermediates"
16NATIVE_LIBRARIES=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/out/host/linux-x86/lib64/"
17JAVA_LIBRARIES=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/out/soong/.intermediates/platform_testing/libraries/screenshot/deviceless/layoutlib/tests/res/testApp/MyApplication/layoutlib-myapplication/android_common/javac"
18SDK=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/out/host/linux-x86/sdk/sdk*/android-sdk*"
19SDK_REPO=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/out/host/linux-x86/sdk-repo"
20FONT_DIR=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/out/host/common/obj/PACKAGING/fonts_intermediates"
21KEYBOARD_DIR=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/out/host/common/obj/PACKAGING/keyboards_intermediates"
22ICU_DATA_PATH=${SCRIPT_DIR}"/${SCRIPT_TO_TOP}/out/host/linux-x86/com.android.i18n/etc/icu/icudt71l.dat"
23TMP_DIR=$(mktemp -d)
24PLATFORM=${TMP_DIR}/"android"
25
26# Copy resources to a temp directory
27cp -r ${SDK}/platforms/android* ${PLATFORM}
28
29# Unzip build-tools to access aapt2
30mkdir ${TMP_DIR}/build-tools
31unzip -q ${SDK_REPO}/sdk-repo-linux-build-tools.zip -d ${TMP_DIR}/build-tools
32
33# Compile 9-patch files
34mkdir ${TMP_DIR}/compiled
35mkdir ${TMP_DIR}/manifest
36echo \
37'<?xml version="1.0" encoding="utf-8"?>
38<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.google.android.layoutlib" />' \
39> ${TMP_DIR}/manifest/AndroidManifest.xml
40find ${SDK}/platforms/android*/data/res -name "*.9.png" -print0 | xargs -0 ${TMP_DIR}/build-tools/android-*/aapt2 compile -o ${TMP_DIR}/compiled/
41find ${TMP_DIR}/compiled -name "*.flat" -print0 | xargs -0 -s 1000000 ${TMP_DIR}/build-tools/android-*/aapt2 link -o ${TMP_DIR}/compiled.apk --manifest ${TMP_DIR}/manifest/AndroidManifest.xml -R
42unzip -q ${TMP_DIR}/compiled.apk -d ${TMP_DIR}
43for f in ${TMP_DIR}/res/*; do mv "$f" "${f/-v4/}";done
44cp -RL ${TMP_DIR}/res ${PLATFORM}/data
45
46# Run layoutlib tests
47${STUDIO_JDK}/bin/java -ea \
48    -Dnative.lib.path=${NATIVE_LIBRARIES} \
49    -Dfont.dir=${FONT_DIR} \
50    -Dicu.data.path=${ICU_DATA_PATH} \
51    -Dkeyboard.dir=${KEYBOARD_DIR} \
52    -Dplatform.dir=${PLATFORM} \
53    -Dtest_res.dir=${SCRIPT_DIR}/res \
54    -Dtest_failure.dir=${OUT_DIR}/${FAILURE_DIR} \
55    -cp ${MISC_COMMON}/tools-common/tools-common-prebuilt.jar:${MISC_COMMON}/ninepatch/ninepatch-prebuilt.jar:${MISC_COMMON}/sdk-common/sdk-common.jar:${MISC_COMMON}/kxml2/kxml2-2.3.0.jar:${MISC_COMMON}/layoutlib_api/layoutlib_api-prebuilt.jar:${OUT_INTERMEDIATES}/prebuilts/tools/common/m2/trove-prebuilt/linux_glibc_common/combined/trove-prebuilt.jar:${OUT_INTERMEDIATES}/external/junit/junit/linux_glibc_common/javac/junit.jar:${OUT_INTERMEDIATES}/external/guava/guava-jre/linux_glibc_common/javac/guava-jre.jar:${OUT_INTERMEDIATES}/external/hamcrest/hamcrest-core/hamcrest/linux_glibc_common/javac/hamcrest.jar:${OUT_INTERMEDIATES}/external/mockito/mockito/linux_glibc_common/combined/mockito.jar:${OUT_INTERMEDIATES}/external/objenesis/objenesis/linux_glibc_common/javac/objenesis.jar:${OUT_INTERMEDIATES}/frameworks/layoutlib/bridge/layoutlib/linux_glibc_common/withres/layoutlib.jar:${OUT_INTERMEDIATES}/frameworks/layoutlib/temp_layoutlib/linux_glibc_common/gen/temp_layoutlib.jar:${OUT_INTERMEDIATES}/frameworks/layoutlib/bridge/tests/layoutlib-tests/linux_glibc_common/withres/layout-testlib.jar:${OUT_INTERMEDIATES}/platform_testing/libraries/screenshot/deviceless/layoutlib/tests/platform-layoutlib-sample-tests/linux_glibc_common/withres/platform-layoutlib-sample-tests.jar:${OUT_INTERMEDIATES}/platform_testing/libraries/screenshot/deviceless/layoutlib/tests/res/testApp/MyApplication/layoutlib-myapplication/android_common/javac/layoutlib-myapplication.jar \
56    org.junit.runner.JUnitCore \
57    com.android.layoutlib.bridge.android.Main
58
59test_exit_code=$?
60
61# Create zip of all failure screenshots
62if [[ -d "${OUT_DIR}/${FAILURE_DIR}" ]]; then
63    zip -q -j -r ${OUT_DIR}/${FAILURE_ZIP} ${OUT_DIR}/${FAILURE_DIR}
64fi
65
66# Move failure zip to dist directory if specified
67if [[ -d "${DIST_DIR}" ]] && [[ -e "${OUT_DIR}/${FAILURE_ZIP}" ]]; then
68    mv ${OUT_DIR}/${FAILURE_ZIP} ${DIST_DIR}
69fi
70
71# Clean
72rm -rf ${TMP_DIR}
73rm -rf ${OUT_DIR}/${FAILURE_DIR}
74
75exit ${test_exit_code}
76
77