diff --git a/Modules/IGT/Testing/mitkNavigationDataSequentialPlayerTest.cpp b/Modules/IGT/Testing/mitkNavigationDataSequentialPlayerTest.cpp index c7ba6139a3..277845cfa9 100644 --- a/Modules/IGT/Testing/mitkNavigationDataSequentialPlayerTest.cpp +++ b/Modules/IGT/Testing/mitkNavigationDataSequentialPlayerTest.cpp @@ -1,268 +1,223 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include "mitkTestingMacros.h" #include #include //foe exceptions #include "mitkIGTException.h" #include "mitkIGTIOException.h" const char* XML_STRING = "" "" "" "" "" "" "" ""; +const char* XML_INVALID_TESTSTRING = + "< ToolCount=\"2\">" + "" + "" + "" + "" + "" + "" + ""; + vnl_vector tTool0Snapshot1(3); vnl_vector tTool1Snapshot2(3); mitk::Quaternion qTool0Snapshot0; mitk::Quaternion qTool1Snapshot1; mitk::NavigationDataSequentialPlayer::Pointer player( mitk::NavigationDataSequentialPlayer::New()); -void runLoop() +bool runLoop() { + + + bool success = true; mitk::NavigationData::Pointer nd0; mitk::NavigationData::Pointer nd1; for(unsigned int i=0; iGetNumberOfSnapshots();++i) { player->Update(); nd0 = player->GetOutput(); nd1 = player->GetOutput(1); // test some values - MITK_TEST_CONDITION_REQUIRED(nd0.IsNotNull(), "nd0.IsNotNull()"); - MITK_TEST_CONDITION_REQUIRED(nd1.IsNotNull(), "nd1.IsNotNull()"); + if(nd0.IsNull() || nd1.IsNull()) return false; if(i==0) { - MITK_TEST_CONDITION(qTool0Snapshot0.as_vector() == nd0->GetOrientation().as_vector(), - "qTool0Snapshot0.as_vector() == nd0->GetOrientation().as_vector()"); + if (!(qTool0Snapshot0.as_vector() == nd0->GetOrientation().as_vector())) {success = false;} } else if(i==1) { - MITK_TEST_CONDITION(tTool0Snapshot1 == nd0->GetPosition().GetVnlVector(), - "tTool0Snapshot1 == nd0->GetPosition().GetVnlVector()"); - - MITK_TEST_CONDITION(qTool1Snapshot1.as_vector() == nd1->GetOrientation().as_vector(), - "qTool1Snapshot1.as_vector() == nd1->GetOrientation().as_vector()"); + if (!(tTool0Snapshot1 == nd0->GetPosition().GetVnlVector())) {success = false;} + else if (!(qTool1Snapshot1.as_vector() == nd1->GetOrientation().as_vector())) {success = false;} } else if(i==2) // should be repeated { - MITK_TEST_CONDITION(tTool1Snapshot2 == nd1->GetPosition().GetVnlVector(), - "tTool1Snapshot2 == nd1->GetPosition().GetVnlVector()"); + if (!(tTool1Snapshot2 == nd1->GetPosition().GetVnlVector())) {success = false;} } } + return success; } +void TestStandardWorkflow() +{ + // create test values valid for the xml data above + tTool0Snapshot1[0] = -336.65; + tTool0Snapshot1[1] = 138.5; + tTool0Snapshot1[2]= -2061.07; + tTool1Snapshot2[0] = -56.93; + tTool1Snapshot2[1] = 233.79; + tTool1Snapshot2[2]= -2042.6; + vnl_vector_fixed qVec; + qVec[0] = 0.0085; + qVec[1] = -0.0576; + qVec[2]= -0.0022; + qVec[3]= 0.9982; + qTool0Snapshot0 = mitk::Quaternion(qVec); + qVec[0] = 0.4683; + qVec[1] = 0.0188; + qVec[2]= -0.8805; + qVec[3]= 0.0696; + qTool1Snapshot1 = mitk::Quaternion(qVec); + + //test SetXMLString() + player->SetXMLString(XML_STRING); + MITK_TEST_CONDITION_REQUIRED(player->GetNumberOfSnapshots() == 3,"Testing method SetXMLString with 3 navigation datas."); + + //rest repeat + player->SetRepeat(true); + MITK_TEST_CONDITION_REQUIRED(runLoop(),"Testing first run."); + MITK_TEST_CONDITION_REQUIRED(runLoop(),"Testing second run."); //repeat is on should work a second time + + // now test the go to snapshot function + player->GoToSnapshot(3); + mitk::NavigationData::Pointer nd1 = player->GetOutput(1); + MITK_TEST_CONDITION(tTool1Snapshot2 == nd1->GetPosition().GetVnlVector(), + "Testing GoToSnapshot() [1]"); + + player->GoToSnapshot(1); + mitk::NavigationData::Pointer nd0 = player->GetOutput(0); + MITK_TEST_CONDITION(qTool0Snapshot0.as_vector() == nd0->GetOrientation().as_vector(), + "Testing GoToSnapshot() [2]"); + + player->GoToSnapshot(3); + + // and a third time + MITK_TEST_CONDITION_REQUIRED(runLoop(),"Tested if repeat works again."); + +} void TestSetFileNameException() { //testing exception if file name hasnt been set mitk::NavigationDataSequentialPlayer::Pointer myTestPlayer = mitk::NavigationDataSequentialPlayer::New(); bool exceptionThrown=false; try { myTestPlayer->SetFileName(""); } catch(mitk::IGTIOException) { exceptionThrown=true; MITK_TEST_OUTPUT(<<"Tested exception for the case when file version is wrong in SetFileName. Application should not crash."); } MITK_TEST_CONDITION_REQUIRED(exceptionThrown, "Testing SetFileName method if exception (if file name hasnt been set) was thrown."); //testing ReInItXML method if data element is not found mitk::NavigationDataSequentialPlayer::Pointer myTestPlayer1 = mitk::NavigationDataSequentialPlayer::New(); std::string file = mitk::StandardFileLocations::GetInstance()->FindFile("NavigationDataTestDataInvalidTags.xml", "Modules/IGT/Testing/Data"); bool exceptionThrown1=false; try { myTestPlayer1->SetFileName(file); } catch(mitk::IGTException) { exceptionThrown1=true; - MITK_TEST_OUTPUT(<<"Tested exception for the case when file version is wrong in SetFileName. Application should not crash."); } MITK_TEST_CONDITION_REQUIRED(exceptionThrown1, "Testing SetFileName method if exception (if data element not found) was thrown."); } void TestGoToSnapshotException() { //testing GoToSnapShot for exception - - mitk::NavigationDataSequentialPlayer::Pointer myTestPlayer2 = mitk::NavigationDataSequentialPlayer::New(); - //std::string file2 = mitk::StandardFileLocations::GetInstance()->FindFile("NavigationDataTestDataInvalidTags.xml", "Modules/IGT/Testing/Data"); - // create test values valid for the xml data above - tTool0Snapshot1[0] = -336.65; - tTool0Snapshot1[1] = 138.5; - tTool0Snapshot1[2]= -2061.07; - tTool1Snapshot2[0] = -56.93; - tTool1Snapshot2[1] = 233.79; - tTool1Snapshot2[2]= -2042.6; + mitk::NavigationDataSequentialPlayer::Pointer myTestPlayer2 = mitk::NavigationDataSequentialPlayer::New(); + myTestPlayer2->SetXMLString(XML_STRING); - myTestPlayer2 - ->SetXMLString(XML_STRING); - - MITK_TEST_CONDITION_REQUIRED(player->GetNumberOfSnapshots() == 3, - "player->GetNumberOfSnapshots() == 3"); - myTestPlayer2 - ->SetRepeat(true); - - runLoop(); - // repeat is on should work a second time - runLoop(); - bool exceptionThrown2=false; try { - myTestPlayer2->GoToSnapshot(1000000); + myTestPlayer2->GoToSnapshot(1000); } catch(mitk::IGTException) { - exceptionThrown2=true; - MITK_TEST_OUTPUT(<<"Tested exception for the case when cannot go back to particular snapshot in GoToSnapShot. Application should not crash."); + exceptionThrown2=true; } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown2, "Testing GoToSnapShot method if exception (cannot go back to particular snapshot) was thrown."); - + MITK_TEST_CONDITION_REQUIRED(exceptionThrown2, "Testing if exception is thrown when GoToSnapShot method is called with an index that doesn't exist."); } void TestSetXMLStringException() { -mitk::NavigationDataSequentialPlayer::Pointer myTestPlayer3 = mitk::NavigationDataSequentialPlayer::New(); - // create test values valid for the xml data above - tTool0Snapshot1[0] = -336.65; - tTool0Snapshot1[1] = 138.5; - tTool0Snapshot1[2]= -2061.07; - tTool1Snapshot2[0] = -56.93; - tTool1Snapshot2[1] = 233.79; - tTool1Snapshot2[2]= -2042.6; - vnl_vector_fixed qVec; - qVec[0] = 0.0085; - qVec[1] = -0.0576; - qVec[2]= -0.0022; - qVec[3]= 0.9982; - qTool0Snapshot0 = mitk::Quaternion(qVec); - qVec[0] = 0.4683; - qVec[1] = 0.0188; - qVec[2]= -0.8805; - qVec[3]= 0.0696; - qTool1Snapshot1 = mitk::Quaternion(qVec); - -const char* XML_TESTSTRING = - "< ToolCount=\"2\">" - "" - "" - "" - "" - "" - "" - ""; + mitk::NavigationDataSequentialPlayer::Pointer myTestPlayer3 = mitk::NavigationDataSequentialPlayer::New(); bool exceptionThrown3=false; - //The string above XML_TESTSTRING is a wrong string, some element were deleted in above + + //The string above XML_INVALID_TESTSTRING is a wrong string, some element were deleted in above try { - myTestPlayer3->SetXMLString(XML_TESTSTRING); + myTestPlayer3->SetXMLString(XML_INVALID_TESTSTRING); } catch(mitk::IGTException) { - exceptionThrown3=true; - MITK_TEST_OUTPUT(<<"Tested exception for the case when. Application should not crash."); + exceptionThrown3=true; } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown3, "Testing SetXMLString method if exception (string is not an XML String) was thrown."); + MITK_TEST_CONDITION_REQUIRED(exceptionThrown3, "Testing SetXMLString method with an invalid XML string."); } /**Documentation * test for the class "NavigationDataRecorder". */ int mitkNavigationDataSequentialPlayerTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("NavigationDataSequentialPlayer"); - // create test values valid for the xml data above - tTool0Snapshot1[0] = -336.65; - tTool0Snapshot1[1] = 138.5; - tTool0Snapshot1[2]= -2061.07; - tTool1Snapshot2[0] = -56.93; - tTool1Snapshot2[1] = 233.79; - tTool1Snapshot2[2]= -2042.6; - vnl_vector_fixed qVec; - qVec[0] = 0.0085; - qVec[1] = -0.0576; - qVec[2]= -0.0022; - qVec[3]= 0.9982; - qTool0Snapshot0 = mitk::Quaternion(qVec); - qVec[0] = 0.4683; - qVec[1] = 0.0188; - qVec[2]= -0.8805; - qVec[3]= 0.0696; - qTool1Snapshot1 = mitk::Quaternion(qVec); - - player - ->SetXMLString(XML_STRING); - - MITK_TEST_CONDITION_REQUIRED(player->GetNumberOfSnapshots() == 3, - "player->GetNumberOfSnapshots() == 3"); - player - ->SetRepeat(true); - - runLoop(); - // repeat is on should work a second time - runLoop(); - - // now test the go to snapshot function - player->GoToSnapshot(3); - mitk::NavigationData::Pointer nd1 = player->GetOutput(1); - MITK_TEST_CONDITION(tTool1Snapshot2 == nd1->GetPosition().GetVnlVector(), - "tTool1Snapshot2 == nd1->GetPosition().GetVnlVector()"); - - player->GoToSnapshot(1); - mitk::NavigationData::Pointer nd0 = player->GetOutput(0); - MITK_TEST_CONDITION(qTool0Snapshot0.as_vector() == nd0->GetOrientation().as_vector(), - "qTool0Snapshot0.as_vector() == nd0->GetOrientation().as_vector()"); - - player->GoToSnapshot(3); - - // and a third time - runLoop(); - - //Testing exceptions + TestStandardWorkflow(); TestSetFileNameException(); TestSetXMLStringException(); TestGoToSnapshotException(); - - - // always end with this! + MITK_TEST_END(); }