Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkIGTLDeviceSourceTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
19 
20 #include "mitkTestingMacros.h"
22 #include "itksys/SystemTools.hxx"
23 
27 int mitkTrackingDeviceSourceTest(int /* argc */, char* /*argv*/[])
28 {
29  MITK_TEST_BEGIN("TrackingDeviceSource");
30 
31  // let's create an object of our class
33 
34  // first test: did this work?
35  // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since
36  // it makes no sense to continue without an object.
37  MITK_TEST_CONDITION_REQUIRED(mySource.IsNotNull(), "Testing instantiation");
38 
39  mySource->SetTrackingDevice(NULL);
40  MITK_TEST_CONDITION(mySource->GetTrackingDevice() == NULL, "Testing Set/GetTrackingDevice(NULL)");
41  MITK_TEST_CONDITION(mySource->GetNumberOfOutputs() == 0, "Testing GetNumberOfOutputs with NULL td");
42  MITK_TEST_FOR_EXCEPTION(std::invalid_argument, mySource->Connect());
43  MITK_TEST_FOR_EXCEPTION(std::invalid_argument, mySource->StartTracking());
44 
46  tracker->SetRefreshRate(10);
47 
48 
49  mySource->SetTrackingDevice(tracker);
50  MITK_TEST_CONDITION(mySource->GetTrackingDevice() == tracker.GetPointer(), "Testing Set/GetTrackingDevice(tracker)");
51  MITK_TEST_CONDITION(mySource->GetNumberOfOutputs() == 0, "Testing GetNumberOfOutputs with no tool tracker");
54 
55  tracker->AddTool("T0");
56  tracker->AddTool("T1");
57  mySource->SetTrackingDevice(tracker);
58  MITK_TEST_CONDITION(mySource->GetTrackingDevice() == tracker.GetPointer(), "Testing Set/GetTrackingDevice(tracker2)");
59  MITK_TEST_CONDITION(mySource->GetNumberOfOutputs() == 2, "Testing GetNumberOfOutputs with 2 tools tracker");
60  MITK_TEST_CONDITION(mySource->IsConnected() == false, "Testing IsConnected()");
61  mySource->Connect();
62  MITK_TEST_CONDITION(mySource->IsConnected() == true, "Testing Connect()/IsConnected()");
63  MITK_TEST_CONDITION(tracker->GetState() == mitk::TrackingDevice::Ready, "Testing Connect()/IsConnected() 2");
64  mySource->Disconnect();
65  MITK_TEST_CONDITION(mySource->IsConnected() == false, "Testing Disconnect()/IsConnected()");
66  MITK_TEST_CONDITION(tracker->GetState() == mitk::TrackingDevice::Setup, "Testing Disconnect()/IsConnected() 2");
67 
68  mySource->Connect();
69  mySource->StartTracking();
70  MITK_TEST_CONDITION(mySource->IsConnected() == true, "Testing StartTracking()/IsConnected()");
71  MITK_TEST_CONDITION(mySource->IsTracking() == true, "Testing StartTracking()/IsTracking()");
72  MITK_TEST_CONDITION(tracker->GetState() == mitk::TrackingDevice::Tracking, "Testing StartTracking()/IsTracking() 2");
73 
74  unsigned long modTime = mySource->GetMTime();
75  mySource->UpdateOutputInformation();
76  MITK_TEST_CONDITION(mySource->GetMTime() != modTime, "Testing if UpdateOutputInformation() modifies the object");
77 
78  //test getOutput()
79  mitk::NavigationData* nd0 = mySource->GetOutput();
80  MITK_TEST_CONDITION_REQUIRED(nd0!=NULL,"Testing GetOutput() [1]");
81  nd0 = mySource->GetOutput(nd0->GetName());
82  MITK_TEST_CONDITION_REQUIRED(nd0!=NULL,"Testing GetOutput() [2]");
83 
84  //test getOutputIndex()
85  MITK_TEST_CONDITION(mySource->GetOutputIndex(nd0->GetName())==0,"Testing GetOutputIndex()");
86 
87  //test GraftNthOutput()
89  mySource->GraftNthOutput(1,nd0);
90  ndCopy = mySource->GetOutput(1);
91  MITK_TEST_CONDITION(std::string(ndCopy->GetName())==std::string(nd0->GetName()),"Testing GraftNthOutput()");
92 
93  //test GetParameters()
94  mitk::PropertyList::ConstPointer p = mySource->GetParameters();
95  MITK_TEST_CONDITION(p.IsNotNull(),"Testing GetParameters()");
96 
97  nd0->Update();
98  mitk::NavigationData::PositionType pos = nd0->GetPosition();
99  unsigned long tmpMTime0 = nd0->GetMTime();
100  itksys::SystemTools::Delay(500); // allow the tracking thread to advance the tool position
101  nd0->Update();
102  mitk::NavigationData::PositionType newPos = nd0->GetPosition();
103  if(nd0->GetMTime() == tmpMTime0) //tool not modified yet
104  {
105  MITK_TEST_CONDITION(mitk::Equal(newPos, pos) == true, "Testing if output changes on each update");
106  }
107  else
108  {
109  MITK_TEST_CONDITION(mitk::Equal(newPos, pos) == false, "Testing if output changes on each update");
110  }
111 
112  mySource->StopTracking();
113  mySource->Disconnect();
114 
116  mySource->SetTrackingDevice(tracker);
117  MITK_TEST_CONDITION(watch->GetReferenceCount() == 0, "Testing if reference to previous tracker object is released");
118  watch = NULL;
119 
120  MITK_TEST_FOR_EXCEPTION(std::runtime_error, mySource->StartTracking()); // new tracker, needs Connect() before StartTracking()
121 
122  mySource->Connect();
123  mySource->StartTracking();
124  // itksys::SystemTools::Delay(800); // wait for tracking thread to start properly //DEBUG ONLY --> race condition. will the thread start before the object is destroyed? --> maybe hold a smartpointer?
125  try
126  {
127  mySource = NULL; // delete source
128  tracker = NULL; // delete tracker --> both should not result in any exceptions or deadlocks
129  }
130  catch (...)
131  {
132  MITK_TEST_FAILED_MSG(<< "exception during destruction of source or tracker!");
133  }
134 
135  // always end with this!
136  MITK_TEST_END();
137 }
int mitkTrackingDeviceSourceTest(int, char *[])
static Pointer New()
itk::SmartPointer< Self > Pointer
Navigation Data.
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
#define MITK_TEST_FOR_EXCEPTION(EXCEPTIONCLASS, STATEMENT)
Simplified version of MITK_TEST_FOR_EXCEPTION_BEGIN / END for a single statement. ...
Keeps track of the reference count of an object even if it is destroyed.
#define MITK_TEST_CONDITION(COND, MSG)
#define MITK_TEST_FAILED_MSG(MSG)
Fail and finish test with message MSG.
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
and MITK_TEST_END()