blob: 0cf0e44bc01b67060373090ff16a71dc37016b15 [file] [log] [blame]
Jaewan Kim1516ee92018-03-08 17:49:13 +09001#!/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
18function _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 ' -s [device_id]: Specify a device name to run test against.'
27 echo ' You can define ${ADBHOST} instead.'
28 echo ' -r [count]: Repeat tests for given count. It will stop when fails.'
29 echo ' --ignore: Keep repeating tests even when it fails.'
30 echo ' -t [test]: Only run the specific test. Can be either a class or a method.'
31}
32
33function runtest-cts-MediaComponents() {
34 # Edit here if you want to support other tests.
35 # List up libs and apks in the media_api needed for tests, and place test target at the last.
36 local TEST_PACKAGE_DIR=("frameworks/av/packages/MediaComponents/test")
37 local TEST_PACKAGE=("android.media.cts")
38 local BUILD_TARGETS=("MediaComponents" "CtsMediaTestCases")
39 # Don't include MediaComponents -- if we simply install it, system server
40 # wouldn't use the installed one.
41 local INSTALL_TARGETS=("CtsMediaTestCases")
42 local TEST_RUNNER="android.support.test.runner.AndroidJUnitRunner"
43 local DEPENDENCIES=("mockito-target-minus-junit4" "android-support-test" "compatibility-device-util")
44 local DEFAULT_TEST_TARGET=""
45 DEFAULT_TEST_TARGET+="android.media.cts.MediaBrowser2Test"
46 DEFAULT_TEST_TARGET+=",android.media.cts.MediaController2Test"
47 DEFAULT_TEST_TARGET+=",android.media.cts.MediaMetadata2Test"
48 DEFAULT_TEST_TARGET+=",android.media.cts.MediaSession2Test"
49 DEFAULT_TEST_TARGET+=",android.media.cts.MediaSession2_PermissionTest"
50 DEFAULT_TEST_TARGET+=",android.media.cts.MediaSessionManager_MediaSession2Test"
51 DEFAULT_TEST_TARGET+=",android.media.cts.SessionToken2Test"
52 if [[ -z "${ANDROID_BUILD_TOP}" ]]; then
53 echo "Needs to lunch a target first"
54 return
55 fi
56
57 local old_path=${OLDPWD}
58 while true; do
59 local OPTION_SKIP="false"
60 local OPTION_MIN="false"
61 local OPTION_REPEAT_COUNT="1"
62 local OPTION_IGNORE="false"
63 local OPTION_TEST_TARGET="${DEFAULT_TEST_TARGET}"
64 local adbhost_local
65 while (( "$#" )); do
66 case "${1}" in
67 -h|--help)
68 _runtest_cts_mediacomponent_usage
69 return
70 ;;
71 --skip)
72 OPTION_SKIP="true"
73 ;;
74 --min)
75 OPTION_MIN="true"
76 ;;
77 -s)
78 shift
79 adbhost_local=${1}
80 ;;
81 -r)
82 shift
83 OPTION_REPEAT_COUNT="${1}"
84 ;;
85 --ignore)
86 OPTION_IGNORE="true"
87 ;;
88 -t)
89 shift
90 OPTION_TEST_TARGET="${1}"
91 esac
92 shift
93 done
94
95 # Build adb command.
96 local adb
97 if [[ -z "${adbhost_local}" ]]; then
98 adbhost_local=${ADBHOST}
99 fi
100 if [[ -z "${adbhost_local}" ]]; then
101 local device_count=$(adb devices | sed '/^[[:space:]]*$/d' | wc -l)
102 if [[ "${device_count}" != "2" ]]; then
103 echo "Too many devices. Specify a device." && break
104 fi
105 adb="adb"
106 else
107 adb="adb -s ${adbhost_local}"
108 fi
109
110 local target_dir="${ANDROID_BUILD_TOP}/${TEST_PACKAGE_DIR}"
111 #local TEST_PACKAGE=$(sed -n 's/^.*\bpackage\b="\([a-z0-9\.]*\)".*$/\1/p' ${target_dir}/AndroidManifest.xml)
112
113 if [[ "${OPTION_SKIP}" != "true" ]]; then
114 # Build dependencies if needed.
115 local dependency
116 local build_dependency=""
117 for dependency in ${DEPENDENCIES[@]}; do
118 if [[ "${dependency}" == "out/"* ]]; then
119 if [[ ! -f ${ANDROID_BUILD_TOP}/${dependency} ]]; then
120 build_dependency="true"
121 break
122 fi
123 else
124 if [[ "$(find ${OUT} -name ${dependency}_intermediates | wc -l)" == "0" ]]; then
125 build_dependency="true"
126 break
127 fi
128 fi
129 done
130 if [[ "${build_dependency}" == "true" ]]; then
131 echo "Building dependencies. Will only print stderr."
132 m ${DEPENDENCIES[@]} -j > /dev/null
133 fi
134
135 # Build test apk and required apk.
136 local build_targets="${BUILD_TARGETS[@]}"
137 if [[ "${OPTION_MIN}" != "true" ]]; then
138 build_targets="${build_targets} droid"
139 fi
140 m ${build_targets} -j || break
141
142 local device_build_type="$(${adb} shell getprop ro.build.type)"
143 if [[ "${device_build_type}" == "user" ]]; then
144 # User build. Cannot adb sync
145 ${adb} reboot bootloader
146 fastboot flashall
147 else
148 ${adb} root
149 local device_verity_mode="$(${adb} shell getprop ro.boot.veritymode)"
150 if [[ "${device_verity_mode}" != "disabled" ]]; then
151 ${adb} disable-verity
152 ${adb} reboot
153 ${adb} wait-for-device || break
154 ${adb} root
155 fi
156 ${adb} remount
157 ${adb} shell stop
158 ${adb} shell setprop log.tag.MediaSessionService DEBUG
159 ${adb} sync
160 ${adb} shell start
161 fi
162 ${adb} wait-for-device || break
163 # Ensure package manager is loaded.
164 # TODO(jaewan): Find better way to wait
165 sleep 15
166
167 # Install apks
168 local install_failed="false"
169 for target in ${INSTALL_TARGETS[@]}; do
170 local apk_path=$(find ${OUT}/system ${OUT}/data -name ${target}.apk)
171 local apk_num=$(find ${OUT}/system ${OUT}/data -name ${target}.apk | wc -l)
172 if [[ "${apk_num}" != "1" ]]; then
173 echo "Cannot locate a ${target}.apk. Found ${apk_num} apks" && break
174 fi
175 echo "Installing ${target}.apk. path=${apk_path}"
176 ${adb} install -r ${apk_path}
177 if [[ "${?}" != "0" ]]; then
178 install_failed="true"
179 break
180 fi
181 done
182 if [[ "${install_failed}" == "true" ]]; then
183 echo "Failed to install. Test wouldn't run."
184 break
185 fi
186 fi
187
188 local test_target=""
189 if [[ -n "${OPTION_TEST_TARGET}" ]]; then
190 test_target="-e class ${OPTION_TEST_TARGET}"
191 fi
192
193 local i
194 local tmpfile=$(tempfile)
195 for ((i=1; i <= ${OPTION_REPEAT_COUNT}; i++)); do
196 echo "Run test ${i}/${OPTION_REPEAT_COUNT}"
197 ${adb} shell am instrument ${test_target} -w ${TEST_PACKAGE}/${TEST_RUNNER} >& ${tmpfile}
198 cat ${tmpfile}
199 if [[ "${OPTION_IGNORE}" != "true" ]]; then
200 if [[ -n "$(grep ${tmpfile} -e 'FAILURE\|crashed')" ]]; then
201 # am instrument doesn't return error code so need to grep result message instead
202 break
203 fi
204 fi
205 done
206 rm ${tmpfile}
207 break
208 done
209}
210
211echo "Following functions are added to your environment:"
212_runtest_cts_mediacomponent_usage