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 woth different conditions.""" 54 target = 'libui' 55 config = mock.Mock() 56 mock_get_inst.return_value = config 57 config.is_skip_build = True 58 nativeInfo = native_project_info.NativeProjectInfo 59 nativeInfo.modules_info = mock.Mock() 60 nativeInfo.modules_info.is_module.return_value = [True, True] 61 nativeInfo.modules_info.is_module_need_build.return_value = [True, True] 62 native_project_info.NativeProjectInfo.generate_projects([target]) 63 self.assertTrue(mock_mod_info.called) 64 self.assertTrue(mock_print.called) 65 self.assertFalse(mock_info.called) 66 67 mock_mod_info.reset_mock() 68 mock_print.reset_mock() 69 mock_info.reset_mock() 70 config.is_skip_build = False 71 nativeInfo.modules_info.is_module_need_build.return_value = [ 72 False, False] 73 mock_get_need.return_value = ['mod1', 'mod2'] 74 native_project_info.NativeProjectInfo.generate_projects([target]) 75 self.assertTrue(mock_mod_info.called) 76 self.assertTrue(mock_get_need.called) 77 self.assertTrue(mock_batch.called) 78 self.assertFalse(mock_print.called) 79 self.assertTrue(mock_info.called) 80 81 def test_get_need_builds_without_needed_build(self): 82 """Test _get_need_builds method without needed build.""" 83 targets = ['mod1', 'mod2'] 84 nativeInfo = native_project_info.NativeProjectInfo 85 nativeInfo.modules_info = mock.Mock() 86 nativeInfo.modules_info.is_module.return_value = [True, True] 87 nativeInfo.modules_info.is_module_need_build.return_value = [True, True] 88 self.assertEqual( 89 set(targets), 90 native_project_info.NativeProjectInfo._get_need_builds(targets)) 91 92 93if __name__ == '__main__': 94 unittest.main() 95