1 /*
2  * Copyright (C) 2024 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 "bta/le_audio/gmap_client.h"
18 
19 #include <bluetooth/log.h>
20 #include <com_android_bluetooth_flags.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <hardware/bluetooth.h>
24 
25 #include "bta/le_audio/le_audio_types.h"
26 #include "fake_osi.h"
27 #include "test/mock/mock_osi_properties.h"
28 
29 using bluetooth::le_audio::GmapClient;
30 using ::testing::_;
31 
32 static constexpr char kGmapEnabledSysProp[] = "bluetooth.profile.gmap.enabled";
33 
34 void osi_property_set_bool(const char* key, bool value);
35 
36 class GmapClientTest : public ::testing::Test {
37 public:
38   RawAddress addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
39   GmapClient gmapClient = GmapClient(addr);
40 };
41 
TEST_F(GmapClientTest,test_parse_role)42 TEST_F(GmapClientTest, test_parse_role) {
43   const uint8_t role = 0b0001;
44   gmapClient.parseAndSaveGmapRole(1, &role);
45 
46   ASSERT_EQ(gmapClient.getRole(), role);
47 }
48 
TEST_F(GmapClientTest,test_parse_invalid_role)49 TEST_F(GmapClientTest, test_parse_invalid_role) {
50   const uint8_t role = 0b0001;
51   ASSERT_FALSE(gmapClient.parseAndSaveGmapRole(2, &role));
52 }
53 
TEST_F(GmapClientTest,test_parse_ugt_feature)54 TEST_F(GmapClientTest, test_parse_ugt_feature) {
55   const uint8_t value = 0b0001;
56   gmapClient.parseAndSaveUGTFeature(1, &value);
57 
58   ASSERT_EQ(gmapClient.getUGTFeature(), value);
59 }
60 
TEST_F(GmapClientTest,test_parse_invalid_ugt_feature)61 TEST_F(GmapClientTest, test_parse_invalid_ugt_feature) {
62   const uint8_t value = 0b0001;
63   ASSERT_FALSE(gmapClient.parseAndSaveUGTFeature(2, &value));
64 }
65 
TEST_F(GmapClientTest,test_add_from_storage)66 TEST_F(GmapClientTest, test_add_from_storage) {
67   const uint8_t role = 0b0001;
68   const uint16_t role_handle = 2;
69   const uint8_t UGT_feature = 0b0011;
70   const uint16_t UGT_feature_handle = 4;
71   gmapClient.AddFromStorage(addr, role, role_handle, UGT_feature, UGT_feature_handle);
72   ASSERT_EQ(gmapClient.getRole(), role);
73   ASSERT_EQ(gmapClient.getRoleHandle(), role_handle);
74   ASSERT_EQ(gmapClient.getUGTFeature(), UGT_feature);
75   ASSERT_EQ(gmapClient.getUGTFeatureHandle(), UGT_feature_handle);
76 }
77 
TEST_F(GmapClientTest,test_role_handle)78 TEST_F(GmapClientTest, test_role_handle) {
79   const uint16_t handle = 5;
80   gmapClient.setRoleHandle(handle);
81   ASSERT_EQ(gmapClient.getRoleHandle(), handle);
82 }
83 
TEST_F(GmapClientTest,test_ugt_feature_handle)84 TEST_F(GmapClientTest, test_ugt_feature_handle) {
85   const uint16_t handle = 6;
86   gmapClient.setUGTFeatureHandle(handle);
87   ASSERT_EQ(gmapClient.getUGTFeatureHandle(), handle);
88 }
89 
TEST_F(GmapClientTest,test_is_gmap_client_enabled)90 TEST_F(GmapClientTest, test_is_gmap_client_enabled) {
91   GmapClient::UpdateGmapOffloaderSupport(false);
92   ASSERT_EQ(GmapClient::IsGmapClientEnabled(), false);
93 
94   com::android::bluetooth::flags::provider_->leaudio_gmap_client(true);
95   osi_property_set_bool(kGmapEnabledSysProp, true);
96 
97   GmapClient::UpdateGmapOffloaderSupport(true);
98 
99   ASSERT_EQ(GmapClient::IsGmapClientEnabled(), true);
100   osi_property_set_bool(kGmapEnabledSysProp, false);
101 }
102