diff --git a/Modules/IGT/Testing/mitkTrackingDeviceSourceTest.cpp b/Modules/IGT/Testing/mitkTrackingDeviceSourceTest.cpp index 746bb50e19..a3cbd8b5ee 100644 --- a/Modules/IGT/Testing/mitkTrackingDeviceSourceTest.cpp +++ b/Modules/IGT/Testing/mitkTrackingDeviceSourceTest.cpp @@ -1,120 +1,121 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-05-13 14:52:01 +0200 (Mi, 13. Mai 2009) $ Version: $Revision: 17230 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #include "mitkTrackingDeviceSource.h" #include "mitkVirtualTrackingDevice.h" #include "mitkTestingMacros.h" #include #include "itksys/SystemTools.hxx" /**Documentation * test for the class "NavigationDataSource". */ int mitkTrackingDeviceSourceTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("TrackingDeviceSource"); // let's create an object of our class mitk::TrackingDeviceSource::Pointer mySource = mitk::TrackingDeviceSource::New(); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(mySource.IsNotNull(), "Testing instantiation"); mySource->SetTrackingDevice(NULL); MITK_TEST_CONDITION(mySource->GetTrackingDevice() == NULL, "Testing Set/GetTrackingDevice(NULL)"); MITK_TEST_CONDITION(mySource->GetNumberOfOutputs() == 0, "Testing GetNumberOfOutputs with NULL td"); MITK_TEST_FOR_EXCEPTION(std::invalid_argument, mySource->Connect()); MITK_TEST_FOR_EXCEPTION(std::invalid_argument, mySource->StartTracking()); mitk::VirtualTrackingDevice::Pointer tracker = mitk::VirtualTrackingDevice::New(); + tracker->SetRefreshRate(10); mySource->SetTrackingDevice(tracker); MITK_TEST_CONDITION(mySource->GetTrackingDevice() == tracker.GetPointer(), "Testing Set/GetTrackingDevice(tracker)"); MITK_TEST_CONDITION(mySource->GetNumberOfOutputs() == 0, "Testing GetNumberOfOutputs with no tool tracker"); tracker = mitk::VirtualTrackingDevice::New(); mitk::ReferenceCountWatcher::Pointer watch = new mitk::ReferenceCountWatcher(tracker); tracker->AddTool("T0"); tracker->AddTool("T1"); mySource->SetTrackingDevice(tracker); MITK_TEST_CONDITION(mySource->GetTrackingDevice() == tracker.GetPointer(), "Testing Set/GetTrackingDevice(tracker2)"); MITK_TEST_CONDITION(mySource->GetNumberOfOutputs() == 2, "Testing GetNumberOfOutputs with 2 tools tracker"); MITK_TEST_CONDITION(mySource->IsConnected() == false, "Testing IsConnected()"); mySource->Connect(); MITK_TEST_CONDITION(mySource->IsConnected() == true, "Testing Connect()/IsConnected()"); MITK_TEST_CONDITION(tracker->GetState() == mitk::TrackingDevice::Ready, "Testing Connect()/IsConnected() 2"); mySource->Disconnect(); MITK_TEST_CONDITION(mySource->IsConnected() == false, "Testing Disconnect()/IsConnected()"); MITK_TEST_CONDITION(tracker->GetState() == mitk::TrackingDevice::Setup, "Testing Disconnect()/IsConnected() 2"); mySource->Connect(); mySource->StartTracking(); MITK_TEST_CONDITION(mySource->IsConnected() == true, "Testing StartTracking()/IsConnected()"); MITK_TEST_CONDITION(mySource->IsTracking() == true, "Testing StartTracking()/IsTracking()"); MITK_TEST_CONDITION(tracker->GetState() == mitk::TrackingDevice::Tracking, "Testing StartTracking()/IsTracking() 2"); unsigned long modTime = mySource->GetMTime(); mySource->UpdateOutputInformation(); MITK_TEST_CONDITION(mySource->GetMTime() != modTime, "Testing if UpdateOutputInformation() modifies the object"); mitk::NavigationData* nd0 = mySource->GetOutput(0); nd0->Update(); mitk::NavigationData::PositionType pos = nd0->GetPosition(); unsigned long tmpMTime0 = nd0->GetMTime(); itksys::SystemTools::Delay(500); // allow the tracking thread to advance the tool position nd0->Update(); mitk::NavigationData::PositionType newPos = nd0->GetPosition(); if(nd0->GetMTime() == tmpMTime0) //tool not modified yet { MITK_TEST_CONDITION(mitk::Equal(newPos, pos) == true, "Testing if output changes on each update"); } else { MITK_TEST_CONDITION(mitk::Equal(newPos, pos) == false, "Testing if output changes on each update"); } mySource->StopTracking(); mySource->Disconnect(); tracker = mitk::VirtualTrackingDevice::New(); mySource->SetTrackingDevice(tracker); MITK_TEST_CONDITION(watch->GetReferenceCount() == 0, "Testing if reference to previous tracker object is released"); watch = NULL; MITK_TEST_FOR_EXCEPTION(std::runtime_error, mySource->StartTracking()); // new tracker, needs Connect() before StartTracking() mySource->Connect(); mySource->StartTracking(); // 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? try { mySource = NULL; // delete source tracker = NULL; // delete tracker --> both should not result in any exceptions or deadlocks } catch (...) { MITK_TEST_FAILED_MSG(<< "exception during destruction of source or tracker!"); } // always end with this! MITK_TEST_END(); } diff --git a/Modules/IGT/Testing/mitkVirtualTrackingDeviceTest.cpp b/Modules/IGT/Testing/mitkVirtualTrackingDeviceTest.cpp index 83a8e99848..ec23cde06d 100644 --- a/Modules/IGT/Testing/mitkVirtualTrackingDeviceTest.cpp +++ b/Modules/IGT/Testing/mitkVirtualTrackingDeviceTest.cpp @@ -1,147 +1,148 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2010-02-21 19:30:44 +0100 (So, 21 Feb 2010) $ Version: $Revision: 7837 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #include "mitkVirtualTrackingDevice.h" #include "itksys/SystemTools.hxx" #include "mitkTestingMacros.h" #include "mitkTrackingTool.h" #include int mitkVirtualTrackingDeviceTest(int /* argc */, char* /*argv*/[]) { // always start with this! MITK_TEST_BEGIN("VirtualTrackingDevice"); // let's create an object of our class mitk::VirtualTrackingDevice::Pointer tracker = mitk::VirtualTrackingDevice::New(); + tracker->SetRefreshRate(10); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(tracker.IsNotNull(),"Testing instantiation\n"); MITK_TEST_CONDITION_REQUIRED(tracker->GetState() == mitk::TrackingDevice::Setup ,"Checking tracking device state == setup.\n"); //CloseConnection MITK_TEST_CONDITION( (tracker->CloseConnection()), "Testing behavior of method CloseConnection()."); //StartTracking MITK_TEST_CONDITION( tracker->StartTracking() == false, "Testing behavior of method StartTracking()."); tracker->SetRefreshRate(43); MITK_TEST_CONDITION( tracker->GetRefreshRate() == 43, "Testing Set-/GetRefreshRate()"); MITK_TEST_CONDITION( tracker->GetToolCount() == 0, "Testing GetToolCount() before AddTool()"); MITK_TEST_CONDITION( tracker->AddTool("Tool0"), "Testing AddTool() for tool 0."); MITK_TEST_CONDITION( tracker->GetToolCount() == 1, "Testing GetToolCount() after AddTool()"); mitk::TrackingTool::Pointer tool = tracker->GetTool(0); MITK_TEST_CONDITION_REQUIRED( tool.IsNotNull(), "Testing GetTool() for tool 0."); MITK_TEST_CONDITION( tracker->GetToolByName("Tool0") == tool.GetPointer(), "Testing GetTool() equals GetToolByName() for tool 0."); mitk::ScalarType bounds[6] = {0.0, 10.0, 1.0, 20.0, 3.0, 30.0}; tracker->SetBounds(bounds); MITK_TEST_CONDITION( tracker->GetBounds()[0] == bounds[0] && tracker->GetBounds()[1] == bounds[1] && tracker->GetBounds()[2] == bounds[2] && tracker->GetBounds()[3] == bounds[3] && tracker->GetBounds()[4] == bounds[4] && tracker->GetBounds()[5] == bounds[5] , "Testing Set-/GetBounds()"); MITK_TEST_CONDITION( tracker->AddTool("Tool1"), "Testing AddTool() for tool 1."); MITK_TEST_CONDITION( tracker->GetToolCount() == 2, "Testing GetToolCount() after AddTool()"); tracker->SetToolSpeed(0, 0.1); // no exception expected tracker->SetToolSpeed(1, 0.1); // no exception expected MITK_TEST_FOR_EXCEPTION(std::invalid_argument, tracker->SetToolSpeed(2, 0.1)); // exception expected mitk::ScalarType lengthBefore = tracker->GetSplineChordLength(0); // no exception expected MITK_TEST_FOR_EXCEPTION(std::invalid_argument, tracker->GetSplineChordLength(2)); // exception expected MITK_TEST_CONDITION( tracker->OpenConnection() == true, "Testing OpenConnection()."); MITK_TEST_CONDITION( tracker->GetSplineChordLength(0) == lengthBefore, "Testing GetSplineChordLength() after initalization"); //StartTracking mitk::Point3D posBefore0; tool->GetPosition(posBefore0); unsigned long tmpMTimeBefore0 = tool->GetMTime(); mitk::Point3D posBefore1; tracker->GetToolByName("Tool1")->GetPosition(posBefore1); unsigned long tmpMTimeBefore1 = tracker->GetToolByName("Tool1")->GetMTime(); mitk::Point3D posAfter0; tool->GetPosition(posAfter0); MITK_TEST_CONDITION( mitk::Equal(posBefore0, posAfter0) == true, "Testing if position value is constant before StartTracking()"); MITK_TEST_CONDITION( tracker->StartTracking() == true, "Testing behavior of method StartTracking()."); itksys::SystemTools::Delay(500); // wait for tracking thread to start generating positions tool->GetPosition(posAfter0); if(tracker->GetToolByName("Tool0")->GetMTime() == tmpMTimeBefore0) //tool not modified yet { MITK_TEST_CONDITION( mitk::Equal(posBefore0, posAfter0) == true, "Testing if tracking is producing new position values in tool 0."); } else { MITK_TEST_CONDITION( mitk::Equal(posBefore0, posAfter0) == false, "Testing if tracking is producing new position values in tool 0."); } mitk::Point3D posAfter1; tracker->GetToolByName("Tool1")->GetPosition(posAfter1); if(tracker->GetToolByName("Tool1")->GetMTime() == tmpMTimeBefore1) //tool not modified yet { MITK_TEST_CONDITION( mitk::Equal(posBefore1, posAfter1) == true, "Testing if tracking is producing new position values in tool 1."); } else { MITK_TEST_CONDITION( mitk::Equal(posBefore1, posAfter1) == false, "Testing if tracking is producing new position values in tool 1."); } // add tool while tracking is in progress tracker->AddTool("while Running"); itksys::SystemTools::Delay(100); // wait for tracking thread to start generating positions tracker->GetToolByName("while Running")->GetPosition(posBefore0); unsigned long tmpBeforeMTime = tracker->GetToolByName("while Running")->GetMTime(); itksys::SystemTools::Delay(100); // wait for tracking thread to start generating positions tracker->GetToolByName("while Running")->GetPosition(posAfter0); unsigned long tmpAfterMTime = tracker->GetToolByName("while Running")->GetMTime(); MITK_INFO << "If this test fails, please reopen bug 8033 and commit this output: "; MITK_INFO << std::setprecision(16) << "Value of posBefore0 " << posBefore0; MITK_INFO << std::setprecision(16) << "Value of posAfter0 " << posAfter0; MITK_INFO << std::setprecision(16) << "tmpTime " << tmpBeforeMTime; MITK_INFO << std::setprecision(16) << "current time " << tmpAfterMTime; if(tmpAfterMTime == tmpBeforeMTime) //tool not modified yet { //hence the tool was not modified, the position has to be equal MITK_TEST_CONDITION( mitk::Equal(posBefore0, posAfter0) == true, "Testing if the position values for the 'while running' tool remain the same."); } else //tool was modified => position should have changed { MITK_TEST_CONDITION( mitk::Equal(posBefore0, posAfter0) == false, "Testing if tracking is producing new position values for 'while running' tool."); } // always end with this! MITK_TEST_END(); }