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