1 /*
2  * Copyright 2023 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 #pragma once
18 
19 #include <bluetooth/log.h>
20 #include <gmock/gmock.h>
21 
22 #include "bta/dm/bta_dm_int.h"
23 #include "bta/include/bta_api.h"
24 #include "bta/sys/bta_sys.h"
25 #include "osi/include/allocator.h"
26 #include "stack/include/btm_client_interface.h"
27 #include "stack/include/btm_status.h"
28 #include "stack/include/main_thread.h"
29 #include "test/common/main_handler.h"
30 #include "test/common/mock_functions.h"
31 #include "test/fake/fake_osi.h"
32 #include "test/mock/mock_main_shim_entry.h"
33 #include "test/mock/mock_stack_btm_interface.h"
34 #include "test/mock/mock_stack_gatt_api.h"
35 #include "test/mock/mock_stack_rnr_interface.h"
36 
37 constexpr tGATT_IF kGattRegisteredIf = 5;
38 
39 void BTA_dm_on_hw_on();
40 void BTA_dm_on_hw_off();
41 
42 extern tBTA_DM_CB bta_dm_cb;
43 
44 // Set up base mocks and fakes
45 class BtaWithFakesTest : public ::testing::Test {
46 protected:
SetUp()47   void SetUp() override {
48     bta_dm_cb = {};
49     fake_osi_ = std::make_unique<test::fake::FakeOsi>();
50   }
51 
TearDown()52   void TearDown() override { fake_osi_.reset(); }
53   std::unique_ptr<test::fake::FakeOsi> fake_osi_;
54 };
55 
56 // Setup any default or optional mocks
57 class BtaWithMocksTest : public BtaWithFakesTest {
58 protected:
SetUp()59   void SetUp() override {
60     BtaWithFakesTest::SetUp();
61     reset_mock_function_count_map();
62     reset_mock_btm_client_interface();
63     ASSERT_NE(get_btm_client_interface().lifecycle.btm_init, nullptr);
64     ASSERT_NE(get_btm_client_interface().lifecycle.btm_free, nullptr);
65 
66     bluetooth::hci::testing::mock_controller_ = &mock_controller_;
67     bluetooth::testing::stack::rnr::set_interface(&mock_stack_rnr_interface_);
68 
69     test::mock::stack_gatt_api::GATT_Register.body =
70             [](const bluetooth::Uuid& /*p_app_uuid128*/, const std::string /*name*/,
71                tGATT_CBACK* /*p_cb_info*/,
72                bool /*eatt_support*/) -> tGATT_IF { return kGattRegisteredIf; };
73     mock_btm_client_interface.eir.BTM_GetEirSupportedServices =
74             [](uint32_t* /*p_eir_uuid*/, uint8_t** /*p*/, uint8_t /*max_num_uuid16*/,
75                uint8_t* /*p_num_uuid16*/) -> uint8_t { return 0; };
76     mock_btm_client_interface.eir.BTM_WriteEIR = [](BT_HDR* p_buf) -> tBTM_STATUS {
77       osi_free(p_buf);
78       return tBTM_STATUS::BTM_SUCCESS;
79     };
80     mock_btm_client_interface.security.BTM_SecRegister =
81             [](const tBTM_APPL_INFO* /*p_cb_info*/) -> bool { return true; };
82   }
83 
TearDown()84   void TearDown() override {
85     test::mock::stack_gatt_api::GATT_Register = {};
86 
87     mock_btm_client_interface.eir.BTM_GetEirSupportedServices = {};
88     mock_btm_client_interface.eir.BTM_WriteEIR = {};
89 
90     bluetooth::testing::stack::rnr::reset_interface();
91     bluetooth::hci::testing::mock_controller_ = nullptr;
92 
93     BtaWithFakesTest::TearDown();
94   }
95 
96   bluetooth::hci::testing::MockControllerInterface mock_controller_;
97   bluetooth::testing::stack::rnr::Mock mock_stack_rnr_interface_;
98 };
99 
100 class BtaWithContextTest : public BtaWithMocksTest {
101 protected:
SetUp()102   void SetUp() override {
103     BtaWithMocksTest::SetUp();
104     main_thread_start_up();
105     post_on_bt_main([]() { bluetooth::log::info("Main thread started up"); });
106   }
TearDown()107   void TearDown() override {
108     post_on_bt_main([]() { bluetooth::log::info("Main thread shutting down"); });
109     main_thread_shut_down();
110     BtaWithMocksTest::TearDown();
111   }
112 };
113 
114 class BtaWithHwOnTest : public BtaWithContextTest {
115 protected:
SetUp()116   void SetUp() override {
117     BtaWithContextTest::SetUp();
118     BTA_dm_on_hw_on();
119   }
120 
TearDown()121   void TearDown() override {
122     BTA_dm_on_hw_off();
123     BtaWithContextTest::TearDown();
124   }
125 };
126