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