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
17import os
18import subprocess
19import sys
20
21INSTRUMENTED_PACKAGE_RUNNER = ('com.android.frameworks.servicestests/'
22                               'android.support.test.runner.AndroidJUnitRunner')
23
24PACKAGE_WHITELIST = (
25    'android.net',
26    'com.android.server.connectivity',
27)
28
29COLOR_RED = '\033[0;31m'
30COLOR_NONE ='\033[0m'
31
32def run(shell_command, echo=True):
33    if echo:
34        print '%s + %s%s' % (
35                COLOR_RED,
36                echo if isinstance(echo, str) else shell_command,
37                COLOR_NONE)
38    return subprocess.check_call(shell_command, shell=True)
39
40
41def main():
42    build_top = os.environ.get('ANDROID_BUILD_TOP', None)
43    out_dir = os.environ.get('OUT', None)
44    if build_top is None or out_dir is None:
45        print 'You need to source and lunch before you can use this script'
46        return 1
47
48    print 'Building tests...'
49    run('make -j32 -C %s -f build/core/main.mk '
50        'MODULES-IN-frameworks-base-services-tests-servicestests' % build_top,
51        echo='mmma -j32 %s/frameworks/base/services/tests/servicestests' %
52             build_top)
53
54    print 'Installing tests...'
55    run('adb root')
56    run('adb wait-for-device')
57    apk_path = (
58            '%s/data/app/FrameworksServicesTests/FrameworksServicesTests.apk' %
59            out_dir)
60    run('adb install -r -g "%s"' % apk_path)
61
62    print 'Running tests...'
63    if len(sys.argv) != 1:
64        run('adb shell am instrument -w %s "%s"' %
65            (' '.join(sys.argv[1:]), INSTRUMENTED_PACKAGE_RUNNER))
66        return 0
67
68    # It would be nice if the activity manager accepted a list of packages, but
69    # in lieu of that...
70    for package in PACKAGE_WHITELIST:
71        run('adb shell am instrument -w -e package %s %s' %
72            (package, INSTRUMENTED_PACKAGE_RUNNER))
73
74    return 0
75
76
77if __name__ == '__main__':
78    sys.exit(main())
79