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
65# special case pre-processing for 'runtest' command
66if [ "${cmd}" == "runtest" ]; then
67  # first parse the jar paths
68  while [ true ]; do
69    if [ -z "${1}" ]; then
70      echo "Error: more parameters expected for runtest; please see usage for details"
71      cmd="help"
72      break
73    fi
74    jar=${1}
75    if [ "${1:0:1}" = "-" ]; then
76      # we are done with jars, starting with parameters now
77      break
78    fi
79    # if relative path, append the default path prefix
80    if [ "${1:0:1}" != "/" ]; then
81      jar=${run_base}/${1}
82    fi
83    # about to add the file to class path, check if it's valid
84    if [ ! -f ${jar} ]; then
85      echo "Error: ${jar} does not exist"
86      # force to print help message
87      cmd="help"
88      break
89    fi
90    CLASSPATH=${CLASSPATH}:${jar}
91    # done processing current arg, moving on
92    shift
93  done
94  # look for --nohup: if found, consume it and trap SIG_HUP, otherwise just
95  # append the arg to args
96  while [ -n "${1}" ]; do
97    if [ "${1}" = "--nohup" ]; then
98      trap "" HUP
99      shift
100    else
101      args="${args} ${1}"
102      shift
103    fi
104  done
105else
106  # if cmd is not 'runtest', just take the rest of the args
107  args=${@}
108fi
109
110args="${cmd} ${args}"
111
112export CLASSPATH
113exec app_process ${base}/bin com.android.commands.uiautomator.Launcher ${args}
114