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
15"""Bluetooth nearby connection functional test actor."""
16
17from betocq import nc_constants
18from betocq import nearby_connection_wrapper
19from betocq.function_tests import function_test_actor_base
20
21
22class BtBleFunctionTestActor(function_test_actor_base.FunctionTestActorBase):
23  """The actor for running the BT/BlE function test."""
24
25  def test_bt_ble_connection(self):
26    """Test the basic BT and BLE connection.
27
28    Use BLE as discovery/advertising medium
29    Use BT as connection and upgrade medium
30    """
31
32    # 1. set up BT connection
33    advertising_discovery_medium = nc_constants.NearbyMedium.BLE_ONLY
34
35    nearby_snippet = nearby_connection_wrapper.NearbyConnectionWrapper(
36        self.advertiser,
37        self.discoverer,
38        self.advertiser.nearby,
39        self.discoverer.nearby,
40        advertising_discovery_medium=advertising_discovery_medium,
41        connection_medium=nc_constants.NearbyMedium.BT_ONLY,
42        upgrade_medium=nc_constants.NearbyMedium.BT_ONLY,
43    )
44    connection_setup_timeouts = nc_constants.ConnectionSetupTimeouts(
45        nc_constants.FIRST_DISCOVERY_TIMEOUT,
46        nc_constants.FIRST_CONNECTION_INIT_TIMEOUT,
47        nc_constants.FIRST_CONNECTION_RESULT_TIMEOUT)
48    try:
49      nearby_snippet.start_nearby_connection(
50          timeouts=connection_setup_timeouts,
51          medium_upgrade_type=nc_constants.MediumUpgradeType.NON_DISRUPTIVE)
52    finally:
53      self._test_failure_reason = nearby_snippet.test_failure_reason
54      self._test_result.quality_info = (
55          nearby_snippet.connection_quality_info
56      )
57
58    # 2. transfer file through bluetooth
59    try:
60      self._test_result.file_transfer_throughput_kbps = (
61          nearby_snippet.transfer_file(
62              nc_constants.TRANSFER_FILE_SIZE_1KB,
63              nc_constants.BT_1K_PAYLOAD_TRANSFER_TIMEOUT,
64              nc_constants.PayloadType.FILE,
65          )
66      )
67    finally:
68      self._test_failure_reason = nearby_snippet.test_failure_reason
69
70    # 3. disconnect
71    nearby_snippet.disconnect_endpoint()
72
73  def get_test_result_message(self) -> str:
74    """Gets the test result of current test."""
75    if (
76        self._test_failure_reason
77        == nc_constants.SingleTestFailureReason.SUCCESS
78    ):
79      return 'PASS'
80    if (
81        self._test_failure_reason
82        is nc_constants.SingleTestFailureReason.FILE_TRANSFER_FAIL
83    ):
84      return 'The Bluetooth performance is really bad or unknown reason.'
85    return ''.join([
86        f'FAIL: due to {self._test_failure_reason.name} - ',
87        f'{nc_constants.COMMON_TRIAGE_TIP.get(self._test_failure_reason)}'
88        ])
89