1# Copyright 2024 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Tests for ui_interaction_utils.""" 15 16import unittest 17import unittest.mock 18 19from snippet_uiautomator import uidevice 20from snippet_uiautomator import uiobject2 21 22import its_device_utils 23import ui_interaction_utils 24 25_LOG_PATH = '/foo/bar/baz' 26_SCREENSHOT_PREFIX_KEYWORD_ARGUMENT = 'prefix' 27 28 29def _get_mock_ui_object(visibility): 30 """Returns a mock snippet-uiautomator UI object with specified visibility.""" 31 mock_ui_object = unittest.mock.create_autospec( 32 uiobject2.UiObject2, instance=True) 33 mock_ui_object.wait.exists.return_value = visibility 34 return mock_ui_object 35 36 37def _get_mock_dut(visibility): 38 """Returns a mock Android device object with specified UI visibility.""" 39 # Mock used because accessing the `ui` attribute is unwieldy with autospec. 40 mock_dut = unittest.mock.Mock() 41 mock_ui = unittest.mock.create_autospec(uidevice.UiDevice, instance=True) 42 mock_ui.wait.exists.return_value = visibility 43 mock_dut.ui.return_value = mock_ui 44 return mock_dut 45 46 47class UiInteractionUtilsTest(unittest.TestCase): 48 """Unit tests for this module.""" 49 50 def setUp(self): 51 super().setUp() 52 self.mock_visible_ui_object = _get_mock_ui_object(True) 53 self.mock_not_visible_ui_object = _get_mock_ui_object(False) 54 self.mock_call_on_fail = unittest.mock.Mock() 55 self.mock_visible_dut = _get_mock_dut(True) 56 self.mock_not_visible_dut = _get_mock_dut(False) 57 self.addCleanup(unittest.mock.patch.stopall) 58 unittest.mock.patch.object(its_device_utils, 'run', autospec=True).start() 59 60 def test_verify_ui_object_visible_with_visible_object_and_call(self): 61 ui_interaction_utils.verify_ui_object_visible( 62 self.mock_visible_ui_object, call_on_fail=self.mock_call_on_fail) 63 self.mock_call_on_fail.assert_not_called() 64 65 def test_verify_ui_object_visible_with_not_visible_object_no_call(self): 66 with self.assertRaises(AssertionError): 67 ui_interaction_utils.verify_ui_object_visible( 68 self.mock_not_visible_ui_object) 69 70 def test_verify_ui_object_visible_with_not_visible_object_and_call(self): 71 with self.assertRaises(AssertionError): 72 ui_interaction_utils.verify_ui_object_visible( 73 self.mock_not_visible_ui_object, call_on_fail=self.mock_call_on_fail) 74 self.mock_call_on_fail.assert_called_once() 75 76 def test_open_jca_viewfinder_success(self): 77 ui_interaction_utils.open_jca_viewfinder(self.mock_visible_dut, _LOG_PATH) 78 self.mock_visible_dut.take_screenshot.assert_called_once() 79 mock_args, mock_kwargs = self.mock_visible_dut.take_screenshot.call_args 80 self.assertEqual(mock_args, (_LOG_PATH,)) 81 self.assertEqual(mock_kwargs[_SCREENSHOT_PREFIX_KEYWORD_ARGUMENT], 82 ui_interaction_utils.VIEWFINDER_VISIBLE_PREFIX) 83 84 @unittest.mock.patch.object(ui_interaction_utils, 85 'verify_ui_object_visible', 86 autospec=True) 87 def test_open_jca_viewfinder_fail(self, _): 88 with self.assertRaises(AssertionError): 89 ui_interaction_utils.open_jca_viewfinder( 90 self.mock_not_visible_dut, _LOG_PATH) 91 self.mock_not_visible_dut.take_screenshot.assert_called_once() 92 mock_args, mock_kwargs = ( 93 self.mock_not_visible_dut.take_screenshot.call_args 94 ) 95 self.assertEqual(mock_args, (_LOG_PATH,)) 96 self.assertEqual(mock_kwargs[_SCREENSHOT_PREFIX_KEYWORD_ARGUMENT], 97 ui_interaction_utils.VIEWFINDER_NOT_VISIBLE_PREFIX) 98 self.mock_not_visible_dut.ui.dump.assert_called_once() 99 100 101if __name__ == '__main__': 102 unittest.main() 103