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"""This Test is to test the bluetooth and wifi 2G coex.
16
17The AP requirements:
18  wifi channel: 6 (2437)
19"""
20
21import datetime
22import logging
23import os
24import sys
25
26# Allows local imports to be resolved via relative path, so the test can be run
27# without building.
28_betocq_dir = os.path.dirname(os.path.dirname(__file__))
29if _betocq_dir not in sys.path:
30  sys.path.append(_betocq_dir)
31
32from mobly  import base_test
33from mobly import test_runner
34
35from betocq import d2d_performance_test_base
36from betocq import nc_constants
37
38_PERFORMANCE_TEST_COUNT = 100
39_PERFORMANCE_TEST_MAX_CONSECUTIVE_ERROR = 10
40
41
42class Bt2gWifiCoexTest(d2d_performance_test_base.D2dPerformanceTestBase):
43  """Test class for BT and 2G wifi coex with a complicated stress test."""
44
45  def _get_country_code(self) -> str:
46    return 'US'
47
48  def setup_class(self):
49    self.test_parameters.requires_bt_multiplex = True
50    # we don't care speed so that there is no need to wait
51    self.test_parameters.target_post_wifi_connection_idle_time_sec = 0
52    super().setup_class()
53    self.performance_test_iterations = getattr(
54        self.test_bt_2g_wifi_coex, base_test.ATTR_REPEAT_CNT
55    )
56    logging.info(
57        'performance test iterations: %s', self.performance_test_iterations
58    )
59
60  @base_test.repeat(
61      count=_PERFORMANCE_TEST_COUNT,
62      max_consecutive_error=_PERFORMANCE_TEST_MAX_CONSECUTIVE_ERROR,
63  )
64  def test_bt_2g_wifi_coex(self):
65    """Test the BT and 2G wifi coex with a stress test."""
66    self._test_connection_medium_performance(
67        nc_constants.NearbyMedium.UPGRADE_TO_ALL_WIFI,
68        wifi_ssid=self.test_parameters.wifi_2g_ssid,
69        wifi_password=self.test_parameters.wifi_2g_password,
70    )
71
72  def _get_transfer_file_size(self) -> int:
73    # For 2G wifi medium
74    return nc_constants.TRANSFER_FILE_SIZE_20MB
75
76  def _get_file_transfer_timeout(self) -> datetime.timedelta:
77    return nc_constants.WIFI_2G_20M_PAYLOAD_TRANSFER_TIMEOUT
78
79  def _get_file_transfer_failure_tip(self) -> str:
80    return (
81        'The Wifi Direct connection might be broken, check related log.'
82    )
83
84  # @typing.override
85  def _get_throughput_benchmark(
86      self, sta_frequency: int, sta_max_link_speed_mbps: int
87  ) -> tuple[float, float]:
88    # no requirement for throughput.
89    return (0.0, 0.0)
90
91  def _get_throughput_low_tip(self) -> str:
92    return (
93        f'{self._throughput_low_string}.'
94        ' This should never happen, you may ignore this error, this is'
95        ' not required for this case.'
96    )
97
98  def _is_wifi_ap_ready(self) -> bool:
99    return True if self.test_parameters.wifi_2g_ssid else False
100
101
102if __name__ == '__main__':
103  test_runner.main()
104