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 <memory>
22
23 #include "bta/hf_client/bta_hf_client_int.h"
24 #include "test/fake/fake_osi.h"
25 #include "types/raw_address.h"
26
27 namespace {
28 const RawAddress bdaddr1({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
29 const RawAddress bdaddr2({0x66, 0x55, 0x44, 0x33, 0x22, 0x11});
30 } // namespace
31
32 class BtaHfClientTest : public testing::Test {
33 protected:
SetUp()34 void SetUp() override {
35 fake_osi_ = std::make_unique<test::fake::FakeOsi>();
36 // Reset the memory block, this is the state on which the allocate handle
37 // would start operating
38 bta_hf_client_cb_arr_init();
39 }
40 std::unique_ptr<test::fake::FakeOsi> fake_osi_;
41 };
42
43 // Test that when we can allocate a device on the block and then check
44 // the status of the blocks
TEST_F(BtaHfClientTest,test_allocate_block_one_device)45 TEST_F(BtaHfClientTest, test_allocate_block_one_device) {
46 uint16_t p_handle = 0;
47 bool status = bta_hf_client_allocate_handle(bdaddr1, &p_handle);
48
49 // Allocation should succeed
50 EXPECT_EQ(true, status);
51 EXPECT_GT(p_handle, 0);
52 }
53
54 // Test that we cannot allocate the same device on two separate control blocks
TEST_F(BtaHfClientTest,test_no_allocate_block_same_device)55 TEST_F(BtaHfClientTest, test_no_allocate_block_same_device) {
56 uint16_t p_handle;
57 bool status = bta_hf_client_allocate_handle(bdaddr1, &p_handle);
58
59 // Allocation should succeed
60 EXPECT_EQ(true, status);
61 EXPECT_GT(p_handle, 0);
62
63 EXPECT_TRUE(bta_hf_client_find_cb_by_bda(bdaddr1) != NULL);
64
65 status = bta_hf_client_allocate_handle(bdaddr1, &p_handle);
66
67 // Allocation should fail
68 EXPECT_EQ(false, status);
69 }
70
71 // Test that we can allocate two different devices as separate control blocks
TEST_F(BtaHfClientTest,test_allocate_block_diff_device)72 TEST_F(BtaHfClientTest, test_allocate_block_diff_device) {
73 uint16_t p_handle_first;
74 bool status = bta_hf_client_allocate_handle(bdaddr1, &p_handle_first);
75
76 // Allocation should succeed
77 EXPECT_EQ(true, status);
78 EXPECT_GT(p_handle_first, 0);
79
80 EXPECT_TRUE(bta_hf_client_find_cb_by_bda(bdaddr2) == NULL);
81
82 uint16_t p_handle_second;
83 status = bta_hf_client_allocate_handle(bdaddr2, &p_handle_second);
84
85 // Allocation should succeed
86 EXPECT_EQ(true, status);
87 EXPECT_GT(p_handle_second, 0);
88 EXPECT_NE(p_handle_first, p_handle_second);
89 }
90