opensurgsim
MockObjects.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest LLC.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_UNITTESTS_MOCKOBJECTS_H
17 #define SURGSIM_FRAMEWORK_UNITTESTS_MOCKOBJECTS_H
18 
19 #include <memory>
20 
22 #include "SurgSim/Framework/BasicThread.h"
23 #include "SurgSim/Framework/Behavior.h"
24 #include "SurgSim/Framework/Component.h"
25 #include "SurgSim/Framework/ComponentManager.h"
26 #include "SurgSim/Framework/Representation.h"
27 #include "SurgSim/Framework/Runtime.h"
28 #include "SurgSim/Framework/SamplingMetricBase.h"
29 #include "SurgSim/Framework/Scene.h"
30 #include "SurgSim/Framework/SceneElement.h"
31 
34 {
35 public:
36  explicit MockSceneElement(const std::string& name = "MockSceneElement") :
37  SceneElement(name),
38  didInit(false),
39  didUpdate(false),
40  didLateUpdate(false),
41  didFixedUpdate(false)
42  {
43  m_localRuntime = std::make_shared<SurgSim::Framework::Runtime>();
44  setRuntime(m_localRuntime);
45 
46  m_localScene = m_localRuntime->getScene();
47  setScene(m_localScene);
48  }
49 
50  virtual void update(double dt)
51  {
52  didUpdate = true;
53  }
54  virtual void lateUpdate(double dt)
55  {
56  didLateUpdate = true;
57  }
58  virtual void fixedRateUpdate(double dt)
59  {
60  didFixedUpdate = true;
61  }
62 
63  virtual bool doInitialize()
64  {
65  didInit = true;
66  return didInit;
67  };
68 
69  bool didInit;
70  bool didUpdate;
71  bool didLateUpdate;
72  bool didFixedUpdate;
73 private:
74  std::shared_ptr<SurgSim::Framework::Runtime> m_localRuntime;
75  std::shared_ptr<SurgSim::Framework::Scene> m_localScene;
76 
77 };
78 
80 {
81 public:
82  explicit MockThread(int runCount = -1) :
83  count(runCount),
84  totalTime(0.0),
85  didBeforeStop(false),
86  runIndefinetly(false),
87  didInitialize(false),
88  didStartUp(false)
89  {
90  }
91 
92  virtual ~MockThread()
93  {
94  }
95 
96  int count;
97  double totalTime;
98 
99  bool didBeforeStop;
100  bool runIndefinetly;
101  bool didInitialize;
102  bool didStartUp;
103 
104 private:
105  virtual bool doInitialize()
106  {
107  didInitialize = true;
108  return true;
109  };
110 
111  virtual bool doStartUp()
112  {
113  didStartUp = true;
114  return true;
115  };
116 
117  virtual bool doUpdate(double dt)
118  {
119  --count;
120  totalTime += dt;
121 
122  return count != 0;
123  };
124 
125  virtual void doBeforeStop()
126  {
127  didBeforeStop = true;
128  };
129 };
130 
131 
133 {
134 public:
135  explicit MockComponent(const std::string& name, bool succeedInit = true, bool succeedWakeUp = true);
136 
137  SURGSIM_CLASSNAME(MockComponent);
138 
139  virtual ~MockComponent();
140 
141  bool doInitialize() override;
142  bool doWakeUp() override;
143  void doRetire() override;
144 
145  bool getSucceedWithInit() const;
146  void setSucceedWithInit(bool val);
147 
148  bool getSucceedWithWakeUp() const;
149  void setSucceedWithWakeUp(bool val);
150 
151  void eventCallback(const SurgSim::Framework::Messenger::Event& event);
152 
153  bool succeedWithInit;
154  bool succeedWithWakeUp;
155  bool didWakeUp;
156  bool didInit;
157  bool didRetire;
158  bool didCallback;
159 };
160 
162 {
163 public:
164  explicit MockBehavior(const std::string& name, bool succeedInit = true, bool succeedWakeUp = true) :
165  Behavior(name),
166  succeedWithInit(succeedInit),
167  succeedWithWakeUp(succeedWakeUp),
168  updateCount(0)
169  {
170  }
171  virtual ~MockBehavior()
172  {
173  }
174 
175  virtual bool doInitialize()
176  {
177  return succeedWithInit;
178  }
179 
180  virtual bool doWakeUp()
181  {
182  return succeedWithWakeUp;
183  }
184 
185  virtual void update(double dt)
186  {
187  updateCount++;
188  }
189 
190  bool succeedWithInit;
191  bool succeedWithWakeUp;
192  int updateCount;
193 };
194 
195 
197 {
198 public:
199  explicit MockManager(bool succeedInit = true, bool succeedStartup = true) :
200  succeedInit(succeedInit),
201  succeedStartup(succeedStartup),
202  didInitialize(false),
203  didStartUp(false),
204  didBeforeStop(false),
205  count(0)
206  {
207  }
208 
209  virtual ~MockManager()
210  {
211  }
212 
213  int getType() const override
214  {
215  return SurgSim::Framework::MANAGER_TYPE_NONE;
216  }
217 
218  const std::vector<std::shared_ptr<MockComponent>>& getComponents()
219  {
220  return m_components;
221  }
222 
223  bool testTryAddComponent(const std::shared_ptr<SurgSim::Framework::Component>& component)
224  {
225  return executeAdditions(component);
226  }
227 
228  bool testTryRemoveComponent(const std::shared_ptr<SurgSim::Framework::Component>& component)
229  {
230  return executeRemovals(component);
231  }
232 
233  void testProcessComponents()
234  {
235  processComponents();
236  }
237 
238  bool succeedInit;
239  bool succeedStartup;
240  bool didInitialize;
241  bool didStartUp;
242  bool didBeforeStop;
243  int count;
244 
245 private:
246  virtual bool doInitialize()
247  {
248  didInitialize = true;
249  return succeedInit;
250  };
251  virtual bool doStartUp()
252  {
253  didStartUp = true;
254  return succeedStartup;
255  };
256  virtual bool doUpdate(double dt)
257  {
258  ++count;
259  processComponents();
260  return true;
261  };
262 
263  virtual void doBeforeStop()
264  {
265  didBeforeStop = true;
266  retireComponents(m_components);
267  ComponentManager::doBeforeStop();
268  }
269 
270  bool executeAdditions(const std::shared_ptr<SurgSim::Framework::Component>& component) override
271  {
272  return tryAddComponent(component, &m_components) != nullptr;
273  }
274 
275  bool executeRemovals(const std::shared_ptr<SurgSim::Framework::Component>& component) override
276  {
277  return tryRemoveComponent(component, &m_components);
278  }
279 
280 
281  std::vector<std::shared_ptr<MockComponent>> m_components;
282 
283 
284 };
285 
287 {
288 public:
294  explicit MockRepresentation(const std::string& name) : SurgSim::Framework::Representation(name),
295  m_didInit(false),
296  m_didWakeUp(false)
297  {
298  }
299 
300  SURGSIM_CLASSNAME(MockRepresentation);
301 
303  bool didInit() const
304  {
305  return m_didInit;
306  }
307 
309  bool didWakeUp() const
310  {
311  return m_didWakeUp;
312  }
313 
314 private:
316  bool m_didInit;
318  bool m_didWakeUp;
319 
322  virtual bool doInitialize()
323  {
324  m_didInit = true;
325  return true;
326  }
327 
330  virtual bool doWakeUp()
331  {
332  m_didWakeUp = true;
333  return true;
334  }
335 };
336 
337 
339 {
340 public:
343  explicit MockSamplingMetric(const std::string& name,
344  bool ableToMeasure = true,
345  double initialMeasurement = 0.0) :
346  SurgSim::Framework::SamplingMetricBase(name),
347  m_canMeasure(ableToMeasure),
348  m_measurement(initialMeasurement)
349  {
350  }
351 
352  SURGSIM_CLASSNAME(MockSamplingMetric);
353 
358 
366 
367 private:
369  bool m_canMeasure;
370 
372  double m_measurement;
373 
376  bool canMeasure(double dt)
377  {
378  return m_canMeasure;
379  }
380 
381  double performMeasurement(double dt)
382  {
383  return ++m_measurement;
384  }
385 };
386 
387 #endif // SURGSIM_FRAMEWORK_UNITTESTS_MOCKOBJECTS_H
bool didWakeUp() const
Returns true if the representation has been woken up, otherwise false.
Definition: MockObjects.h:309
Class to catch the calls made to the scene element, does nothing.
Definition: MockObjects.h:33
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
SurgSim::Framework::SamplingMetricBase::MeasurementsType MeasurementsType
Type of the cumulative entries data structure.
Definition: MockObjects.h:365
Definition: MockObjects.h:132
Representation class for testing.
Definition: MockObjects.h:286
void setRuntime(std::weak_ptr< Runtime > runtime)
Sets the Runtime.
Definition: SceneElement.cpp:238
std::vector< std::shared_ptr< Component > > getComponents() const
Gets all the components of this SceneElement.
Definition: SceneElement.cpp:195
std::pair< double, double > MeasurementEntryType
Type of the individual entries in the measurement data structure.
Definition: SamplingMetricBase.h:55
SurgSim::Framework::SamplingMetricBase::MeasurementEntryType MeasurementEntryType
Type of the individual entries in the measurement data structure.
Definition: MockObjects.h:357
virtual bool doInitialize()
Interface to be implemented by derived classes.
Definition: MockObjects.h:175
Base Component Manager class.
Definition: ComponentManager.h:49
Datastructure to contain basic event data.
Definition: Messenger.h:45
Manager class for testing.
Definition: MockObjects.h:196
Component is the main interface class to pass information to the system managers each will decide whe...
Definition: Component.h:42
SamplingMetricBase provides a base class to support metric development.
Definition: SamplingMetricBase.h:45
Definition: MockObjects.h:338
virtual bool doInitialize()
Method to initialize this SceneElement.
Definition: MockObjects.h:63
virtual bool doWakeUp()
Interface to be implemented by derived classes.
Definition: MockObjects.h:180
The header that provides the assertion API.
Basic thread implementation, tries to maintain a constant rate, supplies startup an initialization...
Definition: BasicThread.h:48
Behaviors perform actions.
Definition: Behavior.h:40
std::deque< MeasurementEntryType > MeasurementsType
Type of the cumulative entries data structure.
Definition: SamplingMetricBase.h:63
Representations are manifestations of a SceneElement.
Definition: Representation.h:33
Definition: MockObjects.h:79
bool didInit() const
Returns true if the representation has been initialized, otherwise false.
Definition: MockObjects.h:303
MockSamplingMetric(const std::string &name, bool ableToMeasure=true, double initialMeasurement=0.0)
Constructor.
Definition: MockObjects.h:343
SceneElement is the basic part of a scene, it is a container of components.
Definition: SceneElement.h:51
int getType() const override
Returns the type of Manager.
Definition: MockObjects.h:213
Definition: MockObjects.h:161
MockRepresentation(const std::string &name)
Constructor.
Definition: MockObjects.h:294
virtual void update(double dt)
Update the behavior.
Definition: MockObjects.h:185
SceneElement(const std::string &name)
Constructor.
Definition: SceneElement.cpp:34
void setScene(std::weak_ptr< Scene > scene)
Sets the Scene.
Definition: SceneElement.cpp:224