xref: /aosp_15_r20/tools/asuite/aidegen/idea/iml_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2020, The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for IML class."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerimport os
20*c2e18aaaSAndroid Build Coastguard Workerimport shutil
21*c2e18aaaSAndroid Build Coastguard Workerimport tempfile
22*c2e18aaaSAndroid Build Coastguard Workerimport unittest
23*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
24*c2e18aaaSAndroid Build Coastguard Worker
25*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen import templates
26*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import common_util
27*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.idea import iml
28*c2e18aaaSAndroid Build Coastguard Workerfrom atest import module_info
29*c2e18aaaSAndroid Build Coastguard Worker
30*c2e18aaaSAndroid Build Coastguard Worker
31*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=protected-access
32*c2e18aaaSAndroid Build Coastguard Workerclass IMLGenUnittests(unittest.TestCase):
33*c2e18aaaSAndroid Build Coastguard Worker    """Unit tests for IMLGenerator class."""
34*c2e18aaaSAndroid Build Coastguard Worker
35*c2e18aaaSAndroid Build Coastguard Worker    _TEST_DIR = None
36*c2e18aaaSAndroid Build Coastguard Worker
37*c2e18aaaSAndroid Build Coastguard Worker    def setUp(self):
38*c2e18aaaSAndroid Build Coastguard Worker        """Prepare the testdata related path."""
39*c2e18aaaSAndroid Build Coastguard Worker        IMLGenUnittests._TEST_DIR = tempfile.mkdtemp()
40*c2e18aaaSAndroid Build Coastguard Worker        module = {
41*c2e18aaaSAndroid Build Coastguard Worker            'module_name': 'test',
42*c2e18aaaSAndroid Build Coastguard Worker            'iml_name': 'test_iml',
43*c2e18aaaSAndroid Build Coastguard Worker            'path': ['a/b'],
44*c2e18aaaSAndroid Build Coastguard Worker            'srcs': ['a/b/src'],
45*c2e18aaaSAndroid Build Coastguard Worker            'tests': ['a/b/tests'],
46*c2e18aaaSAndroid Build Coastguard Worker            'srcjars': ['x/y.srcjar'],
47*c2e18aaaSAndroid Build Coastguard Worker            'jars': ['s.jar'],
48*c2e18aaaSAndroid Build Coastguard Worker            'dependencies': ['m1']
49*c2e18aaaSAndroid Build Coastguard Worker        }
50*c2e18aaaSAndroid Build Coastguard Worker        with mock.patch.object(common_util, 'get_android_root_dir') as obj:
51*c2e18aaaSAndroid Build Coastguard Worker            obj.return_value = IMLGenUnittests._TEST_DIR
52*c2e18aaaSAndroid Build Coastguard Worker            self.iml = iml.IMLGenerator(module)
53*c2e18aaaSAndroid Build Coastguard Worker
54*c2e18aaaSAndroid Build Coastguard Worker    def tearDown(self):
55*c2e18aaaSAndroid Build Coastguard Worker        """Clear the testdata related path."""
56*c2e18aaaSAndroid Build Coastguard Worker        self.iml = None
57*c2e18aaaSAndroid Build Coastguard Worker        shutil.rmtree(IMLGenUnittests._TEST_DIR)
58*c2e18aaaSAndroid Build Coastguard Worker
59*c2e18aaaSAndroid Build Coastguard Worker    def test_init(self):
60*c2e18aaaSAndroid Build Coastguard Worker        """Test initialize the attributes."""
61*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(self.iml._mod_info['module_name'], 'test')
62*c2e18aaaSAndroid Build Coastguard Worker
63*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'get_android_root_dir')
64*c2e18aaaSAndroid Build Coastguard Worker    def test_iml_path(self, mock_root_path):
65*c2e18aaaSAndroid Build Coastguard Worker        """Test iml_path."""
66*c2e18aaaSAndroid Build Coastguard Worker        mock_root_path.return_value = IMLGenUnittests._TEST_DIR
67*c2e18aaaSAndroid Build Coastguard Worker        iml_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/test_iml.iml')
68*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(self.iml.iml_path, iml_path)
69*c2e18aaaSAndroid Build Coastguard Worker
70*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'get_android_root_dir')
71*c2e18aaaSAndroid Build Coastguard Worker    def test_create(self, mock_root_path):
72*c2e18aaaSAndroid Build Coastguard Worker        """Test create."""
73*c2e18aaaSAndroid Build Coastguard Worker        mock_root_path.return_value = IMLGenUnittests._TEST_DIR
74*c2e18aaaSAndroid Build Coastguard Worker        module_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b')
75*c2e18aaaSAndroid Build Coastguard Worker        src_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/src')
76*c2e18aaaSAndroid Build Coastguard Worker        test_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/tests')
77*c2e18aaaSAndroid Build Coastguard Worker        srcjar_path = os.path.join(IMLGenUnittests._TEST_DIR, 'x/y.srcjar')
78*c2e18aaaSAndroid Build Coastguard Worker        jar_path = os.path.join(IMLGenUnittests._TEST_DIR, 's.jar')
79*c2e18aaaSAndroid Build Coastguard Worker        expected = """<?xml version="1.0" encoding="UTF-8"?>
80*c2e18aaaSAndroid Build Coastguard Worker<module type="JAVA_MODULE" version="4">
81*c2e18aaaSAndroid Build Coastguard Worker    <component name="NewModuleRootManager" inherit-compiler-output="true">
82*c2e18aaaSAndroid Build Coastguard Worker        <exclude-output />
83*c2e18aaaSAndroid Build Coastguard Worker        <content url="file://{MODULE_PATH}">
84*c2e18aaaSAndroid Build Coastguard Worker            <sourceFolder url="file://{SRC_PATH}" isTestSource="false" />
85*c2e18aaaSAndroid Build Coastguard Worker            <sourceFolder url="file://{TEST_PATH}" isTestSource="true" />
86*c2e18aaaSAndroid Build Coastguard Worker        </content>
87*c2e18aaaSAndroid Build Coastguard Worker        <orderEntry type="sourceFolder" forTests="false" />
88*c2e18aaaSAndroid Build Coastguard Worker        <content url="jar://{SRCJAR}!/">
89*c2e18aaaSAndroid Build Coastguard Worker            <sourceFolder url="jar://{SRCJAR}!/" isTestSource="False" />
90*c2e18aaaSAndroid Build Coastguard Worker        </content>
91*c2e18aaaSAndroid Build Coastguard Worker        <orderEntry type="module" module-name="m1" />
92*c2e18aaaSAndroid Build Coastguard Worker        <orderEntry type="module-library" exported="">
93*c2e18aaaSAndroid Build Coastguard Worker          <library>
94*c2e18aaaSAndroid Build Coastguard Worker            <CLASSES>
95*c2e18aaaSAndroid Build Coastguard Worker              <root url="jar://{JAR}!/" />
96*c2e18aaaSAndroid Build Coastguard Worker            </CLASSES>
97*c2e18aaaSAndroid Build Coastguard Worker            <JAVADOC />
98*c2e18aaaSAndroid Build Coastguard Worker            <SOURCES />
99*c2e18aaaSAndroid Build Coastguard Worker          </library>
100*c2e18aaaSAndroid Build Coastguard Worker        </orderEntry>
101*c2e18aaaSAndroid Build Coastguard Worker        <orderEntry type="inheritedJdk" />
102*c2e18aaaSAndroid Build Coastguard Worker    </component>
103*c2e18aaaSAndroid Build Coastguard Worker</module>
104*c2e18aaaSAndroid Build Coastguard Worker""".format(MODULE_PATH=module_path,
105*c2e18aaaSAndroid Build Coastguard Worker           SRC_PATH=src_path,
106*c2e18aaaSAndroid Build Coastguard Worker           TEST_PATH=test_path,
107*c2e18aaaSAndroid Build Coastguard Worker           SRCJAR=srcjar_path,
108*c2e18aaaSAndroid Build Coastguard Worker           JAR=jar_path)
109*c2e18aaaSAndroid Build Coastguard Worker        self.iml.create({'srcs': True, 'srcjars': True, 'dependencies': True,
110*c2e18aaaSAndroid Build Coastguard Worker                         'jars': True})
111*c2e18aaaSAndroid Build Coastguard Worker        gen_iml = os.path.join(IMLGenUnittests._TEST_DIR,
112*c2e18aaaSAndroid Build Coastguard Worker                               self.iml._mod_info['path'][0],
113*c2e18aaaSAndroid Build Coastguard Worker                               self.iml._mod_info['iml_name'] + '.iml')
114*c2e18aaaSAndroid Build Coastguard Worker        result = common_util.read_file_content(gen_iml)
115*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(result, expected)
116*c2e18aaaSAndroid Build Coastguard Worker
117*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'get_android_root_dir')
118*c2e18aaaSAndroid Build Coastguard Worker    def test_gen_dep_sources(self, mock_root_path):
119*c2e18aaaSAndroid Build Coastguard Worker        """Test _generate_dep_srcs."""
120*c2e18aaaSAndroid Build Coastguard Worker        mock_root_path.return_value = IMLGenUnittests._TEST_DIR
121*c2e18aaaSAndroid Build Coastguard Worker        src_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/src')
122*c2e18aaaSAndroid Build Coastguard Worker        test_path = os.path.join(IMLGenUnittests._TEST_DIR, 'a/b/tests')
123*c2e18aaaSAndroid Build Coastguard Worker        expected = """<?xml version="1.0" encoding="UTF-8"?>
124*c2e18aaaSAndroid Build Coastguard Worker<module type="JAVA_MODULE" version="4">
125*c2e18aaaSAndroid Build Coastguard Worker    <component name="NewModuleRootManager" inherit-compiler-output="true">
126*c2e18aaaSAndroid Build Coastguard Worker        <exclude-output />
127*c2e18aaaSAndroid Build Coastguard Worker        <content url="file://{SRC_PATH}">
128*c2e18aaaSAndroid Build Coastguard Worker            <sourceFolder url="file://{SRC_PATH}" isTestSource="false" />
129*c2e18aaaSAndroid Build Coastguard Worker        </content>
130*c2e18aaaSAndroid Build Coastguard Worker        <content url="file://{TEST_PATH}">
131*c2e18aaaSAndroid Build Coastguard Worker            <sourceFolder url="file://{TEST_PATH}" isTestSource="true" />
132*c2e18aaaSAndroid Build Coastguard Worker        </content>
133*c2e18aaaSAndroid Build Coastguard Worker        <orderEntry type="sourceFolder" forTests="false" />
134*c2e18aaaSAndroid Build Coastguard Worker        <orderEntry type="inheritedJdk" />
135*c2e18aaaSAndroid Build Coastguard Worker    </component>
136*c2e18aaaSAndroid Build Coastguard Worker</module>
137*c2e18aaaSAndroid Build Coastguard Worker""".format(SRC_PATH=src_path,
138*c2e18aaaSAndroid Build Coastguard Worker           TEST_PATH=test_path)
139*c2e18aaaSAndroid Build Coastguard Worker        self.iml.create({'dep_srcs': True})
140*c2e18aaaSAndroid Build Coastguard Worker        gen_iml = os.path.join(IMLGenUnittests._TEST_DIR,
141*c2e18aaaSAndroid Build Coastguard Worker                               self.iml._mod_info['path'][0],
142*c2e18aaaSAndroid Build Coastguard Worker                               self.iml._mod_info['iml_name'] + '.iml')
143*c2e18aaaSAndroid Build Coastguard Worker        result = common_util.read_file_content(gen_iml)
144*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(result, expected)
145*c2e18aaaSAndroid Build Coastguard Worker
146*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(iml.IMLGenerator, '_create_iml')
147*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(iml.IMLGenerator, '_generate_dependencies')
148*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(iml.IMLGenerator, '_generate_srcjars')
149*c2e18aaaSAndroid Build Coastguard Worker    def test_skip_create_iml(self, mock_gen_srcjars, mock_gen_dep,
150*c2e18aaaSAndroid Build Coastguard Worker                             mock_create_iml):
151*c2e18aaaSAndroid Build Coastguard Worker        """Test skipping create_iml."""
152*c2e18aaaSAndroid Build Coastguard Worker        self.iml.create({'srcjars': False, 'dependencies': False})
153*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_gen_srcjars.called)
154*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_gen_dep.called)
155*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_create_iml.called)
156*c2e18aaaSAndroid Build Coastguard Worker
157*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
158*c2e18aaaSAndroid Build Coastguard Worker    def test_generate_facet(self, mock_exists):
159*c2e18aaaSAndroid Build Coastguard Worker        """Test _generate_facet."""
160*c2e18aaaSAndroid Build Coastguard Worker        mock_exists.return_value = False
161*c2e18aaaSAndroid Build Coastguard Worker        self.iml._generate_facet()
162*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(self.iml._facet, '')
163*c2e18aaaSAndroid Build Coastguard Worker        mock_exists.return_value = True
164*c2e18aaaSAndroid Build Coastguard Worker        self.iml._generate_facet()
165*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(self.iml._facet, templates.FACET)
166*c2e18aaaSAndroid Build Coastguard Worker
167*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'get_android_root_dir')
168*c2e18aaaSAndroid Build Coastguard Worker    def test_get_uniq_iml_name(self, mock_root_path):
169*c2e18aaaSAndroid Build Coastguard Worker        """Test the unique name cache mechanism.
170*c2e18aaaSAndroid Build Coastguard Worker
171*c2e18aaaSAndroid Build Coastguard Worker        By using the path data in module info json as input, if the count of
172*c2e18aaaSAndroid Build Coastguard Worker        name data set is the same as sub folder path count, then it means
173*c2e18aaaSAndroid Build Coastguard Worker        there's no duplicated name, the test PASS.
174*c2e18aaaSAndroid Build Coastguard Worker        """
175*c2e18aaaSAndroid Build Coastguard Worker        root_path = '/usr/abc/code/root'
176*c2e18aaaSAndroid Build Coastguard Worker        mock_root_path.return_value = root_path
177*c2e18aaaSAndroid Build Coastguard Worker        # Add following test path.
178*c2e18aaaSAndroid Build Coastguard Worker        test_paths = {
179*c2e18aaaSAndroid Build Coastguard Worker            'cts/tests/tests/app',
180*c2e18aaaSAndroid Build Coastguard Worker            'cts/tests/app',
181*c2e18aaaSAndroid Build Coastguard Worker            'cts/tests/app/app1/../app',
182*c2e18aaaSAndroid Build Coastguard Worker            'cts/tests/app/app2/../app',
183*c2e18aaaSAndroid Build Coastguard Worker            'cts/tests/app/app3/../app',
184*c2e18aaaSAndroid Build Coastguard Worker            'frameworks/base/tests/xxxxxxxxxxxx/base',
185*c2e18aaaSAndroid Build Coastguard Worker            'frameworks/base',
186*c2e18aaaSAndroid Build Coastguard Worker            'external/xxxxx-xxx/robolectric',
187*c2e18aaaSAndroid Build Coastguard Worker            'external/robolectric',
188*c2e18aaaSAndroid Build Coastguard Worker        }
189*c2e18aaaSAndroid Build Coastguard Worker        # Add whole source tree test paths.
190*c2e18aaaSAndroid Build Coastguard Worker        whole_source_tree_paths = {
191*c2e18aaaSAndroid Build Coastguard Worker            root_path,
192*c2e18aaaSAndroid Build Coastguard Worker            root_path + "/",
193*c2e18aaaSAndroid Build Coastguard Worker        }
194*c2e18aaaSAndroid Build Coastguard Worker
195*c2e18aaaSAndroid Build Coastguard Worker        mod_info = module_info.ModuleInfo()
196*c2e18aaaSAndroid Build Coastguard Worker        test_paths.update(mod_info._get_path_to_module_info(
197*c2e18aaaSAndroid Build Coastguard Worker            mod_info.name_to_module_info).keys())
198*c2e18aaaSAndroid Build Coastguard Worker        print('\n{} {}.'.format('Test_paths length:', len(test_paths)))
199*c2e18aaaSAndroid Build Coastguard Worker
200*c2e18aaaSAndroid Build Coastguard Worker        path_list = []
201*c2e18aaaSAndroid Build Coastguard Worker        for path in test_paths:
202*c2e18aaaSAndroid Build Coastguard Worker            path_list.append(path)
203*c2e18aaaSAndroid Build Coastguard Worker        print('{} {}.'.format('path list with length:', len(path_list)))
204*c2e18aaaSAndroid Build Coastguard Worker
205*c2e18aaaSAndroid Build Coastguard Worker        names = [iml.IMLGenerator.get_unique_iml_name(f)
206*c2e18aaaSAndroid Build Coastguard Worker                 for f in path_list]
207*c2e18aaaSAndroid Build Coastguard Worker        print('{} {}.'.format('Names list with length:', len(names)))
208*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(len(names), len(path_list))
209*c2e18aaaSAndroid Build Coastguard Worker        dic = {}
210*c2e18aaaSAndroid Build Coastguard Worker        for i, path in enumerate(path_list):
211*c2e18aaaSAndroid Build Coastguard Worker            dic[names[i]] = path
212*c2e18aaaSAndroid Build Coastguard Worker        print('{} {}.'.format('The size of name set is:', len(dic)))
213*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(len(dic), len(path_list))
214*c2e18aaaSAndroid Build Coastguard Worker
215*c2e18aaaSAndroid Build Coastguard Worker        for path in whole_source_tree_paths:
216*c2e18aaaSAndroid Build Coastguard Worker            self.assertEqual(os.path.basename(root_path),
217*c2e18aaaSAndroid Build Coastguard Worker                             iml.IMLGenerator.get_unique_iml_name(path))
218*c2e18aaaSAndroid Build Coastguard Worker
219*c2e18aaaSAndroid Build Coastguard Worker
220*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
221*c2e18aaaSAndroid Build Coastguard Worker    unittest.main()
222