Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 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 | import copy |
| 18 | import unittest |
| 19 | |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 20 | from acts.test_utils.instrumentation.config_wrapper import ConfigWrapper |
Xianyuan Jia | 31b53e4 | 2019-07-26 12:26:03 -0700 | [diff] [blame] | 21 | from acts.test_utils.instrumentation.instrumentation_base_test import \ |
| 22 | InstrumentationBaseTest |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 23 | |
Xianyuan Jia | 997ca68 | 2019-08-20 17:58:06 -0700 | [diff] [blame] | 24 | MOCK_INSTRUMENTATION_CONFIG = { |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 25 | 'not_file': 'NOT_FILE', |
| 26 | 'file1': 'FILE', |
| 27 | 'lvl1': { |
| 28 | 'file2': 'FILE', |
| 29 | 'lvl2': {'file1': 'FILE'} |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 30 | }, |
| 31 | 'MockController': { |
| 32 | 'param1': 1 |
| 33 | }, |
| 34 | 'MockInstrumentationBaseTest': { |
| 35 | 'MockController': { |
| 36 | 'param2': 2 |
| 37 | }, |
| 38 | 'test_case': { |
| 39 | 'MockController': { |
| 40 | 'param3': 3 |
| 41 | } |
| 42 | } |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | MOCK_ACTS_USERPARAMS = { |
| 47 | 'file1': '/path/to/file1', |
| 48 | 'file2': '/path/to/file2' |
| 49 | } |
| 50 | |
| 51 | |
| 52 | class MockInstrumentationBaseTest(InstrumentationBaseTest): |
| 53 | """Mock test class to initialize required attributes.""" |
| 54 | def __init__(self): |
| 55 | self.user_params = MOCK_ACTS_USERPARAMS |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 56 | self.current_test_name = None |
Xianyuan Jia | 997ca68 | 2019-08-20 17:58:06 -0700 | [diff] [blame] | 57 | self._instrumentation_config = ConfigWrapper( |
| 58 | MOCK_INSTRUMENTATION_CONFIG) |
Xianyuan Jia | e327f5b | 2019-09-19 11:15:04 -0700 | [diff] [blame^] | 59 | self._class_config = self._instrumentation_config.get_config( |
| 60 | self.__class__.__name__) |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 61 | |
| 62 | |
| 63 | class InstrumentationBaseTestTest(unittest.TestCase): |
| 64 | def setUp(self): |
| 65 | self.instrumentation_test = MockInstrumentationBaseTest() |
| 66 | |
| 67 | def test_resolve_files_from_config(self): |
| 68 | """Test that params with the 'FILE' marker are properly substituted |
| 69 | with the corresponding paths from ACTS user_params. |
| 70 | """ |
Xianyuan Jia | 997ca68 | 2019-08-20 17:58:06 -0700 | [diff] [blame] | 71 | mock_config = copy.deepcopy(MOCK_INSTRUMENTATION_CONFIG) |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 72 | self.instrumentation_test._resolve_file_paths(mock_config) |
Xianyuan Jia | 997ca68 | 2019-08-20 17:58:06 -0700 | [diff] [blame] | 73 | self.assertEqual(mock_config['not_file'], |
| 74 | MOCK_INSTRUMENTATION_CONFIG['not_file']) |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 75 | self.assertEqual(mock_config['file1'], MOCK_ACTS_USERPARAMS['file1']) |
| 76 | self.assertEqual(mock_config['lvl1']['file2'], |
| 77 | MOCK_ACTS_USERPARAMS['file2']) |
| 78 | self.assertEqual(mock_config['lvl1']['lvl2']['file1'], |
| 79 | MOCK_ACTS_USERPARAMS['file1']) |
| 80 | |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 81 | def test_get_controller_config_for_test_case(self): |
| 82 | """Test that _get_controller_config returns the corresponding |
| 83 | controller config for the current test case. |
| 84 | """ |
| 85 | self.instrumentation_test.current_test_name = 'test_case' |
| 86 | config = self.instrumentation_test._get_controller_config( |
| 87 | 'MockController') |
| 88 | self.assertNotIn('param1', config) |
| 89 | self.assertNotIn('param2', config) |
| 90 | self.assertIn('param3', config) |
| 91 | |
| 92 | def test_get_controller_config_for_test_class(self): |
| 93 | """Test that _get_controller_config returns the controller config for |
| 94 | the current test class (while no test case is running). |
| 95 | """ |
| 96 | config = self.instrumentation_test._get_controller_config( |
| 97 | 'MockController') |
| 98 | self.assertIn('param1', config) |
| 99 | self.assertIn('param2', config) |
| 100 | self.assertNotIn('param3', config) |
| 101 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 102 | |
| 103 | if __name__ == '__main__': |
| 104 | unittest.main() |