lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
#
# Copyright 2017 - 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 argparse
import os
import re
import sys
 
from build.build_rule_gen import BuildRuleGen
from build.vts_spec_parser import VtsSpecParser
from configure.test_case_creator import TestCaseCreator
from utils.const import Constant
 
TEST_TIME_OUT_PATTERN = '(([0-9]+)(m|s|h))+'
 
"""Generate Android.mk, Android.bp, and AndroidTest.xml files for given hal
 
Usage:
  python launch_hal_test.py [--options] hal_package name
 
  where options are:
  --test_type: Test type, currently support two types: target and host.
  --time_out: Timeout for the test, default is 1m.
  --package_root: Prefix of the HAL package. default is android.hardware
  --path_root: Root path that stores the HAL definition. default is hardware/interfaces
  --test_binary_file: Test binary file for target-side HAL test.
  --test_script_file: Test script file for host-side HAL test.
  --test_config_dir: Directory path to store the test configure files.
  --replay: Whether this is a replay test.
  --disable_stop_runtime: Whether to stop framework before the test.
Example:
  python launch_hal_test.py android.hardware.nfc@1.0
  python launch_hal_test.py --test_type=host --time_out=5m android.hardware.nfc@1.0
  python launch_hal_test.py --package_root com.qualcomm.qti
  --path_root vendor/qcom/proprietary/interfaces/com/qualcomm/qti/
  --test_configure_dir temp/ com.qualcomm.qti.qcril.qcrilhook@1.0
"""
 
 
def main():
    parser = argparse.ArgumentParser(description='Initiate a test case.')
    parser.add_argument(
        '--test_type',
        dest='test_type',
        required=False,
        default='target',
        help='Test type, currently support two types: target and host.')
    parser.add_argument(
        '--time_out',
        dest='time_out',
        required=False,
        default='1m',
        help='Timeout for the test, default is 1m.')
    parser.add_argument(
        '--replay',
        dest='is_replay',
        action='store_true',
        required=False,
        help='Whether this is a replay test.')
    parser.add_argument(
        '--disable_stop_runtime',
        dest='disable_stop_runtime',
        action='store_true',
        required=False,
        help='Whether to stop framework before the test.')
    parser.add_argument(
        '--package_root',
        dest='package_root',
        required=False,
        default=Constant.HAL_PACKAGE_PREFIX,
        help='Prefix of the HAL package. e.g. android.hardware')
    parser.add_argument(
        '--path_root',
        dest='path_root',
        required=False,
        default=Constant.HAL_INTERFACE_PATH,
        help=
        'Root path that stores the HAL definition. e.g. hardware/interfaces')
    parser.add_argument(
        '--test_binary_file',
        dest='test_binary_file',
        required=False,
        help='Test binary file for target-side HAL test.')
    parser.add_argument(
        '--test_script_file',
        dest='test_script_file',
        required=False,
        help='Test script file for host-side HAL test.')
    parser.add_argument(
        '--test_config_dir',
        dest='test_config_dir',
        required=False,
        help='Directory path to store the test configure files.')
    parser.add_argument(
        'hal_package_name',
        help='hal package name (e.g. android.hardware.nfc@1.0).')
    args = parser.parse_args()
 
    regex = re.compile(Constant.HAL_PACKAGE_NAME_PATTERN)
    result = re.match(regex, args.hal_package_name)
    if not result:
        print 'Invalid hal package name. Exiting..'
        sys.exit(1)
 
    if not args.hal_package_name.startswith(args.package_root + '.'):
        print 'hal_package_name does not start with package_root. Exiting...'
        sys.exit(1)
 
    if args.test_type != 'target' and args.test_type != 'host':
        print 'Unsupported test type. Exiting...'
        sys.exit(1)
    elif args.test_type == 'host' and args.is_replay:
        print 'Host side replay test is not supported yet. Exiting...'
        sys.exit(1)
 
    regex = re.compile(TEST_TIME_OUT_PATTERN)
    result = re.match(regex, args.time_out)
    if not result:
        print 'Invalid test time out format. Exiting...'
        sys.exit(1)
 
    if not args.test_config_dir:
        if args.package_root == Constant.HAL_PACKAGE_PREFIX:
            args.test_config_dir = Constant.VTS_HAL_TEST_CASE_PATH
        else:
            args.test_config_dir = args.path_root
 
    stop_runtime = False
    if args.test_type == 'target' and not args.disable_stop_runtime:
        stop_runtime = True
 
    vts_spec_parser = VtsSpecParser(
        package_root=args.package_root, path_root=args.path_root)
    test_case_creater = TestCaseCreator(vts_spec_parser, args.hal_package_name)
    if not test_case_creater.LaunchTestCase(
            args.test_type,
            args.time_out,
            is_replay=args.is_replay,
            stop_runtime=stop_runtime,
            test_binary_file=args.test_binary_file,
            test_script_file=args.test_script_file,
            test_config_dir=args.test_config_dir,
            package_root=args.package_root,
            path_root=args.path_root):
        print('Error: Failed to launch test for %s. Exiting...' %
              args.hal_package_name)
        sys.exit(1)
 
if __name__ == '__main__':
    main()