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 | |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame^] | 17 | import mock |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 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 |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame^] | 21 | from acts.test_utils.instrumentation.device.command.adb_command_types import \ |
| 22 | GenericCommand |
Xianyuan Jia | 31b53e4 | 2019-07-26 12:26:03 -0700 | [diff] [blame] | 23 | from acts.test_utils.instrumentation.instrumentation_base_test import \ |
| 24 | InstrumentationBaseTest |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 25 | |
Xianyuan Jia | 997ca68 | 2019-08-20 17:58:06 -0700 | [diff] [blame] | 26 | MOCK_INSTRUMENTATION_CONFIG = { |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 27 | 'MockController': { |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 28 | 'param1': 1, |
| 29 | 'param2': 4 |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 30 | }, |
| 31 | 'MockInstrumentationBaseTest': { |
| 32 | 'MockController': { |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 33 | 'param2': 2, |
| 34 | 'param3': 5 |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 35 | }, |
| 36 | 'test_case': { |
| 37 | 'MockController': { |
| 38 | 'param3': 3 |
| 39 | } |
| 40 | } |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 44 | |
| 45 | class MockInstrumentationBaseTest(InstrumentationBaseTest): |
| 46 | """Mock test class to initialize required attributes.""" |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame^] | 47 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 48 | def __init__(self): |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 49 | self.current_test_name = None |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame^] | 50 | self.ad_dut = mock.Mock() |
| 51 | self.log = mock.Mock() |
Xianyuan Jia | 997ca68 | 2019-08-20 17:58:06 -0700 | [diff] [blame] | 52 | self._instrumentation_config = ConfigWrapper( |
| 53 | MOCK_INSTRUMENTATION_CONFIG) |
Xianyuan Jia | e327f5b | 2019-09-19 11:15:04 -0700 | [diff] [blame] | 54 | self._class_config = self._instrumentation_config.get_config( |
| 55 | self.__class__.__name__) |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 56 | |
| 57 | |
| 58 | class InstrumentationBaseTestTest(unittest.TestCase): |
| 59 | def setUp(self): |
| 60 | self.instrumentation_test = MockInstrumentationBaseTest() |
| 61 | |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 62 | def test_get_controller_config_for_test_case(self): |
| 63 | """Test that _get_controller_config returns the corresponding |
| 64 | controller config for the current test case. |
| 65 | """ |
| 66 | self.instrumentation_test.current_test_name = 'test_case' |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 67 | config = self.instrumentation_test._get_merged_config( |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 68 | 'MockController') |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 69 | self.assertEqual(config.get('param1'), 1) |
| 70 | self.assertEqual(config.get('param2'), 2) |
| 71 | self.assertEqual(config.get('param3'), 3) |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 72 | |
| 73 | def test_get_controller_config_for_test_class(self): |
| 74 | """Test that _get_controller_config returns the controller config for |
| 75 | the current test class (while no test case is running). |
| 76 | """ |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 77 | config = self.instrumentation_test._get_merged_config( |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 78 | 'MockController') |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 79 | self.assertEqual(config.get('param1'), 1) |
| 80 | self.assertEqual(config.get('param2'), 2) |
| 81 | self.assertEqual(config.get('param3'), 5) |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 82 | |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame^] | 83 | def test_adb_run_literal_commands(self): |
| 84 | result = self.instrumentation_test.adb_run('ls /something') |
| 85 | self.assertIn('ls /something', result.keys()) |
| 86 | |
| 87 | result = self.instrumentation_test.adb_run( |
| 88 | ['ls /something', 'ls /other']) |
| 89 | self.assertIn('ls /something', result.keys()) |
| 90 | self.assertIn('ls /other', result.keys()) |
| 91 | |
| 92 | def test_adb_run_generic_commands(self): |
| 93 | result = self.instrumentation_test.adb_run( |
| 94 | GenericCommand('ls /something')) |
| 95 | self.assertIn('ls /something', result.keys()) |
| 96 | |
| 97 | result = self.instrumentation_test.adb_run( |
| 98 | [GenericCommand('ls /something'), |
| 99 | GenericCommand('ls /other')]) |
| 100 | self.assertIn('ls /something', result.keys()) |
| 101 | self.assertIn('ls /other', result.keys()) |
| 102 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 103 | |
| 104 | if __name__ == '__main__': |
| 105 | unittest.main() |