1 /*
2 * Copyright 2022 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <stdarg.h>
20
21 #include <string>
22
23 #include "bta/dm/bta_dm_int.h"
24 #include "test/common/main_handler.h"
25 #include "test/mock/mock_osi_alarm.h"
26 #include "test/mock/mock_osi_allocator.h"
27 #include "test/mock/mock_stack_gatt_api.h"
28
29 void BTA_dm_on_hw_on();
30 void BTA_dm_on_hw_off();
31
32 struct alarm_t {
alarm_talarm_t33 alarm_t(const char* name){};
34 int any_value;
35 };
36
37 class BtaSdpTest : public testing::Test {
38 protected:
SetUp()39 void SetUp() override {
40 test::mock::osi_allocator::osi_calloc.body = [](size_t size) -> void* {
41 return calloc(1, size);
42 };
43 test::mock::osi_allocator::osi_free.body = [](void* ptr) { free(ptr); };
44 test::mock::osi_alarm::alarm_new.body = [](const char* name) -> alarm_t* {
45 return new alarm_t(name);
46 };
47 test::mock::osi_alarm::alarm_free.body = [](alarm_t* alarm) {
48 delete alarm;
49 };
50 test::mock::stack_gatt_api::GATT_Register.body =
51 [](const bluetooth::Uuid& p_app_uuid128, const std::string name,
52 tGATT_CBACK* p_cb_info, bool eatt_support) { return 5; };
53
54 main_thread_start_up();
55 sync_main_handler();
56
57 BTA_dm_on_hw_on();
58 }
59
TearDown()60 void TearDown() override {
61 BTA_dm_on_hw_off();
62
63 sync_main_handler();
64 main_thread_shut_down();
65
66 test::mock::stack_gatt_api::GATT_Register = {};
67 test::mock::osi_allocator::osi_calloc = {};
68 test::mock::osi_allocator::osi_free = {};
69 test::mock::osi_alarm::alarm_new = {};
70 test::mock::osi_alarm::alarm_free = {};
71 }
72 };
73
74 class BtaSdpRegisteredTest : public BtaSdpTest {
75 protected:
SetUp()76 void SetUp() override {
77 BtaSdpTest::SetUp();
78 bta_sys_register(BTA_ID_DM_SEARCH, &bta_sys_reg);
79 }
80
TearDown()81 void TearDown() override {
82 bta_sys_deregister(BTA_ID_DM_SEARCH);
83 BtaSdpTest::TearDown();
84 }
85
86 tBTA_SYS_REG bta_sys_reg = {
__anonb4bf2f580602() 87 .evt_hdlr = [](BT_HDR_RIGID* p_msg) -> bool {
88 osi_free(p_msg);
89 return false;
90 },
__anonb4bf2f580702() 91 .disable = []() {},
92 };
93 };
94
TEST_F(BtaSdpTest,nop)95 TEST_F(BtaSdpTest, nop) {}
96
TEST_F(BtaSdpRegisteredTest,bta_dm_sdp_result_SDP_SUCCESS)97 TEST_F(BtaSdpRegisteredTest, bta_dm_sdp_result_SDP_SUCCESS) {
98 bta_dm_search_cb.service_index = BTA_MAX_SERVICE_ID;
99
100 tBTA_DM_MSG msg = {
101 .sdp_event =
102 {
103 .sdp_result = SDP_SUCCESS,
104 },
105 };
106 bta_dm_sdp_result(&msg);
107 }
108