1#
2# Copyright (C) 2012 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# Script to start "uiautomator" on the device
17#
18# The script does a couple of things:
19# * Use an alternative dalvik cache when running as non-root. Jar file needs
20#   to be dexopt'd to run in Dalvik. For plain jar files, this is done at first
21#   use. shell user does not have write permission to default system Dalvik
22#   cache so we redirect to an alternative cache
23# * special processing for subcommand 'runtest':
24#    * '--nohup' allows process continue to run even if parent process that
25#      started it has already terminated. We parse for this parameter and set
26#      signal trap. This is useful for testing with USB disconnected
27#    * all jar files that the test classes resides in, or dependent on are
28#      provided on command line and exported to CLASSPATH environment variable
29#      before starting the Java code. This offloads the task of class loading
30#      and resolving of cross jar class dependency to Dalvik
31#    * all other subcommand or options are directly passed into Java code for
32#      further parsing
33
34export run_base=/data/local/tmp
35export base=/system
36
37# if not running as root, trick dalvik into using an alternative dex cache
38if [ ${USER_ID} -ne 0 ]; then
39  tmp_cache=${run_base}/dalvik-cache
40
41  if [ ! -d ${tmp_cache} ]; then
42    mkdir -p ${tmp_cache}
43  fi
44
45  export ANDROID_DATA=${run_base}
46fi
47
48# take first parameter as the command
49cmd=${1}
50
51if [ -z "${1}" ]; then
52  cmd="help"
53fi
54
55# strip the command parameter
56if [ -n "${1}" ]; then
57  shift
58fi
59
60CLASSPATH=/system/framework/android.test.runner.jar:${base}/framework/uiautomator.jar
61
62# eventually args will be what get passed down to Java code
63args=
64# we also pass the list of jar files, so we can extract class names for tests
65# if they are not explicitly specified
66jars=
67
68# special case pre-processing for 'runtest' command
69if [ "${cmd}" == "runtest" ]; then
70  # first parse the jar paths
71  while [ true ]; do
72    if [ -z "${1}" ] && [ -z "${jars}" ]; then
73      echo "Error: more parameters expected for runtest; please see usage for details"
74      cmd="help"
75      break
76    fi
77    if [ -z "${1}" ]; then
78      break
79    fi
80    jar=${1}
81    if [ "${1:0:1}" = "-" ]; then
82      # we are done with jars, starting with parameters now
83      break
84    fi
85    # if relative path, append the default path prefix
86    if [ "${1:0:1}" != "/" ]; then
87      jar=${run_base}/${1}
88    fi
89    # about to add the file to class path, check if it's valid
90    if [ ! -f ${jar} ]; then
91      echo "Error: ${jar} does not exist"
92      # force to print help message
93      cmd="help"
94      break
95    fi
96    jars=${jars}:${jar}
97    # done processing current arg, moving on
98    shift
99  done
100  # look for --nohup: if found, consume it and trap SIG_HUP, otherwise just
101  # append the arg to args
102  while [ -n "${1}" ]; do
103    if [ "${1}" = "--nohup" ]; then
104      trap "" HUP
105      shift
106    else
107      args="${args} ${1}"
108      shift
109    fi
110  done
111else
112  # if cmd is not 'runtest', just take the rest of the args
113  args=${@}
114fi
115
116args="${cmd} ${args}"
117if [ -n "${jars}" ]; then
118   args="${args} -e jars ${jars}"
119fi
120
121CLASSPATH=${CLASSPATH}:${jars}
122export CLASSPATH
123exec app_process ${base}/bin com.android.commands.uiautomator.Launcher ${args}
124