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