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