1 /******************************************************************************
2  *
3  *  Copyright (C) 2016 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include <gtest/gtest.h>
20 
21 #include "bta/hf_client/bta_hf_client_int.h"
22 #include "bta/include/bta_hf_client_api.h"
23 
24 namespace {
25 const BD_ADDR bdaddr1 = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
26 const BD_ADDR bdaddr2 = {0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
27 }  // namespace
28 
29 class BtaHfClientTest : public testing::Test {
30  protected:
SetUp()31   void SetUp() override {
32     // Reset the memory block, this is the state on which the allocate handle
33     // would start operating
34     bta_hf_client_cb_arr_init();
35   }
36 };
37 
38 // Test that when we can allocate a device on the block and then check
39 // the status of the blocks
TEST_F(BtaHfClientTest,test_allocate_block_one_device)40 TEST_F(BtaHfClientTest, test_allocate_block_one_device) {
41   uint16_t p_handle = 0;
42   bool status = bta_hf_client_allocate_handle(bdaddr1, &p_handle);
43 
44   // Allocation should succeed
45   EXPECT_EQ(true, status);
46   EXPECT_GT(p_handle, 0);
47 }
48 
49 // Test that we cannot allocate the same device on two separate control blocks
TEST_F(BtaHfClientTest,test_no_allocate_block_same_device)50 TEST_F(BtaHfClientTest, test_no_allocate_block_same_device) {
51   uint16_t p_handle;
52   bool status = bta_hf_client_allocate_handle(bdaddr1, &p_handle);
53 
54   // Allocation should succeed
55   EXPECT_EQ(true, status);
56   EXPECT_GT(p_handle, 0);
57 
58   EXPECT_TRUE(bta_hf_client_find_cb_by_bda(bdaddr1) != NULL);
59 
60   status = bta_hf_client_allocate_handle(bdaddr1, &p_handle);
61 
62   // Allocation should fail
63   EXPECT_EQ(false, status);
64 }
65 
66 // Test that we can allocate two different devices as separate control blocks
TEST_F(BtaHfClientTest,test_allocate_block_diff_device)67 TEST_F(BtaHfClientTest, test_allocate_block_diff_device) {
68   uint16_t p_handle_first;
69   bool status = bta_hf_client_allocate_handle(bdaddr1, &p_handle_first);
70 
71   // Allocation should succeed
72   EXPECT_EQ(true, status);
73   EXPECT_GT(p_handle_first, 0);
74 
75   EXPECT_TRUE(bta_hf_client_find_cb_by_bda(bdaddr2) == NULL);
76 
77   uint16_t p_handle_second;
78   status = bta_hf_client_allocate_handle(bdaddr2, &p_handle_second);
79 
80   // Allocation should succeed
81   EXPECT_EQ(true, status);
82   EXPECT_GT(p_handle_second, 0);
83   EXPECT_NE(p_handle_first, p_handle_second);
84 }
85