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 = set([ 81 'module/path/src', 82 'module/path/test', 83 'out/src', 84 ]) 85 expected_result = [ 86 ' <classpathentry kind="src" path="dependencies/out/src"/>\n', 87 ' <classpathentry kind="src" path="src"/>\n', 88 ' <classpathentry kind="src" path="test"/>\n', 89 ] 90 mock_exist_android_bp.return_value = False 91 generated_result = sorted(self.eclipse._gen_src_path_entries()) 92 self.assertEqual(generated_result, expected_result) 93 94 mock_exist_android_bp.return_value = True 95 expected_result = [ 96 ' <classpathentry excluding="Android.bp" kind="src" ' 97 'path="dependencies/out/src"/>\n', 98 ' <classpathentry excluding="Android.bp" kind="src" ' 99 'path="src"/>\n', 100 ' <classpathentry excluding="Android.bp" kind="src" ' 101 'path="test"/>\n', 102 ] 103 generated_result = sorted(self.eclipse._gen_src_path_entries()) 104 self.assertEqual(generated_result, expected_result) 105 106 107 @mock.patch.object(common_util, 'get_android_root_dir') 108 def test_gen_jar_path_entries(self, mock_get_root): 109 """Test generate jar files' class path entries.""" 110 mock_get_root.return_value = self._ROOT_PATH 111 self.eclipse.jar_module_paths = { 112 '/abspath/to/the/file.jar': 'relpath/to/the/module', 113 } 114 expected_result = [ 115 (' <classpathentry exported="true" kind="lib" ' 116 'path="/abspath/to/the/file.jar" ' 117 'sourcepath="dependencies/relpath/to/the/module"/>\n') 118 ] 119 self.assertEqual(self.eclipse._gen_jar_path_entries(), expected_result) 120 121 def test_get_other_src_folders(self): 122 """Test _get_other_src_folders.""" 123 self.eclipse.src_paths = set([ 124 'module/path/src', 125 'module/path/test', 126 'out/module/path/src', 127 ]) 128 expected_result = ['out/module/path/src'] 129 self.assertEqual(self.eclipse._get_other_src_folders(), expected_result) 130 131 @mock.patch('os.makedirs') 132 @mock.patch('os.path.exists') 133 def test_gen_bin_link(self, mock_exists, mock_mkdir): 134 """Test _gen_bin_link.""" 135 mock_exists.return_value = True 136 self.eclipse._gen_bin_link() 137 self.assertFalse(mock_mkdir.called) 138 mock_exists.return_value = False 139 self.eclipse._gen_bin_link() 140 self.assertTrue(mock_mkdir.called) 141 142 def test_gen_r_path_entries(self): 143 """Test _gen_r_path_entries.""" 144 self.eclipse.r_java_paths = ['a/b', 'c/d'] 145 expected_result = [ 146 ' <classpathentry kind="src" path="dependencies/a/b"/>\n', 147 ' <classpathentry kind="src" path="dependencies/c/d"/>\n', 148 ] 149 self.assertEqual(self.eclipse._gen_r_path_entries(), expected_result) 150 151 def test_gen_bin_dir_entry(self): 152 """Test _gen_bin_dir_entry.""" 153 expected_result = [' <classpathentry kind="src" path="bin"/>\n'] 154 self.assertEqual(self.eclipse._gen_bin_dir_entry(), expected_result) 155 156 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 157 '_gen_jar_path_entries') 158 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 159 '_gen_bin_dir_entry') 160 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 161 '_gen_r_path_entries') 162 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 163 '_gen_src_path_entries') 164 def test_create_classpath_content(self, mock_gen_src, mock_gen_r, 165 mock_gen_bin, mock_gen_jar): 166 """Test _create_classpath_content.""" 167 self.eclipse._create_classpath_content() 168 self.assertTrue(mock_gen_src.called) 169 self.assertTrue(mock_gen_r.called) 170 self.assertTrue(mock_gen_bin.called) 171 self.assertTrue(mock_gen_jar.called) 172 173 @mock.patch.object(common_util, 'file_generate') 174 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 175 '_create_project_content') 176 def test_generate_project_file(self, mock_gen_content, mock_gen_file): 177 """Test generate_project_file.""" 178 self.eclipse.generate_project_file() 179 self.assertTrue(mock_gen_content.called) 180 self.assertTrue(mock_gen_file.called) 181 182 @mock.patch.object(common_util, 'file_generate') 183 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 184 '_create_classpath_content') 185 def test_generate_classpath_file(self, mock_gen_content, mock_gen_file): 186 """Test generate_classpath_file.""" 187 self.eclipse.generate_classpath_file() 188 self.assertTrue(mock_gen_content.called) 189 self.assertTrue(mock_gen_file.called) 190 191 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 192 'generate_classpath_file') 193 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 194 'generate_project_file') 195 def test_generate_ide_project_files(self, mock_gen_project, 196 mock_gen_classpath): 197 """Test generate_ide_project_files.""" 198 self.eclipse.generate_ide_project_files([self.proj_info]) 199 self.assertTrue(mock_gen_project.called) 200 self.assertTrue(mock_gen_classpath.called) 201 202 203if __name__ == '__main__': 204 unittest.main() 205