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