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