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