1 /*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include <tuple>
21 #include <vector>
22
23 #include "bta/ag/bta_ag_int.h"
24 #include "bta/include/bta_le_audio_api.h"
25 #include "hci/controller_interface_mock.h"
26 #include "stack/btm/btm_int_types.h"
27 #include "test/mock/mock_device_esco_parameters.h"
28 #include "test/mock/mock_main_shim_entry.h"
29
btm_peer_supports_esco_ev3(const RawAddress & remote_bda)30 bool btm_peer_supports_esco_ev3(const RawAddress& remote_bda) { return true; }
31 tBTM_CB btm_cb;
Get()32 LeAudioClient* LeAudioClient::Get() { return nullptr; }
IsLeAudioClientInStreaming()33 bool LeAudioClient::IsLeAudioClientInStreaming() { return false; }
34
35 const RawAddress kRawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
36
37 class BtaAgScoParameterSelectionTest
38 : public ::testing::TestWithParam<
39 std::tuple<tBTA_AG_FEAT, tBTA_AG_PEER_FEAT, bool>> {
40 protected:
SetUp()41 void SetUp() override {
42 test::mock::device_esco_parameters::esco_parameters_for_codec.body =
43 [this](esco_codec_t codec) {
44 this->codec = codec;
45 return enh_esco_params_t{};
46 };
47 bluetooth::hci::testing::mock_controller_ = &controller_;
48 }
TearDown()49 void TearDown() override {
50 test::mock::device_esco_parameters::esco_parameters_for_codec = {};
51 bluetooth::hci::testing::mock_controller_ = nullptr;
52 }
53 esco_codec_t codec;
54 bluetooth::hci::testing::MockControllerInterface controller_;
55 };
56
TEST_P(BtaAgScoParameterSelectionTest,create_sco_cvsd)57 TEST_P(BtaAgScoParameterSelectionTest, create_sco_cvsd) {
58 bta_ag_api_set_active_device(kRawAddress);
59
60 const auto [feature, peer_feature, is_local] = GetParam();
61 tBTA_AG_SCB scb{
62 .peer_addr = kRawAddress,
63 .features = feature,
64 .peer_features = peer_feature,
65 .sco_idx = BTM_INVALID_SCO_INDEX,
66 .inuse_codec = UUID_CODEC_CVSD,
67 };
68
69 this->codec = ESCO_CODEC_UNKNOWN;
70 bta_ag_create_sco(&scb, is_local);
71 if ((scb.features & BTA_AG_FEAT_ESCO_S4) &&
72 (scb.peer_features & BTA_AG_PEER_FEAT_ESCO_S4)) {
73 ASSERT_EQ(this->codec, ESCO_CODEC_CVSD_S4);
74 } else {
75 ASSERT_EQ(this->codec, ESCO_CODEC_CVSD_S3);
76 }
77 }
78
TEST_P(BtaAgScoParameterSelectionTest,create_pending_sco_cvsd)79 TEST_P(BtaAgScoParameterSelectionTest, create_pending_sco_cvsd) {
80 bta_ag_api_set_active_device(kRawAddress);
81
82 const auto [feature, peer_feature, is_local] = GetParam();
83 tBTA_AG_SCB scb{
84 .peer_addr = kRawAddress,
85 .features = feature,
86 .peer_features = peer_feature,
87 .sco_idx = BTM_INVALID_SCO_INDEX,
88 .inuse_codec = UUID_CODEC_CVSD,
89 };
90
91 this->codec = ESCO_CODEC_UNKNOWN;
92 if (is_local) {
93 bta_ag_create_sco(&scb, true);
94 } else {
95 // empty data, not used in the function
96 tBTM_ESCO_CONN_REQ_EVT_DATA data;
97 bta_ag_sco_conn_rsp(&scb, &data);
98 }
99 if ((scb.features & BTA_AG_FEAT_ESCO_S4) &&
100 (scb.peer_features & BTA_AG_PEER_FEAT_ESCO_S4)) {
101 ASSERT_EQ(this->codec, ESCO_CODEC_CVSD_S4);
102 } else {
103 ASSERT_EQ(this->codec, ESCO_CODEC_CVSD_S3);
104 }
105 }
106
107 std::vector<std::tuple<tBTA_AG_FEAT, tBTA_AG_PEER_FEAT, bool>>
BtaAgScoParameterSelectionTestParameters()108 BtaAgScoParameterSelectionTestParameters() {
109 tBTA_AG_FEAT features[] = {0, BTA_AG_FEAT_ESCO_S4};
110 tBTA_AG_PEER_FEAT peer_features[] = {0, BTA_AG_PEER_FEAT_ESCO_S4};
111 bool is_local_or_orig[] = {false, true};
112 std::vector<std::tuple<tBTA_AG_FEAT, tBTA_AG_PEER_FEAT, bool>> params;
113
114 for (auto i : features) {
115 for (auto j : peer_features) {
116 for (auto k : is_local_or_orig) {
117 params.push_back({i, j, k});
118 }
119 }
120 }
121 return params;
122 }
123
124 INSTANTIATE_TEST_SUITE_P(
125 BtaAgScoParameterSelectionTests, BtaAgScoParameterSelectionTest,
126 ::testing::ValuesIn(BtaAgScoParameterSelectionTestParameters()));
127