1#!/usr/bin/env python3
2#
3#   Copyright 2022 - 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
17from blueberry.tests.gd.cert.truth import assertThat
18from blueberry.tests.topshim.lib.topshim_base_test import TopshimBaseTest
19from blueberry.tests.topshim.lib.topshim_device import TRANSPORT_CLASSIC
20
21from mobly import test_runner
22
23
24class HfpTest(TopshimBaseTest):
25
26    def setup_test(self):
27        super().setup_test()
28        # Pair dut and cert device.
29        self.dut().enable_inquiry_scan()
30        self.cert().enable_inquiry_scan()
31        self.dut().toggle_discovery(True)
32        self.__paired_device = self.dut().find_device()
33        self.dut().create_bond(address=self.__paired_device, transport=TRANSPORT_CLASSIC)
34
35    def teardown_test(self):
36        super().teardown_test()
37        # Test teardown for dut and cert reset.
38        self.dut().toggle_discovery(False)
39        self.dut().disable_page_scan()
40        self.cert().disable_page_scan()
41
42    def test_hfp_connect_with_bond(self):
43        state, _ = self.dut().start_slc(address=self.__paired_device)
44        assertThat(state).isEqualTo("Connecting")
45        state, _ = self.dut().wait_for_hfp_connection_state_change()
46        assertThat(state).isEqualTo("Connected")
47        state, conn_addr = self.dut().wait_for_hfp_connection_state_change()
48        assertThat(state).isEqualTo("SlcConnected")
49        assertThat(conn_addr).isEqualTo(self.__paired_device)
50
51        #Extra steps to remove bonding to complete teardown.
52        self.dut().remove_bonded_device(self.__paired_device)
53        # This is required currently so that the HFP connection state change
54        # callback doesn't affect other tests.
55        self.dut().wait_for_hfp_connection_state_change()
56
57    def test_hfp_disconnect_with_bond(self):
58        state, _ = self.dut().start_slc(address=self.__paired_device)
59        self.dut().wait_for_hfp_connection_state_change()  # To connected
60        self.dut().wait_for_hfp_connection_state_change()  # To SLC connected
61
62        # Actual test for stopping SLC connection.
63        state, _ = self.dut().stop_slc(address=self.__paired_device)
64        assertThat(state).isEqualTo("Disconnecting")
65        state, _ = self.dut().wait_for_hfp_connection_state_change()
66        assertThat(state).isEqualTo("Disconnected")
67        #Extra steps to remove bonding to complete teardown.
68        self.dut().remove_bonded_device(self.__paired_device)
69
70
71if __name__ == "__main__":
72    test_runner.main()
73