Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python
      2 
      3 # Copyright (C) 2016 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 import os
     18 import subprocess
     19 import sys
     20 
     21 INSTRUMENTED_PACKAGE_RUNNER = ('com.android.frameworks.servicestests/'
     22                                'androidx.test.runner.AndroidJUnitRunner')
     23 
     24 PACKAGE_WHITELIST = (
     25     "com.android.server",
     26 )
     27 
     28 COLOR_RED = '\033[0;31m'
     29 COLOR_NONE ='\033[0m'
     30 
     31 def run(shell_command, echo=True):
     32     if echo:
     33         print '%s + %s%s' % (
     34                 COLOR_RED,
     35                 echo if isinstance(echo, str) else shell_command,
     36                 COLOR_NONE)
     37     return subprocess.check_call(shell_command, shell=True)
     38 
     39 # usage:
     40 #   ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/runtests.py : run tests in com.android.server
     41 #   ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/runtests.py -e package [package name, e.g. com.android.server]
     42 #   ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/runtests.py -e class [class name, e.g. com.android.server.MountServiceTests]
     43 #
     44 #   The available INSTRUMENTED_PACKAGE_RUNNER may differ in different environments.
     45 #   In this case, use "adb shell pm list instrumentation" to query available runners
     46 #   and use the environment variable INSTRUMENTED_PACKAGE_RUNNER to overwrite
     47 #   the default one, e.g.,
     48 #   INSTRUMENTED_PACKAGE_RUNNER=com.android.frameworks.servicestests/androidx.test.runner.AndroidJUnitRunner \
     49 #       ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/runtests.py
     50 #
     51 def main():
     52     build_top = os.environ.get('ANDROID_BUILD_TOP', None)
     53     out_dir = os.environ.get('OUT', None)
     54     runner = os.environ.get('INSTRUMENTED_PACKAGE_RUNNER', None)
     55     if build_top is None or out_dir is None:
     56         print 'You need to source and lunch before you can use this script'
     57         return 1
     58     if runner is None:
     59         runner = INSTRUMENTED_PACKAGE_RUNNER
     60     print 'Building tests...'
     61     run('make -j32 -C %s -f build/core/main.mk '
     62         'MODULES-IN-frameworks-base-services-tests-servicestests' % build_top,
     63         echo='mmma -j32 %s/frameworks/base/services/tests/servicestests' %
     64              build_top)
     65 
     66     print 'Installing tests...'
     67     run('adb root')
     68     run('adb wait-for-device')
     69     apk_path = (
     70             '%s/data/app/FrameworksServicesTests/FrameworksServicesTests.apk' %
     71             out_dir)
     72     run('adb install -t -r -g "%s"' % apk_path)
     73 
     74     print 'Running tests...'
     75     if len(sys.argv) != 1:
     76         run('adb shell am instrument -w %s "%s"' %
     77             (' '.join(sys.argv[1:]), runner))
     78         return 0
     79 
     80     # It would be nice if the activity manager accepted a list of packages, but
     81     # in lieu of that...
     82     for package in PACKAGE_WHITELIST:
     83         run('adb shell am instrument -w -e package %s %s' %
     84             (package, runner))
     85 
     86     return 0
     87 
     88 
     89 if __name__ == '__main__':
     90     sys.exit(main())
     91