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 @mock.patch.object(project_info, 'batch_build_dependencies') 42 @mock.patch.object(native_project_info.NativeProjectInfo, 43 '_get_need_builds') 44 @mock.patch.object(native_project_info.NativeProjectInfo, 45 '_init_modules_info') 46 @mock.patch.object(project_config.ProjectConfig, 'get_instance') 47 def test_generate_projects(self, mock_get_inst, mock_mod_info, 48 mock_get_need, mock_batch): 49 """Test initializing NativeProjectInfo woth different conditions.""" 50 target = 'libui' 51 config = mock.Mock() 52 mock_get_inst.return_value = config 53 config.is_skip_build = True 54 native_project_info.NativeProjectInfo.generate_projects([target]) 55 self.assertFalse(mock_mod_info.called) 56 57 mock_mod_info.reset_mock() 58 config.is_skip_build = False 59 mock_get_need.return_value = ['mod1', 'mod2'] 60 native_project_info.NativeProjectInfo.generate_projects([target]) 61 self.assertTrue(mock_mod_info.called) 62 self.assertTrue(mock_get_need.called) 63 self.assertTrue(mock_batch.called) 64 65 def test_get_need_builds_without_needed_build(self): 66 """Test _get_need_builds method without needed build.""" 67 targets = ['mod1', 'mod2'] 68 nativeInfo = native_project_info.NativeProjectInfo 69 nativeInfo.modules_info = mock.Mock() 70 nativeInfo.modules_info.is_module.return_value = [True, True] 71 nativeInfo.modules_info.is_module_need_build.return_value = [True, True] 72 self.assertEqual( 73 set(targets), 74 native_project_info.NativeProjectInfo._get_need_builds(targets)) 75 76 77if __name__ == '__main__': 78 unittest.main() 79