1#!/usr/bin/env python3 2# 3# Copyright 2019, 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 generate project file of Eclipse.""" 18 19import os 20import unittest 21from unittest import mock 22 23from aidegen import unittest_constants 24from aidegen.lib import common_util 25from aidegen.lib import eclipse_project_file_gen 26from aidegen.lib import project_info 27 28 29# pylint: disable=protected-access 30class EclipseConfUnittests(unittest.TestCase): 31 """Unit tests for generate_project_file.py""" 32 _ROOT_PATH = '/android/root' 33 _PROJECT_RELPATH = 'module/path' 34 _PROJECT_ABSPATH = os.path.join(_ROOT_PATH, _PROJECT_RELPATH) 35 _PROJECT_SAMPLE = os.path.join(unittest_constants.TEST_DATA_PATH, 36 'eclipse.project') 37 38 def setUp(self): 39 """Prepare the EclipseConf object.""" 40 with mock.patch.object(project_info, 'ProjectInfo') as self.proj_info: 41 self.proj_info.project_absolute_path = self._PROJECT_ABSPATH 42 self.proj_info.project_relative_path = self._PROJECT_RELPATH 43 self.proj_info.module_name = 'test' 44 self.eclipse = eclipse_project_file_gen.EclipseConf(self.proj_info) 45 46 def tearDown(self): 47 """Clear the EclipseConf object.""" 48 self.eclipse = None 49 50 @mock.patch.object(common_util, 'get_android_root_dir') 51 def test_gen_link(self, mock_get_root): 52 """Test get_link return a correct link resource config.""" 53 mock_get_root.return_value = '/a' 54 expected_result = (' <link><name>dependencies/b/c</name>' 55 '<type>2</type><location>/a/b/c</location></link>\n') 56 self.assertEqual(self.eclipse._gen_link('b/c'), expected_result) 57 58 @mock.patch('os.path.exists') 59 @mock.patch.object(common_util, 'get_android_out_dir') 60 @mock.patch.object(common_util, 'get_android_root_dir') 61 def test_create_project_content(self, mock_get_root, mock_out_dir, 62 mock_dir_exists): 63 """Test _create_project_content.""" 64 mock_get_root.return_value = self._ROOT_PATH 65 mock_out_dir.return_value = os.path.join(self._ROOT_PATH, 'out') 66 mock_dir_exists.return_value = True 67 self.eclipse.jar_module_paths = { 68 'relative/path/to/file1.jar': self._PROJECT_RELPATH, 69 'relative/path/to/file2.jar': 'source/folder/of/jar', 70 } 71 expected_content = common_util.read_file_content(self._PROJECT_SAMPLE) 72 self.eclipse._create_project_content() 73 self.assertEqual(self.eclipse.project_content, expected_content) 74 75 @mock.patch.object(common_util, 'exist_android_bp') 76 @mock.patch.object(common_util, 'get_android_root_dir') 77 def test_gen_src_path_entries(self, mock_get_root, mock_exist_android_bp): 78 """Test generate source folders' class path entries.""" 79 mock_get_root.return_value = self._ROOT_PATH 80 self.eclipse.src_paths = {'module/path/src', 'module/path/test', 81 'out/src'} 82 expected_result = [ 83 ' <classpathentry kind="src" path="dependencies/out/src"/>\n', 84 ' <classpathentry kind="src" path="src"/>\n', 85 ' <classpathentry kind="src" path="test"/>\n', 86 ] 87 mock_exist_android_bp.return_value = False 88 generated_result = sorted(self.eclipse._gen_src_path_entries()) 89 self.assertEqual(generated_result, expected_result) 90 91 mock_exist_android_bp.return_value = True 92 expected_result = [ 93 ' <classpathentry excluding="Android.bp" kind="src" ' 94 'path="dependencies/out/src"/>\n', 95 ' <classpathentry excluding="Android.bp" kind="src" ' 96 'path="src"/>\n', 97 ' <classpathentry excluding="Android.bp" kind="src" ' 98 'path="test"/>\n', 99 ] 100 generated_result = sorted(self.eclipse._gen_src_path_entries()) 101 self.assertEqual(generated_result, expected_result) 102 103 @mock.patch.object(common_util, 'get_android_root_dir') 104 def test_gen_jar_path_entries(self, mock_get_root): 105 """Test generate jar files' class path entries.""" 106 mock_get_root.return_value = self._ROOT_PATH 107 self.eclipse.jar_module_paths = { 108 '/abspath/to/the/file.jar': 'relpath/to/the/module', 109 } 110 expected_result = [ 111 (' <classpathentry exported="true" kind="lib" ' 112 'path="/abspath/to/the/file.jar" ' 113 'sourcepath="dependencies/relpath/to/the/module"/>\n') 114 ] 115 self.assertEqual(self.eclipse._gen_jar_path_entries(), expected_result) 116 117 def test_get_other_src_folders(self): 118 """Test _get_other_src_folders.""" 119 self.eclipse.src_paths = {'module/path/src', 'module/path/test', 120 'out/module/path/src'} 121 expected_result = ['out/module/path/src'] 122 self.assertEqual(self.eclipse._get_other_src_folders(), expected_result) 123 124 @mock.patch('os.makedirs') 125 @mock.patch('os.path.exists') 126 def test_gen_bin_link(self, mock_exists, mock_mkdir): 127 """Test _gen_bin_link.""" 128 mock_exists.return_value = True 129 self.eclipse._gen_bin_link() 130 self.assertFalse(mock_mkdir.called) 131 mock_exists.return_value = False 132 self.eclipse._gen_bin_link() 133 self.assertTrue(mock_mkdir.called) 134 135 def test_gen_r_path_entries(self): 136 """Test _gen_r_path_entries.""" 137 self.eclipse.r_java_paths = ['a/b', 'c/d'] 138 expected_result = [ 139 ' <classpathentry kind="src" path="dependencies/a/b"/>\n', 140 ' <classpathentry kind="src" path="dependencies/c/d"/>\n', 141 ] 142 self.assertEqual(self.eclipse._gen_r_path_entries(), expected_result) 143 144 def test_gen_bin_dir_entry(self): 145 """Test _gen_bin_dir_entry.""" 146 expected_result = [' <classpathentry kind="src" path="bin"/>\n'] 147 self.assertEqual(self.eclipse._gen_bin_dir_entry(), expected_result) 148 149 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 150 '_gen_jar_path_entries') 151 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 152 '_gen_bin_dir_entry') 153 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 154 '_gen_r_path_entries') 155 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 156 '_gen_src_path_entries') 157 def test_create_classpath_content(self, mock_gen_src, mock_gen_r, 158 mock_gen_bin, mock_gen_jar): 159 """Test _create_classpath_content.""" 160 self.eclipse._create_classpath_content() 161 self.assertTrue(mock_gen_src.called) 162 self.assertTrue(mock_gen_r.called) 163 self.assertTrue(mock_gen_bin.called) 164 self.assertTrue(mock_gen_jar.called) 165 166 @mock.patch.object(common_util, 'file_generate') 167 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 168 '_create_project_content') 169 def test_generate_project_file(self, mock_gen_content, mock_gen_file): 170 """Test generate_project_file.""" 171 self.eclipse.generate_project_file() 172 self.assertTrue(mock_gen_content.called) 173 self.assertTrue(mock_gen_file.called) 174 175 @mock.patch.object(common_util, 'file_generate') 176 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 177 '_create_classpath_content') 178 def test_generate_classpath_file(self, mock_gen_content, mock_gen_file): 179 """Test generate_classpath_file.""" 180 self.eclipse.generate_classpath_file() 181 self.assertTrue(mock_gen_content.called) 182 self.assertTrue(mock_gen_file.called) 183 184 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 185 'generate_classpath_file') 186 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 187 'generate_project_file') 188 def test_generate_ide_project_files(self, mock_gen_project, 189 mock_gen_classpath): 190 """Test generate_ide_project_files.""" 191 self.eclipse.generate_ide_project_files([self.proj_info]) 192 self.assertTrue(mock_gen_project.called) 193 self.assertTrue(mock_gen_classpath.called) 194 195 196if __name__ == '__main__': 197 unittest.main() 198