1#!/bin/bash 2 3# 4# Copyright (C) 2024 The Android Open Source Project 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19# Experimental, simple script to run a robolectric test in AOSP development environment. 20# Intended for faster iteration and more specific options than atest. 21# 22# In particular this script will: 23# - only run tests on current SDK (35) 24# - use sqlite native mode 25# - load native libandroid_runtime built from HEAD 26# - output stdout and stderr directly to terminal 27# 28# Usage: 29# [m -j <test_module_name> libandroid_runtime] 30# run-android-test.sh <test_module_name> <test_class_name> 31 32set -e 33 34if [[ ${ANDROID_HOST_OUT_TESTCASES:-"unset"} == "unset" ]]; then 35 echo "ERROR: android build environment not initialized. Run build/envsetup.sh and lunch." 36 exit -1 37fi 38 39if [ $# -lt 2 -o $# -gt 2 ]; then 40 echo "ERROR: invalid number of arguments. Usage: run-android-test.sh <test_module_name> <test_class_name>." 41 exit -1 42fi 43 44MODULE_NAME=$1 45CLASS_NAME=$2 46 47# uncomment this to wait for debugger to attach 48#DEBUGGER=' -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000 ' 49DEBUGGER=' ' 50 51# android build system integration assumes working directory = test case directory. See https://robolectric.org/build-system-integration/ 52cd $ANDROID_HOST_OUT_TESTCASES/$MODULE_NAME 53 54java -cp $ANDROID_HOST_OUT_TESTCASES/$MODULE_NAME/$MODULE_NAME.jar:$ANDROID_HOST_OUT_TESTCASES/android-all/android-all-current-robolectric-r0.jar \ 55 -Drobolectric.dependency.dir=$ANDROID_HOST_OUT_TESTCASES/android-all \ 56 -Drobolectric.logging=stdout \ 57 -Drobolectric.logging.enabled=true \ 58 -Drobolectric.offline=true \ 59 -Drobolectric.resourcesMode=BINARY \ 60 -Drobolectric.graphicsMode=NATIVE \ 61 -Drobolectric.usePreinstrumentedJars=false \ 62 -Drobolectric.enabledSdks=36 \ 63 -Drobolectric.alwaysIncludeVariantMarkersInTestName=true \ 64 -Dandroid.robolectric.loadLibraryFromPath=true \ 65 -Djava.library.path=$ANDROID_HOST_OUT/lib64:/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib \ 66 -Drobolectric.sqliteMode=NATIVE \ 67 --add-opens=java.base/java.io=ALL-UNNAMED \ 68 $DEBUGGER \ 69 org.junit.runner.JUnitCore \ 70 $CLASS_NAME 71