blob: f19cc5d567ecdd88e5076fb1d640ce3a8e04ead8 [file] [log] [blame]
Christopher Wiley012ab102016-10-20 12:33:31 -07001#!/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
17import os
18import subprocess
19import sys
20
21INSTRUMENTED_PACKAGE_RUNNER = ('com.android.frameworks.servicestests/'
22 'android.support.test.runner.AndroidJUnitRunner')
23
24PACKAGE_WHITELIST = (
Howard Chen3fd94f42018-09-28 15:15:22 +080025 "com.android.server",
Christopher Wiley012ab102016-10-20 12:33:31 -070026)
27
28COLOR_RED = '\033[0;31m'
29COLOR_NONE ='\033[0m'
30
31def 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
Howard Chen3fd94f42018-09-28 15:15:22 +080039# 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#
Christopher Wiley012ab102016-10-20 12:33:31 -070051def main():
52 build_top = os.environ.get('ANDROID_BUILD_TOP', None)
53 out_dir = os.environ.get('OUT', None)
Howard Chen3fd94f42018-09-28 15:15:22 +080054 runner = os.environ.get('INSTRUMENTED_PACKAGE_RUNNER', None)
Christopher Wiley012ab102016-10-20 12:33:31 -070055 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
Howard Chen3fd94f42018-09-28 15:15:22 +080058 if runner is None:
59 runner = INSTRUMENTED_PACKAGE_RUNNER
Christopher Wiley012ab102016-10-20 12:33:31 -070060 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)
Howard Chen3fd94f42018-09-28 15:15:22 +080072 run('adb install -t -r -g "%s"' % apk_path)
Christopher Wiley012ab102016-10-20 12:33:31 -070073
74 print 'Running tests...'
75 if len(sys.argv) != 1:
Etan Cohen31dc3db2016-10-27 10:33:56 -070076 run('adb shell am instrument -w %s "%s"' %
Howard Chen3fd94f42018-09-28 15:15:22 +080077 (' '.join(sys.argv[1:]), runner))
Christopher Wiley012ab102016-10-20 12:33:31 -070078 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' %
Howard Chen3fd94f42018-09-28 15:15:22 +080084 (package, runner))
Christopher Wiley012ab102016-10-20 12:33:31 -070085
86 return 0
87
88
89if __name__ == '__main__':
90 sys.exit(main())