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 | |
Hector | 62faf4d | 2020-04-30 00:22:48 -0700 | [diff] [blame^] | 20 | from unittest.mock import MagicMock |
| 21 | |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 22 | from acts.test_utils.instrumentation.config_wrapper import ConfigWrapper |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame] | 23 | from acts.test_utils.instrumentation.device.command.adb_command_types import \ |
| 24 | GenericCommand |
Xianyuan Jia | 31b53e4 | 2019-07-26 12:26:03 -0700 | [diff] [blame] | 25 | from acts.test_utils.instrumentation.instrumentation_base_test import \ |
| 26 | InstrumentationBaseTest |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 27 | |
Xianyuan Jia | 997ca68 | 2019-08-20 17:58:06 -0700 | [diff] [blame] | 28 | MOCK_INSTRUMENTATION_CONFIG = { |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 29 | 'MockController': { |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 30 | 'param1': 1, |
| 31 | 'param2': 4 |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 32 | }, |
| 33 | 'MockInstrumentationBaseTest': { |
| 34 | 'MockController': { |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 35 | 'param2': 2, |
| 36 | 'param3': 5 |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 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 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 46 | |
| 47 | class MockInstrumentationBaseTest(InstrumentationBaseTest): |
| 48 | """Mock test class to initialize required attributes.""" |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame] | 49 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 50 | def __init__(self): |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 51 | self.current_test_name = None |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame] | 52 | self.ad_dut = mock.Mock() |
| 53 | self.log = mock.Mock() |
Xianyuan Jia | 997ca68 | 2019-08-20 17:58:06 -0700 | [diff] [blame] | 54 | self._instrumentation_config = ConfigWrapper( |
| 55 | MOCK_INSTRUMENTATION_CONFIG) |
Xianyuan Jia | e327f5b | 2019-09-19 11:15:04 -0700 | [diff] [blame] | 56 | self._class_config = self._instrumentation_config.get_config( |
| 57 | self.__class__.__name__) |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 58 | |
| 59 | |
| 60 | class InstrumentationBaseTestTest(unittest.TestCase): |
| 61 | def setUp(self): |
| 62 | self.instrumentation_test = MockInstrumentationBaseTest() |
| 63 | |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 64 | def test_get_controller_config_for_test_case(self): |
| 65 | """Test that _get_controller_config returns the corresponding |
| 66 | controller config for the current test case. |
| 67 | """ |
| 68 | self.instrumentation_test.current_test_name = 'test_case' |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 69 | config = self.instrumentation_test._get_merged_config( |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 70 | 'MockController') |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 71 | self.assertEqual(config.get('param1'), 1) |
| 72 | self.assertEqual(config.get('param2'), 2) |
| 73 | self.assertEqual(config.get('param3'), 3) |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 74 | |
| 75 | def test_get_controller_config_for_test_class(self): |
| 76 | """Test that _get_controller_config returns the controller config for |
| 77 | the current test class (while no test case is running). |
| 78 | """ |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 79 | config = self.instrumentation_test._get_merged_config( |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 80 | 'MockController') |
Xianyuan Jia | a985509 | 2019-10-01 16:20:21 -0700 | [diff] [blame] | 81 | self.assertEqual(config.get('param1'), 1) |
| 82 | self.assertEqual(config.get('param2'), 2) |
| 83 | self.assertEqual(config.get('param3'), 5) |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 84 | |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame] | 85 | def test_adb_run_literal_commands(self): |
| 86 | result = self.instrumentation_test.adb_run('ls /something') |
| 87 | self.assertIn('ls /something', result.keys()) |
| 88 | |
| 89 | result = self.instrumentation_test.adb_run( |
| 90 | ['ls /something', 'ls /other']) |
| 91 | self.assertIn('ls /something', result.keys()) |
| 92 | self.assertIn('ls /other', result.keys()) |
| 93 | |
| 94 | def test_adb_run_generic_commands(self): |
| 95 | result = self.instrumentation_test.adb_run( |
| 96 | GenericCommand('ls /something')) |
| 97 | self.assertIn('ls /something', result.keys()) |
| 98 | |
| 99 | result = self.instrumentation_test.adb_run( |
| 100 | [GenericCommand('ls /something'), |
| 101 | GenericCommand('ls /other')]) |
| 102 | self.assertIn('ls /something', result.keys()) |
| 103 | self.assertIn('ls /other', result.keys()) |
| 104 | |
Hector | 62faf4d | 2020-04-30 00:22:48 -0700 | [diff] [blame^] | 105 | def test_bugreport_on_fail_by_default(self): |
| 106 | self.instrumentation_test._instrumentation_config = ConfigWrapper({}) |
| 107 | self.instrumentation_test._take_bug_report = MagicMock() |
| 108 | |
| 109 | self.instrumentation_test.on_exception('test', 0) |
| 110 | self.assertEqual(1, |
| 111 | self.instrumentation_test._take_bug_report.call_count) |
| 112 | self.instrumentation_test.on_pass('test', 0) |
| 113 | self.assertEqual(2, |
| 114 | self.instrumentation_test._take_bug_report.call_count) |
| 115 | self.instrumentation_test.on_fail('test', 0) |
| 116 | self.assertEqual(3, |
| 117 | self.instrumentation_test._take_bug_report.call_count) |
| 118 | |
| 119 | def test_bugreport_on_end_events_can_be_disabled(self): |
| 120 | self.instrumentation_test._instrumentation_config = ConfigWrapper({ |
| 121 | 'bugreport_on_pass': False, |
| 122 | 'bugreport_on_exception': False, |
| 123 | 'bugreport_on_fail': False |
| 124 | }) |
| 125 | self.instrumentation_test._take_bug_report = MagicMock() |
| 126 | |
| 127 | self.instrumentation_test.on_exception('test', 0) |
| 128 | self.instrumentation_test.on_pass('test', 0) |
| 129 | self.instrumentation_test.on_fail('test', 0) |
| 130 | self.assertFalse(self.instrumentation_test._take_bug_report.called) |
| 131 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 132 | |
| 133 | if __name__ == '__main__': |
| 134 | unittest.main() |