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