blob: b7ef0b0b2ee669fefe8ec9805931fd9e84627e95 [file] [log] [blame]
Dan Shiefb892d2017-12-06 15:57:31 -08001#!/usr/bin/env python
2#
3# Copyright 2017, 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"""Unittests for auto_gen_test_config."""
18
19import os
20import shutil
21import tempfile
22import unittest
23
24import auto_gen_test_config
25
26TEST_MODULE = 'TestModule'
27
28MANIFEST_INVALID = """<?xml version="1.0" encoding="utf-8"?>
29<manifest xmlns:android="http://schemas.android.com/apk/res/android">
30</manifest>
31"""
32
Jingwen Chenf3406e62023-09-18 07:19:11 +000033XMLTREE_JUNIT_TEST = """N: android=http://schemas.android.com/apk/res/android (line=2)
34 E: manifest (line=2)
35 A: package="com.android.my.tests.x" (Raw: "com.android.my.tests.x")
36 E: instrumentation (line=9)
37 A: http://schemas.android.com/apk/res/android:label(0x01010001)="TestModule" (Raw: "TestModule")
38 A: http://schemas.android.com/apk/res/android:name(0x01010003)="androidx.test.runner.AndroidJUnitRunner" (Raw: "androidx.test.runner.AndroidJUnitRunner")
39 A: http://schemas.android.com/apk/res/android:targetPackage(0x01010021)="com.android.my.tests" (Raw: "com.android.my.tests")
40"""
41
42XMLTREE_INSTRUMENTATION_TEST = """N: android=http://schemas.android.com/apk/res/android (line=2)
43 E: manifest (line=2)
44 A: package="com.android.my.tests.x" (Raw: "com.android.my.tests.x")
45 E: instrumentation (line=9)
46 A: http://schemas.android.com/apk/res/android:label(0x01010001)="TestModule" (Raw: "TestModule")
47 A: http://schemas.android.com/apk/res/android:name(0x01010003)="android.test.InstrumentationTestRunner" (Raw: "android.test.InstrumentationTestRunner")
48 A: http://schemas.android.com/apk/res/android:targetPackage(0x01010021)="com.android.my.tests" (Raw: "com.android.my.tests")
49"""
50
Dan Shiefb892d2017-12-06 15:57:31 -080051MANIFEST_JUNIT_TEST = """<?xml version="1.0" encoding="utf-8"?>
Dan Shi9a501682017-12-22 13:32:48 -080052<manifest xmlns:android="http://schemas.android.com/apk/res/android"
53 package="com.android.my.tests.x">
Dan Shiefb892d2017-12-06 15:57:31 -080054 <instrumentation
Brett Chabot49183d32018-12-13 19:06:34 -080055 android:name="androidx.test.runner.AndroidJUnitRunner"
Dan Shiefb892d2017-12-06 15:57:31 -080056 android:targetPackage="com.android.my.tests" />
57</manifest>
58"""
59
60MANIFEST_INSTRUMENTATION_TEST = """<?xml version="1.0" encoding="utf-8"?>
Dan Shi9a501682017-12-22 13:32:48 -080061<manifest xmlns:android="http://schemas.android.com/apk/res/android"
62 package="com.android.my.tests.x">
Dan Shiefb892d2017-12-06 15:57:31 -080063 <instrumentation
64 android:name="android.test.InstrumentationTestRunner"
65 android:targetPackage="com.android.my.tests"
Jingwen Chenf3406e62023-09-18 07:19:11 +000066 android:label="TestModule" />
Dan Shiefb892d2017-12-06 15:57:31 -080067</manifest>
68"""
69
70EXPECTED_JUNIT_TEST_CONFIG = """<?xml version="1.0" encoding="utf-8"?>
Jingwen Chenf3406e62023-09-18 07:19:11 +000071<!-- Copyright (C) 2023 The Android Open Source Project
Dan Shiefb892d2017-12-06 15:57:31 -080072
73 Licensed under the Apache License, Version 2.0 (the "License");
74 you may not use this file except in compliance with the License.
75 You may obtain a copy of the License at
76
77 http://www.apache.org/licenses/LICENSE-2.0
78
79 Unless required by applicable law or agreed to in writing, software
80 distributed under the License is distributed on an "AS IS" BASIS,
81 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82 See the License for the specific language governing permissions and
83 limitations under the License.
84-->
85<!-- This test config file is auto-generated. -->
86<configuration description="Runs TestModule.">
Jingwen Chenf3406e62023-09-18 07:19:11 +000087 <option name="test-suite-tag" value="apct" />
88 <option name="test-suite-tag" value="apct-instrumentation" />
89
Dan Shiefb892d2017-12-06 15:57:31 -080090 <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
Jingwen Chenf3406e62023-09-18 07:19:11 +000091 <option name="cleanup-apks" value="true" />
Dan Shiefb892d2017-12-06 15:57:31 -080092 <option name="test-file-name" value="TestModule.apk" />
93 </target_preparer>
94
95 <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
Dan Shi042bb8c2023-10-31 15:35:19 -070096 <option name="package" value="com.android.my.tests.x" />
Brett Chabot49183d32018-12-13 19:06:34 -080097 <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
Dan Shiefb892d2017-12-06 15:57:31 -080098 </test>
99</configuration>
100"""
101
102EXPECTED_INSTRUMENTATION_TEST_CONFIG = """<?xml version="1.0" encoding="utf-8"?>
Jingwen Chenf3406e62023-09-18 07:19:11 +0000103<!-- Copyright (C) 2023 The Android Open Source Project
Dan Shiefb892d2017-12-06 15:57:31 -0800104
105 Licensed under the Apache License, Version 2.0 (the "License");
106 you may not use this file except in compliance with the License.
107 You may obtain a copy of the License at
108
109 http://www.apache.org/licenses/LICENSE-2.0
110
111 Unless required by applicable law or agreed to in writing, software
112 distributed under the License is distributed on an "AS IS" BASIS,
113 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114 See the License for the specific language governing permissions and
115 limitations under the License.
116-->
117<!-- This test config file is auto-generated. -->
Jingwen Chenf3406e62023-09-18 07:19:11 +0000118<configuration description="Runs TestModule.">
119 <option name="test-suite-tag" value="apct" />
120 <option name="test-suite-tag" value="apct-instrumentation" />
121
Dan Shiefb892d2017-12-06 15:57:31 -0800122 <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
Jingwen Chenf3406e62023-09-18 07:19:11 +0000123 <option name="cleanup-apks" value="true" />
Dan Shiefb892d2017-12-06 15:57:31 -0800124 <option name="test-file-name" value="TestModule.apk" />
125 </target_preparer>
126
127 <test class="com.android.tradefed.testtype.InstrumentationTest" >
Dan Shi042bb8c2023-10-31 15:35:19 -0700128 <option name="package" value="com.android.my.tests.x" />
Dan Shiefb892d2017-12-06 15:57:31 -0800129 <option name="runner" value="android.test.InstrumentationTestRunner" />
130 </test>
131</configuration>
132"""
133
Jingwen Chenf3406e62023-09-18 07:19:11 +0000134EMPTY_TEST_CONFIG_CONTENT = """<?xml version="1.0" encoding="utf-8"?>
135<!-- Copyright (C) 2017 The Android Open Source Project
136
137 Licensed under the Apache License, Version 2.0 (the "License");
138 you may not use this file except in compliance with the License.
139 You may obtain a copy of the License at
140
141 http://www.apache.org/licenses/LICENSE-2.0
142
143 Unless required by applicable law or agreed to in writing, software
144 distributed under the License is distributed on an "AS IS" BASIS,
145 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
146 See the License for the specific language governing permissions and
147 limitations under the License.
148-->
149<!-- No AndroidTest.xml was provided and the manifest does not include
150 instrumentation, hence this apk is not instrumentable.
151-->
152<configuration description="Empty Configuration" />
153"""
154
155INSTRUMENTATION_TEST_CONFIG_TEMPLATE_CONTENT = """<?xml version="1.0" encoding="utf-8"?>
156<!-- Copyright (C) 2023 The Android Open Source Project
157
158 Licensed under the Apache License, Version 2.0 (the "License");
159 you may not use this file except in compliance with the License.
160 You may obtain a copy of the License at
161
162 http://www.apache.org/licenses/LICENSE-2.0
163
164 Unless required by applicable law or agreed to in writing, software
165 distributed under the License is distributed on an "AS IS" BASIS,
166 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
167 See the License for the specific language governing permissions and
168 limitations under the License.
169-->
170<!-- This test config file is auto-generated. -->
171<configuration description="Runs {LABEL}.">
172 <option name="test-suite-tag" value="apct" />
173 <option name="test-suite-tag" value="apct-instrumentation" />
174{EXTRA_CONFIGS}
175 <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
176 <option name="cleanup-apks" value="true" />
177 <option name="test-file-name" value="{MODULE}.apk" />
178 </target_preparer>
179
180 <test class="com.android.tradefed.testtype.{TEST_TYPE}" >
Dan Shi042bb8c2023-10-31 15:35:19 -0700181 <option name="package" value="{PACKAGE}" />
Jingwen Chenf3406e62023-09-18 07:19:11 +0000182 <option name="runner" value="{RUNNER}" />
183 </test>
184</configuration>
185"""
Dan Shiefb892d2017-12-06 15:57:31 -0800186
187
188class AutoGenTestConfigUnittests(unittest.TestCase):
189 """Unittests for auto_gen_test_config."""
190
191 def setUp(self):
192 """Setup directory for test."""
193 self.test_dir = tempfile.mkdtemp()
194 self.config_file = os.path.join(self.test_dir, TEST_MODULE + '.config')
195 self.manifest_file = os.path.join(self.test_dir, 'AndroidManifest.xml')
Jingwen Chenf3406e62023-09-18 07:19:11 +0000196 self.xmltree_file = os.path.join(self.test_dir, TEST_MODULE + '.xmltree')
197 self.empty_test_config_file = os.path.join(self.test_dir, 'empty.config')
198 self.instrumentation_test_config_template_file = os.path.join(
199 self.test_dir, 'instrumentation.config')
200
201 with open(self.empty_test_config_file, 'w') as f:
202 f.write(EMPTY_TEST_CONFIG_CONTENT)
203
204 with open(self.instrumentation_test_config_template_file, 'w') as f:
205 f.write(INSTRUMENTATION_TEST_CONFIG_TEMPLATE_CONTENT)
Dan Shiefb892d2017-12-06 15:57:31 -0800206
207 def tearDown(self):
208 """Cleanup the test directory."""
209 shutil.rmtree(self.test_dir, ignore_errors=True)
210
211 def testInvalidManifest(self):
212 """An empty test config should be generated if AndroidManifest is invalid.
213 """
214 with open(self.manifest_file, 'w') as f:
215 f.write(MANIFEST_INVALID)
216
217 argv = [self.config_file,
218 self.manifest_file,
Jingwen Chenf3406e62023-09-18 07:19:11 +0000219 self.empty_test_config_file,
220 self.instrumentation_test_config_template_file]
Dan Shiefb892d2017-12-06 15:57:31 -0800221 auto_gen_test_config.main(argv)
222 with open(self.config_file) as config_file:
Jingwen Chenf3406e62023-09-18 07:19:11 +0000223 with open(self.empty_test_config_file) as empty_config:
Dan Shiefb892d2017-12-06 15:57:31 -0800224 self.assertEqual(config_file.read(), empty_config.read())
225
226 def testCreateJUnitTestConfig(self):
227 """Test creating test config for AndroidJUnitTest.
228 """
229 with open(self.manifest_file, 'w') as f:
230 f.write(MANIFEST_JUNIT_TEST)
231
232 argv = [self.config_file,
233 self.manifest_file,
Jingwen Chenf3406e62023-09-18 07:19:11 +0000234 self.empty_test_config_file,
235 self.instrumentation_test_config_template_file]
Dan Shiefb892d2017-12-06 15:57:31 -0800236 auto_gen_test_config.main(argv)
237 with open(self.config_file) as config_file:
238 self.assertEqual(config_file.read(), EXPECTED_JUNIT_TEST_CONFIG)
239
240 def testCreateInstrumentationTestConfig(self):
Dan Shi96068b72018-02-21 11:31:06 -0800241 """Test creating test config for InstrumentationTest.
Dan Shiefb892d2017-12-06 15:57:31 -0800242 """
243 with open(self.manifest_file, 'w') as f:
244 f.write(MANIFEST_INSTRUMENTATION_TEST)
245
246 argv = [self.config_file,
247 self.manifest_file,
Jingwen Chenf3406e62023-09-18 07:19:11 +0000248 self.empty_test_config_file,
249 self.instrumentation_test_config_template_file]
250 auto_gen_test_config.main(argv)
251 with open(self.config_file) as config_file:
252 self.assertEqual(
253 config_file.read(), EXPECTED_INSTRUMENTATION_TEST_CONFIG)
254
255 def testCreateJUnitTestConfigWithXMLTree(self):
256 """Test creating test config for AndroidJUnitTest.
257 """
258 with open(self.xmltree_file, 'w') as f:
259 f.write(XMLTREE_JUNIT_TEST)
260
261 argv = [self.config_file,
262 self.xmltree_file,
263 self.empty_test_config_file,
264 self.instrumentation_test_config_template_file]
265 auto_gen_test_config.main(argv)
266 with open(self.config_file) as config_file:
267 self.assertEqual(config_file.read(), EXPECTED_JUNIT_TEST_CONFIG)
268
269 def testCreateInstrumentationTestConfigWithXMLTree(self):
270 """Test creating test config for InstrumentationTest.
271 """
272 with open(self.xmltree_file, 'w') as f:
273 f.write(XMLTREE_INSTRUMENTATION_TEST)
274
275 argv = [self.config_file,
276 self.xmltree_file,
277 self.empty_test_config_file,
278 self.instrumentation_test_config_template_file]
Dan Shiefb892d2017-12-06 15:57:31 -0800279 auto_gen_test_config.main(argv)
280 with open(self.config_file) as config_file:
281 self.assertEqual(
282 config_file.read(), EXPECTED_INSTRUMENTATION_TEST_CONFIG)
283
284if __name__ == '__main__':
285 unittest.main()