1#!/system/bin/sh
2#
3# Copyright (C) 2012 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# Script to start "uiautomator" on the device
18#
19# The script does a couple of things:
20# * Use an alternative dalvik cache when running as non-root. Jar file needs
21#   to be dexopt'd to run in Dalvik. For plain jar files, this is done at first
22#   use. shell user does not have write permission to default system Dalvik
23#   cache so we redirect to an alternative cache
24# * special processing for subcommand 'runtest':
25#    * '--nohup' allows process continue to run even if parent process that
26#      started it has already terminated. We parse for this parameter and set
27#      signal trap. This is useful for testing with USB disconnected
28#    * all jar files that the test classes resides in, or dependent on are
29#      provided on command line and exported to CLASSPATH environment variable
30#      before starting the Java code. This offloads the task of class loading
31#      and resolving of cross jar class dependency to Dalvik
32#    * all other subcommand or options are directly passed into Java code for
33#      further parsing
34
35export run_base=/data/local/tmp
36export base=/system
37
38# if not running as root, trick dalvik into using an alternative dex cache
39if [ ${USER_ID} -ne 0 ]; then
40  tmp_cache=${run_base}/dalvik-cache
41
42  if [ ! -d ${tmp_cache} ]; then
43    mkdir -p ${tmp_cache}
44  fi
45
46  export ANDROID_DATA=${run_base}
47fi
48
49# take first parameter as the command
50cmd=${1}
51
52if [ -z "${1}" ]; then
53  cmd="help"
54fi
55
56# strip the command parameter
57if [ -n "${1}" ]; then
58  shift
59fi
60
61CLASSPATH=/system/framework/android.test.runner.jar:${base}/framework/uiautomator.jar
62
63# eventually args will be what get passed down to Java code
64args=
65# we also pass the list of jar files, so we can extract class names for tests
66# if they are not explicitly specified
67jars=
68
69# special case pre-processing for 'runtest' command
70if [ "${cmd}" == "runtest" ]; then
71  # Print deprecation warning
72  echo "Warning: This version of UI Automator is deprecated. New tests should be written using"
73  echo "UI Automator 2.0 which is available as part of the Android Testing Support Library."
74  echo "See https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html"
75  echo "for more details."
76  # first parse the jar paths
77  while [ true ]; do
78    if [ -z "${1}" ] && [ -z "${jars}" ]; then
79      echo "Error: more parameters expected for runtest; please see usage for details"
80      cmd="help"
81      break
82    fi
83    if [ -z "${1}" ]; then
84      break
85    fi
86    jar=${1}
87    if [ "${1:0:1}" = "-" ]; then
88      # we are done with jars, starting with parameters now
89      break
90    fi
91    # if relative path, append the default path prefix
92    if [ "${1:0:1}" != "/" ]; then
93      jar=${run_base}/${1}
94    fi
95    # about to add the file to class path, check if it's valid
96    if [ ! -f ${jar} ]; then
97      echo "Error: ${jar} does not exist"
98      # force to print help message
99      cmd="help"
100      break
101    fi
102    jars=${jars}:${jar}
103    # done processing current arg, moving on
104    shift
105  done
106  # look for --nohup: if found, consume it and trap SIG_HUP, otherwise just
107  # append the arg to args
108  while [ -n "${1}" ]; do
109    if [ "${1}" = "--nohup" ]; then
110      trap "" HUP
111      shift
112    else
113      args="${args} ${1}"
114      shift
115    fi
116  done
117else
118  # if cmd is not 'runtest', just take the rest of the args
119  args=${@}
120fi
121
122args="${cmd} ${args}"
123if [ -n "${jars}" ]; then
124   args="${args} -e jars ${jars}"
125fi
126
127CLASSPATH=${CLASSPATH}:${jars}
128export CLASSPATH
129exec app_process ${base}/bin com.android.commands.uiautomator.Launcher ${args}
130