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  # Print deprecation warning
71  echo "Warning: This version of UI Automator is deprecated. New tests should be written using"
72  echo "UI Automator 2.0 which is available as part of the Android Testing Support Library."
73  echo "See https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html"
74  echo "for more details."
75  # first parse the jar paths
76  while [ true ]; do
77    if [ -z "${1}" ] && [ -z "${jars}" ]; then
78      echo "Error: more parameters expected for runtest; please see usage for details"
79      cmd="help"
80      break
81    fi
82    if [ -z "${1}" ]; then
83      break
84    fi
85    jar=${1}
86    if [ "${1:0:1}" = "-" ]; then
87      # we are done with jars, starting with parameters now
88      break
89    fi
90    # if relative path, append the default path prefix
91    if [ "${1:0:1}" != "/" ]; then
92      jar=${run_base}/${1}
93    fi
94    # about to add the file to class path, check if it's valid
95    if [ ! -f ${jar} ]; then
96      echo "Error: ${jar} does not exist"
97      # force to print help message
98      cmd="help"
99      break
100    fi
101    jars=${jars}:${jar}
102    # done processing current arg, moving on
103    shift
104  done
105  # look for --nohup: if found, consume it and trap SIG_HUP, otherwise just
106  # append the arg to args
107  while [ -n "${1}" ]; do
108    if [ "${1}" = "--nohup" ]; then
109      trap "" HUP
110      shift
111    else
112      args="${args} ${1}"
113      shift
114    fi
115  done
116else
117  # if cmd is not 'runtest', just take the rest of the args
118  args=${@}
119fi
120
121args="${cmd} ${args}"
122if [ -n "${jars}" ]; then
123   args="${args} -e jars ${jars}"
124fi
125
126CLASSPATH=${CLASSPATH}:${jars}
127export CLASSPATH
128exec app_process ${base}/bin com.android.commands.uiautomator.Launcher ${args}
129