1#!/usr/bin/env python3
2#
3# Copyright 2020, 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 native_util."""
18
19import unittest
20from unittest import mock
21
22from aidegen.lib import native_module_info
23from aidegen.lib import native_project_info
24from aidegen.lib import project_config
25from aidegen.lib import project_info
26
27
28# pylint: disable=protected-access
29class NativeProjectInfoUnittests(unittest.TestCase):
30    """Unit tests for native_project_info.py"""
31
32    @mock.patch.object(native_module_info, 'NativeModuleInfo')
33    def test_init_modules_info(self, mock_mod_info):
34        """Test initializing the class attribute: modules_info."""
35        native_project_info.NativeProjectInfo.modules_info = None
36        native_project_info.NativeProjectInfo._init_modules_info()
37        self.assertEqual(mock_mod_info.call_count, 1)
38        native_project_info.NativeProjectInfo._init_modules_info()
39        self.assertEqual(mock_mod_info.call_count, 1)
40
41    # pylint: disable=too-many-arguments
42    @mock.patch('logging.info')
43    @mock.patch('builtins.print')
44    @mock.patch.object(project_info, 'batch_build_dependencies')
45    @mock.patch.object(native_project_info.NativeProjectInfo,
46                       '_get_need_builds')
47    @mock.patch.object(native_project_info.NativeProjectInfo,
48                       '_init_modules_info')
49    @mock.patch.object(project_config.ProjectConfig, 'get_instance')
50    def test_generate_projects(self, mock_get_inst, mock_mod_info,
51                               mock_get_need, mock_batch, mock_print,
52                               mock_info):
53        """Test initializing NativeProjectInfo with different conditions."""
54        target = 'libui'
55        config = mock.Mock()
56        mock_get_inst.return_value = config
57        config.is_skip_build = True
58        native_info = native_project_info.NativeProjectInfo
59        native_info.modules_info = mock.Mock()
60        native_info.modules_info.is_module.return_value = [True, True]
61        native_info.modules_info.is_module_need_build.return_value = [True,
62                                                                      True]
63        native_project_info.NativeProjectInfo.generate_projects([target])
64        self.assertTrue(mock_mod_info.called)
65        self.assertTrue(mock_print.called)
66        self.assertFalse(mock_info.called)
67
68        mock_mod_info.reset_mock()
69        mock_print.reset_mock()
70        mock_info.reset_mock()
71        config.is_skip_build = False
72        native_info.modules_info.is_module_need_build.return_value = [
73            False, False]
74        mock_get_need.return_value = ['mod1', 'mod2']
75        native_project_info.NativeProjectInfo.generate_projects([target])
76        self.assertTrue(mock_mod_info.called)
77        self.assertTrue(mock_get_need.called)
78        self.assertTrue(mock_batch.called)
79        self.assertFalse(mock_print.called)
80        self.assertTrue(mock_info.called)
81
82    def test_get_need_builds_without_needed_build(self):
83        """Test _get_need_builds method without needed build."""
84        targets = ['mod1', 'mod2']
85        native_info = native_project_info.NativeProjectInfo
86        native_info.modules_info = mock.Mock()
87        native_info.modules_info.is_module.return_value = [True, True]
88        native_info.modules_info.is_module_need_build.return_value = [True,
89                                                                      True]
90        self.assertEqual(
91            set(targets),
92            native_project_info.NativeProjectInfo._get_need_builds(targets))
93
94
95if __name__ == '__main__':
96    unittest.main()
97