blob: 6b4ab1dc251ec1b3662bedadf938e08213da3022 [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 -070018
Hectorcebdb012020-10-02 03:49:57 -070019import unittest
Hector62faf4d2020-04-30 00:22:48 -070020from unittest.mock import MagicMock
21
Xianyuan Jia63751fb2020-11-17 00:07:40 +000022from acts_contrib.test_utils.instrumentation.config_wrapper import ConfigWrapper
Hectorcebdb012020-10-02 03:49:57 -070023
24
Xianyuan Jia63751fb2020-11-17 00:07:40 +000025from acts_contrib.test_utils.instrumentation.device.command.adb_command_types import \
Hector6048d232020-03-05 18:54:19 -080026 GenericCommand
Xianyuan Jia63751fb2020-11-17 00:07:40 +000027from acts_contrib.test_utils.instrumentation.instrumentation_base_test import \
Xianyuan Jia31b53e42019-07-26 12:26:03 -070028 InstrumentationBaseTest
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070029
Xianyuan Jiabe91d242020-10-08 12:01:26 -070030MOCK_TEST_OPTIONS = {
Xianyuan Jia239dbea2019-08-19 16:21:49 -070031 'MockController': {
Xianyuan Jiaa9855092019-10-01 16:20:21 -070032 'param1': 1,
33 'param2': 4
Xianyuan Jia239dbea2019-08-19 16:21:49 -070034 },
35 'MockInstrumentationBaseTest': {
36 'MockController': {
Xianyuan Jiaa9855092019-10-01 16:20:21 -070037 'param2': 2,
38 'param3': 5
Xianyuan Jia239dbea2019-08-19 16:21:49 -070039 },
40 'test_case': {
41 'MockController': {
42 'param3': 3
43 }
44 }
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070045 }
46}
47
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070048
49class MockInstrumentationBaseTest(InstrumentationBaseTest):
50 """Mock test class to initialize required attributes."""
Hector6048d232020-03-05 18:54:19 -080051
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070052 def __init__(self):
Xianyuan Jia239dbea2019-08-19 16:21:49 -070053 self.current_test_name = None
Hector6048d232020-03-05 18:54:19 -080054 self.ad_dut = mock.Mock()
55 self.log = mock.Mock()
Xianyuan Jiabe91d242020-10-08 12:01:26 -070056 self._test_options = ConfigWrapper(MOCK_TEST_OPTIONS)
57 self._class_config = self._test_options.get_config(
Xianyuan Jiae327f5b2019-09-19 11:15:04 -070058 self.__class__.__name__)
Xianyuan Jiaae0d41a2019-04-23 15:09:05 -070059
60
61class InstrumentationBaseTestTest(unittest.TestCase):
62 def setUp(self):
63 self.instrumentation_test = MockInstrumentationBaseTest()
64
Hector6048d232020-03-05 18:54:19 -080065 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
Hector62faf4d2020-04-30 00:22:48 -070085 def test_bugreport_on_fail_by_default(self):
Xianyuan Jiabe91d242020-10-08 12:01:26 -070086 self.instrumentation_test._test_options = ConfigWrapper({})
Hector62faf4d2020-04-30 00:22:48 -070087 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 Jiabe91d242020-10-08 12:01:26 -0700100 self.instrumentation_test._test_options = ConfigWrapper({
Hectorcebdb012020-10-02 03:49:57 -0700101 'bugreport_on_pass': False,
102 'bugreport_on_exception': False,
103 'bugreport_on_fail': False
104 })
Hector62faf4d2020-04-30 00:22:48 -0700105 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 Jiaae0d41a2019-04-23 15:09:05 -0700112
113if __name__ == '__main__':
114 unittest.main()