Home | History | Annotate | Download | only in scripts
      1 _lite_test_general() {
      2   usage="
      3   Usage: $0 [-c CLASSNAME] [-d] [-a | -i] [-e], where
      4 
      5   -c CLASSNAME          Run tests only for the specified class/method. CLASSNAME
      6                           should be of the form SomeClassTest or SomeClassTest#testMethod.
      7   -d                    Waits for a debugger to attach before starting to run tests.
      8   -i                    Rebuild and reinstall the test apk before running tests (mmm).
      9   -a                    Rebuild all dependencies and reinstall the test apk before/
     10                           running tests (mmma).
     11   -e                    Run code coverage. Coverage will be output into the coverage/
     12                           directory in the repo root.
     13   -g                    Run build commands with USE_GOMA=true
     14   -h                    This help message.
     15   "
     16 
     17   local OPTIND=1
     18   local class=
     19   local project=
     20   local install=false
     21   local installwdep=false
     22   local debug=false
     23   local coverage=false
     24   local goma=false
     25 
     26   while getopts "c:p:hadieg" opt; do
     27     case "$opt" in
     28       h)
     29         echo "$usage"
     30         return 0;;
     31       \?)
     32         echo "$usage"
     33         return 0;;
     34       c)
     35         class=$OPTARG;;
     36       d)
     37         debug=true;;
     38       i)
     39         install=true;;
     40       a)
     41         install=true
     42         installwdep=true;;
     43       e)
     44         coverage=true;;
     45       g)
     46         goma=true;;
     47       p)
     48         project=$OPTARG;;
     49     esac
     50   done
     51 
     52   local build_dir=
     53   local apk_loc=
     54   local package_prefix=
     55   local instrumentation=
     56   case "$project" in
     57     "telecom")
     58       build_dir="packages/services/Telecomm/tests"
     59       apk_loc="data/app/TelecomUnitTests/TelecomUnitTests.apk"
     60       package_prefix="com.android.server.telecom.tests"
     61       instrumentation="android.support.test.runner.AndroidJUnitRunner";;
     62     "telephony")
     63       build_dir="frameworks/opt/telephony/tests/"
     64       apk_loc="data/app/FrameworksTelephonyTests/FrameworksTelephonyTests.apk"
     65       package_prefix="com.android.frameworks.telephonytests"
     66       instrumentation="android.support.test.runner.AndroidJUnitRunner";;
     67   esac
     68 
     69   local T=$(gettop)
     70 
     71   if [ $install = true ] ; then
     72     local olddir=$(pwd)
     73     local emma_opt=
     74     local goma_opt=
     75 
     76     cd $T
     77     # Build and exit script early if build fails
     78 
     79     if [ $coverage = true ] ; then
     80       emma_opt="EMMA_INSTRUMENT=true LOCAL_EMMA_INSTRUMENT=true EMMA_INSTRUMENT_STATIC=true"
     81     else
     82       emma_opt="EMMA_INSTRUMENT=false"
     83     fi
     84 
     85     if [ $goma = true ] ; then
     86         goma_opt="USE_GOMA=true"
     87     fi
     88 
     89     if [ $installwdep = true ] ; then
     90       (export ${emma_opt}; mmma ${goma_opt} -j40 "$build_dir")
     91     else
     92       (export ${emma_opt}; mmm ${goma_opt} "$build_dir")
     93     fi
     94     if [ $? -ne 0 ] ; then
     95       echo "Make failed! try using -a instead of -i if building with coverage"
     96       return
     97     fi
     98 
     99     # Strip off any possible aosp_ prefix from the target product
    100     local canonical_product=$(sed 's/^aosp_//' <<< "$TARGET_PRODUCT")
    101 
    102     adb install -r -t "out/target/product/$canonical_product/$apk_loc"
    103     if [ $? -ne 0 ] ; then
    104       cd "$olddir"
    105       return $?
    106     fi
    107     cd "$olddir"
    108   fi
    109 
    110   local e_options=""
    111   if [ -n "$class" ] ; then
    112     if [[ "$class" =~ "\." ]] ; then
    113       e_options="${e_options} -e class ${class}"
    114     else
    115       e_options="${e_options} -e class ${package_prefix}.${class}"
    116     fi
    117   fi
    118   if [ $debug = true ] ; then
    119     e_options="${e_options} -e debug 'true'"
    120   fi
    121   if [ $coverage = true ] && [ $project =~ "telecom" ] ; then
    122     e_options="${e_options} -e coverage 'true'"
    123   fi
    124   adb shell am instrument ${e_options} -w "$package_prefix/$instrumentation"
    125 
    126   # Code coverage only enabled for Telecom.
    127   if [ $coverage = true ] && [ $project =~ "telecom" ] ; then
    128     adb root
    129     adb wait-for-device
    130     adb pull /data/user/0/com.android.server.telecom.tests/files/coverage.ec /tmp/
    131     if [ ! -d "$T/coverage" ] ; then
    132       mkdir -p "$T/coverage"
    133     fi
    134     java -jar "$T/prebuilts/sdk/tools/jack-jacoco-reporter.jar" \
    135       --report-dir "$T/coverage/" \
    136       --metadata-file "$T/out/target/common/obj/APPS/TelecomUnitTests_intermediates/coverage.em" \
    137       --coverage-file "/tmp/coverage.ec" \
    138       --source-dir "$T/packages/services/Telecomm/src/"
    139   fi
    140 }
    141 
    142 lite_test_telecom() {
    143   _lite_test_general -p telecom $@
    144 }
    145 
    146 lite_test_telephony() {
    147   _lite_test_general -p telephony $@
    148 }
    149