1#  Copyright (C) 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
15from unittest.mock import MagicMock, patch
16from absl.testing import parameterized
17from mobly import asserts
18from mobly import base_test
19from mobly import config_parser
20from net_tests_utils.host.python import adb_utils
21from net_tests_utils.host.python.assert_utils import UnexpectedBehaviorError
22
23
24class TestAdbUtils(base_test.BaseTestClass, parameterized.TestCase):
25
26  def __init__(self, configs: config_parser.TestRunConfig):
27    super().__init__(configs)
28
29  def setup_test(self):
30    self.mock_ad = MagicMock()  # Mock Android device object
31    self.mock_ad.log = MagicMock()
32    self.mock_ad.adb.shell.return_value = b""  # Default empty return for shell
33
34  @patch(
35      "net_tests_utils.host.python.adb_utils.expect_dumpsys_state_with_retry"
36  )
37  @patch("net_tests_utils.host.python.adb_utils._set_screen_state")
38  def test_set_doze_mode_enable(
39      self, mock_set_screen_state, mock_expect_dumpsys_state
40  ):
41    adb_utils.set_doze_mode(self.mock_ad, True)
42    mock_set_screen_state.assert_called_once_with(self.mock_ad, False)
43
44  @patch(
45      "net_tests_utils.host.python.adb_utils.expect_dumpsys_state_with_retry"
46  )
47  def test_set_doze_mode_disable(self, mock_expect_dumpsys_state):
48    adb_utils.set_doze_mode(self.mock_ad, False)
49
50  @patch("net_tests_utils.host.python.adb_utils._get_screen_state")
51  def test_set_screen_state_success(self, mock_get_screen_state):
52    mock_get_screen_state.side_effect = [False, True]  # Simulate toggle
53    adb_utils._set_screen_state(self.mock_ad, True)
54
55  @patch("net_tests_utils.host.python.adb_utils._get_screen_state")
56  def test_set_screen_state_failure(self, mock_get_screen_state):
57    mock_get_screen_state.return_value = False  # State doesn't change
58    with asserts.assert_raises(UnexpectedBehaviorError):
59      adb_utils._set_screen_state(self.mock_ad, True)
60
61  @parameterized.parameters(
62      ("Awake", True),
63      ("Asleep", False),
64      ("Dozing", False),
65      ("SomeOtherState", False),
66  )  # Declare inputs for state_str and expected_result.
67  @patch("net_tests_utils.host.python.adb_utils.get_value_of_key_from_dumpsys")
68  def test_get_screen_state(self, state_str, expected_result, mock_get_value):
69    mock_get_value.return_value = state_str
70    asserts.assert_equal(
71        adb_utils._get_screen_state(self.mock_ad), expected_result
72    )
73
74  def test_get_value_of_key_from_dumpsys(self):
75    self.mock_ad.adb.shell.return_value = (
76        b"mWakefulness=Awake\nmOtherKey=SomeValue"
77    )
78    result = adb_utils.get_value_of_key_from_dumpsys(
79        self.mock_ad, "power", "mWakefulness"
80    )
81    asserts.assert_equal(result, "Awake")
82
83  @parameterized.parameters(
84      (True, ["true"]),
85      (False, ["false"]),
86      (
87          True,
88          ["false", "true"],
89      ),  # Expect True, get False which is unexpected, then get True
90      (
91          False,
92          ["true", "false"],
93      ),  # Expect False, get True which is unexpected, then get False
94  )  # Declare inputs for expected_state and returned_value
95  @patch("net_tests_utils.host.python.adb_utils.get_value_of_key_from_dumpsys")
96  def test_expect_dumpsys_state_with_retry_success(
97      self, expected_state, returned_value, mock_get_value
98  ):
99    mock_get_value.side_effect = returned_value
100    # Verify the method returns and does not throw.
101    adb_utils.expect_dumpsys_state_with_retry(
102        self.mock_ad, "service", "key", expected_state, 0
103    )
104
105  @patch("net_tests_utils.host.python.adb_utils.get_value_of_key_from_dumpsys")
106  def test_expect_dumpsys_state_with_retry_failure(self, mock_get_value):
107    mock_get_value.return_value = "false"
108    with asserts.assert_raises(UnexpectedBehaviorError):
109      adb_utils.expect_dumpsys_state_with_retry(
110          self.mock_ad, "service", "key", True, 0
111      )
112
113  @patch("net_tests_utils.host.python.adb_utils.get_value_of_key_from_dumpsys")
114  def test_expect_dumpsys_state_with_retry_not_found(self, mock_get_value):
115    # Simulate the get_value_of_key_from_dumpsys cannot find the give key.
116    mock_get_value.return_value = None
117
118    # Expect the function to raise UnexpectedBehaviorError due to the exception
119    with asserts.assert_raises(UnexpectedBehaviorError):
120      adb_utils.expect_dumpsys_state_with_retry(
121          self.mock_ad, "service", "key", True
122      )
123