1e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu#
2e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# Copyright (C) 2012 The Android Open Source Project
3e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu#
4e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# Licensed under the Apache License, Version 2.0 (the "License");
5e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# you may not use this file except in compliance with the License.
6e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# You may obtain a copy of the License at
7e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu#
8e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu#      http://www.apache.org/licenses/LICENSE-2.0
9e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu#
10e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# Unless required by applicable law or agreed to in writing, software
11e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# distributed under the License is distributed on an "AS IS" BASIS,
12e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# See the License for the specific language governing permissions and
14e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# limitations under the License.
15e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu#
166ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu# Script to start "uiautomator" on the device
17e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu#
186ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu# The script does a couple of things:
196ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu# * Use an alternative dalvik cache when running as non-root. Jar file needs
206ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#   to be dexopt'd to run in Dalvik. For plain jar files, this is done at first
216ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#   use. shell user does not have write permission to default system Dalvik
226ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#   cache so we redirect to an alternative cache
236ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu# * special processing for subcommand 'runtest':
246ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#    * '--nohup' allows process continue to run even if parent process that
256ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#      started it has already terminated. We parse for this parameter and set
266ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#      signal trap. This is useful for testing with USB disconnected
276ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#    * all jar files that the test classes resides in, or dependent on are
286ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#      provided on command line and exported to CLASSPATH environment variable
296ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#      before starting the Java code. This offloads the task of class loading
306ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#      and resolving of cross jar class dependency to Dalvik
316ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#    * all other subcommand or options are directly passed into Java code for
326ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu#      further parsing
33e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
34e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhuexport run_base=/data/local/tmp
35e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhuexport base=/system
36e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
37e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu# if not running as root, trick dalvik into using an alternative dex cache
386ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhuif [ ${USER_ID} -ne 0 ]; then
39e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu  tmp_cache=${run_base}/dalvik-cache
40e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
416ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  if [ ! -d ${tmp_cache} ]; then
426ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    mkdir -p ${tmp_cache}
43e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu  fi
44e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
45e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu  export ANDROID_DATA=${run_base}
46e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhufi
47e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
486ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu# take first parameter as the command
496ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhucmd=${1}
50e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
516ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhuif [ -z "${1}" ]; then
526ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  cmd="help"
53e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhufi
54e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
556ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu# strip the command parameter
566ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhuif [ -n "${1}" ]; then
576ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  shift
58e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhufi
59e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
60d2ca8e4c32020b465c8443a3d006b0af5a5b2befGuang ZhuCLASSPATH=/system/framework/android.test.runner.jar:${base}/framework/uiautomator.jar
61e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
626ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu# eventually args will be what get passed down to Java code
636ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhuargs=
64bc81b387c8d6c464f9b2a0f4f6f3224270466181Maxim Siniavine# we also pass the list of jar files, so we can extract class names for tests
65bc81b387c8d6c464f9b2a0f4f6f3224270466181Maxim Siniavine# if they are not explicitly specified
66bc81b387c8d6c464f9b2a0f4f6f3224270466181Maxim Siniavinejars=
676ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu
686ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu# special case pre-processing for 'runtest' command
696ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhuif [ "${cmd}" == "runtest" ]; then
706ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  # first parse the jar paths
716ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  while [ true ]; do
72517824a1704b10944f2fb07e5ea53e85e8b44aa7Guang Zhu    if [ -z "${1}" ]; then
73517824a1704b10944f2fb07e5ea53e85e8b44aa7Guang Zhu      echo "Error: more parameters expected for runtest; please see usage for details"
74517824a1704b10944f2fb07e5ea53e85e8b44aa7Guang Zhu      cmd="help"
75517824a1704b10944f2fb07e5ea53e85e8b44aa7Guang Zhu      break
76517824a1704b10944f2fb07e5ea53e85e8b44aa7Guang Zhu    fi
776ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    jar=${1}
786ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    if [ "${1:0:1}" = "-" ]; then
796ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      # we are done with jars, starting with parameters now
806ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      break
816ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    fi
826ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    # if relative path, append the default path prefix
836ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    if [ "${1:0:1}" != "/" ]; then
846ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      jar=${run_base}/${1}
856ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    fi
866ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    # about to add the file to class path, check if it's valid
876ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    if [ ! -f ${jar} ]; then
886ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      echo "Error: ${jar} does not exist"
896ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      # force to print help message
906ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      cmd="help"
916ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      break
926ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    fi
93bc81b387c8d6c464f9b2a0f4f6f3224270466181Maxim Siniavine    jars=${jars}:${jar}
946ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    # done processing current arg, moving on
956ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    shift
966ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  done
976ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  # look for --nohup: if found, consume it and trap SIG_HUP, otherwise just
986ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  # append the arg to args
996ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  while [ -n "${1}" ]; do
1006ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    if [ "${1}" = "--nohup" ]; then
1016ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      trap "" HUP
1026ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      shift
1036ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    else
1046ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      args="${args} ${1}"
1056ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu      shift
1066ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu    fi
1076ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  done
1086ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhuelse
1096ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  # if cmd is not 'runtest', just take the rest of the args
1106ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu  args=${@}
1116ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhufi
1126ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhu
1136ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhuargs="${cmd} ${args}"
114bc81b387c8d6c464f9b2a0f4f6f3224270466181Maxim Siniavineif [ -n "${jars}" ]; then
115bc81b387c8d6c464f9b2a0f4f6f3224270466181Maxim Siniavine   args="${args} -e jars ${jars}"
116bc81b387c8d6c464f9b2a0f4f6f3224270466181Maxim Siniavinefi
117e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhu
118bc81b387c8d6c464f9b2a0f4f6f3224270466181Maxim SiniavineCLASSPATH=${CLASSPATH}:${jars}
119e54d649fb83a0a44516e5c25a9ac1992c8950e59Guang Zhuexport CLASSPATH
1206ac3f28e72bc05dce35a3f8d2b6b095a3f4056daGuang Zhuexec app_process ${base}/bin com.android.commands.uiautomator.Launcher ${args}
121