blob: a3760d0fa481c4ac93f4518a84bebd18ffde8250 [file] [log] [blame]
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -07001#!/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
Hector6048d232020-03-05 18:54:19 -080017import mock
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070018import unittest
19
Hector62faf4d2020-04-30 00:22:48 -070020from unittest.mock import MagicMock
21
Xianyuan Jia239dbea2019-08-19 16:21:49 -070022from acts.test_utils.instrumentation.config_wrapper import ConfigWrapper
Hector6048d232020-03-05 18:54:19 -080023from acts.test_utils.instrumentation.device.command.adb_command_types import \
24 GenericCommand
Xianyuan Jia31b53e42019-07-26 12:26:03 -070025from acts.test_utils.instrumentation.instrumentation_base_test import \
26 InstrumentationBaseTest
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070027
Xianyuan Jia997ca682019-08-20 17:58:06 -070028MOCK_INSTRUMENTATION_CONFIG = {
Xianyuan Jia239dbea2019-08-19 16:21:49 -070029 'MockController': {
Xianyuan Jiaa9855092019-10-01 16:20:21 -070030 'param1': 1,
31 'param2': 4
Xianyuan Jia239dbea2019-08-19 16:21:49 -070032 },
33 'MockInstrumentationBaseTest': {
34 'MockController': {
Xianyuan Jiaa9855092019-10-01 16:20:21 -070035 'param2': 2,
36 'param3': 5
Xianyuan Jia239dbea2019-08-19 16:21:49 -070037 },
38 'test_case': {
39 'MockController': {
40 'param3': 3
41 }
42 }
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070043 }
44}
45
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070046
47class MockInstrumentationBaseTest(InstrumentationBaseTest):
48 """Mock test class to initialize required attributes."""
Hector6048d232020-03-05 18:54:19 -080049
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070050 def __init__(self):
Xianyuan Jia239dbea2019-08-19 16:21:49 -070051 self.current_test_name = None
Hector6048d232020-03-05 18:54:19 -080052 self.ad_dut = mock.Mock()
53 self.log = mock.Mock()
Xianyuan Jia997ca682019-08-20 17:58:06 -070054 self._instrumentation_config = ConfigWrapper(
55 MOCK_INSTRUMENTATION_CONFIG)
Xianyuan Jiae327f5b2019-09-19 11:15:04 -070056 self._class_config = self._instrumentation_config.get_config(
57 self.__class__.__name__)
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070058
59
60class InstrumentationBaseTestTest(unittest.TestCase):
61 def setUp(self):
62 self.instrumentation_test = MockInstrumentationBaseTest()
63
Xianyuan Jia239dbea2019-08-19 16:21:49 -070064 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 Jiaa9855092019-10-01 16:20:21 -070069 config = self.instrumentation_test._get_merged_config(
Xianyuan Jia239dbea2019-08-19 16:21:49 -070070 'MockController')
Xianyuan Jiaa9855092019-10-01 16:20:21 -070071 self.assertEqual(config.get('param1'), 1)
72 self.assertEqual(config.get('param2'), 2)
73 self.assertEqual(config.get('param3'), 3)
Xianyuan Jia239dbea2019-08-19 16:21:49 -070074
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 Jiaa9855092019-10-01 16:20:21 -070079 config = self.instrumentation_test._get_merged_config(
Xianyuan Jia239dbea2019-08-19 16:21:49 -070080 'MockController')
Xianyuan Jiaa9855092019-10-01 16:20:21 -070081 self.assertEqual(config.get('param1'), 1)
82 self.assertEqual(config.get('param2'), 2)
83 self.assertEqual(config.get('param3'), 5)
Xianyuan Jia239dbea2019-08-19 16:21:49 -070084
Hector6048d232020-03-05 18:54:19 -080085 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
Hector62faf4d2020-04-30 00:22:48 -0700105 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 Jiaae0d41a2019-04-23 15:09:05 -0700132
133if __name__ == '__main__':
134 unittest.main()