huangcm
2024-08-24 fc4c8ccc838b61e008a4a83be4b26b05dc3603d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
 
# Copyright (C) 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
import os
import subprocess
import sys
 
INSTRUMENTED_PACKAGE_RUNNER = ('com.android.frameworks.servicestests/'
                               'androidx.test.runner.AndroidJUnitRunner')
 
PACKAGE_WHITELIST = (
    "com.android.server",
)
 
COLOR_RED = '\033[0;31m'
COLOR_NONE ='\033[0m'
 
def run(shell_command, echo=True):
    if echo:
        print '%s + %s%s' % (
                COLOR_RED,
                echo if isinstance(echo, str) else shell_command,
                COLOR_NONE)
    return subprocess.check_call(shell_command, shell=True)
 
# usage:
#   ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/runtests.py : run tests in com.android.server
#   ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/runtests.py -e package [package name, e.g. com.android.server]
#   ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/runtests.py -e class [class name, e.g. com.android.server.MountServiceTests]
#
#   The available INSTRUMENTED_PACKAGE_RUNNER may differ in different environments.
#   In this case, use "adb shell pm list instrumentation" to query available runners
#   and use the environment variable INSTRUMENTED_PACKAGE_RUNNER to overwrite
#   the default one, e.g.,
#   INSTRUMENTED_PACKAGE_RUNNER=com.android.frameworks.servicestests/androidx.test.runner.AndroidJUnitRunner \
#       ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/runtests.py
#
def main():
    build_top = os.environ.get('ANDROID_BUILD_TOP', None)
    out_dir = os.environ.get('OUT', None)
    runner = os.environ.get('INSTRUMENTED_PACKAGE_RUNNER', None)
    if build_top is None or out_dir is None:
        print 'You need to source and lunch before you can use this script'
        return 1
    if runner is None:
        runner = INSTRUMENTED_PACKAGE_RUNNER
    print 'Building tests...'
    run('make -j32 -C %s -f build/core/main.mk '
        'MODULES-IN-frameworks-base-services-tests-servicestests' % build_top,
        echo='mmma -j32 %s/frameworks/base/services/tests/servicestests' %
             build_top)
 
    print 'Installing tests...'
    run('adb root')
    run('adb wait-for-device')
    apk_path = (
            '%s/data/app/FrameworksServicesTests/FrameworksServicesTests.apk' %
            out_dir)
    run('adb install -t -r -g "%s"' % apk_path)
 
    print 'Running tests...'
    if len(sys.argv) != 1:
        run('adb shell am instrument -w %s "%s"' %
            (' '.join(sys.argv[1:]), runner))
        return 0
 
    # It would be nice if the activity manager accepted a list of packages, but
    # in lieu of that...
    for package in PACKAGE_WHITELIST:
        run('adb shell am instrument -w -e package %s %s' %
            (package, runner))
 
    return 0
 
 
if __name__ == '__main__':
    sys.exit(main())