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 AndroidSDK class."""
18
19import unittest
20from unittest import mock
21
22from aidegen.lib import common_util
23from aidegen.sdk import android_sdk
24
25
26# pylint: disable=protected-access
27class AndroidSDKUnittests(unittest.TestCase):
28    """Unit tests for AndroidSDK class."""
29
30    def setUp(self):
31        """Prepare the testdata related path."""
32        self.sdk = android_sdk.AndroidSDK()
33
34    def tearDown(self):
35        """Clear the testdata related path."""
36        self.sdk = None
37
38    def test_init(self):
39        """Test initialize the attributes."""
40        self.assertEqual(self.sdk.max_api_level, 0)
41        self.assertEqual(self.sdk.max_code_name, None)
42        self.assertEqual(self.sdk.platform_mapping, {})
43        self.assertEqual(self.sdk.android_sdk_path, None)
44
45    def test_parse_max_api_level(self):
46        """Test _parse_max_api_level."""
47        self.sdk._platform_mapping = {
48            'android-29': {
49                'api_level': 29,
50                'code_name': '29',
51            },
52            'android-28': {
53                'api_level': 28,
54                'code_name': '28',
55            },
56        }
57        api_level = self.sdk._parse_max_api_level()
58        self.assertEqual(api_level, 29)
59
60        self.sdk._platform_mapping = {
61            'android-28': {
62                'api_level': 28,
63                'code_name': '28',
64            },
65            'android-Q': {
66                'api_level': 29,
67                'code_name': 'Q',
68            },
69        }
70        api_level = self.sdk._parse_max_api_level()
71        self.assertEqual(api_level, 29)
72
73    def test_parse_max_code_name(self):
74        """Test _parse_max_code_name."""
75        self.sdk._max_api_level = 29
76        self.sdk._platform_mapping = {
77            'android-29': {
78                'api_level': 29,
79                'code_name': '29',
80            },
81            'android-28': {
82                'api_level': 28,
83                'code_name': '28',
84            },
85        }
86        code_name = self.sdk._parse_max_code_name()
87        self.assertEqual(code_name, '29')
88
89        self.sdk._platform_mapping = {
90            'android-29': {
91                'api_level': 29,
92                'code_name': '29',
93            },
94            'android-Q': {
95                'api_level': 29,
96                'code_name': 'Q',
97            },
98        }
99        code_name = self.sdk._parse_max_code_name()
100        self.assertEqual(code_name, 'Q')
101
102    @mock.patch.object(common_util, 'read_file_content')
103    def test_parse_api_info(self, mock_read_file):
104        """Test _parse_api_info."""
105        mock_read_file.return_value = '\nAndroidVersion.ApiLevel=29\n'
106        expected_result = '29', '29'
107        self.assertEqual(self.sdk._parse_api_info(''), expected_result)
108
109        mock_read_file.return_value = ('\nAndroidVersion.ApiLevel=29\n'
110                                       'AndroidVersion.CodeName=Q\n')
111        expected_result = '29', 'Q'
112        self.assertEqual(self.sdk._parse_api_info(''), expected_result)
113
114        mock_read_file.return_value = ''
115        expected_result = 0, 0
116        self.assertEqual(self.sdk._parse_api_info(''), expected_result)
117
118    @mock.patch.object(android_sdk.AndroidSDK, '_parse_api_info')
119    @mock.patch('glob.glob')
120    def test_gen_platform_mapping(self, mock_glob, mock_parse_api_info):
121        """Test _gen_platform_mapping."""
122        mock_glob.return_value = ['/sdk/platforms/android-29/source.properties']
123        mock_parse_api_info.return_value = 0, 0
124        test_result = self.sdk._gen_platform_mapping('')
125        expected_result = {}
126        self.assertEqual(test_result, False)
127        self.assertEqual(self.sdk._platform_mapping, expected_result)
128
129        mock_parse_api_info.return_value = '29', '29'
130        test_result = self.sdk._gen_platform_mapping('')
131        expected_result = {
132            'android-29': {
133                'api_level': 29,
134                'code_name': '29',
135            }
136        }
137        self.assertEqual(test_result, True)
138        self.assertEqual(self.sdk._platform_mapping, expected_result)
139
140    @mock.patch.object(android_sdk.AndroidSDK, '_gen_platform_mapping')
141    def test_is_android_sdk_path(self, mock_gen_platform_mapping):
142        """Test is_android_sdk_path."""
143        self.sdk._platform_mapping = {
144            'android-29': {
145                'api_level': 29,
146                'code_name': '29',
147            },
148        }
149        mock_gen_platform_mapping.return_value = True
150        self.assertEqual(self.sdk.is_android_sdk_path('a/b'), True)
151        self.assertEqual(self.sdk.android_sdk_path, 'a/b')
152        self.assertEqual(self.sdk.max_api_level, 29)
153
154        mock_gen_platform_mapping.return_value = False
155        self.assertEqual(self.sdk.is_android_sdk_path('a/b'), False)
156
157    @mock.patch('builtins.input')
158    @mock.patch.object(android_sdk.AndroidSDK, 'is_android_sdk_path')
159    def test_path_analysis(self, mock_is_sdk_path, mock_input):
160        """Test path_analysis."""
161        mock_is_sdk_path.return_value = True
162        self.assertEqual(self.sdk.path_analysis('a/b'), True)
163
164        mock_is_sdk_path.return_value = False
165        mock_input.return_value = ''
166        self.assertEqual(self.sdk.path_analysis('a/b'), False)
167
168        mock_is_sdk_path.return_value = False
169        mock_input.return_value = 'a/b'
170        self.assertEqual(self.sdk.path_analysis('a/b'), False)
171
172        self.sdk._INPUT_QUERY_TIMES = 0
173        self.assertEqual(self.sdk.path_analysis('a/b'), False)
174
175
176if __name__ == '__main__':
177    unittest.main()
178