blob: a438b734a8167749b84233bceb86a3afba49db7a [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
33MANIFEST_JUNIT_TEST = """<?xml version="1.0" encoding="utf-8"?>
34<manifest xmlns:android="http://schemas.android.com/apk/res/android">
35 <instrumentation
36 android:name="android.support.test.runner.AndroidJUnitRunner"
37 android:targetPackage="com.android.my.tests" />
38</manifest>
39"""
40
41MANIFEST_INSTRUMENTATION_TEST = """<?xml version="1.0" encoding="utf-8"?>
42<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43 <instrumentation
44 android:name="android.test.InstrumentationTestRunner"
45 android:targetPackage="com.android.my.tests"
46 android:label="My Tests" />
47</manifest>
48"""
49
50EXPECTED_JUNIT_TEST_CONFIG = """<?xml version="1.0" encoding="utf-8"?>
51<!-- Copyright (C) 2017 The Android Open Source Project
52
53 Licensed under the Apache License, Version 2.0 (the "License");
54 you may not use this file except in compliance with the License.
55 You may obtain a copy of the License at
56
57 http://www.apache.org/licenses/LICENSE-2.0
58
59 Unless required by applicable law or agreed to in writing, software
60 distributed under the License is distributed on an "AS IS" BASIS,
61 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
62 See the License for the specific language governing permissions and
63 limitations under the License.
64-->
65<!-- This test config file is auto-generated. -->
66<configuration description="Runs TestModule.">
67 <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
68 <option name="test-file-name" value="TestModule.apk" />
69 </target_preparer>
70
71 <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
72 <option name="package" value="com.android.my.tests" />
73 <option name="runner" value="android.support.test.runner.AndroidJUnitRunner" />
74 </test>
75</configuration>
76"""
77
78EXPECTED_INSTRUMENTATION_TEST_CONFIG = """<?xml version="1.0" encoding="utf-8"?>
79<!-- Copyright (C) 2017 The Android Open Source Project
80
81 Licensed under the Apache License, Version 2.0 (the "License");
82 you may not use this file except in compliance with the License.
83 You may obtain a copy of the License at
84
85 http://www.apache.org/licenses/LICENSE-2.0
86
87 Unless required by applicable law or agreed to in writing, software
88 distributed under the License is distributed on an "AS IS" BASIS,
89 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
90 See the License for the specific language governing permissions and
91 limitations under the License.
92-->
93<!-- This test config file is auto-generated. -->
94<configuration description="Runs My Tests.">
95 <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
96 <option name="test-file-name" value="TestModule.apk" />
97 </target_preparer>
98
99 <test class="com.android.tradefed.testtype.InstrumentationTest" >
100 <option name="package" value="com.android.my.tests" />
101 <option name="runner" value="android.test.InstrumentationTestRunner" />
102 </test>
103</configuration>
104"""
105
106MAKE_ROOT = os.path.dirname(os.path.dirname(__file__))
107EMPTY_TEST_CONFIG = os.path.join(MAKE_ROOT, 'core', 'empty_test_config.xml')
108INSTRUMENTATION_TEST_CONFIG_TEMPLATE = os.path.join(
109 MAKE_ROOT, 'core', 'instrumentation_test_config_template.xml')
110
111
112class AutoGenTestConfigUnittests(unittest.TestCase):
113 """Unittests for auto_gen_test_config."""
114
115 def setUp(self):
116 """Setup directory for test."""
117 self.test_dir = tempfile.mkdtemp()
118 self.config_file = os.path.join(self.test_dir, TEST_MODULE + '.config')
119 self.manifest_file = os.path.join(self.test_dir, 'AndroidManifest.xml')
120
121 def tearDown(self):
122 """Cleanup the test directory."""
123 shutil.rmtree(self.test_dir, ignore_errors=True)
124
125 def testInvalidManifest(self):
126 """An empty test config should be generated if AndroidManifest is invalid.
127 """
128 with open(self.manifest_file, 'w') as f:
129 f.write(MANIFEST_INVALID)
130
131 argv = [self.config_file,
132 self.manifest_file,
133 EMPTY_TEST_CONFIG,
134 INSTRUMENTATION_TEST_CONFIG_TEMPLATE]
135 auto_gen_test_config.main(argv)
136 with open(self.config_file) as config_file:
137 with open(EMPTY_TEST_CONFIG) as empty_config:
138 self.assertEqual(config_file.read(), empty_config.read())
139
140 def testCreateJUnitTestConfig(self):
141 """Test creating test config for AndroidJUnitTest.
142 """
143 with open(self.manifest_file, 'w') as f:
144 f.write(MANIFEST_JUNIT_TEST)
145
146 argv = [self.config_file,
147 self.manifest_file,
148 EMPTY_TEST_CONFIG,
149 INSTRUMENTATION_TEST_CONFIG_TEMPLATE]
150 auto_gen_test_config.main(argv)
151 with open(self.config_file) as config_file:
152 self.assertEqual(config_file.read(), EXPECTED_JUNIT_TEST_CONFIG)
153
154 def testCreateInstrumentationTestConfig(self):
155 """Test creating test config for AndroidJUnitTest.
156 """
157 with open(self.manifest_file, 'w') as f:
158 f.write(MANIFEST_INSTRUMENTATION_TEST)
159
160 argv = [self.config_file,
161 self.manifest_file,
162 EMPTY_TEST_CONFIG,
163 INSTRUMENTATION_TEST_CONFIG_TEMPLATE]
164 auto_gen_test_config.main(argv)
165 with open(self.config_file) as config_file:
166 self.assertEqual(
167 config_file.read(), EXPECTED_INSTRUMENTATION_TEST_CONFIG)
168
169if __name__ == '__main__':
170 unittest.main()