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.max_folder_name, None) 43 self.assertEqual(self.sdk.platform_mapping, {}) 44 self.assertEqual(self.sdk.android_sdk_path, None) 45 46 def test_parse_max_api_level(self): 47 """Test _parse_max_api_level.""" 48 self.sdk._platform_mapping = { 49 'android-29': { 50 'api_level': 29, 51 'code_name': '29', 52 }, 53 'android-28': { 54 'api_level': 28, 55 'code_name': '28', 56 }, 57 } 58 api_level = self.sdk._parse_max_api_level() 59 self.assertEqual(api_level, 29) 60 61 self.sdk._platform_mapping = { 62 'android-28': { 63 'api_level': 28, 64 'code_name': '28', 65 }, 66 'android-Q': { 67 'api_level': 29, 68 'code_name': 'Q', 69 }, 70 } 71 api_level = self.sdk._parse_max_api_level() 72 self.assertEqual(api_level, 29) 73 74 def test_parse_max_code_name(self): 75 """Test _parse_max_code_name.""" 76 self.sdk._max_api_level = 29 77 self.sdk._platform_mapping = { 78 'android-29': { 79 'api_level': 29, 80 'code_name': '29', 81 }, 82 'android-28': { 83 'api_level': 28, 84 'code_name': '28', 85 }, 86 } 87 code_name = self.sdk._parse_max_code_name() 88 self.assertEqual(code_name, '29') 89 90 self.sdk._platform_mapping = { 91 'android-29': { 92 'api_level': 29, 93 'code_name': '29', 94 }, 95 'android-Q': { 96 'api_level': 29, 97 'code_name': 'Q', 98 }, 99 } 100 code_name = self.sdk._parse_max_code_name() 101 self.assertEqual(code_name, 'Q') 102 103 def test_get_max_folder_name(self): 104 """Test _get_max_folder_name.""" 105 self.sdk._max_api_level = 29 106 self.sdk._max_code_name = '29' 107 self.sdk._platform_mapping = { 108 'android-29': { 109 'api_level': 29, 110 'code_name': '29', 111 }, 112 'android-28': { 113 'api_level': 28, 114 'code_name': '28', 115 }, 116 } 117 max_folder_name = self.sdk._get_max_folder_name() 118 self.assertEqual(max_folder_name, 'android-29') 119 120 self.sdk._max_code_name = 'Q' 121 self.sdk._platform_mapping = { 122 'android-29': { 123 'api_level': 29, 124 'code_name': '29', 125 }, 126 'android-Q': { 127 'api_level': 29, 128 'code_name': 'Q', 129 }, 130 } 131 max_folder_name = self.sdk._get_max_folder_name() 132 self.assertEqual(max_folder_name, 'android-Q') 133 134 @mock.patch.object(common_util, 'read_file_content') 135 def test_parse_api_info(self, mock_read_file): 136 """Test _parse_api_info.""" 137 mock_read_file.return_value = '\nAndroidVersion.ApiLevel=29\n' 138 expected_result = '29', '29' 139 self.assertEqual(self.sdk._parse_api_info(''), expected_result) 140 141 mock_read_file.return_value = ('\nAndroidVersion.ApiLevel=29\n' 142 'AndroidVersion.CodeName=Q\n') 143 expected_result = '29', 'Q' 144 self.assertEqual(self.sdk._parse_api_info(''), expected_result) 145 146 mock_read_file.return_value = '' 147 expected_result = 0, 0 148 self.assertEqual(self.sdk._parse_api_info(''), expected_result) 149 150 @mock.patch.object(android_sdk.AndroidSDK, '_parse_api_info') 151 @mock.patch('glob.glob') 152 def test_gen_platform_mapping(self, mock_glob, mock_parse_api_info): 153 """Test _gen_platform_mapping.""" 154 mock_glob.return_value = ['/sdk/platforms/android-29/source.properties'] 155 mock_parse_api_info.return_value = 0, 0 156 test_result = self.sdk._gen_platform_mapping('') 157 expected_result = {} 158 self.assertEqual(test_result, False) 159 self.assertEqual(self.sdk._platform_mapping, expected_result) 160 161 mock_parse_api_info.return_value = '29', '29' 162 test_result = self.sdk._gen_platform_mapping('') 163 expected_result = { 164 'android-29': { 165 'api_level': 29, 166 'code_name': '29', 167 } 168 } 169 self.assertEqual(test_result, True) 170 self.assertEqual(self.sdk._platform_mapping, expected_result) 171 172 @mock.patch.object(android_sdk.AndroidSDK, '_gen_platform_mapping') 173 def test_is_android_sdk_path(self, mock_gen_platform_mapping): 174 """Test is_android_sdk_path.""" 175 self.sdk._platform_mapping = { 176 'android-29': { 177 'api_level': 29, 178 'code_name': '29', 179 }, 180 } 181 mock_gen_platform_mapping.return_value = True 182 self.assertEqual(self.sdk.is_android_sdk_path('a/b'), True) 183 self.assertEqual(self.sdk.android_sdk_path, 'a/b') 184 self.assertEqual(self.sdk.max_api_level, 29) 185 186 mock_gen_platform_mapping.return_value = False 187 self.assertEqual(self.sdk.is_android_sdk_path('a/b'), False) 188 189 @mock.patch('builtins.input') 190 @mock.patch.object(android_sdk.AndroidSDK, 'is_android_sdk_path') 191 def test_path_analysis(self, mock_is_sdk_path, mock_input): 192 """Test path_analysis.""" 193 mock_is_sdk_path.return_value = True 194 self.assertEqual(self.sdk.path_analysis('a/b'), True) 195 196 mock_is_sdk_path.return_value = False 197 mock_input.return_value = '' 198 self.assertEqual(self.sdk.path_analysis('a/b'), False) 199 200 mock_is_sdk_path.return_value = False 201 mock_input.return_value = 'a/b' 202 self.assertEqual(self.sdk.path_analysis('a/b'), False) 203 204 self.sdk._INPUT_QUERY_TIMES = 0 205 self.assertEqual(self.sdk.path_analysis('a/b'), False) 206 207 208if __name__ == '__main__': 209 unittest.main() 210