1 /* 2 * Copyright 2021 HIMSA II K/S - www.himsa.com. 3 * Represented by EHIMA - www.ehima.com 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 #pragma once 19 20 #include <gmock/gmock.h> 21 22 #include "state_machine.h" 23 24 class MockBroadcastStateMachine : public bluetooth::le_audio::broadcaster::BroadcastStateMachine { 25 public: MockBroadcastStateMachine(bluetooth::le_audio::broadcaster::BroadcastStateMachineConfig cfg,bluetooth::le_audio::broadcaster::IBroadcastStateMachineCallbacks * cb,AdvertisingCallbacks * adv_cb)26 MockBroadcastStateMachine(bluetooth::le_audio::broadcaster::BroadcastStateMachineConfig cfg, 27 bluetooth::le_audio::broadcaster::IBroadcastStateMachineCallbacks* cb, 28 AdvertisingCallbacks* adv_cb) 29 : cfg(cfg), cb(cb), adv_cb(adv_cb) { 30 advertising_sid_ = ++instance_counter_; 31 32 ON_CALL(*this, Initialize).WillByDefault([this]() { 33 SetState(State::CONFIGURED); 34 this->cb->OnStateMachineCreateStatus(this->cfg.broadcast_id, result_); 35 return result_; 36 }); 37 38 ON_CALL(*this, ProcessMessage) 39 .WillByDefault( 40 [this](bluetooth::le_audio::broadcaster::BroadcastStateMachine::Message event, 41 const void* /*data*/) { 42 const void* sent_data = nullptr; 43 switch (event) { 44 case Message::START: 45 if (GetState() != State::STREAMING && result_) { 46 SetState(State::STREAMING); 47 this->cb->OnStateMachineEvent(this->cfg.broadcast_id, GetState(), 48 &this->cfg.config.subgroups); 49 } 50 break; 51 case Message::STOP: 52 if (GetState() != State::STOPPED && result_) { 53 SetState(State::STOPPED); 54 this->cb->OnStateMachineEvent(this->cfg.broadcast_id, GetState(), 55 nullptr); 56 } 57 break; 58 case Message::SUSPEND: 59 if (GetState() != State::CONFIGURED && result_) { 60 SetState(State::CONFIGURED); 61 this->cb->OnStateMachineEvent(this->cfg.broadcast_id, GetState(), 62 nullptr); 63 } 64 break; 65 }; 66 }); 67 68 ON_CALL(*this, GetBigConfig).WillByDefault(testing::ReturnRef(big_config_)); 69 70 ON_CALL(*this, RequestOwnAddress()).WillByDefault([this]() { 71 this->cb->OnOwnAddressResponse(this->cfg.broadcast_id, 0, RawAddress()); 72 }); 73 74 ON_CALL(*this, GetCodecConfig()) 75 .WillByDefault([this]() -> const std::vector<bluetooth::le_audio::broadcaster:: 76 BroadcastSubgroupCodecConfig>& { 77 return this->cfg.config.subgroups; 78 }); 79 80 ON_CALL(*this, GetBroadcastConfig()) 81 .WillByDefault( 82 [this]() -> const bluetooth::le_audio::broadcaster::BroadcastConfiguration& { 83 return this->cfg.config; 84 }); 85 86 ON_CALL(*this, GetBroadcastId()).WillByDefault([this]() -> bluetooth::le_audio::BroadcastId { 87 return this->cfg.broadcast_id; 88 }); 89 90 ON_CALL(*this, GetOwnAddress()).WillByDefault([this]() -> RawAddress { return this->addr_; }); 91 92 ON_CALL(*this, GetOwnAddressType()).WillByDefault([this]() -> uint8_t { 93 return this->addr_type_; 94 }); 95 96 ON_CALL(*this, GetPaInterval()).WillByDefault([this]() -> uint8_t { 97 return this->BroadcastStateMachine::GetPaInterval(); 98 }); 99 100 ON_CALL(*this, IsPublicBroadcast()).WillByDefault([this]() -> bool { 101 return this->cfg.is_public; 102 }); 103 104 ON_CALL(*this, GetBroadcastName()).WillByDefault([this]() -> std::string { 105 return this->cfg.broadcast_name; 106 }); 107 108 ON_CALL(*this, GetPublicBroadcastAnnouncement()) 109 .WillByDefault([this]() -> bluetooth::le_audio::PublicBroadcastAnnouncementData& { 110 return this->cfg.public_announcement; 111 }); 112 } 113 ~MockBroadcastStateMachine()114 ~MockBroadcastStateMachine() { cb->OnStateMachineDestroyed(this->cfg.broadcast_id); } 115 116 MOCK_METHOD((bool), Initialize, (), (override)); 117 MOCK_METHOD((const std::vector<bluetooth::le_audio::broadcaster::BroadcastSubgroupCodecConfig>&), 118 GetCodecConfig, (), (const override)); 119 MOCK_METHOD((std::optional<bluetooth::le_audio::broadcaster::BigConfig> const&), GetBigConfig, (), 120 (const override)); 121 MOCK_METHOD((bluetooth::le_audio::broadcaster::BroadcastStateMachineConfig const&), 122 GetStateMachineConfig, (), (const override)); 123 MOCK_METHOD((void), RequestOwnAddress, 124 (base::Callback<void(uint8_t /* address_type*/, RawAddress /*address*/)> cb), 125 (override)); 126 MOCK_METHOD((const bluetooth::le_audio::broadcaster::BroadcastConfiguration&), GetBroadcastConfig, 127 (), (const override)); 128 MOCK_METHOD((void), RequestOwnAddress, (), (override)); 129 MOCK_METHOD((RawAddress), GetOwnAddress, (), (override)); 130 MOCK_METHOD((uint8_t), GetOwnAddressType, (), (override)); 131 MOCK_METHOD((std::optional<bluetooth::le_audio::BroadcastCode>), GetBroadcastCode, (), 132 (const override)); 133 MOCK_METHOD((bluetooth::le_audio::BroadcastId), GetBroadcastId, (), (const override)); 134 MOCK_METHOD((bool), IsPublicBroadcast, (), (override)); 135 MOCK_METHOD((std::string), GetBroadcastName, (), (override)); 136 MOCK_METHOD((bluetooth::le_audio::BasicAudioAnnouncementData&), GetBroadcastAnnouncement, (), 137 (const override)); 138 MOCK_METHOD((bluetooth::le_audio::PublicBroadcastAnnouncementData&), 139 GetPublicBroadcastAnnouncement, (), (const override)); 140 MOCK_METHOD((void), UpdateBroadcastAnnouncement, 141 (bluetooth::le_audio::BasicAudioAnnouncementData announcement), (override)); 142 MOCK_METHOD((void), UpdatePublicBroadcastAnnouncement, 143 (uint32_t broadcast_id, const std::string& broadcast_name, 144 const bluetooth::le_audio::PublicBroadcastAnnouncementData& announcement), 145 (override)); 146 MOCK_METHOD((uint8_t), GetPaInterval, (), (const override)); 147 MOCK_METHOD((void), HandleHciEvent, (uint16_t event, void* data), (override)); 148 MOCK_METHOD((void), OnSetupIsoDataPath, (uint8_t status, uint16_t conn_handle), (override)); 149 MOCK_METHOD((void), OnRemoveIsoDataPath, (uint8_t status, uint16_t conn_handle), (override)); 150 MOCK_METHOD((void), ProcessMessage, 151 (bluetooth::le_audio::broadcaster::BroadcastStateMachine::Message event, 152 const void* data), 153 (override)); 154 MOCK_METHOD((uint8_t), GetAdvertisingSid, (), (const override)); 155 MOCK_METHOD((void), OnCreateAnnouncement, 156 (uint8_t advertising_sid, int8_t tx_power, uint8_t status), (override)); 157 MOCK_METHOD((void), OnEnableAnnouncement, (bool enable, uint8_t status), (override)); 158 MOCK_METHOD((void), OnUpdateAnnouncement, (uint8_t status), (override)); 159 160 bool result_ = true; 161 std::optional<bluetooth::le_audio::broadcaster::BigConfig> big_config_ = std::nullopt; 162 bluetooth::le_audio::broadcaster::BroadcastStateMachineConfig cfg; 163 bluetooth::le_audio::broadcaster::IBroadcastStateMachineCallbacks* cb; 164 AdvertisingCallbacks* adv_cb; SetExpectedState(BroadcastStateMachine::State state)165 void SetExpectedState(BroadcastStateMachine::State state) { SetState(state); } SetExpectedResult(bool result)166 void SetExpectedResult(bool result) { result_ = result; } SetExpectedBigConfig(std::optional<bluetooth::le_audio::broadcaster::BigConfig> big_cfg)167 void SetExpectedBigConfig(std::optional<bluetooth::le_audio::broadcaster::BigConfig> big_cfg) { 168 big_config_ = big_cfg; 169 } 170 171 static MockBroadcastStateMachine* last_instance_; 172 static uint8_t instance_counter_; GetLastInstance()173 static MockBroadcastStateMachine* GetLastInstance() { return last_instance_; } 174 }; 175