1#!/usr/bin/env python3
2#
3# Copyright 2018, 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 module_data."""
18
19import os
20import unittest
21from unittest import mock
22
23from aidegen import unittest_constants
24
25from aidegen.lib import common_util
26from aidegen.lib import module_info
27from aidegen.lib import project_config
28from aidegen.lib import source_locator
29from atest import module_info as amodule_info
30
31
32# pylint: disable=too-many-arguments
33# pylint: disable=protected-access
34class ModuleDataUnittests(unittest.TestCase):
35    """Unit tests for module_data.py"""
36
37    @mock.patch('os.path.dirname')
38    @mock.patch('logging.debug')
39    @mock.patch.object(source_locator.ModuleData, '_get_source_folder')
40    @mock.patch.object(source_locator.ModuleData, '_check_key')
41    @mock.patch.object(common_util, 'is_target')
42    @mock.patch.object(common_util, 'get_android_root_dir')
43    def test_collect_srcs_paths(self, mock_android_root_dir, mock_is_target,
44                                mock_check_key, mock_get_src, mock_log,
45                                mock_dirname):
46        """Test _collect_srcs_paths create the source path list."""
47        module = source_locator.ModuleData(
48            unittest_constants.TEST_MODULE, unittest_constants.MODULE_INFO, 0)
49        mock_check_key.return_value = False
50        module._collect_srcs_paths()
51        self.assertFalse(mock_dirname.called)
52        mock_check_key.return_value = True
53        mock_is_target.return_value = True
54        mock_android_root_dir.return_value = unittest_constants.TEST_DATA_PATH
55        module._collect_srcs_paths()
56        self.assertTrue(mock_is_target.called)
57        self.assertTrue(mock_get_src.called)
58        mock_is_target.return_value = False
59        module._collect_srcs_paths()
60        self.assertTrue(mock_log.called)
61
62    def test_get_package_name(self):
63        """test get the package name from a java file."""
64        result_package_name = 'com.android'
65        test_java = os.path.join(unittest_constants.TEST_DATA_PATH,
66                                 unittest_constants.MODULE_PATH,
67                                 'src/main/java/com/android/java.java')
68        package_name = source_locator.ModuleData._get_package_name(test_java)
69        self.assertEqual(package_name, result_package_name)
70
71        # Test on java file with no package name.
72        result_package_name = None
73        test_java = os.path.join(unittest_constants.TEST_DATA_PATH,
74                                 unittest_constants.MODULE_PATH,
75                                 'src/main/java/com/android/no_package.java')
76        package_name = source_locator.ModuleData._get_package_name(test_java)
77        self.assertEqual(package_name, result_package_name)
78
79    @mock.patch('aidegen.lib.common_util.get_android_root_dir')
80    def test_get_source_folder(self, mock_android_root_dir):
81        """Test _get_source_folder process."""
82        # Test for getting the source path by parse package name from a java.
83        test_java = 'packages/apps/test/src/main/java/com/android/java.java'
84        result_source = 'packages/apps/test/src/main/java'
85        mock_android_root_dir.return_value = unittest_constants.TEST_DATA_PATH
86        module_data = source_locator.ModuleData(
87            unittest_constants.TEST_MODULE, unittest_constants.MODULE_INFO, 0)
88        src_path = module_data._get_source_folder(test_java)
89        self.assertEqual(src_path, result_source)
90
91        # Return path is None if the java file doesn't exist.
92        test_java = 'file_not_exist.java'
93        src_path = module_data._get_source_folder(test_java)
94        self.assertEqual(src_path, None)
95
96        # Return path is None on the java file without package name.
97        test_java = ('packages/apps/test/src/main/java/com/android/'
98                     'no_package.java')
99        src_path = module_data._get_source_folder(test_java)
100        self.assertEqual(src_path, None)
101
102    def test_get_r_dir(self):
103        """Test get_r_dir."""
104        module_data = source_locator.ModuleData(
105            unittest_constants.TEST_MODULE, unittest_constants.MODULE_INFO, 0)
106        # Test for aapt2.srcjar
107        test_aapt2_srcjar = 'a/aapt2.srcjar'
108        expected_result = 'a/aapt2'
109        r_dir = module_data._get_r_dir(test_aapt2_srcjar)
110        self.assertEqual(r_dir, expected_result)
111
112        # Test for R.srcjar
113        test_r_jar = 'b/android/R.srcjar'
114        expected_result = 'b/aapt2/R'
115        r_dir = module_data._get_r_dir(test_r_jar)
116        self.assertEqual(r_dir, expected_result)
117
118        # Test the R.srcjar is not under the android folder.
119        test_wrong_r_jar = 'b/test/R.srcjar'
120        expected_result = None
121        r_dir = module_data._get_r_dir(test_wrong_r_jar)
122        self.assertEqual(r_dir, expected_result)
123
124        # Test for the target file is not aapt2.srcjar or R.srcjar
125        test_unknown_target = 'c/proto.srcjar'
126        expected_result = None
127        r_dir = module_data._get_r_dir(test_unknown_target)
128        self.assertEqual(r_dir, expected_result)
129
130    @mock.patch('os.path.exists')
131    @mock.patch('aidegen.lib.common_util.get_android_root_dir')
132    def test_collect_r_src_path(self, mock_android_root_dir, mock_exists):
133        """Test collect_r_src_path."""
134        mock_exists.return_value = True
135        # Test on target srcjar exists in srcjars.
136        test_module = dict(unittest_constants.MODULE_INFO)
137        test_module['srcs'] = []
138        mock_android_root_dir.return_value = unittest_constants.TEST_DATA_PATH
139        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
140                                                test_module, 0)
141        # Test the module is not APPS.
142        module_data._collect_r_srcs_paths()
143        expected_result = []
144        self.assertEqual(module_data.r_java_paths, expected_result)
145
146        # Test the module is not a target module.
147        test_module['depth'] = 1
148        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
149                                                test_module, 1)
150        module_data._collect_r_srcs_paths()
151        expected_result = []
152        self.assertEqual(module_data.r_java_paths, expected_result)
153
154        # Test the srcjar target doesn't exist.
155        test_module['class'] = ['APPS']
156        test_module['srcjars'] = []
157        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
158                                                test_module, 0)
159        module_data._collect_r_srcs_paths()
160        expected_result = []
161        self.assertEqual(module_data.r_java_paths, expected_result)
162
163        # Test the srcjar target exists.
164        test_module['srcjars'] = [('out/soong/.intermediates/packages/apps/'
165                                   'test_aapt2/aapt2.srcjar')]
166        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
167                                                test_module, 0)
168        module_data._collect_r_srcs_paths()
169        expected_result = [
170            'out/soong/.intermediates/packages/apps/test_aapt2/aapt2'
171        ]
172        self.assertEqual(module_data.r_java_paths, expected_result)
173        mock_exists.return_value = False
174        module_data._collect_r_srcs_paths()
175        expected_result = {('out/soong/.intermediates/packages/apps/'
176                            'test_aapt2/aapt2.srcjar')}
177        self.assertEqual(module_data.build_targets, expected_result)
178
179    def test_parse_source_path(self):
180        """Test _parse_source_path."""
181        # The package name of e.java is c.d.
182        test_java = 'a/b/c/d/e.java'
183        package_name = 'c.d'
184        expected_result = 'a/b'
185        src_path = source_locator.ModuleData._parse_source_path(
186            test_java, package_name)
187        self.assertEqual(src_path, expected_result)
188
189        # The package name of e.java is c.d.
190        test_java = 'a/b/c.d/e.java'
191        package_name = 'c.d'
192        expected_result = 'a/b'
193        src_path = source_locator.ModuleData._parse_source_path(
194            test_java, package_name)
195        self.assertEqual(src_path, expected_result)
196
197        # The package name of e.java is x.y.
198        test_java = 'a/b/c/d/e.java'
199        package_name = 'x.y'
200        expected_result = 'a/b/c/d'
201        src_path = source_locator.ModuleData._parse_source_path(
202            test_java, package_name)
203        self.assertEqual(src_path, expected_result)
204
205        # The package name of f.java is c.d.
206        test_java = 'a/b/c.d/e/c/d/f.java'
207        package_name = 'c.d'
208        expected_result = 'a/b/c.d/e'
209        src_path = source_locator.ModuleData._parse_source_path(
210            test_java, package_name)
211        self.assertEqual(src_path, expected_result)
212
213        # The package name of f.java is c.d.e.
214        test_java = 'a/b/c.d/e/c.d/e/f.java'
215        package_name = 'c.d.e'
216        expected_result = 'a/b/c.d/e'
217        src_path = source_locator.ModuleData._parse_source_path(
218            test_java, package_name)
219        self.assertEqual(src_path, expected_result)
220
221    @mock.patch('aidegen.lib.common_util.get_android_root_dir')
222    def test_append_jar_file(self, mock_android_root_dir):
223        """Test _append_jar_file process."""
224        # Append an existing jar file path to module_data.jar_files.
225        test_jar_file = os.path.join(unittest_constants.MODULE_PATH, 'test.jar')
226        result_jar_list = [test_jar_file]
227        mock_android_root_dir.return_value = unittest_constants.TEST_DATA_PATH
228        module_data = source_locator.ModuleData(
229            unittest_constants.TEST_MODULE, unittest_constants.MODULE_INFO, 0)
230        module_data._append_jar_file(test_jar_file)
231        module_data._append_jar_file(test_jar_file)
232        self.assertEqual(module_data.jar_files, result_jar_list)
233
234        # Skip if the jar file doesn't exist.
235        test_jar_file = os.path.join(unittest_constants.MODULE_PATH,
236                                     'jar_not_exist.jar')
237        module_data.jar_files = []
238        module_data._append_jar_file(test_jar_file)
239        self.assertEqual(module_data.jar_files, [])
240
241        # Skip if it's not a jar file.
242        test_jar_file = os.path.join(unittest_constants.MODULE_PATH,
243                                     'test.java')
244        module_data.jar_files = []
245        module_data._append_jar_file(test_jar_file)
246        self.assertEqual(module_data.jar_files, [])
247
248    @mock.patch.object(source_locator.ModuleData, '_check_key')
249    @mock.patch('aidegen.lib.common_util.get_android_root_dir')
250    def test_append_jar_from_installed(self, mock_android_root_dir,
251                                       mock_check_key):
252        """Test _append_jar_from_installed handling."""
253        mock_check_key.return_value = True
254        # Test appends the first jar file of 'installed'.
255        mod_info = dict(unittest_constants.MODULE_INFO)
256        mod_info['installed'] = [
257            os.path.join(unittest_constants.MODULE_PATH, 'test.aar'),
258            os.path.join(unittest_constants.MODULE_PATH, 'test.jar'),
259            os.path.join(unittest_constants.MODULE_PATH,
260                         'tests/test_second.jar')
261        ]
262        result_jar_list = [
263            os.path.join(unittest_constants.MODULE_PATH, 'test.jar')
264        ]
265        mock_android_root_dir.return_value = unittest_constants.TEST_DATA_PATH
266        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
267                                                mod_info, 0)
268        module_data._append_jar_from_installed()
269        self.assertEqual(module_data.jar_files, result_jar_list)
270
271        # Test on the jar file path matches the path prefix.
272        module_data.jar_files = []
273        result_jar_list = [
274            os.path.join(unittest_constants.MODULE_PATH,
275                         'tests/test_second.jar')
276        ]
277        module_data._append_jar_from_installed(
278            os.path.join(unittest_constants.MODULE_PATH, 'tests/'))
279        self.assertEqual(module_data.jar_files, result_jar_list)
280        mock_check_key.return_value = False
281        module_data.jar_files = []
282        module_data._append_jar_from_installed(
283            os.path.join(unittest_constants.MODULE_PATH, 'tests/'))
284        self.assertEqual(module_data.jar_files, [])
285
286    @mock.patch('aidegen.lib.common_util.get_android_root_dir')
287    def test_set_jars_jarfile(self, mock_android_root_dir):
288        """Test _set_jars_jarfile handling."""
289        # Combine the module path with jar file name in 'jars' and then append
290        # it to module_data.jar_files.
291        mod_info = dict(unittest_constants.MODULE_INFO)
292        mod_info['jars'] = [
293            'test.jar',
294            'src/test.jar',  # This jar file doesn't exist.
295            'tests/test_second.jar'
296        ]
297        result_jar_list = [
298            os.path.join(unittest_constants.MODULE_PATH, 'test.jar'),
299            os.path.join(unittest_constants.MODULE_PATH,
300                         'tests/test_second.jar')
301        ]
302        result_missing_jars = set()
303        mock_android_root_dir.return_value = unittest_constants.TEST_DATA_PATH
304        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
305                                                mod_info, 0)
306        module_data._set_jars_jarfile()
307        self.assertEqual(module_data.jar_files, result_jar_list)
308        self.assertEqual(module_data.missing_jars, result_missing_jars)
309
310    @mock.patch('aidegen.lib.common_util.get_android_root_dir')
311    def test_locate_sources_path(self, mock_android_root_dir):
312        """Test locate_sources_path handling."""
313        # Test collect source path.
314        mod_info = dict(unittest_constants.MODULE_INFO)
315        result_src_list = ['packages/apps/test/src/main/java']
316        result_test_list = ['packages/apps/test/tests']
317        result_jar_list = []
318        result_r_path = []
319        mock_android_root_dir.return_value = unittest_constants.TEST_DATA_PATH
320        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
321                                                mod_info, 0)
322        module_data.locate_sources_path()
323        self.assertEqual(module_data.src_dirs, result_src_list)
324        self.assertEqual(module_data.test_dirs, result_test_list)
325        self.assertEqual(module_data.jar_files, result_jar_list)
326        self.assertEqual(module_data.r_java_paths, result_r_path)
327
328        # Test find jar files.
329        jar_file = ('out/soong/.intermediates/packages/apps/test/test/'
330                    'android_common/test.jar')
331        mod_info['jarjar_rules'] = ['jarjar-rules.txt']
332        mod_info['installed'] = [jar_file]
333        result_jar_list = [jar_file]
334        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
335                                                mod_info, 0)
336        module_data.locate_sources_path()
337        self.assertEqual(module_data.jar_files, result_jar_list)
338
339    @mock.patch('aidegen.lib.common_util.get_android_root_dir')
340    def test_collect_jar_by_depth_value(self, mock_android_root_dir):
341        """Test parameter --depth handling."""
342        # Test find jar by module's depth greater than the --depth value from
343        # command line.
344        depth_by_source = 2
345        mod_info = dict(unittest_constants.MODULE_INFO)
346        mod_info['depth'] = 3
347        mod_info['installed'] = [
348            ('out/soong/.intermediates/packages/apps/test/test/android_common/'
349             'test.jar')
350        ]
351        result_src_list = []
352        result_jar_list = [
353            ('out/soong/.intermediates/packages/apps/test/test/'
354             'android_common/test.jar')
355        ]
356        mock_android_root_dir.return_value = unittest_constants.TEST_DATA_PATH
357        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
358                                                mod_info, depth_by_source)
359        module_data.locate_sources_path()
360        self.assertEqual(module_data.src_dirs, result_src_list)
361        self.assertEqual(module_data.jar_files, result_jar_list)
362
363        # Test find source folder when module's depth equal to the --depth value
364        # from command line.
365        depth_by_source = 2
366        mod_info = dict(unittest_constants.MODULE_INFO)
367        mod_info['depth'] = 2
368        result_src_list = ['packages/apps/test/src/main/java']
369        result_test_list = ['packages/apps/test/tests']
370        result_jar_list = []
371        result_r_path = []
372        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
373                                                mod_info, depth_by_source)
374        module_data.locate_sources_path()
375        self.assertEqual(module_data.src_dirs, result_src_list)
376        self.assertEqual(module_data.test_dirs, result_test_list)
377        self.assertEqual(module_data.jar_files, result_jar_list)
378        self.assertEqual(module_data.r_java_paths, result_r_path)
379
380        # Test find source folder when module's depth smaller than the --depth
381        # value from command line.
382        depth_by_source = 3
383        mod_info = dict(unittest_constants.MODULE_INFO)
384        mod_info['depth'] = 2
385        result_src_list = ['packages/apps/test/src/main/java']
386        result_test_list = ['packages/apps/test/tests']
387        result_jar_list = []
388        result_r_path = []
389        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
390                                                mod_info, depth_by_source)
391        module_data.locate_sources_path()
392        self.assertEqual(module_data.src_dirs, result_src_list)
393        self.assertEqual(module_data.test_dirs, result_test_list)
394        self.assertEqual(module_data.jar_files, result_jar_list)
395        self.assertEqual(module_data.r_java_paths, result_r_path)
396
397    def test_collect_srcjar_path(self):
398        """Test collect srcjar path."""
399        srcjar_path = 'a/b/aapt2.srcjar'
400        test_module = dict(unittest_constants.MODULE_INFO)
401        test_module['srcjars'] = [srcjar_path]
402        expected_result = [srcjar_path]
403        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
404                                                test_module, 0)
405        module_data._collect_srcjar_path('R.java')
406        self.assertEqual(module_data.srcjar_paths, [])
407        module_data._collect_srcjar_path(srcjar_path)
408        self.assertEqual(module_data.srcjar_paths, expected_result)
409
410    @mock.patch('os.path.exists')
411    def test_collect_all_srcjar_path(self, mock_exists):
412        """Test collect all srcjar paths as source root folders."""
413        mock_exists.return_value = True
414        test_module = dict(unittest_constants.MODULE_INFO)
415        test_module['srcjars'] = [
416            'a/b/aidl0.srcjar',
417            'a/b/aidl2.srcjar',
418            'a/b/aidl1.srcjar',
419            'a/b/aidl2.srcjar'
420        ]
421        expected_result = [
422            'a/b/aidl0.srcjar',
423            'a/b/aidl2.srcjar',
424            'a/b/aidl1.srcjar'
425        ]
426        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
427                                                test_module, 0)
428        module_data._collect_all_srcjar_paths()
429        self.assertEqual(module_data.srcjar_paths, expected_result)
430
431        mock_exists.return_value = False
432        test_module['srcjars'] = ['a/b/aidl0.srcjar']
433        expected_result = {'a/b/aidl0.srcjar'}
434        module_data = source_locator.ModuleData(unittest_constants.TEST_MODULE,
435                                                test_module, 0)
436        module_data._collect_all_srcjar_paths()
437        self.assertEqual(module_data.build_targets, expected_result)
438
439    def test_collect_missing_jars(self):
440        """Test _collect_missing_jars."""
441        mod_name = 'test'
442        mod_info = {'name': 'test'}
443        test_path = 'a/b/c'
444        mod_data = source_locator.EclipseModuleData(mod_name, mod_info,
445                                                    test_path)
446        mod_data.missing_jars = set('a')
447        mod_data.referenced_by_jar = False
448        mod_data._collect_missing_jars()
449        self.assertEqual(mod_data.build_targets, set())
450        mod_data.referenced_by_jar = True
451        mod_data._collect_missing_jars()
452        self.assertEqual(mod_data.build_targets, {'a'})
453
454    @mock.patch.object(source_locator.ModuleData, '_check_key')
455    def test_check_classes_jar_exist(self, mock_check_key):
456        """Test _check_classes_jar_exist."""
457        mod_data = source_locator.ModuleData(
458            unittest_constants.TEST_MODULE, unittest_constants.MODULE_INFO, 0)
459        mod_data._check_classes_jar_exist()
460        self.assertTrue(mock_check_key.called)
461
462    @mock.patch('os.path.exists')
463    @mock.patch.object(common_util, 'get_android_root_dir')
464    def test_switch_repackaged(self, mock_android_root_dir, mock_exist):
465        """Test _switch_repackaged."""
466        mock_android_root_dir.return_value = '/a'
467        mock_exist.return_value = False
468        mod_data = source_locator.ModuleData(
469            unittest_constants.TEST_MODULE, unittest_constants.MODULE_INFO, 0)
470        self.assertEqual(mod_data._switch_repackaged('b/c'), 'b/c')
471        mock_exist.return_value = True
472        self.assertEqual(mod_data._switch_repackaged('b/c'), 'b/repackaged/c')
473
474    def test_add_to_source_or_test_dirs(self):
475        """Test _add_to_source_or_test_dirs."""
476        mod_name = 'test'
477        mod_info = {'name': 'test'}
478        mod_data = source_locator.ModuleData(mod_name, mod_info, 0)
479        mod_data._add_to_source_or_test_dirs('libcore/ojluni/src/lambda/java')
480        self.assertEqual(mod_data.src_dirs, [])
481        mod_data._add_to_source_or_test_dirs('a')
482        self.assertEqual(mod_data.src_dirs, ['a'])
483        mod_data._add_to_source_or_test_dirs('b')
484        self.assertEqual(mod_data.src_dirs, ['a', 'b'])
485        mod_data._add_to_source_or_test_dirs('a')
486        self.assertEqual(mod_data.src_dirs, ['a', 'b'])
487        mod_data._add_to_source_or_test_dirs('tests/src')
488        self.assertEqual(mod_data.test_dirs, ['tests/src'])
489
490    @mock.patch.object(source_locator.ModuleData, '_append_jar_file')
491    def test_append_classes_jar(self, mock_append_jar):
492        """Test _append_classes_jar."""
493        mod_name = 'test'
494        mod_info = {'name': 'test'}
495        mod_data = source_locator.ModuleData(mod_name, mod_info, 0)
496        mod_data.module_data['classes_jar'] = ['a.jar']
497        mod_data._append_classes_jar()
498        self.assertTrue(mock_append_jar.called)
499        mock_append_jar.return_value = False
500        mod_data._append_classes_jar()
501        self.assertEqual(mod_data.jar_files, [])
502
503    @mock.patch.object(amodule_info, 'ModuleInfo')
504    @mock.patch.object(amodule_info.ModuleInfo, 'get_paths')
505    @mock.patch.object(project_config.ProjectConfig, 'get_instance')
506    def test_collect_dep_paths(self, mock_config, mock_get_paths,
507                               mock_atest_module_info):
508        """Test _collect_dep_paths."""
509        mod_name = 'test'
510        mod_info = {
511            'name': 'test',
512            'path': ['frameworks/base'],
513            'dependencies': ['test_module']
514        }
515        mod_data = source_locator.ModuleData(mod_name, mod_info, 0)
516        mock_instance = mock_config.return_value
517        mock_instance.atest_module_info = mock_atest_module_info
518        mock_instance.atest_module_info.get_paths = mock_get_paths
519        mock_get_paths.return_value = []
520        expected = [
521            'frameworks/base/framework_srcjars',
522            'libcore',
523        ]
524        mod_data._collect_dep_paths()
525        self.assertEqual(mod_data.dep_paths, expected)
526        mod_info['path'] = ['libcore']
527        mod_data = source_locator.ModuleData(mod_name, mod_info, 0)
528        expected = [
529            'frameworks/base',
530            'frameworks/base/framework_srcjars',
531        ]
532        mod_data._collect_dep_paths()
533        self.assertEqual(mod_data.dep_paths, expected)
534        mock_get_paths.return_value = ['test']
535        mod_info['path'] = ['test']
536        mod_data = source_locator.ModuleData(mod_name, mod_info, 0)
537        expected = [
538            'frameworks/base',
539            'frameworks/base/framework_srcjars',
540            'libcore',
541        ]
542        mod_data._collect_dep_paths()
543        self.assertEqual(mod_data.dep_paths, expected)
544        mock_get_paths.return_value = ['dep/path']
545        expected.append('dep/path')
546        mod_data._collect_dep_paths()
547        self.assertEqual(mod_data.dep_paths, expected)
548
549
550class EclipseModuleDataUnittests(unittest.TestCase):
551    """Unit tests for the EclipseModuleData in module_data.py"""
552
553    @mock.patch.object(module_info.AidegenModuleInfo,
554                       'is_project_path_relative_module')
555    @mock.patch.object(source_locator.ModuleData, '__init__')
556    def test___init__(self, mock_base_init, mock_method):
557        """Test the implement of __init__()."""
558        mod_name = 'test'
559        mod_info = {'name': 'test'}
560        test_path = 'a/b/c'
561        source_locator.EclipseModuleData(mod_name, mod_info, test_path)
562        self.assertTrue(mock_base_init.called)
563        self.assertTrue(mock_method.called)
564
565    @mock.patch.object(source_locator.ModuleData, '_collect_missing_jars')
566    @mock.patch.object(source_locator.ModuleData, '_collect_classes_jars')
567    @mock.patch.object(source_locator.EclipseModuleData, '_locate_jar_path')
568    @mock.patch.object(source_locator.EclipseModuleData,
569                       '_locate_project_source_path')
570    def test_locate_sources_path(self, mock_src, mock_jar, mock_class_jar,
571                                 mock_missing_jar):
572        """Test locate_sources_path."""
573        mod_name = 'test'
574        mod_info = {'name': 'test'}
575        test_path = 'a/b/c'
576        mod_data = source_locator.EclipseModuleData(mod_name, mod_info,
577                                                    test_path)
578        mod_data.is_project = True
579        mod_data.locate_sources_path()
580        self.assertTrue(mock_src.called)
581        self.assertTrue(mock_class_jar.called)
582        self.assertTrue(mock_missing_jar.called)
583
584        mock_src.reset()
585        mock_jar.reset()
586        mod_data.is_project = False
587        mod_data.locate_sources_path()
588        self.assertTrue(mock_jar.called)
589
590    @mock.patch.object(source_locator.ModuleData, '_collect_srcs_paths')
591    @mock.patch.object(source_locator.ModuleData, '_collect_r_srcs_paths')
592    def test_locate_project_source_path(self, mock_src, mock_r):
593        """Test _locate_project_source_path."""
594        mod_name = 'test'
595        mod_info = {'name': 'test'}
596        test_path = 'a/b/c'
597        mod_data = source_locator.EclipseModuleData(mod_name, mod_info,
598                                                    test_path)
599        mod_data._locate_project_source_path()
600        self.assertTrue(mock_src.called)
601        self.assertTrue(mock_r.called)
602
603    @mock.patch.object(source_locator.ModuleData, '_append_classes_jar')
604    @mock.patch.object(source_locator.ModuleData, '_check_key')
605    @mock.patch.object(source_locator.ModuleData, '_set_jars_jarfile')
606    @mock.patch.object(source_locator.ModuleData, '_check_jars_exist')
607    @mock.patch.object(source_locator.ModuleData, '_append_jar_from_installed')
608    @mock.patch.object(source_locator.ModuleData, '_check_jarjar_rules_exist')
609    def test_locate_jar_path(self, mock_jarjar, mock_append_jar, mock_check_jar,
610                             mock_set_jar, mock_check_key, mock_append_class):
611        """Test _locate_jar_path."""
612        mod_name = 'test'
613        mod_info = {'name': 'test', 'path': 'x/y'}
614        test_path = 'a/b/c'
615        mod_data = source_locator.EclipseModuleData(mod_name, mod_info,
616                                                    test_path)
617        mock_jarjar.return_value = False
618        mock_check_jar.return_value = False
619        mock_check_key.return_value = False
620        mod_data._locate_jar_path()
621        self.assertTrue(mock_append_jar.called)
622        self.assertFalse(mock_set_jar.called)
623        self.assertFalse(mock_append_class.called)
624
625        mock_append_jar.reset_mock()
626        mock_jarjar.return_value = False
627        mock_check_jar.return_value = False
628        mock_check_key.return_value = True
629        mod_data._locate_jar_path()
630        self.assertFalse(mock_append_jar.called)
631        self.assertFalse(mock_set_jar.called)
632        self.assertTrue(mock_append_class.called)
633
634        mock_check_key.reset_mock()
635        mock_append_class.reset_mock()
636        mock_append_jar.reset_mock()
637        mock_jarjar.return_value = False
638        mock_check_jar.return_value = True
639        mod_data._locate_jar_path()
640        self.assertFalse(mock_append_jar.called)
641        self.assertTrue(mock_set_jar.called)
642        self.assertFalse(mock_check_key.called)
643        self.assertFalse(mock_append_class.called)
644
645        mock_append_jar.reset_mock()
646        mock_set_jar.reset_mock()
647        mock_check_jar.reset_mock()
648        mock_check_key.reset_mock()
649        mock_append_class.reset_mock()
650
651        mock_jarjar.return_value = True
652        mod_data._locate_jar_path()
653        self.assertTrue(mock_append_jar.called)
654        self.assertFalse(mock_check_jar.called)
655        self.assertFalse(mock_set_jar.called)
656        self.assertFalse(mock_check_key.called)
657        self.assertFalse(mock_append_class.called)
658
659    def test_add_to_source_or_test_dirs(self):
660        """Test _add_to_source_or_test_dirs."""
661        mod_name = 'test'
662        mod_info = {'name': 'test'}
663        test_path = 'a/b/c'
664        mod_data = source_locator.EclipseModuleData(mod_name, mod_info,
665                                                    test_path)
666        mod_data._add_to_source_or_test_dirs('libcore/ojluni/src/lambda/java')
667        self.assertEqual(mod_data.src_dirs, [])
668        mod_data._add_to_source_or_test_dirs('a')
669        self.assertEqual(mod_data.src_dirs, ['a'])
670
671
672if __name__ == '__main__':
673    unittest.main()
674