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 ~MockBroadcastStateMachine()90 ~MockBroadcastStateMachine() { 91 cb->OnStateMachineDestroyed(this->cfg.broadcast_id); 92 } 93 94 MOCK_METHOD((bool), Initialize, (), (override)); 95 MOCK_METHOD((const le_audio::broadcaster::BroadcastCodecWrapper&), 96 GetCodecConfig, (), (const override)); 97 MOCK_METHOD((std::optional<le_audio::broadcaster::BigConfig> const&), 98 GetBigConfig, (), (const override)); 99 MOCK_METHOD((le_audio::broadcaster::BroadcastStateMachineConfig const&), 100 GetStateMachineConfig, (), (const override)); 101 MOCK_METHOD( 102 (void), RequestOwnAddress, 103 (base::Callback<void(uint8_t /* address_type*/, RawAddress /*address*/)> 104 cb), 105 (override)); 106 MOCK_METHOD((void), RequestOwnAddress, (), (override)); 107 MOCK_METHOD((RawAddress), GetOwnAddress, (), (override)); 108 MOCK_METHOD((uint8_t), GetOwnAddressType, (), (override)); 109 MOCK_METHOD((std::optional<bluetooth::le_audio::BroadcastCode>), 110 GetBroadcastCode, (), (const override)); 111 MOCK_METHOD((bluetooth::le_audio::BroadcastId), GetBroadcastId, (), 112 (const override)); 113 MOCK_METHOD((bluetooth::le_audio::BasicAudioAnnouncementData&), 114 GetBroadcastAnnouncement, (), (const override)); 115 MOCK_METHOD((void), UpdateBroadcastAnnouncement, 116 (bluetooth::le_audio::BasicAudioAnnouncementData announcement), 117 (override)); 118 MOCK_METHOD((uint8_t), GetPaInterval, (), (const override)); 119 MOCK_METHOD((void), HandleHciEvent, (uint16_t event, void* data), (override)); 120 MOCK_METHOD((void), OnSetupIsoDataPath, 121 (uint8_t status, uint16_t conn_handle), (override)); 122 MOCK_METHOD((void), OnRemoveIsoDataPath, 123 (uint8_t status, uint16_t conn_handle), (override)); 124 MOCK_METHOD((void), ProcessMessage, 125 (le_audio::broadcaster::BroadcastStateMachine::Message event, 126 const void* data), 127 (override)); 128 MOCK_METHOD((uint8_t), GetAdvertisingSid, (), (const override)); 129 130 bool result_ = true; 131 std::optional<le_audio::broadcaster::BigConfig> big_config_ = std::nullopt; 132 le_audio::broadcaster::BroadcastStateMachineConfig cfg; 133 le_audio::broadcaster::IBroadcastStateMachineCallbacks* cb; SetExpectedState(BroadcastStateMachine::State state)134 void SetExpectedState(BroadcastStateMachine::State state) { SetState(state); } SetExpectedResult(bool result)135 void SetExpectedResult(bool result) { result_ = result; } SetExpectedBigConfig(std::optional<le_audio::broadcaster::BigConfig> big_cfg)136 void SetExpectedBigConfig( 137 std::optional<le_audio::broadcaster::BigConfig> big_cfg) { 138 big_config_ = big_cfg; 139 } 140 141 static MockBroadcastStateMachine* last_instance_; 142 static uint8_t instance_counter_; GetLastInstance()143 static MockBroadcastStateMachine* GetLastInstance() { return last_instance_; } 144 }; 145