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