1 /******************************************************************************
2  *
3  *  Copyright 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 #include "common/message_loop_thread.h"
24 
25 namespace base {
26 class MessageLoop;
27 }  // namespace base
28 
get_main_thread()29 bluetooth::common::MessageLoopThread* get_main_thread() { return nullptr; }
30 
31 namespace {
32 const RawAddress bdaddr1({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
33 const RawAddress bdaddr2({0x66, 0x55, 0x44, 0x33, 0x22, 0x11});
34 }  // namespace
35 
36 // TODO(jpawlowski): there is some weird dependency issue in tests, and the
37 // tests here fail to compile without this definition.
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)38 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}
39 
40 class BtaHfClientTest : public testing::Test {
41  protected:
SetUp()42   void SetUp() override {
43     // Reset the memory block, this is the state on which the allocate handle
44     // would start operating
45     bta_hf_client_cb_arr_init();
46   }
47 };
48 
49 // Test that when we can allocate a device on the block and then check
50 // the status of the blocks
TEST_F(BtaHfClientTest,test_allocate_block_one_device)51 TEST_F(BtaHfClientTest, test_allocate_block_one_device) {
52   uint16_t p_handle = 0;
53   bool status = bta_hf_client_allocate_handle(bdaddr1, &p_handle);
54 
55   // Allocation should succeed
56   EXPECT_EQ(true, status);
57   EXPECT_GT(p_handle, 0);
58 }
59 
60 // Test that we cannot allocate the same device on two separate control blocks
TEST_F(BtaHfClientTest,test_no_allocate_block_same_device)61 TEST_F(BtaHfClientTest, test_no_allocate_block_same_device) {
62   uint16_t p_handle;
63   bool status = bta_hf_client_allocate_handle(bdaddr1, &p_handle);
64 
65   // Allocation should succeed
66   EXPECT_EQ(true, status);
67   EXPECT_GT(p_handle, 0);
68 
69   EXPECT_TRUE(bta_hf_client_find_cb_by_bda(bdaddr1) != NULL);
70 
71   status = bta_hf_client_allocate_handle(bdaddr1, &p_handle);
72 
73   // Allocation should fail
74   EXPECT_EQ(false, status);
75 }
76 
77 // Test that we can allocate two different devices as separate control blocks
TEST_F(BtaHfClientTest,test_allocate_block_diff_device)78 TEST_F(BtaHfClientTest, test_allocate_block_diff_device) {
79   uint16_t p_handle_first;
80   bool status = bta_hf_client_allocate_handle(bdaddr1, &p_handle_first);
81 
82   // Allocation should succeed
83   EXPECT_EQ(true, status);
84   EXPECT_GT(p_handle_first, 0);
85 
86   EXPECT_TRUE(bta_hf_client_find_cb_by_bda(bdaddr2) == NULL);
87 
88   uint16_t p_handle_second;
89   status = bta_hf_client_allocate_handle(bdaddr2, &p_handle_second);
90 
91   // Allocation should succeed
92   EXPECT_EQ(true, status);
93   EXPECT_GT(p_handle_second, 0);
94   EXPECT_NE(p_handle_first, p_handle_second);
95 }
96