MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationDataPlayerTest.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14#include "mitkNavigationData.h"
16
17#include "mitkTestingMacros.h"
18#include "mitkStandardFileLocations.h"
19#include "mitkIGTTimeStamp.h"
20
21#include <iostream>
22#include <sstream>
23#include <fstream>
24
25#include "mitkIGTException.h"
26#include "mitkIGTIOException.h"
27
28
29
30#include <itksys/SystemTools.hxx>
31
33 {
34 public:
35 static mitk::NavigationDataSet::Pointer GetNavigationDataSetFromXML(std::string filename)
36 {
37 mitk::NavigationDataReaderXML::Pointer reader = mitk::NavigationDataReaderXML::New();
38 return reader->Read(filename);
39 }
40
41 static void TestInstantiation()
42 {
43 // let's create an object of our class
44 mitk::NavigationDataPlayer::Pointer player = mitk::NavigationDataPlayer::New();
45
46 // first test: did this work?
47 // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since
48 // it makes no sense to continue without an object.
49 MITK_TEST_CONDITION_REQUIRED(player.IsNotNull(), "Testing instantiation");
50 }
51
52 static void TestSimpleDataPlay()
53 {
54 std::string tmp = "";
55
56 // let's create an object of our class
57 mitk::NavigationDataPlayer::Pointer player = mitk::NavigationDataPlayer::New();
58
59 // create a file reader for the navigation data xml file
60 mitk::NavigationDataReaderXML::Pointer navigationDataReader = mitk::NavigationDataReaderXML::New();
61 std::string file = mitk::StandardFileLocations::GetInstance()->FindFile("NavigationDataTestData.xml", "Modules/IGT/Testing/Data");
62
63 // set NavigationDataSet to player
64 mitk::NavigationDataSet::Pointer navigationDataSet = navigationDataReader->Read(file);
65 player->SetNavigationDataSet( navigationDataSet );
66 MITK_TEST_CONDITION_REQUIRED( navigationDataSet == player->GetNavigationDataSet() ,
67 "Testing SetNavigationDataSet and GetNavigationDataSet." );
68
69 player->StartPlaying();
70 player->Update();
71 player->StopPlaying();
72
73 mitk::NavigationData::Pointer nd = player->GetOutput();
74 mitk::Point3D pnt;
75 pnt[0] = 1;
76 pnt[1] = 0;
77 pnt[2] = 3;
78
79 MITK_TEST_CONDITION_REQUIRED( nd->GetPosition() == pnt, "Testing position of replayed NavigaionData" );
80
81 player = mitk::NavigationDataPlayer::New();
82 player->SetNavigationDataSet( navigationDataReader->Read(file) );
83
84 std::vector<double> times, refTimes;
85 refTimes.resize(5);
86 refTimes[0] = 3.9;
87 refTimes[1] = 83.6;
88 refTimes[2] = 174.4;
89 refTimes[3] = 275.0;
90 refTimes[4] = 385.39;
91 std::vector<mitk::Point3D> points, refPoints;
92 refPoints.resize(5);
93 refPoints[0][0] = 1; refPoints[0][1] = 0; refPoints[0][2] = 3;
94 refPoints[1][0] = 2; refPoints[1][1] = 1; refPoints[1][2] = 4;
95 refPoints[2][0] = 3; refPoints[2][1] = 2; refPoints[2][2] = 5;
96 refPoints[3][0] = 4; refPoints[3][1] = 3; refPoints[3][2] = 6;
97 refPoints[4][0] = 5; refPoints[4][1] = 4; refPoints[4][2] = 7;
98
99 mitk::IGTTimeStamp::Pointer timer = mitk::IGTTimeStamp::GetInstance();
100 timer->Initialize();
101
102 itk::Object::Pointer obj = itk::Object::New();
103
104 mitk::Point3D oldPos;
105 oldPos[0] = 1;
106 oldPos[1] = 0;
107 oldPos[2] = 3;
108
109 timer->Start( obj );
110 player->StartPlaying();
111 while( times.size()<5 )
112 {
113 player->Update();
114 pnt = player->GetOutput()->GetPosition();
115 if ( pnt != oldPos )
116 {
117 times.push_back( timer->GetElapsed(obj) );
118 points.push_back(oldPos);
119 oldPos = pnt;
120 }
121 }
122 player->StopPlaying();
123
124 // if this test fails, it may be because the dartclient runs on a virtual machine.
125 // Under these circumstances, it may be impossible to achieve a time-accuracy of 10ms
126 for ( int i=0;i<5;i++ )
127 {
128 if ((times[i]>refTimes[i]-150 && times[i]<refTimes[i]+150)) {MITK_TEST_OUTPUT(<< "ref: " << refTimes[i] << " / time elapsed: " << times[i]);}
129 MITK_TEST_CONDITION_REQUIRED( (times[i]>refTimes[i]-150 && times[i]<refTimes[i]+150), "checking for more or less correct time-line" );
130 MITK_TEST_CONDITION_REQUIRED(points[i] == refPoints[i], "checking if the point coordinates are correct")
131 }
132
133 }
134
135 static void TestPauseAndResume()
136 {
137 std::string tmp = "";
138
139 // let's create an object of our class
140 mitk::NavigationDataPlayer::Pointer player = mitk::NavigationDataPlayer::New();
141
142 // create a file reader for the navigation data xml file
143 mitk::NavigationDataReaderXML::Pointer navigationDataReader = mitk::NavigationDataReaderXML::New();
144 std::string file = mitk::StandardFileLocations::GetInstance()->FindFile("NavigationDataTestData.xml", "Modules/IGT/Testing/Data");
145
146 // set NavigationDataSet to player
147 player->SetNavigationDataSet( navigationDataReader->Read(file) );
148
149 player->StartPlaying();
150 player->Update();
151
152 mitk::NavigationData::Pointer nd = player->GetOutput();
153 mitk::Point3D pnt;
154 pnt[0] = 1;
155 pnt[1] = 0;
156 pnt[2] = 3;
157
158 MITK_TEST_CONDITION_REQUIRED( nd->GetPosition() == pnt, "Testing position of replayed NavigaionData" );
159
160 MITK_TEST_OUTPUT(<<"Test double call of Pause() method!");
161 player->Pause(); //test pause method
162 player->Pause(); //call again to see if this causes an error
163
164 MITK_TEST_OUTPUT(<<"Test double call of Resume() method!");
165 player->Resume(); //test resume method
166 player->Resume(); //call again to see if this causes an error
167
168 player->Update();
169 player->StopPlaying();
170
171 player = mitk::NavigationDataPlayer::New();
172 player->SetNavigationDataSet( navigationDataReader->Read(file) );
173
174 std::vector<double> times, refTimes;
175 refTimes.resize(5);
176 refTimes[0] = 3.9;
177 refTimes[1] = 83.6;
178 refTimes[2] = 174.4;
179 refTimes[3] = 275.0;
180 refTimes[4] = 385.39;
181 std::vector<mitk::Point3D> points, refPoints;
182 refPoints.resize(5);
183 refPoints[0][0] = 1; refPoints[0][1] = 0; refPoints[0][2] = 3;
184 refPoints[1][0] = 2; refPoints[1][1] = 1; refPoints[1][2] = 4;
185 refPoints[2][0] = 3; refPoints[2][1] = 2; refPoints[2][2] = 5;
186 refPoints[3][0] = 4; refPoints[3][1] = 3; refPoints[3][2] = 6;
187 refPoints[4][0] = 5; refPoints[4][1] = 4; refPoints[4][2] = 7;
188
189 mitk::IGTTimeStamp::Pointer timer = mitk::IGTTimeStamp::GetInstance();
190 timer->Initialize();
191
192 itk::Object::Pointer obj = itk::Object::New();
193
194 mitk::Point3D oldPos;
195 oldPos[0] = 1;
196 oldPos[1] = 0;
197 oldPos[2] = 3;
198
199 timer->Start( obj );
200 player->StartPlaying();
201
202 MITK_TEST_CONDITION_REQUIRED(!player->IsAtEnd(), "Testing method IsAtEnd() #0");
203
204 while( times.size()<3 )
205 {
206 player->Update();
207 pnt = player->GetOutput()->GetPosition();
208 if ( pnt != oldPos )
209 {
210 times.push_back( timer->GetElapsed(obj) );
211 points.push_back(oldPos);
212 oldPos = pnt;
213 }
214 }
215 MITK_TEST_OUTPUT(<<"Test pause method!");
216 player->Pause();
217
218 MITK_TEST_CONDITION_REQUIRED(!player->IsAtEnd(), "Testing method IsAtEnd() #1");
219
220 MITK_TEST_OUTPUT(<<"Test resume method!");
221 player->Resume();
222 while( times.size()<5 )
223 {
224 player->Update();
225 pnt = player->GetOutput()->GetPosition();
226 if ( pnt != oldPos )
227 {
228 times.push_back( timer->GetElapsed(obj) );
229 points.push_back(oldPos);
230 oldPos = pnt;
231 }
232 }
233
234
235 player->StopPlaying();
236
237 // if this test fails, it may be because the dartclient runs on a virtual machine.
238 // Under these circumstances, it may be impossible to achieve a time-accuracy of 10ms
239 for ( int i=0;i<5;i++ )
240 {
241 if ((times[i]>refTimes[i]-150 && times[i]<refTimes[i]+150)) {MITK_TEST_OUTPUT(<< "ref: " << refTimes[i] << " / time elapsed: " << times[i]);}
242 MITK_TEST_CONDITION_REQUIRED( (times[i]>refTimes[i]-150 && times[i]<refTimes[i]+150), "checking for more or less correct time-line" );
243 MITK_TEST_CONDITION_REQUIRED(points[i] == refPoints[i], "checking if the point coordinates are correct")
244 }
245
246 MITK_TEST_CONDITION_REQUIRED(player->IsAtEnd(), "Testing method IsAtEnd() #2");
247 }
248
249 static void TestInvalidStream()
250 {
251 MITK_TEST_OUTPUT(<<"#### Testing invalid input data: errors are expected. ####");
252
253 //declarate test variables
254 mitk::NavigationDataPlayer::Pointer player;
255 std::string file;
256
257 //case 0: stream not set
258 player = mitk::NavigationDataPlayer::New();
259 bool InvalidStreamException0 = false;
260 try
261 {
262 player->StartPlaying();
263 }
264 catch(mitk::IGTException)
265 {
266 InvalidStreamException0=true;
267 player->Update();
268 player->StopPlaying();
269 MITK_TEST_OUTPUT(<<"#0: Tested stream not set. Application should not crash.");
270 }
271
272 MITK_TEST_CONDITION_REQUIRED(InvalidStreamException0, "Testing Invalid Stream method if exception (stream not set) was thrown.");
273
274 //case 1: non-existing file
275 player = mitk::NavigationDataPlayer::New();
276 bool InvalidStreamException1 = false;
277 MITK_TEST_FOR_EXCEPTION(mitk::IGTIOException,
278 player->SetNavigationDataSet(mitkNavigationDataPlayerTestClass::GetNavigationDataSetFromXML(file)));
279 try
280 {
281 player->StartPlaying();
282 }
283 catch(mitk::IGTException)
284 {
285 InvalidStreamException1=true;
286 player->Update();
287 player->StopPlaying();
288 MITK_TEST_OUTPUT(<<"#1: Tested non-existing file. Application should not crash.");
289 }
290
291 MITK_TEST_CONDITION_REQUIRED(InvalidStreamException1, "Testing Invalid Stream method if exception (non-existing file) was thrown.");
292
293 //case 2: wrong file format
294 player = mitk::NavigationDataPlayer::New();
295 bool InvalidStreamException2 = false;
296 file = mitk::StandardFileLocations::GetInstance()->FindFile("SROMFile.rom", "Modules/IGT/Testing/Data");
297 player->SetNavigationDataSet(mitkNavigationDataPlayerTestClass::GetNavigationDataSetFromXML(file));
298 try
299 {
300 player->StartPlaying();
301 }
302 catch(mitk::IGTException)
303 {
304 InvalidStreamException2=true;
305 player->Update();
306 player->StopPlaying();
307 MITK_TEST_OUTPUT(<<"#2: Tested wrong file format. Application should not crash.");
308 }
309
310 MITK_TEST_CONDITION_REQUIRED(InvalidStreamException2, "Testing Invalid Stream method if exception (wrong file format) was thrown.");
311
312 //case 3: wrong file version
313 player = mitk::NavigationDataPlayer::New();
314 file = mitk::StandardFileLocations::GetInstance()->FindFile("InvalidVersionNavigationDataTestData.xml", "Modules/IGT/Testing/Data");
315 player->SetNavigationDataSet(mitkNavigationDataPlayerTestClass::GetNavigationDataSetFromXML(file));
316 bool InvalidStreamException3 = false;
317 try
318 {
319 player->StartPlaying();
320 }
321 catch(mitk::IGTException)
322 {
323 InvalidStreamException3 = true;
324 player->Update();
325 player->StopPlaying();
326 MITK_TEST_OUTPUT(<<"#3: Tested wrong file version. Application should not crash.");
327 }
328 MITK_TEST_CONDITION_REQUIRED(InvalidStreamException3, "Testing Invalid Stream method if exception (wrong file version) was thrown.");
329
330 //case 4: wrong file
331 mitk::NavigationDataSet::Pointer navigationDataSet;
332 mitk::NavigationDataReaderXML::Pointer navigationDataReader = mitk::NavigationDataReaderXML::New();
333 MITK_TEST_FOR_EXCEPTION(mitk::IGTIOException,
334 navigationDataSet = navigationDataReader->Read("cs:\fsd/$%§²³ffdsd"));
335
336 player = mitk::NavigationDataPlayer::New();
337 player->SetNavigationDataSet( navigationDataSet );
338
339 bool InvalidStreamException4=false;
340 try
341 {
342 player->StartPlaying();
343 }
344 catch(mitk::IGTException)
345 {
346 InvalidStreamException4=true;
347 MITK_TEST_OUTPUT(<<"#4: Tested wrong file. Application should not crash.");
348 }
349
350 MITK_TEST_CONDITION_REQUIRED(InvalidStreamException4, "Testing Invalid Stream method if exception (wrong file) was thrown.");
351
352 //case 5: null stream
353 player = mitk::NavigationDataPlayer::New();
354 bool InvalidStreamException5=false;
355 try
356 {
357 player->StartPlaying();
358 }
359 catch(mitk::IGTException)
360 {
361 InvalidStreamException5=true;
362 player->Update();
363 player->StopPlaying();
364 MITK_TEST_OUTPUT(<<"#5: Tested null stream. Application should not crash.");
365 }
366
367 MITK_TEST_CONDITION_REQUIRED(InvalidStreamException5, "Testing Invalid Stream method if exception (null stream) was thrown.");
368
369 //case 6: empty stream, exception is thrown in setstream
370 player = mitk::NavigationDataPlayer::New();
371 bool InvalidStreamException6=false;
372 std::ifstream* myEmptyStream;
373 try
374 {
375 myEmptyStream = new std::ifstream("");
376 mitk::NavigationDataReaderXML::Pointer reader = mitk::NavigationDataReaderXML::New();
377 reader->Read( myEmptyStream );
378 }
380 {
381 InvalidStreamException6=true;
382 MITK_TEST_OUTPUT(<<"#6: Tested empty stream. Application should not crash.");
383 }
384 MITK_TEST_CONDITION_REQUIRED(InvalidStreamException6, "Testing Invalid Stream method if exception (empty stream) was thrown.");
385
386
387
388
389 //case 7: wrong stream
390 player = mitk::NavigationDataPlayer::New();
391 file = mitk::StandardFileLocations::GetInstance()->FindFile("SROMFile.rom", "Modules/IGT/Testing/Data");
392
393 bool InvalidStreamException7=false;
394 std::ifstream* myWrongStream;
395 myWrongStream = new std::ifstream(file.c_str());
396 try
397 {
398 mitk::NavigationDataReaderXML::Pointer reader = mitk::NavigationDataReaderXML::New();
399 reader->Read( myWrongStream );
400 }
402 {
403 InvalidStreamException7=true;
404 MITK_TEST_OUTPUT(<<"#7: Tested wrong stream. Application should not crash.");
405 }
406
407 MITK_TEST_CONDITION_REQUIRED(InvalidStreamException7, "Testing Invalid Stream method if exception (wrong stream) was thrown.");
408
409
410
411 //case 8: invalid
412 player = mitk::NavigationDataPlayer::New();
413 file = mitk::StandardFileLocations::GetInstance()->FindFile("InvalidDataNavigationDataTestData.xml", "Modules/IGT/Testing/Data");
414 bool InvalidStreamException8=false;
415 try
416 {
417 mitk::NavigationDataReaderXML::Pointer reader = mitk::NavigationDataReaderXML::New();
418 reader->Read( myWrongStream );
419 player->SetNavigationDataSet( reader->Read( file ) );
420 player->StartPlaying();
421 }
423 {
424 InvalidStreamException8=true;
425 MITK_TEST_OUTPUT(<<"#8: Tested invalid file version. Application should not crash.");
426 }
427 MITK_TEST_CONDITION_REQUIRED(InvalidStreamException8, "Testing Invalid Stream method if exception (Invalid) was thrown.");
428
429 //clean up
430 delete myEmptyStream;
431 delete myWrongStream;
432 }
433
435 {
436 mitk::NavigationDataPlayer::Pointer myTestPlayer = mitk::NavigationDataPlayer::New();
437 std::string file = mitk::StandardFileLocations::GetInstance()->FindFile("NavigationDataTestData.xml", "Modules/IGT/Testing/Data");
438
439 myTestPlayer->SetNavigationDataSet( mitkNavigationDataPlayerTestClass::GetNavigationDataSetFromXML( file ) );
440
441 bool exceptionThrown=false;
442
443 try
444 {
445 std::istream* stream = nullptr;
446 mitk::NavigationDataReaderXML::Pointer reader = mitk::NavigationDataReaderXML::New();
447 reader->Read( stream );
448 }
450 {
451 exceptionThrown = true;
452 MITK_TEST_OUTPUT(<<"#9: Tested exceptions in SetStream. Application should not crash.");
453
454 }
455 MITK_TEST_CONDITION_REQUIRED(exceptionThrown, "Testing SetStream method in exception was thrown.");
456
457 }
458
460 {
461 MITK_INFO <<"In the following, exceptions are tested. Errors will occur and are expected.";
462
463
464 //Case1 Testing if stream=nullptr
465 mitk::NavigationDataPlayer::Pointer myTestPlayer1 = mitk::NavigationDataPlayer::New();
466 bool exceptionThrown1 = false;
467 try
468 {
469 myTestPlayer1->StartPlaying();
470
471 }
472 catch(mitk::IGTException)
473 {
474 exceptionThrown1 = true;
475 myTestPlayer1->StopPlaying();
476 MITK_TEST_OUTPUT(<<"#10: Tested exception for the case when stream=nullptr in StartPlaying. Application should not crash.");
477 }
478
479 MITK_TEST_CONDITION_REQUIRED(exceptionThrown1, "Testing StartPlaying method if exception (stream=nullptr) was thrown.");
480
481 //Case2 Testing if file does not exist
482 mitk::NavigationDataPlayer::Pointer myTestPlayer2 = mitk::NavigationDataPlayer::New();
483
484 MITK_TEST_FOR_EXCEPTION(mitk::IGTIOException,
485 myTestPlayer2->SetNavigationDataSet( mitkNavigationDataPlayerTestClass::GetNavigationDataSetFromXML("ffdsd") ));
486 bool exceptionThrown2 = false;
487 try{
488 myTestPlayer2->StartPlaying();
489 }
490 catch(mitk::IGTException)
491 {
492 exceptionThrown2 = true;
493 myTestPlayer2->StopPlaying();
494 MITK_TEST_OUTPUT(<<"#11: Tested exception for the case when file does not exist in StartPlaying. Application should not crash.");
495 }
496 MITK_TEST_CONDITION_REQUIRED(exceptionThrown2, "Testing StartPlaying method if exception is thrown when file does not exist.");
497
498
499 //Case3 Testing if wrong file format
500 mitk::NavigationDataPlayer::Pointer myTestPlayer3 = mitk::NavigationDataPlayer::New();
501 std::string file3 = mitk::StandardFileLocations::GetInstance()->FindFile("SROMFile.rom", "Modules/IGT/Testing/Data");
502
503 bool exceptionThrown3 = false;
504 try
505 {
506 myTestPlayer3->SetNavigationDataSet( mitkNavigationDataPlayerTestClass::GetNavigationDataSetFromXML(file3) );
507 }
509 {
510 MITK_TEST_OUTPUT(<<"#12: Tested exception for the case when file format is wrong. Application should not crash.");
511 exceptionThrown3 = true;
512 }
513 MITK_TEST_CONDITION_REQUIRED(exceptionThrown3, "Testing StartPlaying method if exception (file format is wrong) was thrown.");
514
515 //Case4 Testing if wrong file version
516 mitk::NavigationDataPlayer::Pointer myTestPlayer4 = mitk::NavigationDataPlayer::New();
517 std::string file4 = mitk::StandardFileLocations::GetInstance()->FindFile("InvalidVersionNavigationDataTestData.xml", "Modules/IGT/Testing/Data");
518 bool exceptionThrown4 = false;
519 try
520 {
521 mitk::NavigationDataSet::Pointer navigationDataSet
523 myTestPlayer4->SetNavigationDataSet( navigationDataSet );
524 }
526 {
527 exceptionThrown4 = true;
528 MITK_TEST_OUTPUT(<<"#13: Tested exception for the case when file version is wrong in StartPlaying. Application should not crash.");
529 }
530 MITK_TEST_CONDITION_REQUIRED(exceptionThrown4, "Testing StartPlaying method if exception (file version is wrong) was thrown.");
531
532 //Case5 Testing if not existing file name
533 mitk::NavigationDataPlayer::Pointer myTestPlayer5 = mitk::NavigationDataPlayer::New();
534 bool exceptionThrown5 = false;
535
536 try
537 {
538 mitk::NavigationDataSet::Pointer navigationDataSet
540 myTestPlayer4->SetNavigationDataSet( navigationDataSet );
541 }
543 {
544 exceptionThrown5 = true;
545 MITK_TEST_OUTPUT(<<"#14: Tested exception for the case when non-existing file name in StartPlaying. Application should not crash.");
546 }
547 MITK_TEST_CONDITION_REQUIRED(exceptionThrown5, "Testing StartPlaying method if exception (non-existing file name) was thrown.");
548
549 }
550
551};
552
557int mitkNavigationDataPlayerTest(int /* argc */, char* /*argv*/[])
558{
559 MITK_TEST_BEGIN("NavigationDataPlayer");
560 std::string tmp = "";
561
565 //mitkNavigationDataPlayerTestClass::TestStartPlayingExceptions();
567 //mitkNavigationDataPlayerTestClass::TestInvalidStream();
568
569 // always end with this!
570 MITK_TEST_END();
571}
572
573
574
static mitk::NavigationDataSet::Pointer GetNavigationDataSetFromXML(std::string filename)
An object of this class represents an exception of the MITK-IGT module.
An object of this class represents an exception of the MITK-IGT module which are releated to the inpu...
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
int mitkNavigationDataPlayerTest(int, char *[])