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 <android-base/properties.h>
18 #include <base/functional/bind.h>
19 #include <base/location.h>
20 #include <bluetooth/log.h>
21 #include <com_android_bluetooth_flags.h>
22 #include <flag_macros.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 
26 #include <memory>
27 #include <string>
28 
29 #include "bta/ag/bta_ag_int.h"
30 #include "bta/include/bta_ag_swb_aptx.h"
31 #include "hci/controller_interface_mock.h"
32 #include "test/common/main_handler.h"
33 #include "test/common/mock_functions.h"
34 #include "test/fake/fake_osi.h"
35 #include "test/mock/mock_bta_sys_main.h"
36 #include "test/mock/mock_device_esco_parameters.h"
37 #include "test/mock/mock_main_shim_entry.h"
38 #include "test/mock/mock_osi_alarm.h"
39 #include "test/mock/mock_stack_acl.h"
40 #include "test/mock/mock_stack_btm_interface.h"
41 
42 #define TEST_BT com::android::bluetooth::flags
43 
44 using namespace bluetooth;
45 
46 namespace {
47 
bta_ag_hdl_event(const BT_HDR_RIGID * p_msg)48 bool bta_ag_hdl_event(const BT_HDR_RIGID* p_msg) { return true; };
BTA_AgDisable()49 void BTA_AgDisable() { bta_sys_deregister(BTA_ID_AG); }
50 
51 const tBTA_SYS_REG bta_ag_reg = {bta_ag_hdl_event, BTA_AgDisable};
52 
53 }  // namespace
54 
55 const std::string kBtCodecAptxVoiceEnabled =
56     "bluetooth.hfp.codec_aptx_voice.enabled";
57 
enable_aptx_voice_property(bool enable)58 static bool enable_aptx_voice_property(bool enable) {
59   const std::string value = enable ? "true" : "false";
60   return android::base::SetProperty(kBtCodecAptxVoiceEnabled, value);
61 }
62 
63 class BtaAgTest : public testing::Test {
64  protected:
SetUp()65   void SetUp() override {
66     reset_mock_function_count_map();
67     fake_osi_ = std::make_unique<test::fake::FakeOsi>();
68     bluetooth::hci::testing::mock_controller_ = &controller_;
69 
70     main_thread_start_up();
71     post_on_bt_main([]() { log::info("Main thread started up"); });
72 
73     bta_sys_register(BTA_ID_AG, &bta_ag_reg);
74 
75     bta_ag_cb.p_cback = [](tBTA_AG_EVT event, tBTA_AG* p_data) {};
76     RawAddress::FromString("00:11:22:33:44:55", addr);
77     test::mock::device_esco_parameters::esco_parameters_for_codec.body =
78         [this](esco_codec_t codec) {
79           this->codec = codec;
80           return enh_esco_params_t{};
81         };
82   }
TearDown()83   void TearDown() override {
84     test::mock::device_esco_parameters::esco_parameters_for_codec = {};
85     bta_sys_deregister(BTA_ID_AG);
86     post_on_bt_main([]() { log::info("Main thread shutting down"); });
87     main_thread_shut_down();
88     bluetooth::hci::testing::mock_controller_ = nullptr;
89   }
90 
91   std::unique_ptr<test::fake::FakeOsi> fake_osi_;
92   const char test_strings[5][13] = {"0,4,6,7", "4,6,7", "test,0,4", "9,8,7",
93                                     "4,6,7,test"};
94   uint32_t tmp_num = 0xFFFF;
95   RawAddress addr;
96   esco_codec_t codec;
97   bluetooth::hci::testing::MockControllerInterface controller_;
98 };
99 
TEST_F_WITH_FLAGS(BtaAgTest,nop,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))100 TEST_F_WITH_FLAGS(BtaAgTest, nop,
101                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
102                                                       hfp_codec_aptx_voice))) {
103   bool status = true;
104   ASSERT_EQ(true, status);
105 }
106 
107 class BtaAgSwbTest : public BtaAgTest {
108  protected:
SetUp()109   void SetUp() override { BtaAgTest::SetUp(); }
TearDown()110   void TearDown() override { BtaAgTest::TearDown(); }
111 };
112 
TEST_F_WITH_FLAGS(BtaAgSwbTest,parse_qac_at_command,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))113 TEST_F_WITH_FLAGS(BtaAgSwbTest, parse_qac_at_command,
114                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
115                                                       hfp_codec_aptx_voice))) {
116   tBTA_AG_PEER_CODEC codec = bta_ag_parse_qac((char*)test_strings[0]);
117   codec = bta_ag_parse_qac((char*)test_strings[0]);
118   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q0_MASK);
119   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q1_MASK);
120   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q2_MASK);
121   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q3_MASK);
122 
123   codec = bta_ag_parse_qac((char*)test_strings[1]);
124   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q1_MASK);
125   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q2_MASK);
126   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q3_MASK);
127 
128   codec = bta_ag_parse_qac((char*)test_strings[2]);
129   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q0_MASK);
130   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q1_MASK);
131 
132   codec = bta_ag_parse_qac((char*)test_strings[3]);
133   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q3_MASK);
134 
135   codec = bta_ag_parse_qac((char*)test_strings[4]);
136   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q1_MASK);
137   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q2_MASK);
138   ASSERT_TRUE(codec & BTA_AG_SCO_APTX_SWB_SETTINGS_Q3_MASK);
139 }
140 
TEST_F_WITH_FLAGS(BtaAgSwbTest,enable_swb_codec_fail,REQUIRES_FLAGS_DISABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))141 TEST_F_WITH_FLAGS(BtaAgSwbTest, enable_swb_codec_fail,
142                   REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(TEST_BT,
143                                                        hfp_codec_aptx_voice))) {
144   ASSERT_TRUE(enable_aptx_voice_property(false));
145   ASSERT_EQ(BT_STATUS_FAIL, enable_aptx_swb_codec(true, &addr));
146   ASSERT_FALSE(get_swb_codec_status(
147       bluetooth::headset::BTHF_SWB_CODEC_VENDOR_APTX, &addr));
148 }
149 
TEST_F_WITH_FLAGS(BtaAgSwbTest,enable_swb_codec_success,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))150 TEST_F_WITH_FLAGS(BtaAgSwbTest, enable_swb_codec_success,
151                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
152                                                       hfp_codec_aptx_voice))) {
153   ASSERT_TRUE(enable_aptx_voice_property(true));
154   ASSERT_EQ(BT_STATUS_SUCCESS, enable_aptx_swb_codec(true, &addr));
155   ASSERT_TRUE(get_swb_codec_status(
156       bluetooth::headset::BTHF_SWB_CODEC_VENDOR_APTX, &addr));
157   ASSERT_TRUE(enable_aptx_voice_property(false));
158 }
159 
160 class BtaAgActTest : public BtaAgTest {
161  protected:
SetUp()162   void SetUp() override { BtaAgTest::SetUp(); }
TearDown()163   void TearDown() override { BtaAgTest::TearDown(); }
164 };
165 
TEST_F_WITH_FLAGS(BtaAgActTest,set_codec_q0_success,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))166 TEST_F_WITH_FLAGS(BtaAgActTest, set_codec_q0_success,
167                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
168                                                       hfp_codec_aptx_voice))) {
169   tBTA_AG_SCB* p_scb = &bta_ag_cb.scb[0];
170   const tBTA_AG_DATA data = {.api_setcodec.codec =
171                                  BTA_AG_SCO_APTX_SWB_SETTINGS_Q0};
172 
173   bta_ag_cb.p_cback = [](tBTA_AG_EVT event, tBTA_AG* p_data) {
174     tBTA_AG_VAL* val = (tBTA_AG_VAL*)p_data;
175     ASSERT_EQ(val->num, BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
176     ASSERT_EQ(val->hdr.status, BTA_AG_SUCCESS);
177   };
178 
179   p_scb->peer_codecs = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0;
180   p_scb->sco_codec = BTM_SCO_CODEC_NONE;
181   p_scb->codec_updated = false;
182 
183   bta_ag_setcodec(p_scb, data);
184   ASSERT_EQ(p_scb->sco_codec, BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
185 }
186 
TEST_F_WITH_FLAGS(BtaAgActTest,set_codec_q1_fail_unsupported,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))187 TEST_F_WITH_FLAGS(BtaAgActTest, set_codec_q1_fail_unsupported,
188                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
189                                                       hfp_codec_aptx_voice))) {
190   tBTA_AG_SCB* p_scb = &bta_ag_cb.scb[0];
191   const tBTA_AG_DATA data = {.api_setcodec.codec =
192                                  BTA_AG_SCO_APTX_SWB_SETTINGS_Q1};
193 
194   ASSERT_TRUE(enable_aptx_voice_property(true));
195 
196   bta_ag_cb.p_cback = [](tBTA_AG_EVT event, tBTA_AG* p_data) {
197     tBTA_AG_VAL* val = (tBTA_AG_VAL*)p_data;
198     ASSERT_EQ(val->num, BTA_AG_SCO_APTX_SWB_SETTINGS_Q1);
199     ASSERT_EQ(val->hdr.status, BTA_AG_FAIL_RESOURCES);
200   };
201 
202   p_scb->peer_codecs = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0;
203   p_scb->sco_codec = BTM_SCO_CODEC_NONE;
204   p_scb->codec_updated = false;
205 
206   bta_ag_setcodec(p_scb, data);
207   ASSERT_TRUE(enable_aptx_voice_property(false));
208 }
209 
210 class BtaAgCmdTest : public BtaAgTest {
211  protected:
SetUp()212   void SetUp() override { BtaAgTest::SetUp(); }
TearDown()213   void TearDown() override { BtaAgTest::TearDown(); }
214 };
215 
TEST_F_WITH_FLAGS(BtaAgCmdTest,check_flag_disabling_guarding_with_prop,REQUIRES_FLAGS_DISABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))216 TEST_F_WITH_FLAGS(BtaAgCmdTest, check_flag_disabling_guarding_with_prop,
217                   REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(TEST_BT,
218                                                        hfp_codec_aptx_voice))) {
219   ASSERT_FALSE(com::android::bluetooth::flags::hfp_codec_aptx_voice());
220   ASSERT_TRUE(enable_aptx_voice_property(false));
221   ASSERT_FALSE(is_hfp_aptx_voice_enabled());
222 
223   ASSERT_TRUE(enable_aptx_voice_property(true));
224   ASSERT_FALSE(is_hfp_aptx_voice_enabled());
225 }
226 
TEST_F_WITH_FLAGS(BtaAgCmdTest,check_flag_guarding_with_prop,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))227 TEST_F_WITH_FLAGS(BtaAgCmdTest, check_flag_guarding_with_prop,
228                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
229                                                       hfp_codec_aptx_voice))) {
230   ASSERT_TRUE(com::android::bluetooth::flags::hfp_codec_aptx_voice());
231   ASSERT_TRUE(enable_aptx_voice_property(false));
232   ASSERT_FALSE(is_hfp_aptx_voice_enabled());
233 
234   ASSERT_TRUE(enable_aptx_voice_property(true));
235   ASSERT_TRUE(is_hfp_aptx_voice_enabled());
236 }
237 
TEST_F_WITH_FLAGS(BtaAgCmdTest,at_hfp_cback__qac_ev_codec_disabled,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))238 TEST_F_WITH_FLAGS(BtaAgCmdTest, at_hfp_cback__qac_ev_codec_disabled,
239                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
240                                                       hfp_codec_aptx_voice))) {
241   tBTA_AG_SCB p_scb = {
242       .peer_addr = addr,
243       .app_id = 0,
244   };
245 
246   ASSERT_TRUE(enable_aptx_voice_property(false));
247 
248   bta_ag_at_hfp_cback(&p_scb, BTA_AG_AT_QAC_EVT, 0, (char*)&test_strings[0][0],
249                       (char*)&test_strings[0][12],
250                       BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
251   ASSERT_FALSE(p_scb.codec_updated);
252   ASSERT_FALSE(p_scb.is_aptx_swb_codec);
253   ASSERT_EQ(1, get_func_call_count("PORT_WriteData"));
254 }
255 
TEST_F_WITH_FLAGS(BtaAgCmdTest,at_hfp_cback__qac_ev_codec_enabled,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))256 TEST_F_WITH_FLAGS(BtaAgCmdTest, at_hfp_cback__qac_ev_codec_enabled,
257                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
258                                                       hfp_codec_aptx_voice))) {
259   tBTA_AG_SCB p_scb = {.peer_addr = addr,
260                        .app_id = 0,
261                        .peer_codecs = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0_MASK};
262 
263   ASSERT_TRUE(enable_aptx_voice_property(true));
264   ASSERT_EQ(BT_STATUS_SUCCESS, enable_aptx_swb_codec(true, &addr));
265   bta_ag_at_hfp_cback(&p_scb, BTA_AG_AT_QAC_EVT, 0, (char*)&test_strings[0][0],
266                       (char*)&test_strings[0][12],
267                       BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
268   ASSERT_TRUE(p_scb.codec_updated);
269   ASSERT_TRUE(p_scb.is_aptx_swb_codec);
270   ASSERT_EQ(2, get_func_call_count("PORT_WriteData"));
271   ASSERT_EQ(p_scb.sco_codec, BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
272   ASSERT_TRUE(enable_aptx_voice_property(false));
273 }
274 
TEST_F_WITH_FLAGS(BtaAgCmdTest,at_hfp_cback__qcs_ev_codec_disabled,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))275 TEST_F_WITH_FLAGS(BtaAgCmdTest, at_hfp_cback__qcs_ev_codec_disabled,
276                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
277                                                       hfp_codec_aptx_voice))) {
278   tBTA_AG_SCB p_scb = {
279       .peer_addr = addr,
280       .app_id = 0,
281   };
282 
283   ASSERT_TRUE(enable_aptx_voice_property(false));
284 
285   bta_ag_at_hfp_cback(&p_scb, BTA_AG_AT_QCS_EVT, 0, (char*)&test_strings[0][0],
286                       (char*)&test_strings[0][12],
287                       BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
288   ASSERT_FALSE(p_scb.codec_updated);
289   ASSERT_FALSE(p_scb.is_aptx_swb_codec);
290   ASSERT_EQ(1, get_func_call_count("PORT_WriteData"));
291 }
292 
TEST_F_WITH_FLAGS(BtaAgCmdTest,at_hfp_cback__qcs_ev_codec_q0_enabled,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))293 TEST_F_WITH_FLAGS(BtaAgCmdTest, at_hfp_cback__qcs_ev_codec_q0_enabled,
294                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
295                                                       hfp_codec_aptx_voice))) {
296   reset_mock_btm_client_interface();
297   mock_btm_client_interface.sco.BTM_SetEScoMode =
298       [](enh_esco_params_t* p_parms) -> tBTM_STATUS {
299     inc_func_call_count("BTM_SetEScoMode");
300     return BTM_SUCCESS;
301   };
302 
303   tBTA_AG_SCB p_scb = {.peer_addr = addr,
304                        .sco_idx = BTM_INVALID_SCO_INDEX,
305                        .app_id = 0,
306                        .sco_codec = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0,
307                        .is_aptx_swb_codec = true};
308 
309   ASSERT_TRUE(enable_aptx_voice_property(true));
310 
311   bta_ag_cb.sco.state = BTA_AG_SCO_CODEC_ST;
312   bta_ag_api_set_active_device(addr);
313   ASSERT_EQ(addr, bta_ag_get_active_device());
314 
315   ASSERT_EQ(BT_STATUS_SUCCESS, enable_aptx_swb_codec(true, &addr));
316   bta_ag_at_hfp_cback(&p_scb, BTA_AG_AT_QCS_EVT, 0, (char*)&test_strings[0][0],
317                       (char*)&test_strings[0][12],
318                       BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
319 
320   ASSERT_EQ(1, get_func_call_count("alarm_cancel"));
321   ASSERT_EQ(1, get_func_call_count("esco_parameters_for_codec"));
322   ASSERT_EQ(BT_STATUS_SUCCESS, enable_aptx_swb_codec(true, &addr));
323   ASSERT_EQ(1, get_func_call_count("BTM_SetEScoMode"));
324   ASSERT_EQ(1, get_func_call_count("BTM_CreateSco"));
325   ASSERT_EQ(this->codec, ESCO_CODEC_SWB_Q0);
326   ASSERT_TRUE(enable_aptx_voice_property(false));
327 }
328 
TEST_F_WITH_FLAGS(BtaAgCmdTest,handle_swb_at_event__qcs_ev_codec_q1_fallback_to_q0,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))329 TEST_F_WITH_FLAGS(BtaAgCmdTest,
330                   handle_swb_at_event__qcs_ev_codec_q1_fallback_to_q0,
331                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
332                                                       hfp_codec_aptx_voice))) {
333   reset_mock_btm_client_interface();
334   mock_btm_client_interface.sco.BTM_SetEScoMode =
335       [](enh_esco_params_t* p_parms) -> tBTM_STATUS {
336     inc_func_call_count("BTM_SetEScoMode");
337     return BTM_SUCCESS;
338   };
339 
340   tBTA_AG_SCB p_scb = {.peer_addr = addr,
341                        .sco_idx = BTM_INVALID_SCO_INDEX,
342                        .app_id = 0,
343                        .sco_codec = BTA_AG_SCO_APTX_SWB_SETTINGS_Q1,
344                        .codec_fallback = false,
345                        .is_aptx_swb_codec = true};
346 
347   ASSERT_TRUE(enable_aptx_voice_property(true));
348 
349   bta_ag_cb.sco.state = BTA_AG_SCO_CODEC_ST;
350   bta_ag_api_set_active_device(addr);
351   ASSERT_EQ(addr, bta_ag_get_active_device());
352 
353   ASSERT_EQ(BT_STATUS_SUCCESS, enable_aptx_swb_codec(true, &addr));
354   bta_ag_at_hfp_cback(&p_scb, BTA_AG_AT_QCS_EVT, 0, (char*)&test_strings[0][0],
355                       (char*)&test_strings[0][12],
356                       BTA_AG_SCO_APTX_SWB_SETTINGS_Q1);
357 
358   ASSERT_EQ(1, get_func_call_count("alarm_cancel"));
359   ASSERT_EQ(1, get_func_call_count("esco_parameters_for_codec"));
360   ASSERT_EQ(BT_STATUS_SUCCESS, enable_aptx_swb_codec(true, &addr));
361   ASSERT_EQ(1, get_func_call_count("BTM_SetEScoMode"));
362   ASSERT_EQ(1, get_func_call_count("BTM_CreateSco"));
363   ASSERT_EQ(this->codec, ESCO_CODEC_SWB_Q0);
364   ASSERT_TRUE(enable_aptx_voice_property(false));
365 }
366 
367 class BtaAgScoTest : public BtaAgTest {
368  protected:
SetUp()369   void SetUp() override {
370     BtaAgTest::SetUp();
371     test::mock::stack_acl::BTM_ReadRemoteFeatures.body =
372         [this](const RawAddress& addr) { return this->data; };
373   }
TearDown()374   void TearDown() override { BtaAgTest::TearDown(); }
375 
376   uint8_t data[3] = {1, 2, 3};
377 };
378 
TEST_F_WITH_FLAGS(BtaAgScoTest,codec_negotiate__aptx_state_on,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))379 TEST_F_WITH_FLAGS(BtaAgScoTest, codec_negotiate__aptx_state_on,
380                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
381                                                       hfp_codec_aptx_voice))) {
382   tBTA_AG_SCB* p_scb = &bta_ag_cb.scb[0];
383   p_scb->app_id = 0;
384   p_scb->peer_addr = addr;
385   p_scb->codec_negotiation_timer =
386       alarm_new("bta_ag.scb_codec_negotiation_timer");
387   p_scb->peer_codecs = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0_MASK;
388   p_scb->is_aptx_swb_codec = false;
389 
390   ASSERT_TRUE(enable_aptx_voice_property(true));
391   ASSERT_EQ(BT_STATUS_SUCCESS, enable_aptx_swb_codec(true, &addr));
392   bta_ag_codec_negotiate(p_scb);
393   ASSERT_EQ(1, get_func_call_count("BTM_ReadRemoteFeatures"));
394   ASSERT_EQ(1, get_func_call_count("PORT_WriteData"));
395   ASSERT_EQ(1, get_func_call_count("alarm_set_on_mloop"));
396   ASSERT_TRUE(p_scb->is_aptx_swb_codec);
397   ASSERT_EQ(p_scb->sco_codec, BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
398   ASSERT_TRUE(enable_aptx_voice_property(false));
399 }
400 
TEST_F_WITH_FLAGS(BtaAgScoTest,codec_negotiate__aptx_state_off,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))401 TEST_F_WITH_FLAGS(BtaAgScoTest, codec_negotiate__aptx_state_off,
402                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
403                                                       hfp_codec_aptx_voice))) {
404   tBTA_AG_SCB* p_scb = &bta_ag_cb.scb[0];
405   p_scb->app_id = 0;
406   p_scb->peer_addr = addr;
407   p_scb->codec_negotiation_timer =
408       alarm_new("bta_ag.scb_codec_negotiation_timer");
409   p_scb->peer_codecs = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0_MASK;
410   p_scb->is_aptx_swb_codec = true;
411 
412   ASSERT_TRUE(enable_aptx_voice_property(true));
413   ASSERT_EQ(BT_STATUS_SUCCESS, enable_aptx_swb_codec(false, &addr));
414   bta_ag_codec_negotiate(p_scb);
415   ASSERT_EQ(1, get_func_call_count("BTM_ReadRemoteFeatures"));
416   ASSERT_EQ(1, get_func_call_count("PORT_WriteData"));
417   ASSERT_EQ(1, get_func_call_count("alarm_set_on_mloop"));
418   ASSERT_FALSE(p_scb->is_aptx_swb_codec);
419   ASSERT_EQ(p_scb->sco_codec, BTM_SCO_CODEC_MSBC);
420   ASSERT_TRUE(enable_aptx_voice_property(false));
421 }
422 
TEST_F_WITH_FLAGS(BtaAgScoTest,codec_negotiate__aptx_disabled,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,hfp_codec_aptx_voice)))423 TEST_F_WITH_FLAGS(BtaAgScoTest, codec_negotiate__aptx_disabled,
424                   REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT,
425                                                       hfp_codec_aptx_voice))) {
426   // const char* test_flag[] = {"INIT_aptx_voice=false", nullptr};
427   tBTA_AG_SCB* p_scb = &bta_ag_cb.scb[0];
428   p_scb->app_id = 0;
429   p_scb->peer_addr = addr;
430   p_scb->codec_negotiation_timer =
431       alarm_new("bta_ag.scb_codec_negotiation_timer");
432   p_scb->peer_codecs = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0_MASK;
433   p_scb->is_aptx_swb_codec = true;
434   p_scb->codec_updated = true;
435 
436   // bluetooth::common::InitFlags::Load(test_flag);
437   ASSERT_TRUE(enable_aptx_voice_property(false));
438   ASSERT_EQ(BT_STATUS_FAIL, enable_aptx_swb_codec(false, &addr));
439   bta_ag_codec_negotiate(p_scb);
440   ASSERT_EQ(1, get_func_call_count("BTM_ReadRemoteFeatures"));
441   ASSERT_EQ(0, get_func_call_count("PORT_WriteData"));
442   ASSERT_EQ(0, get_func_call_count("alarm_set_on_mloop"));
443   ASSERT_FALSE(p_scb->codec_updated);
444 }
445