diff --git a/Modules/IGT/IGTFilters/mitkNavigationDataPlayer.cpp b/Modules/IGT/IGTFilters/mitkNavigationDataPlayer.cpp index 49687cf867..5a62e6fbd7 100644 --- a/Modules/IGT/IGTFilters/mitkNavigationDataPlayer.cpp +++ b/Modules/IGT/IGTFilters/mitkNavigationDataPlayer.cpp @@ -1,537 +1,537 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-02-10 18:08:54 +0100 (Di, 10 Feb 2009) $ Version: $Revision: 16228 $ 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 "mitkNavigationDataPlayer.h" //for the pause #include #include #include mitk::NavigationDataPlayer::NavigationDataPlayer() : mitk::NavigationDataPlayerBase() { m_NumberOfOutputs = 0; m_Pause = false; m_Playing = false; m_Stream = NULL; m_PlayerMode = NormalFile; m_FileName = ""; m_FileVersion = 1; m_Playing = false; m_Pause = false; m_NumberOfOutputs = 0; m_StartPlayingTimeStamp = 0.0; m_PauseTimeStamp = 0.0; m_parentElement = NULL; m_currentNode = NULL; m_StreamEnd = false; m_StreamSetOutsideFromClass = false; //To get a start time mitk::TimeStamp::GetInstance()->Start(this); } mitk::NavigationDataPlayer::~NavigationDataPlayer() { StopPlaying(); delete m_parentElement; } void mitk::NavigationDataPlayer::GenerateData() { //Only produce new output if the player is started if (!m_Playing) { //The output is not valid anymore for (unsigned int index = 0; index < m_NumberOfOutputs; index++) { mitk::NavigationData* output = this->GetOutput(index); assert(output); mitk::NavigationData::Pointer nd = mitk::NavigationData::New(); mitk::NavigationData::PositionType position; mitk::NavigationData::OrientationType orientation(0.0,0.0,0.0,0.0); position.Fill(0.0); nd->SetPosition(position); nd->SetOrientation(orientation); nd->SetDataValid(false); output->Graft(nd); } return; } //first of all get current time TimeStampType now = mitk::TimeStamp::GetInstance()->GetElapsed(); //now we make a little time arithmetic //to get the elapsed time since the start of the player TimeStampType timeSinceStart = now - m_StartPlayingTimeStamp; //init the vectors std::vector< NavigationData::Pointer > nextCandidates; std::vector< NavigationData::Pointer > lastCandidates; std::vector< NavigationData::TimeStampType > currentTimeOfData; for (unsigned int index=0; index < m_NumberOfOutputs; index++) { nextCandidates.push_back(m_NextToPlayNavigationData.at(index)); lastCandidates.push_back(m_NextToPlayNavigationData.at(index)); currentTimeOfData.push_back(timeSinceStart + m_StartTimeOfData.at(index)); } if (m_NextToPlayNavigationData.size() != m_NumberOfOutputs) { - std::cout << "Mismatch in data" << std::endl; + MITK_ERROR << "Mismatch in data"; return; } // Now we try to find next NavigationData in the stream: // This means we step through the stream of NavigationDatas until we find // a NavigationData which has a current timestamp (currentTimeOfData) greater // then the current playing time. Then we store the data in // m_NextToPlayNavigationData and take the last data (lastCandidates) for the // output of this filter. // // The loop will stop when a suitable NavigationData is found or we reach EOF. // The timestamps of each recorded NavigationData should be equal // therefore we take always the time from the first. while( nextCandidates[0]->GetTimeStamp() < currentTimeOfData[0]) { for (unsigned int index=0; index < m_NumberOfOutputs; index++) { lastCandidates[index] = nextCandidates.at(index); switch(m_FileVersion) { case 1: nextCandidates[index] = ReadVersion1(); break; default: //this case should not happen! therefore the return at this point return; break; } //check if the input stream delivered a correct NavigationData object for (unsigned int i = 0; i < m_NumberOfOutputs; i++) { if (nextCandidates.at(index).IsNull()) { m_StreamEnd = true; StopPlaying(); return; //the case if no NavigationData is found, e.g. EOF, bad stream } } } } //Now lastCandidates stores the new output and nextCandidates is stored to the m_NextToPlay vector for (unsigned int index = 0; index < m_NumberOfOutputs; index++) { mitk::NavigationData* output = this->GetOutput(index); assert(output); output->Graft(lastCandidates.at(index)); m_NextToPlayNavigationData[index] = nextCandidates.at(index); } } void mitk::NavigationDataPlayer::UpdateOutputInformation() { this->Modified(); // make sure that we need to be updated Superclass::UpdateOutputInformation(); } void mitk::NavigationDataPlayer::InitPlayer() { if (m_Stream == NULL) { StreamInvalid("Playing not possible. Wrong file name or path?"); return; } if (!m_Stream->good()) { StreamInvalid("Playing not possible. Stream is not good!"); return; } m_FileVersion = GetFileVersion(m_Stream); //first get the file version //check if we have a valid version if (m_FileVersion < 1) { - StreamInvalid("Playing not possible. Stream is not good!"); + StreamInvalid("Playing not possible. Invalid file version!"); return; } //now read the number of Tracked Tools if(m_NumberOfOutputs == 0){m_NumberOfOutputs = GetNumberOfNavigationDatas(m_Stream);} //with the information about the tracked tool number we can generate the output if (m_NumberOfOutputs > 0) { //Generate the output only if there are changes to the amount of outputs //This happens when the player is stopped and start again with different file if (this->GetNumberOfOutputs() != m_NumberOfOutputs) {SetNumberOfOutputs(m_NumberOfOutputs);} //initialize the player with first data GetFirstData(); //set stream valid m_ErrorMessage = ""; m_StreamValid = true; } else { StreamInvalid("The input stream seems to have NavigationData incompatible format"); return; } } unsigned int mitk::NavigationDataPlayer::GetFileVersion(std::istream* stream) { if (stream==NULL) { - std::cout << "No input stream set!" << std::endl; + MITK_ERROR << "No input stream set!"; return 0; } if (!stream->good()) { - std::cout << "Stream not good!" << std::endl; + MITK_ERROR << "Stream not good!"; return 0; } int version = 1; TiXmlDeclaration* dec = new TiXmlDeclaration(); *stream >> *dec; if(strcmp(dec->Version(),"") == 0){ - std::cout << "The input stream seems to have XML incompatible format" << std::endl; + MITK_ERROR << "The input stream seems to have XML incompatible format"; return 0; } m_parentElement = new TiXmlElement(""); *stream >> *m_parentElement; //2nd line this is the file version std::string tempValue = m_parentElement->Value(); if(tempValue != "Version") { if(tempValue == "Data"){ m_parentElement->QueryIntAttribute("version",&version); } } else { m_parentElement->QueryIntAttribute("Ver",&version); } if (version > 0) return version; else return 0; } unsigned int mitk::NavigationDataPlayer::GetNumberOfNavigationDatas(std::istream* stream) { if (stream == NULL) { - std::cout << "No input stream set!" << std::endl; + MITK_ERROR << "No input stream set!"; return 0; } if (!stream->good()) { - std::cout << "Stream not good!" << std::endl; + MITK_ERROR << "Stream not good!"; return 0; } //If something has changed in a future version of the XML definition e.g. navigationcount or addional parameters //catch this here with a select case block (see GenerateData() method) int numberOfTools = 0; std::string tempValue = m_parentElement->Value(); if(tempValue == "Version"){ *stream >> *m_parentElement; } m_parentElement->QueryIntAttribute("ToolCount",&numberOfTools); if (numberOfTools > 0) return numberOfTools; return 0; } mitk::NavigationData::Pointer mitk::NavigationDataPlayer::ReadVersion1() { if (m_Stream == NULL) { m_Playing = false; - std::cout << "Playing not possible. Wrong file name or path? " << std::endl; + MITK_ERROR << "Playing not possible. Wrong file name or path? "; return NULL; } if (!m_Stream->good()) { m_Playing = false; - std::cout << "Playing not possible. Stream is not good!" << std::endl; + MITK_ERROR << "Playing not possible. Stream is not good!"; return NULL; } /*TiXmlElement* elem = new TiXmlElement(""); m_currentNode = m_parentElement->IterateChildren(m_currentNode); if(m_currentNode) { elem = m_currentNode->ToElement(); }*/ TiXmlElement* elem; m_currentNode = m_parentElement->IterateChildren(m_currentNode); bool delElem; if(m_currentNode) { elem = m_currentNode->ToElement(); delElem = false; } else { elem = new TiXmlElement(""); delElem = true; } mitk::NavigationData::Pointer nd = this->ReadNavigationData(elem); if(delElem) delete elem; return nd; } void mitk::NavigationDataPlayer::StartPlaying() { if (m_Stream == NULL) { m_Playing = false; //Perhaps the SetStream method was not called so we do this when a FileName is set with SetStream(PlayerMode) if (m_FileName != "") { //The PlayerMode is initialized with LastSetStream //SetStream also calls InitPlayer() SetStream(m_PlayerMode); } //now check again if (m_Stream == NULL) { StopPlaying(); - std::cout << "Playing not possible. Wrong file name or path? " << std::endl; + MITK_ERROR << "Playing not possible. Wrong file name or path?"; return; } } if (!m_Playing && m_Stream->good()) { m_Playing = true; //starts the player m_StartPlayingTimeStamp = mitk::TimeStamp::GetInstance()->GetElapsed(); } else { - std::cout << "Player already started or stream is not good" << std::endl; + MITK_ERROR << "Player already started or stream is not good!"; StopPlaying(); } } void mitk::NavigationDataPlayer::StopPlaying() { //re init all data!! for playing again with different data //only PlayerMode and FileName are not changed m_Pause = false; m_Playing = false; if (!m_StreamSetOutsideFromClass) {delete m_Stream;} m_Stream = NULL; m_FileVersion = 1; m_Playing = false; m_Pause = false; m_StartPlayingTimeStamp = 0.0; m_PauseTimeStamp = 0.0; m_NextToPlayNavigationData.clear(); m_StartTimeOfData.clear(); } void mitk::NavigationDataPlayer::GetFirstData() { //Here we read the first lines of input (dependend on the number of inputs) for (unsigned int index=0; index < m_NumberOfOutputs; index++) { //Here we init the vector for later use m_NextToPlayNavigationData.push_back(NULL); m_StartTimeOfData.push_back(0.0); mitk::NavigationData::Pointer nd = this->GetOutput(index); switch(m_FileVersion) { case 1: m_NextToPlayNavigationData[index] = ReadVersion1(); //check if there is valid data in it if (m_NextToPlayNavigationData[index].IsNull()) { m_StreamEnd = true; StopPlaying(); - std::cout << "XML File is corrupt or has no NavigationData" << std::endl; + MITK_ERROR << "XML File is corrupt or has no NavigationData" << std::endl; return; } //Have a look it the output was set already without this check the pipline will disconnect after a start/stop cycle if (nd.IsNull()) this->SetNthOutput(index, m_NextToPlayNavigationData[index]); m_StartTimeOfData[index] = m_NextToPlayNavigationData[index]->GetTimeStamp(); break; default: //this case should not happen! therefore the return at this point return; break; } } } void mitk::NavigationDataPlayer::Pause() { //player runs and pause was called -> pause the player if(m_Playing && !m_Pause) { m_Playing = false; m_Pause = true; m_PauseTimeStamp = mitk::TimeStamp::GetInstance()->GetElapsed(); } else { - std::cout << "Player is either not started or already is paused" << std::endl; + MITK_ERROR << "Player is either not started or already is paused" << std::endl; } } void mitk::NavigationDataPlayer::Resume() { //player is in pause mode -> play at the last position if(!m_Playing && m_Pause) { m_Playing = true; m_Pause = false; mitk::NavigationData::TimeStampType now = mitk::TimeStamp::GetInstance()->GetElapsed(); // in this case m_StartPlayingTimeStamp is set to the total elapsed time with NO playback m_StartPlayingTimeStamp = now - (m_PauseTimeStamp - m_StartPlayingTimeStamp); } else { - std::cout << "Player is not paused!" << std::endl; + MITK_ERROR << "Player is not paused!" << std::endl; } } void mitk::NavigationDataPlayer::SetStream( PlayerMode /*mode*/ ) { m_Stream = NULL; if (!itksys::SystemTools::FileExists(m_FileName.c_str())) { - std::cout << "File dont exist!" << std::endl; + MITK_ERROR << "File dont exist!" << std::endl; return; } switch(m_PlayerMode) { case NormalFile: m_Stream = new std::ifstream(m_FileName.c_str()); m_StreamSetOutsideFromClass = false; break; case ZipFile: m_Stream = NULL; - std::cout << "Sorry no ZipFile support yet"; + MITK_ERROR << "Sorry no ZipFile support yet"; break; default: m_Stream = NULL; break; } this->Modified(); InitPlayer(); } void mitk::NavigationDataPlayer::SetStream( std::istream* stream ) { - if (!stream->good()) + if ( (stream == NULL) || (!stream->good())) { m_StreamEnd = true; - std::cout << "The stream is not good" << std::endl; + MITK_ERROR << "The stream is not good"; return; } m_Stream = stream; m_StreamSetOutsideFromClass = true; this->Modified(); InitPlayer(); } bool mitk::NavigationDataPlayer::IsAtEnd() { return this->m_StreamEnd; } void mitk::NavigationDataPlayer::StreamInvalid(std::string message) { m_StreamEnd = true; StopPlaying(); m_ErrorMessage = message; m_StreamValid = false; MITK_ERROR << m_ErrorMessage; return; } \ No newline at end of file diff --git a/Modules/IGT/Testing/Data/InvalidDataNavigationDataTestData.xml b/Modules/IGT/Testing/Data/InvalidDataNavigationDataTestData.xml new file mode 100644 index 0000000000..7e28877b81 --- /dev/null +++ b/Modules/IGT/Testing/Data/InvalidDataNavigationDataTestData.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/Modules/IGT/Testing/Data/InvalidVersionNavigationDataTestData.xml b/Modules/IGT/Testing/Data/InvalidVersionNavigationDataTestData.xml new file mode 100644 index 0000000000..aaaabb0cba --- /dev/null +++ b/Modules/IGT/Testing/Data/InvalidVersionNavigationDataTestData.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/Modules/IGT/Testing/mitkNavigationDataPlayerTest.cpp b/Modules/IGT/Testing/mitkNavigationDataPlayerTest.cpp index 6e745bbb72..2e76c1d420 100644 --- a/Modules/IGT/Testing/mitkNavigationDataPlayerTest.cpp +++ b/Modules/IGT/Testing/mitkNavigationDataPlayerTest.cpp @@ -1,118 +1,348 @@ /*========================================================================= 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 "mitkNavigationDataPlayer.h" #include "mitkNavigationData.h" #include "mitkTestingMacros.h" #include "mitkStandardFileLocations.h" #include "mitkTimeStamp.h" #include #include +class mitkNavigationDataPlayerTestClass + { + public: + static void TestInstantiation() + { + // let's create an object of our class + mitk::NavigationDataPlayer::Pointer player = mitk::NavigationDataPlayer::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(player.IsNotNull(), "Testing instantiation"); + } + + static void TestSimpleDataPlay() + { + std::string tmp = ""; + + // let's create an object of our class + mitk::NavigationDataPlayer::Pointer player = mitk::NavigationDataPlayer::New(); + + std::string file = mitk::StandardFileLocations::GetInstance()->FindFile("NavigationDataTestData.xml", "Modules/IGT/Testing/Data"); + + player->SetFileName( file ); + + MITK_TEST_CONDITION_REQUIRED( strcmp(player->GetFileName(), file.c_str()) == 0, "Testing SetFileName and GetFileName"); + + player->SetStream( mitk::NavigationDataPlayer::NormalFile ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + + mitk::NavigationData::Pointer nd = player->GetOutput(); + mitk::Point3D pnt; + pnt[0] = 1; + pnt[1] = 0; + pnt[2] = 3; + + MITK_TEST_CONDITION_REQUIRED( nd->GetPosition() == pnt, "Testing position of replayed NavigaionData" ); + + player = mitk::NavigationDataPlayer::New(); + player->SetFileName( file ); + player->SetStream( mitk::NavigationDataPlayer::NormalFile ); + + std::vector times, refTimes; + refTimes.resize(5); + refTimes[0] = 3.9; + refTimes[1] = 83.6; + refTimes[2] = 174.4; + refTimes[3] = 275.0; + refTimes[4] = 385.39; + std::vector points, refPoints; + refPoints.resize(5); + refPoints[0][0] = 1; refPoints[0][1] = 0; refPoints[0][2] = 3; + refPoints[1][0] = 2; refPoints[1][1] = 1; refPoints[1][2] = 4; + refPoints[2][0] = 3; refPoints[2][1] = 2; refPoints[2][2] = 5; + refPoints[3][0] = 4; refPoints[3][1] = 3; refPoints[3][2] = 6; + refPoints[4][0] = 5; refPoints[4][1] = 4; refPoints[4][2] = 7; + + mitk::TimeStamp::Pointer timer = mitk::TimeStamp::GetInstance(); + timer->Initialize(); + + itk::Object::Pointer obj = itk::Object::New(); + + mitk::Point3D oldPos; + oldPos[0] = 1; + oldPos[1] = 0; + oldPos[2] = 3; + + timer->Start( obj ); + player->StartPlaying(); + while( times.size()<5 ) + { + player->Update(); + pnt = player->GetOutput()->GetPosition(); + if ( pnt != oldPos ) + { + times.push_back( timer->GetElapsed(obj) ); + points.push_back(oldPos); + oldPos = pnt; + } + } + player->StopPlaying(); + + // if this test fails, it may be because the dartclient runs on a virtual machine. + // Under these circumstances, it may be impossible to achieve a time-accuracy of 10ms + for ( int i=0;i<5;i++ ) + { + if ((times[i]>refTimes[i]-150 && times[i]refTimes[i]-150 && times[i]FindFile("NavigationDataTestData.xml", "Modules/IGT/Testing/Data"); + + player->SetFileName( file ); + + MITK_TEST_CONDITION_REQUIRED( strcmp(player->GetFileName(), file.c_str()) == 0, "Testing SetFileName and GetFileName"); + + player->SetStream( mitk::NavigationDataPlayer::NormalFile ); + player->StartPlaying(); + player->Update(); + + MITK_TEST_OUTPUT(<<"Test double call of Pause() method!"); + player->Pause(); //test pause method + player->Pause(); //call again to see if this causes an error + + MITK_TEST_OUTPUT(<<"Test double call of Resume() method!"); + player->Resume(); //test resume method + player->Resume(); //call again to see if this causes an error + + player->Update(); + player->StopPlaying(); + + mitk::NavigationData::Pointer nd = player->GetOutput(); + mitk::Point3D pnt; + pnt[0] = 1; + pnt[1] = 0; + pnt[2] = 3; + + MITK_TEST_CONDITION_REQUIRED( nd->GetPosition() == pnt, "Testing position of replayed NavigaionData" ); + + player = mitk::NavigationDataPlayer::New(); + player->SetFileName( file ); + player->SetStream( mitk::NavigationDataPlayer::NormalFile ); + + std::vector times, refTimes; + refTimes.resize(5); + refTimes[0] = 3.9; + refTimes[1] = 83.6; + refTimes[2] = 174.4; + refTimes[3] = 275.0; + refTimes[4] = 385.39; + std::vector points, refPoints; + refPoints.resize(5); + refPoints[0][0] = 1; refPoints[0][1] = 0; refPoints[0][2] = 3; + refPoints[1][0] = 2; refPoints[1][1] = 1; refPoints[1][2] = 4; + refPoints[2][0] = 3; refPoints[2][1] = 2; refPoints[2][2] = 5; + refPoints[3][0] = 4; refPoints[3][1] = 3; refPoints[3][2] = 6; + refPoints[4][0] = 5; refPoints[4][1] = 4; refPoints[4][2] = 7; + + mitk::TimeStamp::Pointer timer = mitk::TimeStamp::GetInstance(); + timer->Initialize(); + + itk::Object::Pointer obj = itk::Object::New(); + + mitk::Point3D oldPos; + oldPos[0] = 1; + oldPos[1] = 0; + oldPos[2] = 3; + + timer->Start( obj ); + player->StartPlaying(); + + MITK_TEST_CONDITION_REQUIRED(!player->IsAtEnd(), "Testing method IsAtEnd() #0"); + + while( times.size()<3 ) + { + player->Update(); + pnt = player->GetOutput()->GetPosition(); + if ( pnt != oldPos ) + { + times.push_back( timer->GetElapsed(obj) ); + points.push_back(oldPos); + oldPos = pnt; + } + } + MITK_TEST_OUTPUT(<<"Test pause method!"); + player->Pause(); + + MITK_TEST_CONDITION_REQUIRED(!player->IsAtEnd(), "Testing method IsAtEnd() #1"); + + MITK_TEST_OUTPUT(<<"Test resume method!"); + player->Resume(); + while( times.size()<5 ) + { + player->Update(); + pnt = player->GetOutput()->GetPosition(); + if ( pnt != oldPos ) + { + times.push_back( timer->GetElapsed(obj) ); + points.push_back(oldPos); + oldPos = pnt; + } + } + + + player->StopPlaying(); + + // if this test fails, it may be because the dartclient runs on a virtual machine. + // Under these circumstances, it may be impossible to achieve a time-accuracy of 10ms + for ( int i=0;i<5;i++ ) + { + if ((times[i]>refTimes[i]-150 && times[i]refTimes[i]-150 && times[i]IsAtEnd(), "Testing method IsAtEnd() #2"); + } + + static void TestInvalidStream() + { + MITK_TEST_OUTPUT(<<"#### Testing invalid input data: errors are expected. ####"); + + //declarate test variables + mitk::NavigationDataPlayer::Pointer player; + std::string file; + + //case 0: stream not set + player = mitk::NavigationDataPlayer::New(); + player->SetStream( mitk::NavigationDataPlayer::ZipFile ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + MITK_TEST_OUTPUT(<<"#0: Tested stream not set. Application should not crash."); + + //case 1: non-existing file + player = mitk::NavigationDataPlayer::New(); + player->SetFileName( "ffdsd" ); + player->SetStream( mitk::NavigationDataPlayer::NormalFile ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + MITK_TEST_OUTPUT(<<"#1: Tested non-existing file. Application should not crash."); + + //case 2: wrong file format + player = mitk::NavigationDataPlayer::New(); + file = mitk::StandardFileLocations::GetInstance()->FindFile("SROMFile.rom", "Modules/IGT/Testing/Data"); + player->SetFileName( file ); + player->SetStream( mitk::NavigationDataPlayer::NormalFile ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + MITK_TEST_OUTPUT(<<"#2: Tested wrong file format. Application should not crash."); + + //case 3: wrong file version + player = mitk::NavigationDataPlayer::New(); + file = mitk::StandardFileLocations::GetInstance()->FindFile("InvalidVersionNavigationDataTestData.xml", "Modules/IGT/Testing/Data"); + player->SetFileName( file ); + player->SetStream( mitk::NavigationDataPlayer::NormalFile ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + MITK_TEST_OUTPUT(<<"#3: Tested wrong file version. Application should not crash."); + + //case 4: wrong file + player = mitk::NavigationDataPlayer::New(); + player->SetFileName( "cs:\fsd/$%§²³ffdsd" ); + player->SetStream( mitk::NavigationDataPlayer::NormalFile ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + MITK_TEST_OUTPUT(<<"#4: Tested wrong file. Application should not crash."); + + //case 5: null stream + player = mitk::NavigationDataPlayer::New(); + player->SetStream( NULL ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + MITK_TEST_OUTPUT(<<"#5: Tested null stream. Application should not crash."); + + //case 6: empty stream + player = mitk::NavigationDataPlayer::New(); + player->SetStream( new std::ifstream("") ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + MITK_TEST_OUTPUT(<<"#6: Tested empty stream. Application should not crash."); + + //case 7: wrong stream + player = mitk::NavigationDataPlayer::New(); + file = mitk::StandardFileLocations::GetInstance()->FindFile("SROMFile.rom", "Modules/IGT/Testing/Data"); + player->SetStream( new std::ifstream(file.c_str()) ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + MITK_TEST_OUTPUT(<<"#7: Tested wrong stream. Application should not crash."); + + //case 8: invalid + player = mitk::NavigationDataPlayer::New(); + file = mitk::StandardFileLocations::GetInstance()->FindFile("InvalidDataNavigationDataTestData.xml", "Modules/IGT/Testing/Data"); + player->SetFileName( file ); + player->SetStream( mitk::NavigationDataPlayer::NormalFile ); + player->StartPlaying(); + player->Update(); + player->StopPlaying(); + MITK_TEST_OUTPUT(<<"#8: Tested invalid file version. Application should not crash."); + + + + } + + }; + /**Documentation * test for the class "NavigationDataPlayer". */ int mitkNavigationDataPlayerTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("NavigationDataPlayer"); std::string tmp = ""; - // let's create an object of our class - mitk::NavigationDataPlayer::Pointer player = mitk::NavigationDataPlayer::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(player.IsNotNull(), "Testing instantiation"); - - std::string file = mitk::StandardFileLocations::GetInstance()->FindFile("NavigationDataTestData.xml", "Modules/IGT/Testing/Data"); - - player->SetFileName( file ); - - MITK_TEST_CONDITION_REQUIRED( strcmp(player->GetFileName(), file.c_str()) == 0, "Testing SetFileName and GetFileName"); - - player->SetStream( mitk::NavigationDataPlayer::NormalFile ); - player->StartPlaying(); - player->Update(); - player->StopPlaying(); - - mitk::NavigationData::Pointer nd = player->GetOutput(); - mitk::Point3D pnt; - pnt[0] = 1; - pnt[1] = 0; - pnt[2] = 3; - - MITK_TEST_CONDITION_REQUIRED( nd->GetPosition() == pnt, "Testing position of replayed NavigaionData" ); - - player = mitk::NavigationDataPlayer::New(); - player->SetFileName( file ); - player->SetStream( mitk::NavigationDataPlayer::NormalFile ); - - std::vector times, refTimes; - refTimes.resize(5); - refTimes[0] = 3.9; - refTimes[1] = 83.6; - refTimes[2] = 174.4; - refTimes[3] = 275.0; - refTimes[4] = 385.39; - std::vector points, refPoints; - refPoints.resize(5); - refPoints[0][0] = 1; refPoints[0][1] = 0; refPoints[0][2] = 3; - refPoints[1][0] = 2; refPoints[1][1] = 1; refPoints[1][2] = 4; - refPoints[2][0] = 3; refPoints[2][1] = 2; refPoints[2][2] = 5; - refPoints[3][0] = 4; refPoints[3][1] = 3; refPoints[3][2] = 6; - refPoints[4][0] = 5; refPoints[4][1] = 4; refPoints[4][2] = 7; - - mitk::TimeStamp::Pointer timer = mitk::TimeStamp::GetInstance(); - timer->Initialize(); - - itk::Object::Pointer obj = itk::Object::New(); - - mitk::Point3D oldPos; - oldPos[0] = 1; - oldPos[1] = 0; - oldPos[2] = 3; - - timer->Start( obj ); - player->StartPlaying(); - while( times.size()<5 ) - { - player->Update(); - pnt = player->GetOutput()->GetPosition(); - if ( pnt != oldPos ) - { - times.push_back( timer->GetElapsed(obj) ); - points.push_back(oldPos); - oldPos = pnt; - } - } - player->StopPlaying(); - - // if this test fails, it may be because the dartclient runs on a virtual machine. - // Under these circumstances, it may be impossible to achieve a time-accuracy of 10ms - for ( int i=0;i<5;i++ ) - { - if ((times[i]>refTimes[i]-150 && times[i]refTimes[i]-150 && times[i]