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 vscode_native_project_file_gen."""
18
19import os
20import unittest
21from unittest import mock
22
23from aidegen.vscode import vscode_native_project_file_gen
24
25_NATIVE_INCLUDES1 = [
26    'out/soong/.intermediates/android.frameworks.bufhub@1.0_genc++_headers/gen'
27]
28_NATIVE_INCLUDES2 = [
29    'frameworks/native/include',
30    'frameworks/native/libs/ui',
31]
32
33
34# pylint: disable=protected-access
35class VSCodeNativeProjectFileGenUnittests(unittest.TestCase):
36    """Unit tests for vscode_native_project_file_gen.py"""
37
38    @mock.patch.object(os, 'mkdir')
39    @mock.patch.object(os.path, 'isdir')
40    def test_init(self, mock_isdir, mock_mkdir):
41        """Test initializing VSCodeNativeProjectFileGenerator."""
42        mod_dir = 'a/b/packages/apps/Settings'
43        mock_isdir.return_value = True
44        vscode_native_project_file_gen.VSCodeNativeProjectFileGenerator(mod_dir)
45        self.assertFalse(mock_mkdir.called)
46        mock_mkdir.mock_reset()
47        mock_isdir.return_value = False
48        vscode_native_project_file_gen.VSCodeNativeProjectFileGenerator(mod_dir)
49        self.assertTrue(mock_mkdir.called)
50
51    @mock.patch.object(os.path, 'isdir')
52    @mock.patch.object(os.path, 'isfile')
53    def test_create_c_cpp_properties_dict(self, mock_isfile, mock_isdir):
54        """Test _create_c_cpp_properties_dict with conditions."""
55        mock_isfile.return_value = True
56        mock_isdir.return_value = True
57        includes = set(_NATIVE_INCLUDES1)
58        includes.update(set(_NATIVE_INCLUDES2))
59        mod_dir = 'a/b/shared/path/to/be/used2/multiarch'
60        ccgen = vscode_native_project_file_gen.VSCodeNativeProjectFileGenerator(
61            mod_dir)
62        cc_mod_info = mock.Mock()
63        cc_mod_info.get_module_includes.return_value = includes
64        data = ccgen._create_c_cpp_properties_dict(cc_mod_info, ['multiarch'])
65        config = vscode_native_project_file_gen._CONFIG
66        include = vscode_native_project_file_gen._INC_PATH
67        compp = vscode_native_project_file_gen._COMPILE_PATH
68        compiler = vscode_native_project_file_gen._COMPILER_PATH
69        res = vscode_native_project_file_gen._make_header_file_paths(includes)
70        self.assertEqual(set(res), set(data[config][0][include]))
71        self.assertEqual(compiler, data[config][0][compp])
72
73        mock_isfile.return_value = False
74        data = ccgen._create_c_cpp_properties_dict(cc_mod_info, ['multiarch'])
75        self.assertEqual(set(res), set(data[config][0][include]))
76        self.assertEqual('', data[config][0][compp])
77
78
79if __name__ == '__main__':
80    unittest.main()
81