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 | |
Hector | cebdb01 | 2020-10-02 03:49:57 -0700 | [diff] [blame] | 19 | import unittest |
Hector | 62faf4d | 2020-04-30 00:22:48 -0700 | [diff] [blame] | 20 | from unittest.mock import MagicMock |
| 21 | |
Xianyuan Jia | 63751fb | 2020-11-17 00:07:40 +0000 | [diff] [blame^] | 22 | from acts_contrib.test_utils.instrumentation.config_wrapper import ConfigWrapper |
Hector | cebdb01 | 2020-10-02 03:49:57 -0700 | [diff] [blame] | 23 | |
| 24 | |
Xianyuan Jia | 63751fb | 2020-11-17 00:07:40 +0000 | [diff] [blame^] | 25 | from acts_contrib.test_utils.instrumentation.device.command.adb_command_types import \ |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame] | 26 | GenericCommand |
Xianyuan Jia | 63751fb | 2020-11-17 00:07:40 +0000 | [diff] [blame^] | 27 | from acts_contrib.test_utils.instrumentation.instrumentation_base_test import \ |
Xianyuan Jia | 31b53e4 | 2019-07-26 12:26:03 -0700 | [diff] [blame] | 28 | InstrumentationBaseTest |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 29 | |
Xianyuan Jia | be91d24 | 2020-10-08 12:01:26 -0700 | [diff] [blame] | 30 | MOCK_TEST_OPTIONS = { |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 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 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 48 | |
| 49 | class MockInstrumentationBaseTest(InstrumentationBaseTest): |
| 50 | """Mock test class to initialize required attributes.""" |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame] | 51 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 52 | def __init__(self): |
Xianyuan Jia | 239dbea | 2019-08-19 16:21:49 -0700 | [diff] [blame] | 53 | self.current_test_name = None |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame] | 54 | self.ad_dut = mock.Mock() |
| 55 | self.log = mock.Mock() |
Xianyuan Jia | be91d24 | 2020-10-08 12:01:26 -0700 | [diff] [blame] | 56 | self._test_options = ConfigWrapper(MOCK_TEST_OPTIONS) |
| 57 | self._class_config = self._test_options.get_config( |
Xianyuan Jia | e327f5b | 2019-09-19 11:15:04 -0700 | [diff] [blame] | 58 | self.__class__.__name__) |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | class InstrumentationBaseTestTest(unittest.TestCase): |
| 62 | def setUp(self): |
| 63 | self.instrumentation_test = MockInstrumentationBaseTest() |
| 64 | |
Hector | 6048d23 | 2020-03-05 18:54:19 -0800 | [diff] [blame] | 65 | def test_adb_run_literal_commands(self): |
| 66 | result = self.instrumentation_test.adb_run('ls /something') |
| 67 | self.assertIn('ls /something', result.keys()) |
| 68 | |
| 69 | result = self.instrumentation_test.adb_run( |
| 70 | ['ls /something', 'ls /other']) |
| 71 | self.assertIn('ls /something', result.keys()) |
| 72 | self.assertIn('ls /other', result.keys()) |
| 73 | |
| 74 | def test_adb_run_generic_commands(self): |
| 75 | result = self.instrumentation_test.adb_run( |
| 76 | GenericCommand('ls /something')) |
| 77 | self.assertIn('ls /something', result.keys()) |
| 78 | |
| 79 | result = self.instrumentation_test.adb_run( |
| 80 | [GenericCommand('ls /something'), |
| 81 | GenericCommand('ls /other')]) |
| 82 | self.assertIn('ls /something', result.keys()) |
| 83 | self.assertIn('ls /other', result.keys()) |
| 84 | |
Hector | 62faf4d | 2020-04-30 00:22:48 -0700 | [diff] [blame] | 85 | def test_bugreport_on_fail_by_default(self): |
Xianyuan Jia | be91d24 | 2020-10-08 12:01:26 -0700 | [diff] [blame] | 86 | self.instrumentation_test._test_options = ConfigWrapper({}) |
Hector | 62faf4d | 2020-04-30 00:22:48 -0700 | [diff] [blame] | 87 | self.instrumentation_test._take_bug_report = MagicMock() |
| 88 | |
| 89 | self.instrumentation_test.on_exception('test', 0) |
| 90 | self.assertEqual(1, |
| 91 | self.instrumentation_test._take_bug_report.call_count) |
| 92 | self.instrumentation_test.on_pass('test', 0) |
| 93 | self.assertEqual(2, |
| 94 | self.instrumentation_test._take_bug_report.call_count) |
| 95 | self.instrumentation_test.on_fail('test', 0) |
| 96 | self.assertEqual(3, |
| 97 | self.instrumentation_test._take_bug_report.call_count) |
| 98 | |
| 99 | def test_bugreport_on_end_events_can_be_disabled(self): |
Xianyuan Jia | be91d24 | 2020-10-08 12:01:26 -0700 | [diff] [blame] | 100 | self.instrumentation_test._test_options = ConfigWrapper({ |
Hector | cebdb01 | 2020-10-02 03:49:57 -0700 | [diff] [blame] | 101 | 'bugreport_on_pass': False, |
| 102 | 'bugreport_on_exception': False, |
| 103 | 'bugreport_on_fail': False |
| 104 | }) |
Hector | 62faf4d | 2020-04-30 00:22:48 -0700 | [diff] [blame] | 105 | self.instrumentation_test._take_bug_report = MagicMock() |
| 106 | |
| 107 | self.instrumentation_test.on_exception('test', 0) |
| 108 | self.instrumentation_test.on_pass('test', 0) |
| 109 | self.instrumentation_test.on_fail('test', 0) |
| 110 | self.assertFalse(self.instrumentation_test._take_bug_report.called) |
| 111 | |
Xianyuan Jia | ae0d41a | 2019-04-23 15:09:05 -0700 | [diff] [blame] | 112 | |
| 113 | if __name__ == '__main__': |
| 114 | unittest.main() |