blob: 5c0ef516272f8b3e1c76aecb2039db59ade50058 [file] [log] [blame]
Jaewan Kim35a6aa32018-01-21 20:54:43 +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 '. runtest.sh'
17
18function _runtest_mediacomponent_usage() {
19 echo 'runtest-MediaComponents [option]: Run MediaComponents test'
20 echo ' -h|--help: This help'
21 echo ' --skip: Skip build. Just rerun-tests.'
22 echo ' --min: Only rebuild test apk and updatable library.'
23 echo ' -s [device_id]: Specify a device name to run test against.'
24 echo ' You can define ${ADBHOST} instead.'
25 echo ' -r [count]: Repeat tests for given count. It will stop when fails.'
26 echo ' --ignore: Keep repeating tests even when it fails.'
27 echo ' -t [test]: Only run the specific test. Can be either a class or a method.'
28}
29
30function runtest-MediaComponents() {
31 # Edit here if you want to support other tests.
32 # List up libs and apks in the media_api needed for tests, and place test target at the last.
33 local TEST_PACKAGE_DIR=("frameworks/av/packages/MediaComponents/test")
34 local BUILD_TARGETS=("MediaComponents" "MediaComponentsTest")
35 local INSTALL_TARGETS=("MediaComponentsTest")
36 local TEST_RUNNER="android.support.test.runner.AndroidJUnitRunner"
37 local DEPENDENCIES=("mockito-target-minus-junit4" "android-support-test" "compatibility-device-util")
38
39 if [[ -z "${ANDROID_BUILD_TOP}" ]]; then
40 echo "Needs to lunch a target first"
41 return
42 fi
43
44 local old_path=${OLDPWD}
45 while true; do
46 local OPTION_SKIP="false"
47 local OPTION_MIN="false"
48 local OPTION_REPEAT_COUNT="1"
49 local OPTION_IGNORE="false"
50 local OPTION_TEST_TARGET=""
51 local adbhost_local
52 while (( "$#" )); do
53 case "${1}" in
54 -h|--help)
55 _runtest_mediacomponent_usage
56 return
57 ;;
58 --skip)
59 OPTION_SKIP="true"
60 ;;
61 --min)
62 OPTION_MIN="true"
63 ;;
64 -s)
65 shift
66 adbhost_local=${1}
67 ;;
68 -r)
69 shift
70 OPTION_REPEAT_COUNT="${1}"
71 ;;
72 --ignore)
73 OPTION_IGNORE="true"
74 ;;
75 -t)
76 shift
77 OPTION_TEST_TARGET="${1}"
78 esac
79 shift
80 done
81
82 # Build adb command.
83 local adb
84 if [[ -z "${adbhost_local}" ]]; then
85 adbhost_local=${ADBHOST}
86 fi
87 if [[ -z "${adbhost_local}" ]]; then
88 local device_count=$(adb devices | sed '/^[[:space:]]*$/d' | wc -l)
89 if [[ "${device_count}" != "2" ]]; then
90 echo "Too many devices. Specify a device." && break
91 fi
92 adb="adb"
93 else
94 adb="adb -s ${adbhost_local}"
95 fi
96
97 local target_dir="${ANDROID_BUILD_TOP}/${TEST_PACKAGE_DIR}"
98 local TEST_PACKAGE=$(sed -n 's/^.*\bpackage\b="\([a-z0-9\.]*\)".*$/\1/p' ${target_dir}/AndroidManifest.xml)
99
100 if [[ "${OPTION_SKIP}" != "true" ]]; then
101 # Build dependencies if needed.
102 local dependency
103 local build_dependency=""
104 for dependency in ${DEPENDENCIES[@]}; do
105 if [[ "${dependency}" == "out/"* ]]; then
106 if [[ ! -f ${ANDROID_BUILD_TOP}/${dependency} ]]; then
107 build_dependency="true"
108 break
109 fi
110 else
111 if [[ "$(find ${OUT} -name ${dependency}_intermediates | wc -l)" == "0" ]]; then
112 build_dependency="true"
113 break
114 fi
115 fi
116 done
117 if [[ "${build_dependency}" == "true" ]]; then
118 echo "Building dependencies. Will only print stderr."
119 m ${DEPENDENCIES[@]} -j > /dev/null
120 fi
121
122 # Build test apk and required apk.
123 local build_targets="${BUILD_TARGETS[@]}"
124 if [[ "${OPTION_MIN}" != "true" ]]; then
125 build_targets="${build_targets} droid"
126 fi
Jaewan Kim3bad8ce2018-01-22 16:56:45 +0900127 m ${build_targets} -j || break
Jaewan Kim35a6aa32018-01-21 20:54:43 +0900128
129 ${adb} root
130 ${adb} remount
131 ${adb} shell stop
132 ${adb} sync
133 ${adb} shell start
134 ${adb} wait-for-device || break
135 # Ensure package manager is loaded.
136 sleep 5
137
138 # Install apks
139 local install_failed="false"
140 for target in ${INSTALL_TARGETS[@]}; do
141 echo "${target}"
142 local target_dir=$(mgrep -l -e '^LOCAL_PACKAGE_NAME.*'"${target}$")
143 if [[ -z ${target_dir} ]]; then
144 continue
145 fi
146 target_dir=$(dirname ${target_dir})
147 local package=$(sed -n 's/^.*\bpackage\b="\([a-z0-9\._]*\)".*$/\1/p' ${target_dir}/AndroidManifest.xml)
148 local apk_path=$(find ${OUT} -name ${target}.apk)
149 if [[ -z "${apk_path}" ]]; then
150 echo "Cannot locate ${target}.apk" && break
151 fi
152 echo "Installing ${target}.apk. path=${apk_path}"
153 ${adb} install -r ${apk_path}
154 if [[ "${?}" != "0" ]]; then
155 install_failed="true"
156 break
157 fi
158 done
159 if [[ "${install_failed}" == "true" ]]; then
160 echo "Failed to install. Test wouldn't run."
161 break
162 fi
163 fi
164
165 local test_target=""
166 if [[ -n "${OPTION_TEST_TARGET}" ]]; then
167 test_target="-e class ${OPTION_TEST_TARGET}"
168 fi
169
170 local i
171 local tmpfile=$(tempfile)
172 for ((i=1; i <= ${OPTION_REPEAT_COUNT}; i++)); do
173 echo "Run test ${i}/${OPTION_REPEAT_COUNT}"
174 ${adb} shell am instrument ${test_target} -w ${TEST_PACKAGE}/${TEST_RUNNER} >& ${tmpfile}
175 cat ${tmpfile}
176 if [[ "${OPTION_IGNORE}" != "true" ]]; then
177 if [[ -n "$(grep ${tmpfile} -e 'FAILURE\|crashed')" ]]; then
178 # am instrument doesn't return error code so need to grep result message instead
179 break
180 fi
181 fi
182 done
183 rm ${tmpfile}
184 break
185 done
186}
187
188echo "Following functions are added to your environment:"
189_runtest_mediacomponent_usage