1 #!/bin/bash 2 # Copyright 2018 The Android Open Source Project 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 # Usage '. runcts.sh' 17 18 function _runtest_cts_mediacomponent_usage() { 19 echo 'runtest-cts-MediaComponents [option]: Build, flash device,' 20 echo ' and run subset of CtsMediaTestCases that MediaComponents covers.' 21 echo ' *Warning* This bypasses CTS setup (e.g. download media contents from server)' 22 echo ' For running CTS in official way, use atest or cts-tradefed ' 23 echo ' -h|--help: This help' 24 echo ' --skip: Skip build and flash. Just rerun-tests' 25 echo ' --min: Only rebuild tests and updatable library.' 26 echo ' --test: Only rebuild tests' 27 echo ' -s [device_id]: Specify a device name to run test against.' 28 echo ' You can define ${ADBHOST} instead.' 29 echo ' -r [count]: Repeat tests for given count. It will stop when fails.' 30 echo ' --ignore: Keep repeating tests even when it fails.' 31 echo ' -t [test]: Only run the specific test. Can be either a class or a method.' 32 } 33 34 function runtest-cts-MediaComponents() { 35 # Edit here if you want to support other tests. 36 # List up libs and apks in the media_api needed for tests, and place test target at the last. 37 local TEST_PACKAGE_DIR=("frameworks/av/packages/MediaComponents/test") 38 local TEST_PACKAGE=("android.media.cts") 39 local BUILD_TARGETS=("MediaComponents" "CtsMediaTestCases") 40 # Don't include MediaComponents -- if we simply install it, system server 41 # wouldn't use the installed one. 42 local INSTALL_TARGETS=("CtsMediaTestCases") 43 local TEST_RUNNER="android.support.test.runner.AndroidJUnitRunner" 44 local DEPENDENCIES=("mockito-target-minus-junit4" "android-support-test" "compatibility-device-util") 45 local DEFAULT_TEST_TARGET="" 46 DEFAULT_TEST_TARGET+="android.media.cts.MediaBrowser2Test" 47 DEFAULT_TEST_TARGET+=",android.media.cts.MediaController2Test" 48 DEFAULT_TEST_TARGET+=",android.media.cts.MediaMetadata2Test" 49 DEFAULT_TEST_TARGET+=",android.media.cts.MediaSession2Test" 50 DEFAULT_TEST_TARGET+=",android.media.cts.MediaSession2_PermissionTest" 51 DEFAULT_TEST_TARGET+=",android.media.cts.MediaSessionManager_MediaSession2Test" 52 DEFAULT_TEST_TARGET+=",android.media.cts.SessionToken2Test" 53 if [[ -z "${ANDROID_BUILD_TOP}" ]]; then 54 echo "Needs to lunch a target first" 55 return 56 fi 57 58 local old_path=${OLDPWD} 59 while true; do 60 local OPTION_SKIP="false" 61 local OPTION_MIN="false" 62 local OPTION_TEST="false" 63 local OPTION_REPEAT_COUNT="1" 64 local OPTION_IGNORE="false" 65 local OPTION_TEST_TARGET="${DEFAULT_TEST_TARGET}" 66 local adbhost_local 67 while (( "$#" )); do 68 case "${1}" in 69 -h|--help) 70 _runtest_cts_mediacomponent_usage 71 return 72 ;; 73 --skip) 74 OPTION_SKIP="true" 75 ;; 76 --min) 77 OPTION_MIN="true" 78 ;; 79 --test) 80 OPTION_TEST="true" 81 ;; 82 -s) 83 shift 84 adbhost_local=${1} 85 ;; 86 -r) 87 shift 88 OPTION_REPEAT_COUNT="${1}" 89 ;; 90 --ignore) 91 OPTION_IGNORE="true" 92 ;; 93 -t) 94 shift 95 OPTION_TEST_TARGET="${1}" 96 esac 97 shift 98 done 99 100 # Build adb command. 101 local adb 102 if [[ -z "${adbhost_local}" ]]; then 103 adbhost_local=${ADBHOST} 104 fi 105 if [[ -z "${adbhost_local}" ]]; then 106 local device_count=$(adb devices | sed '/^[[:space:]]*$/d' | wc -l) 107 if [[ "${device_count}" != "2" ]]; then 108 echo "Too many devices. Specify a device." && break 109 fi 110 adb="adb" 111 else 112 adb="adb -s ${adbhost_local}" 113 fi 114 115 local target_dir="${ANDROID_BUILD_TOP}/${TEST_PACKAGE_DIR}" 116 #local TEST_PACKAGE=$(sed -n 's/^.*\bpackage\b="\([a-z0-9\.]*\)".*$/\1/p' ${target_dir}/AndroidManifest.xml) 117 118 if [[ "${OPTION_SKIP}" != "true" ]]; then 119 # Build dependencies if needed. 120 local dependency 121 local build_dependency="" 122 for dependency in ${DEPENDENCIES[@]}; do 123 if [[ "${dependency}" == "out/"* ]]; then 124 if [[ ! -f ${ANDROID_BUILD_TOP}/${dependency} ]]; then 125 build_dependency="true" 126 break 127 fi 128 else 129 if [[ "$(find ${OUT} -name ${dependency}_intermediates | wc -l)" == "0" ]]; then 130 build_dependency="true" 131 break 132 fi 133 fi 134 done 135 if [[ "${build_dependency}" == "true" ]]; then 136 echo "Building dependencies. Will only print stderr." 137 m ${DEPENDENCIES[@]} -j > /dev/null 138 fi 139 140 # Build test apk and required apk. 141 local build_targets 142 if [[ "${OPTION_TEST}" == "true" ]]; then 143 build_targets="${INSTALL_TARGETS[@]}" 144 elif [[ "${OPTION_MIN}" == "true" ]]; then 145 build_targets="${BUILD_TARGETS[@]}" 146 else 147 build_targets="${BUILD_TARGETS[@]} droid" 148 fi 149 m ${build_targets} -j || break 150 151 if [[ "${OPTION_TEST}" != "true" ]]; then 152 # Flash only when needed 153 local device_build_type="$(${adb} shell getprop ro.build.type)" 154 if [[ "${device_build_type}" == "user" ]]; then 155 # User build. Cannot adb sync 156 ${adb} reboot bootloader 157 fastboot flashall 158 else 159 ${adb} root 160 local device_verity_mode="$(${adb} shell getprop ro.boot.veritymode)" 161 if [[ "${device_verity_mode}" != "disabled" ]]; then 162 ${adb} disable-verity 163 ${adb} reboot 164 ${adb} wait-for-device || break 165 ${adb} root 166 fi 167 ${adb} remount 168 ${adb} shell stop 169 ${adb} shell setprop log.tag.MediaSessionService DEBUG 170 ${adb} sync 171 ${adb} shell start 172 fi 173 ${adb} wait-for-device || break 174 # Ensure package manager is loaded. 175 # TODO(jaewan): Find better way to wait 176 sleep 15 177 fi 178 179 # Install apks 180 local install_failed="false" 181 for target in ${INSTALL_TARGETS[@]}; do 182 local apk_path=$(find ${OUT}/system ${OUT}/data -name ${target}.apk) 183 local apk_num=$(find ${OUT}/system ${OUT}/data -name ${target}.apk | wc -l) 184 if [[ "${apk_num}" != "1" ]]; then 185 echo "Cannot locate a ${target}.apk. Found ${apk_num} apks" && break 186 fi 187 echo "Installing ${target}.apk. path=${apk_path}" 188 ${adb} install -r ${apk_path} 189 if [[ "${?}" != "0" ]]; then 190 install_failed="true" 191 break 192 fi 193 done 194 if [[ "${install_failed}" == "true" ]]; then 195 echo "Failed to install. Test wouldn't run." 196 break 197 fi 198 fi 199 200 local test_target="" 201 if [[ -n "${OPTION_TEST_TARGET}" ]]; then 202 test_target="-e class ${OPTION_TEST_TARGET}" 203 fi 204 205 local i 206 local tmpfile=$(tempfile) 207 for ((i=1; i <= ${OPTION_REPEAT_COUNT}; i++)); do 208 echo "Run test ${i}/${OPTION_REPEAT_COUNT}" 209 ${adb} shell am instrument ${test_target} -w ${TEST_PACKAGE}/${TEST_RUNNER} >& ${tmpfile} 210 cat ${tmpfile} 211 if [[ "${OPTION_IGNORE}" != "true" ]]; then 212 if [[ -n "$(grep ${tmpfile} -e 'FAILURE\|crashed')" ]]; then 213 # am instrument doesn't return error code so need to grep result message instead 214 break 215 fi 216 fi 217 done 218 rm ${tmpfile} 219 break 220 done 221 } 222 223 echo "Following functions are added to your environment:" 224 _runtest_cts_mediacomponent_usage 225