Debian Codegraph @

olad/plugin_api/TestCommon.h

raw
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 * TestCommon.h
17 * Test fixture for various olad classes
18 * Copyright (C) 2005 Simon Newton
19 */
20
21 #ifndef OLAD_PLUGIN_API_TESTCOMMON_H_
22 #define OLAD_PLUGIN_API_TESTCOMMON_H_
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <memory>
25 #include <set>
26 #include <string>
27 #include <vector>
28
29 #include "ola/Clock.h"
30 #include "ola/DmxBuffer.h"
31 #include "ola/rdm/RDMCommand.h"
32 #include "ola/rdm/UIDSet.h"
33 #include "olad/Device.h"
34 #include "olad/Plugin.h"
35 #include "olad/Port.h"
36 #include "olad/plugin_api/PortManager.h"
37
38 /*
39 * Mock out an Input Port
40 */
41 class TestMockInputPort: public ola::BasicInputPort {
42 public:
43 TestMockInputPort(ola::AbstractDevice *parent,
44 unsigned int port_id,
45 const ola::PluginAdaptor *plugin_adaptor)
46 : ola::BasicInputPort(parent, port_id, plugin_adaptor) {}
47 ~TestMockInputPort() {}
48
49 std::string Description() const { return ""; }
50 bool WriteDMX(const ola::DmxBuffer &buffer) {
51 m_buffer = buffer;
52 return true;
53 }
54 const ola::DmxBuffer &ReadDMX() const { return m_buffer; }
55
56 private:
57 ola::DmxBuffer m_buffer;
58 };
59
60
61 /*
62 * Same as above but this supports priorities
63 */
64 class TestMockPriorityInputPort: public TestMockInputPort {
65 public:
66 TestMockPriorityInputPort(ola::AbstractDevice *parent,
67 unsigned int port_id,
68 const ola::PluginAdaptor *plugin_adaptor)
69 : TestMockInputPort(parent, port_id, plugin_adaptor),
70 m_inherited_priority(ola::dmx::SOURCE_PRIORITY_DEFAULT) {
71 }
72
73 uint8_t InheritedPriority() const {
74 return m_inherited_priority;
75 }
76
77 void SetInheritedPriority(uint8_t priority) {
78 m_inherited_priority = priority;
79 }
80
81 protected:
82 bool SupportsPriorities() const { return true; }
83
84 private:
85 uint8_t m_inherited_priority;
86 };
87
88
89 /*
90 * Mock out an OutputPort
91 */
92 class TestMockOutputPort: public ola::BasicOutputPort {
93 public:
94 TestMockOutputPort(ola::AbstractDevice *parent,
95 unsigned int port_id,
96 bool start_rdm_discovery_on_patch = false,
97 bool supports_rdm = false)
98 : ola::BasicOutputPort(parent, port_id, start_rdm_discovery_on_patch,
99 supports_rdm) {
100 }
101 ~TestMockOutputPort() {}
102
103 std::string Description() const { return ""; }
104 bool WriteDMX(const ola::DmxBuffer &buffer, uint8_t priority) {
105 m_buffer = buffer;
106 (void) priority;
107 return true;
108 }
109 const ola::DmxBuffer &ReadDMX() const { return m_buffer; }
110
111 private:
112 ola::DmxBuffer m_buffer;
113 };
114
115
116 /*
117 * Mock out an RDM OutputPort
118 */
119 class TestMockRDMOutputPort: public TestMockOutputPort {
120 public:
121 typedef ola::BaseCallback2<void,
122 const ola::rdm::RDMRequest*,
123 ola::rdm::RDMCallback*> RDMRequestHandler;
124
125 TestMockRDMOutputPort(ola::AbstractDevice *parent,
126 unsigned int port_id,
127 ola::rdm::UIDSet *uids,
128 bool start_rdm_discovery_on_patch = false,
129 RDMRequestHandler *rdm_handler = NULL)
130 : TestMockOutputPort(parent,
131 port_id,
132 start_rdm_discovery_on_patch,
133 true),
134 m_uids(uids),
135 m_rdm_handler(rdm_handler) {
136 }
137 ~TestMockRDMOutputPort() {}
138
139 void SetRDMHandler(RDMRequestHandler *handler) {
140 m_rdm_handler.reset(handler);
141 }
142
143 void SendRDMRequest(ola::rdm::RDMRequest *request,
144 ola::rdm::RDMCallback *callback) {
145 // if a RDMRequestHandler was provided use that.
146 if (m_rdm_handler.get()) {
147 m_rdm_handler->Run(request, callback);
148 return;
149 }
150
151 // otherwise just return a RDM_FAILED_TO_SEND
152 delete request;
153 RunRDMCallback(callback, ola::rdm::RDM_FAILED_TO_SEND);
154 }
155
156 void RunFullDiscovery(ola::rdm::RDMDiscoveryCallback *on_complete) {
157 on_complete->Run(*m_uids);
158 }
159
160 void RunIncrementalDiscovery(ola::rdm::RDMDiscoveryCallback *on_complete) {
161 on_complete->Run(*m_uids);
162 }
163
164 private:
165 ola::rdm::UIDSet *m_uids;
166 std::auto_ptr<RDMRequestHandler> m_rdm_handler;
167 };
168
169
170 /*
171 * Same as above but this supports priorities
172 */
173 class TestMockPriorityOutputPort: public TestMockOutputPort {
174 public:
175 TestMockPriorityOutputPort(ola::AbstractDevice *parent,
176 unsigned int port_id):
177 TestMockOutputPort(parent, port_id) {}
178
179 protected:
180 bool SupportsPriorities() const { return true; }
181 };
182
183
184 /*
185 * A mock device
186 */
187 class MockDevice: public ola::Device {
188 public:
189 MockDevice(ola::AbstractPlugin *owner, const std::string &name)
190 : Device(owner, name) {}
191 std::string DeviceId() const { return Name(); }
192 bool AllowLooping() const { return false; }
193 bool AllowMultiPortPatching() const { return false; }
194 };
195
196
197 /*
198 * A mock device with looping and multiport patching enabled
199 */
200 class MockDeviceLoopAndMulti: public ola::Device {
201 public:
202 MockDeviceLoopAndMulti(ola::AbstractPlugin *owner, const std::string &name)
203 : Device(owner, name) {}
204 std::string DeviceId() const { return Name(); }
205 bool AllowLooping() const { return true; }
206 bool AllowMultiPortPatching() const { return true; }
207 };
208
209
210 /*
211 * A mock plugin.
212 */
213 class TestMockPlugin: public ola::Plugin {
214 public:
215 TestMockPlugin(ola::PluginAdaptor *plugin_adaptor,
216 ola::ola_plugin_id plugin_id,
217 bool enabled = true)
218 : Plugin(plugin_adaptor),
219 m_is_running(false),
220 m_enabled(enabled),
221 m_id(plugin_id) {}
222
223 TestMockPlugin(ola::PluginAdaptor *plugin_adaptor,
224 ola::ola_plugin_id plugin_id,
225 const std::set<ola::ola_plugin_id> &conflict_set,
226 bool enabled = true)
227 : Plugin(plugin_adaptor),
228 m_is_running(false),
229 m_enabled(enabled),
230 m_id(plugin_id),
231 m_conflict_set(conflict_set) {}
232
233 void ConflictsWith(std::set<ola::ola_plugin_id> *conflict_set) const {
234 *conflict_set = m_conflict_set;
235 }
236 bool LoadPreferences() {
237 m_preferences = m_plugin_adaptor->NewPreference(PluginPrefix());
238 return true;
239 }
240 std::string PreferencesSource() const { return ""; }
241 bool IsEnabled() const { return m_enabled; }
242 bool StartHook() {
243 m_is_running = true;
244 return true;
245 }
246
247 bool StopHook() {
248 m_is_running = false;
249 return true;
250 }
251
252 std::string Name() const {
253 std::ostringstream str;
254 str << m_id;
255 return str.str();
256 }
257 std::string Description() const { return "bar"; }
258 ola::ola_plugin_id Id() const { return m_id; }
259 std::string PluginPrefix() const { return "test"; }
260
261 bool IsRunning() { return m_is_running; }
262
263 private:
264 bool m_is_running;
265 bool m_enabled;
266 ola::ola_plugin_id m_id;
267 std::set<ola::ola_plugin_id> m_conflict_set;
268 };
269
270
271 /**
272 * We mock this out so we can manipulate the wake up time. It was either this
273 * or the mocking the plugin adaptor.
274 */
275 class MockSelectServer: public ola::io::SelectServerInterface {
276 public:
277 explicit MockSelectServer(const ola::TimeStamp *wake_up)
278 : SelectServerInterface(),
279 m_wake_up(wake_up) {}
280 ~MockSelectServer() {}
281
282 bool AddReadDescriptor(ola::io::ReadFileDescriptor *descriptor) {
283 (void) descriptor;
284 return true;
285 }
286
287 bool AddReadDescriptor(ola::io::ConnectedDescriptor *descriptor,
288 bool delete_on_close = false) {
289 (void) descriptor;
290 (void) delete_on_close;
291 return true;
292 }
293
294 void RemoveReadDescriptor(ola::io::ReadFileDescriptor *descriptor) {
295 (void) descriptor;
296 }
297
298 void RemoveReadDescriptor(ola::io::ConnectedDescriptor *descriptor) {
299 (void) descriptor;
300 }
301
302 bool AddWriteDescriptor(ola::io::WriteFileDescriptor *descriptor) {
303 (void) descriptor;
304 return true;
305 }
306
307 void RemoveWriteDescriptor(ola::io::WriteFileDescriptor *descriptor) {
308 (void) descriptor;
309 }
310
311 ola::thread::timeout_id RegisterRepeatingTimeout(
312 unsigned int,
313 ola::Callback0<bool> *) {
314 return ola::thread::INVALID_TIMEOUT;
315 }
316 ola::thread::timeout_id RegisterRepeatingTimeout(
317 const ola::TimeInterval&,
318 ola::Callback0<bool>*) {
319 return ola::thread::INVALID_TIMEOUT;
320 }
321 ola::thread::timeout_id RegisterSingleTimeout(
322 unsigned int,
323 ola::SingleUseCallback0<void> *) {
324 return ola::thread::INVALID_TIMEOUT;
325 }
326 ola::thread::timeout_id RegisterSingleTimeout(
327 const ola::TimeInterval&,
328 ola::SingleUseCallback0<void> *) {
329 return ola::thread::INVALID_TIMEOUT;
330 }
331 void RemoveTimeout(ola::thread::timeout_id id) { (void) id; }
332 const ola::TimeStamp *WakeUpTime() const { return m_wake_up; }
333
334 void Execute(ola::BaseCallback0<void> *callback) {
335 callback->Run();
336 }
337
338 void DrainCallbacks() {}
339
340 private:
341 const ola::TimeStamp *m_wake_up;
342 };
343 #endif // OLAD_PLUGIN_API_TESTCOMMON_H_