17 #include "itkMersenneTwisterRandomVariateGenerator.h"
21 #include <itkComposeImageFilter.h>
22 #include <itkEventObject.h>
23 #include <itkImageDuplicator.h>
24 #include <itkImageIterator.h>
25 #include <itkMersenneTwisterRandomVariateGenerator.h>
30 class mitkLevelWindowManagerTestClass
33 static void TestInstantiation()
40 static void TestSetGetDataStorage()
50 manager->SetDataStorage(ds);
52 catch (std::exception &e)
61 static void TestMethodsWithInvalidParameters()
66 manager->SetDataStorage(ds);
72 manager->SetLevelWindowProperty(levelWindowProperty);
78 MITK_TEST_CONDITION(success,
"Testing mitk::LevelWindowManager SetLevelWindowProperty with invalid parameter");
81 static void TestOtherMethods()
86 manager->SetDataStorage(ds);
88 MITK_TEST_CONDITION(manager->isAutoTopMost(),
"Testing mitk::LevelWindowManager isAutoTopMost");
107 manager->SetAutoTopMostImage(
true);
108 MITK_TEST_CONDITION(manager->isAutoTopMost(),
"Testing mitk::LevelWindowManager isAutoTopMost()");
111 static void TestRemoveObserver(std::string testImageFile)
116 manager->SetDataStorage(ds);
121 node1->SetData(image1);
124 node2->SetData(image2);
130 static_cast<int>(manager->GetRelevantNodes()->size()) == manager->GetNumberOfObservers(),
131 "Test if number of nodes is similar to number of observers");
135 node3->SetData(image3);
139 static_cast<int>(manager->GetRelevantNodes()->size()) == manager->GetNumberOfObservers(),
140 "Test if number of nodes is similar to number of observers");
155 static bool VerifyRenderingModes()
165 static void TestLevelWindowSliderVisibility(std::string testImageFile)
167 bool renderingModesValid = mitkLevelWindowManagerTestClass::VerifyRenderingModes();
168 if (!renderingModesValid)
170 MITK_ERROR <<
"Exception: Image Rendering.Mode property value types inconsistent.";
176 manager->SetDataStorage(ds);
181 node1->SetData(image1);
187 std::vector<mitk::DataNode::Pointer> nodeVec;
189 nodeVec.push_back(node1);
190 nodeVec.push_back(node2);
191 nodeVec.push_back(node3);
193 typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType;
197 for (
unsigned int i = 0; i < 8; ++i)
199 unsigned int parity = i;
201 for (
unsigned int img = 0; img < 3; ++img)
205 int mode = rnd->GetIntegerVariate() % 3;
210 int mode = rnd->GetIntegerVariate() % 2;
217 renderingModesValid && ((!manager->GetLevelWindowProperty() && !i) || (manager->GetLevelWindowProperty() && i)),
218 "Testing level window property member according to rendering mode");
222 static void TestSetLevelWindowProperty(std::string testImageFile)
226 manager->SetDataStorage(ds);
233 node3->SetIntProperty(
"layer", 1);
234 node2->SetIntProperty(
"layer", 2);
235 node1->SetIntProperty(
"layer", 3);
237 manager->SetAutoTopMostImage(
true);
239 bool isImageForLevelWindow1, isImageForLevelWindow2, isImageForLevelWindow3;
240 node1->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow1);
241 node2->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow2);
242 node3->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow3);
244 MITK_TEST_CONDITION(isImageForLevelWindow1 && !isImageForLevelWindow2 && !isImageForLevelWindow3,
245 "Testing exclusive imageForLevelWindow property for node 1.");
247 manager->SetAutoTopMostImage(
false);
251 manager->SetLevelWindowProperty(prop);
252 node1->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow1);
253 node2->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow2);
254 node3->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow3);
256 MITK_TEST_CONDITION(!isImageForLevelWindow1 && isImageForLevelWindow2 && !isImageForLevelWindow3,
257 "Testing exclusive imageForLevelWindow property for node 2.");
260 manager->SetLevelWindowProperty(prop);
261 node1->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow1);
262 node2->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow2);
263 node3->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow3);
265 MITK_TEST_CONDITION(!isImageForLevelWindow1 && !isImageForLevelWindow2 && isImageForLevelWindow3,
266 "Testing exclusive imageForLevelWindow property for node 3.");
269 manager->SetLevelWindowProperty(prop);
270 node1->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow1);
271 node2->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow2);
272 node3->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow3);
274 MITK_TEST_CONDITION(isImageForLevelWindow1 && !isImageForLevelWindow2 && !isImageForLevelWindow3,
275 "Testing exclusive imageForLevelWindow property for node 3.");
278 static void TestImageForLevelWindowOnVisibilityChange(std::string testImageFile)
282 manager->SetDataStorage(ds);
289 node3->SetIntProperty(
"layer", 1);
290 node2->SetIntProperty(
"layer", 2);
291 node1->SetIntProperty(
"layer", 3);
293 manager->SetAutoTopMostImage(
false);
295 bool isImageForLevelWindow1, isImageForLevelWindow2, isImageForLevelWindow3;
296 node1->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow1);
297 node2->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow2);
298 node3->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow3);
300 MITK_TEST_CONDITION(isImageForLevelWindow1 && !isImageForLevelWindow2 && !isImageForLevelWindow3,
301 "Testing initial imageForLevelWindow setting.");
303 node1->SetVisibility(
false);
304 node1->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow1);
305 node2->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow2);
306 node3->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow3);
308 MITK_TEST_CONDITION(!isImageForLevelWindow1 && isImageForLevelWindow2 && !isImageForLevelWindow3,
309 "Testing exclusive imageForLevelWindow property for node 2.");
311 node2->SetVisibility(
false);
312 node1->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow1);
313 node2->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow2);
314 node3->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow3);
316 MITK_TEST_CONDITION(!isImageForLevelWindow1 && !isImageForLevelWindow2 && isImageForLevelWindow3,
317 "Testing exclusive imageForLevelWindow property for node 3.");
319 node3->SetVisibility(
false);
320 node1->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow1);
321 node2->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow2);
322 node3->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow3);
324 MITK_TEST_CONDITION(!isImageForLevelWindow1 && !isImageForLevelWindow2 && isImageForLevelWindow3,
325 "Testing exclusive imageForLevelWindow property for node 3.");
327 node1->SetVisibility(
true);
328 node1->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow1);
329 node2->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow2);
330 node3->GetBoolProperty(
"imageForLevelWindow", isImageForLevelWindow3);
332 MITK_TEST_CONDITION(isImageForLevelWindow1 && !isImageForLevelWindow2 && !isImageForLevelWindow3,
333 "Testing exclusive imageForLevelWindow property for node 3.");
336 static void TestImageForLevelWindowOnRandomPropertyChange(std::string testImageFile)
338 typedef std::vector<bool> BoolVecType;
339 typedef BoolVecType::iterator IteratorType;
340 typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType;
345 manager->SetDataStorage(ds);
351 node3->SetIntProperty(
"layer", 1);
352 node2->SetIntProperty(
"layer", 2);
353 node1->SetIntProperty(
"layer", 3);
356 std::vector<bool> nodesVisible;
357 nodesVisible.resize(3);
358 std::fill(nodesVisible.begin(), nodesVisible.end(),
true);
361 std::vector<bool> nodesForLevelWindow;
362 nodesForLevelWindow.resize(3);
363 std::fill(nodesForLevelWindow.begin(), nodesForLevelWindow.end(),
false);
366 std::vector<mitk::DataNode::Pointer> nodes;
367 nodes.push_back(node1);
368 nodes.push_back(node2);
369 nodes.push_back(node3);
372 manager->SetAutoTopMostImage(
false);
374 bool lvlWin1, lvlWin2, lvlWin3;
375 node1->GetBoolProperty(
"imageForLevelWindow", lvlWin1);
376 node2->GetBoolProperty(
"imageForLevelWindow", lvlWin2);
377 node3->GetBoolProperty(
"imageForLevelWindow", lvlWin3);
379 MITK_TEST_CONDITION(lvlWin1 && !lvlWin2 && !lvlWin3,
"Testing initial imageForLevelWindow setting.");
381 nodesForLevelWindow[0] = lvlWin1;
382 nodesForLevelWindow[1] = lvlWin2;
383 nodesForLevelWindow[2] = lvlWin3;
387 ranGen->Initialize();
391 int invalidCount = 0;
392 int mustHaveLvlWindow = 4;
393 for (
int run = 0; run < ranCount; ++run)
396 int ran = ranGen->GetIntegerVariate(2);
397 nodes[ran]->SetBoolProperty(
"imageForLevelWindow", !nodesForLevelWindow[ran]);
400 std::vector<bool>::const_iterator found = std::find(nodesForLevelWindow.begin(), nodesForLevelWindow.end(),
true);
401 if (found == nodesForLevelWindow.end())
407 found = std::find(nodesVisible.begin(), nodesVisible.end(),
true);
409 if (!nodesForLevelWindow[ran])
411 mustHaveLvlWindow = pow(2, 2 - ran);
415 mustHaveLvlWindow = 4;
419 node1->GetBoolProperty(
"imageForLevelWindow", lvlWin1);
420 node2->GetBoolProperty(
"imageForLevelWindow", lvlWin2);
421 node3->GetBoolProperty(
"imageForLevelWindow", lvlWin3);
422 nodesForLevelWindow[0] = lvlWin1;
423 nodesForLevelWindow[1] = lvlWin2;
424 nodesForLevelWindow[2] = lvlWin3;
426 int hasLevelWindow = 0;
427 for (
int i = 0; i < 3; ++i)
429 if (nodesForLevelWindow[i])
431 hasLevelWindow += pow(2, 2 - i);
435 validCount += hasLevelWindow == mustHaveLvlWindow ? 1 : 0;
439 while (falseran == 0)
441 falseran = ranGen->GetIntegerVariate(7);
443 BoolVecType falseNodes;
444 falseNodes.push_back(falseran & 1 == 1 ? !lvlWin1 : lvlWin1);
446 falseNodes.push_back(falseran & 1 == 1 ? !lvlWin2 : lvlWin2);
448 falseNodes.push_back(falseran & 1 == 1 ? !lvlWin3 : lvlWin3);
449 int falseLevelWindow = 0;
450 for (
int i = 0; i < 3; ++i)
454 falseLevelWindow += pow(2, 2 - i);
458 invalidCount += falseLevelWindow == mustHaveLvlWindow ? 0 : 1;
461 mustHaveLvlWindow = hasLevelWindow;
464 MITK_TEST_CONDITION(validCount == ranCount,
"Testing proper node for level window property.");
468 static void TestImageForLevelWindowOnRandomVisibilityChange(std::string testImageFile)
470 typedef std::vector<bool> BoolVecType;
471 typedef BoolVecType::iterator IteratorType;
472 typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType;
477 manager->SetDataStorage(ds);
483 node3->SetIntProperty(
"layer", 1);
484 node2->SetIntProperty(
"layer", 2);
485 node1->SetIntProperty(
"layer", 3);
488 std::vector<bool> nodesVisible;
489 nodesVisible.resize(3);
490 std::fill(nodesVisible.begin(), nodesVisible.end(),
true);
493 std::vector<bool> nodesForLevelWindow;
494 nodesForLevelWindow.resize(3);
495 std::fill(nodesForLevelWindow.begin(), nodesForLevelWindow.end(),
false);
498 std::vector<mitk::DataNode::Pointer> nodes;
499 nodes.push_back(node1);
500 nodes.push_back(node2);
501 nodes.push_back(node3);
504 manager->SetAutoTopMostImage(
false);
506 bool lvlWin1, lvlWin2, lvlWin3;
507 node1->GetBoolProperty(
"imageForLevelWindow", lvlWin1);
508 node2->GetBoolProperty(
"imageForLevelWindow", lvlWin2);
509 node3->GetBoolProperty(
"imageForLevelWindow", lvlWin3);
511 MITK_TEST_CONDITION(lvlWin1 && !lvlWin2 && !lvlWin3,
"Testing initial imageForLevelWindow setting.");
513 nodesForLevelWindow[0] = lvlWin1;
514 nodesForLevelWindow[1] = lvlWin2;
515 nodesForLevelWindow[2] = lvlWin3;
519 ranGen->Initialize();
523 int invalidCount = 0;
524 int mustHaveLvlWindow = 4;
525 for (
int run = 0; run < ranCount; ++run)
528 int ran = ranGen->GetIntegerVariate(2);
529 nodesVisible[ran] = !nodesVisible[ran];
530 nodes[ran]->SetVisibility(nodesVisible[ran]);
533 std::vector<bool>::const_iterator found = std::find(nodesForLevelWindow.begin(), nodesForLevelWindow.end(),
true);
534 if (found == nodesForLevelWindow.end())
538 int ind = found - nodesForLevelWindow.begin();
541 found = std::find(nodesVisible.begin(), nodesVisible.end(),
true);
542 bool allInvisible = (found == nodesVisible.end());
545 if (!allInvisible && !nodesVisible[ind])
548 for (std::vector<bool>::const_iterator it = nodesVisible.begin(); it != nodesVisible.end(); ++it, ++count)
552 mustHaveLvlWindow = pow(2, 2 - count);
559 node1->GetBoolProperty(
"imageForLevelWindow", lvlWin1);
560 node2->GetBoolProperty(
"imageForLevelWindow", lvlWin2);
561 node3->GetBoolProperty(
"imageForLevelWindow", lvlWin3);
562 nodesForLevelWindow[0] = lvlWin1;
563 nodesForLevelWindow[1] = lvlWin2;
564 nodesForLevelWindow[2] = lvlWin3;
566 int hasLevelWindow = 0;
567 for (
int i = 0; i < 3; ++i)
569 if (nodesForLevelWindow[i])
571 hasLevelWindow += pow(2, 2 - i);
575 if (hasLevelWindow != mustHaveLvlWindow)
580 validCount += hasLevelWindow == mustHaveLvlWindow ? 1 : 0;
584 while (falseran == 0)
586 falseran = ranGen->GetIntegerVariate(7);
588 BoolVecType falseNodes;
589 falseNodes.push_back(falseran & 1 == 1 ? !lvlWin1 : lvlWin1);
591 falseNodes.push_back(falseran & 1 == 1 ? !lvlWin2 : lvlWin2);
593 falseNodes.push_back(falseran & 1 == 1 ? !lvlWin3 : lvlWin3);
594 int falseLevelWindow = 0;
595 for (
int i = 0; i < 3; ++i)
599 falseLevelWindow += pow(2, 2 - i);
603 invalidCount += falseLevelWindow == mustHaveLvlWindow ? 0 : 1;
606 mustHaveLvlWindow = hasLevelWindow;
609 MITK_TEST_CONDITION(validCount == ranCount,
"Testing proper node for level window property.");
619 std::string testImage = args[1];
621 mitkLevelWindowManagerTestClass::TestInstantiation();
622 mitkLevelWindowManagerTestClass::TestSetGetDataStorage();
623 mitkLevelWindowManagerTestClass::TestMethodsWithInvalidParameters();
624 mitkLevelWindowManagerTestClass::TestOtherMethods();
625 mitkLevelWindowManagerTestClass::TestRemoveObserver(testImage);
626 mitkLevelWindowManagerTestClass::TestLevelWindowSliderVisibility(testImage);
627 mitkLevelWindowManagerTestClass::TestSetLevelWindowProperty(testImage);
628 mitkLevelWindowManagerTestClass::TestImageForLevelWindowOnVisibilityChange(testImage);
629 mitkLevelWindowManagerTestClass::TestImageForLevelWindowOnRandomVisibilityChange(testImage);
630 mitkLevelWindowManagerTestClass::TestImageForLevelWindowOnRandomPropertyChange(testImage);
itk::SmartPointer< Self > Pointer
The LevelWindowProperty class Property for the mitk::LevelWindow.
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
#define MITK_TEST_OUTPUT(x)
Output some text.
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
#define MITK_TEST_CONDITION(COND, MSG)
int mitkLevelWindowManagerTest(int argc, char *args[])
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.