Procházet zdrojové kódy

Merge pull request #42 from michael-jeulinl/ctkCore-unitTests

Add unit test in ctkCore & increase code coverage
Julien Finet před 13 roky
rodič
revize
f0ed78f02a

+ 4 - 2
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -12,8 +12,6 @@ ctkMacroBuildLib(
 GET_TARGET_PROPERTY(ctkDummyPluginPATH "CTKDummyPlugin" LOCATION)
 REMOVE_DEFINITIONS(-DCTKDummyPlugin)
 
-
-
 SET(KIT ${PROJECT_NAME})
 
 #
@@ -31,6 +29,7 @@ SET(KITTests_SRCS
   ctkErrorLogModelTest1.cpp
   ctkErrorLogModelTest2.cpp
   ctkErrorLogModelTest3.cpp
+  ctkErrorLogModelTest4.cpp
   ctkHistogramTest1.cpp
   ctkLoggerTest1.cpp
   ctkModelTesterTest1.cpp
@@ -49,6 +48,7 @@ SET(KITTests_SRCS
   ctkSingletonTest1.cpp
   ctkTransferFunctionTest1.cpp
   ctkTransferFunctionRepresentationTest1.cpp
+  ctkTransferFunctionRepresentationTest2.cpp
   ctkWorkflowTest1.cpp
   ctkWorkflowTest2.cpp
   ctkWorkflowTest3.cpp
@@ -134,6 +134,7 @@ SIMPLE_TEST( ctkDependencyGraphTest2 )
 SIMPLE_TEST( ctkErrorLogModelTest1 )
 SIMPLE_TEST( ctkErrorLogModelTest2 )
 SIMPLE_TEST( ctkErrorLogModelTest3 )
+SIMPLE_TEST( ctkErrorLogModelTest4 )
 SIMPLE_TEST( ctkHistogramTest1 )
 SIMPLE_TEST( ctkLoggerTest1 )
 SET_TESTS_PROPERTIES(ctkErrorLogModelTest3 PROPERTIES PASS_REGULAR_EXPRESSION
@@ -145,6 +146,7 @@ SIMPLE_TEST( ctkScopedCurrentDirTest1 )
 SIMPLE_TEST( ctkSingletonTest1 )
 SIMPLE_TEST( ctkTransferFunctionTest1 )
 SIMPLE_TEST( ctkTransferFunctionRepresentationTest1 )
+SIMPLE_TEST( ctkTransferFunctionRepresentationTest2 )
 SIMPLE_TEST( ctkUtilsClosestPowerOfTenTest1 )
 SIMPLE_TEST( ctkUtilsOrderOfMagnitudeTest1 )
 SIMPLE_TEST( ctkUtilsSignificantDecimalsTest1 )

+ 91 - 6
Libs/Core/Testing/Cpp/ctkCheckableModelHelperTest1.cpp

@@ -60,20 +60,104 @@ int ctkCheckableModelHelperTest1(int argc, char * argv [] )
       row2[0]->checkState() != Qt::Unchecked)
     {
     std::cerr << "QStandardItem default failed: "
-	      << static_cast<int>(row0[0]->checkState()) << " "
+              << static_cast<int>(row0[0]->checkState()) << " "
               << static_cast<int>(row1[0]->checkState()) << " "
               << static_cast<int>(row2[0]->checkState()) << std::endl;
     return EXIT_FAILURE;
     }
 
-  // Header is checked by default
+  // CheckForce & Default model
+  QStandardItemModel modelForce;
+  modelForce.appendRow(row0);
+
+  QModelIndex modelIndex;
+  ctkCheckableModelHelper* modelHelperCF =
+    new ctkCheckableModelHelper(Qt::Horizontal);
+
+  modelHelperCF->setForceCheckability(true);
+  if (!modelHelperCF->forceCheckability())
+    {
+    std::cerr << "ctkCheckableModelHelper::setForceCheckability() failed: "
+              << static_cast<int>(modelHelperCF->forceCheckability()) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  modelHelperCF->isCheckable(modelIndex);
+  modelHelperCF->toggleCheckState(modelIndex);
+
+  modelHelperCF->setForceCheckability(false);
+  if (modelHelperCF->forceCheckability())
+    {
+    std::cerr << "ctkCheckableModelHelper::setForceCheckability() failed: "
+              << static_cast<int>(modelHelperCF->forceCheckability()) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  modelHelperCF->setCheckState(modelIndex, Qt::Unchecked);
+
+  modelHelperCF->setDefaultCheckState(Qt::Checked);
+  if (modelHelperCF->defaultCheckState() != Qt::Checked)
+    {
+    std::cerr << "ctkCheckableModelHelper::setDefaultCheckState() failed: "
+              << static_cast<int>(modelHelperCF->defaultCheckState()) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  modelHelperCF->setRootIndex(modelIndex);
+  modelHelperCF->setForceCheckability(true);
+  modelHelperCF->setCheckState(modelIndex, Qt::Checked);
+  Qt::CheckState statutCheck = Qt::Checked;
+  if (modelHelperCF->checkState(modelIndex, statutCheck))
+    {
+    std::cerr << "ctkCheckableModelHelper::setCheckState() failed: "
+            << static_cast<int>(modelHelperCF->checkState(modelIndex, statutCheck)) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  modelHelperCF->checkState(modelIndex);
+
+  modelHelperCF->setModel(&modelForce);
+  if (!modelHelperCF->model())
+    {
+    std::cerr << "ctkCheckableModelHelper::setModel() failed: "
+              << "is null" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  modelHelperCF->setCheckState(modelIndex, Qt::Checked);
+  if (modelHelperCF->checkState(modelIndex, statutCheck))
+    {
+    std::cerr << "ctkCheckableModelHelper::setCheckState() failed: "
+            << static_cast<int>(modelHelperCF->checkState(modelIndex, statutCheck)) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+    // Row & Column dummy insert
+  QList<QStandardItem*> col0;
+  col0 << new QStandardItem << new QStandardItem << new QStandardItem;
+  col0[0]->setText("not user checkable");
+  modelForce.appendColumn(col0);
+  modelForce.appendRow(row1);
+
+  delete modelHelperCF;
+
+  ctkCheckableModelHelper* modelHelperRC =
+    new ctkCheckableModelHelper(Qt::Vertical);
+  modelHelperRC->setForceCheckability(true);
+  modelHelperRC->setModel(&modelForce);
+  modelForce.appendColumn(col0);
+  modelForce.appendRow(row1);
+
+  //Header is checked by default
   model.setHeaderData(0, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole);
 
   ctkCheckableModelHelper* modelHelper =
     new ctkCheckableModelHelper(Qt::Horizontal);
   modelHelper->setModel(&model);
+
   // propagatetoitems is true by default
   //modelHelper->setPropagateToItems(true);
+  modelHelper->toggleHeaderCheckState(-1);
 
   // As propagateToItems is true, once the model is set to the modelHelper,
   // the checkable header is updated from the check state of all the items
@@ -85,7 +169,7 @@ int ctkCheckableModelHelperTest1(int argc, char * argv [] )
     {
     std::cerr << "ctkCheckableModelHelper::checkstate() failed: "
               << static_cast<int>(modelHelper->headerCheckState(0)) << " "
-	      << static_cast<int>(row0[0]->checkState()) << " "
+              << static_cast<int>(row0[0]->checkState()) << " "
               << static_cast<int>(row1[0]->checkState()) << " "
               << static_cast<int>(row2[0]->checkState()) << std::endl;
     return EXIT_FAILURE;
@@ -119,7 +203,7 @@ int ctkCheckableModelHelperTest1(int argc, char * argv [] )
     {
     std::cerr << "ctkCheckableModelHelper::propagateToItems() failed: "
               << static_cast<int>(modelHelper->headerCheckState(0)) << " "
-	      << static_cast<int>(row0[0]->checkState()) << " "
+              << static_cast<int>(row0[0]->checkState()) << " "
               << static_cast<int>(row1[0]->checkState()) << " "
               << static_cast<int>(row2[0]->checkState()) << std::endl;
     return EXIT_FAILURE;
@@ -135,8 +219,8 @@ int ctkCheckableModelHelperTest1(int argc, char * argv [] )
       row2[0]->checkState() != Qt::Unchecked)
     {
     std::cerr << __LINE__ << " ctkCheckableModelHelper::toggleCheckState() failed: "
-              << static_cast<int>(modelHelper->headerCheckState(0)) << " "
-	      << static_cast<int>(row0[0]->checkState()) << " "
+              << static_cast<int>(modelHelper->headerCheckState(0))
+              << " "        << static_cast<int>(row0[0]->checkState()) << " "
               << static_cast<int>(row1[0]->checkState()) << " "
               << static_cast<int>(row2[0]->checkState()) << std::endl;
     return EXIT_FAILURE;
@@ -253,5 +337,6 @@ int ctkCheckableModelHelperTest1(int argc, char * argv [] )
     return EXIT_FAILURE;
     }
 
+  delete modelHelper;
   return EXIT_SUCCESS;
 }

+ 61 - 0
Libs/Core/Testing/Cpp/ctkErrorLogModelTest4.cpp

@@ -0,0 +1,61 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QCoreApplication>
+#include <QDebug>
+#include <QStringList>
+
+// CTK includes
+#include "ctkErrorLogModel.h"
+#include "ctkErrorLogQtMessageHandler.h"
+#include "ctkModelTester.h"
+
+// STL includes
+#include <cstdlib>
+#include <iostream>
+
+int ctkErrorLogModelTest4(int argc, char * argv [])
+{
+  QCoreApplication app(argc, argv);
+  Q_UNUSED(app);
+  ctkErrorLogModel model;
+  ctkModelTester modelTester;
+  modelTester.setVerbose(false);
+
+  try
+    {
+    model.enableAllMsgHandler();
+    model.msgHandlerNames();
+    model.filterEntry();
+    model.filterEntry(ctkErrorLogModel::Fatal, true);
+
+    model.logLevelFilter();
+    model.logEntryGrouping();
+    }
+  catch (const char* error)
+    {
+    model.disableAllMsgHandler();
+    std::cerr << error << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}

+ 130 - 0
Libs/Core/Testing/Cpp/ctkTransferFunctionRepresentationTest2.cpp

@@ -0,0 +1,130 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QCoreApplication>
+#include <QColor>
+
+// CTK includes
+#include "ctkTransferFunction.h"
+#include "ctkTransferFunctionRepresentation.h"
+
+// STL includes
+#include <cstdlib>
+#include <iostream>
+
+class ctkForcedTransferFunction: public ctkTransferFunction
+{
+public:
+  ctkForcedTransferFunction(QObject* parent = 0):ctkTransferFunction(parent), _discrete(true) {}
+  virtual ~ctkForcedTransferFunction(){}
+
+  // attributes test
+  bool _discrete;
+
+  virtual ctkControlPoint* controlPoint(int index)const
+  {
+    QColor rgb = QColor::fromRgbF(55, 56, 57);
+    ctkControlPoint* cp = new ctkControlPoint();
+    cp->P.X = index;
+    cp->P.Value = rgb;
+    return cp;
+  }
+  virtual QVariant value(qreal pos)const
+  {
+    QColor rgb = QColor::fromRgbF(55, static_cast<int>(pos), 57);
+    return rgb;
+  }
+
+  virtual int count()const
+  {
+    return 2;
+  }
+  virtual bool isDiscrete()const
+  {
+    return _discrete;
+  }
+  virtual bool isEditable()const
+  {
+    return false;
+  }
+
+  virtual void range(qreal& minRange, qreal& maxRange)const
+  {
+    minRange = 0.;
+    maxRange = 1.;
+  }
+  virtual QVariant minValue()const
+  {
+    return 0.;
+  }
+  virtual QVariant maxValue()const
+  {
+    return 0.;
+  }
+  virtual int insertControlPoint(const ctkControlPoint& cp)
+  {
+    Q_UNUSED(cp);
+    return -1;
+  }
+
+  virtual int insertControlPoint(qreal pos)
+  {
+    Q_UNUSED(pos);
+    return -1;
+  }
+
+  virtual void removeControlPoint( qreal pos )
+  {
+    Q_UNUSED(pos);
+  }
+
+  virtual void setControlPointPos(int index, qreal pos)
+  {
+    Q_UNUSED(pos);
+    Q_UNUSED(index);
+  }
+  virtual void setControlPointValue(int index, const QVariant& value)
+  {
+    Q_UNUSED(index);
+    Q_UNUSED(value);
+  }
+};
+
+int ctkTransferFunctionRepresentationTest2( int argc, char * argv [])
+{
+  Q_UNUSED(argc);
+  Q_UNUSED(argv);
+
+  ctkForcedTransferFunction* dummy = new ctkForcedTransferFunction();
+  ctkTransferFunctionRepresentation representation;
+
+  representation.setTransferFunction(dummy);
+  representation.computeCurve();
+  representation.computeGradient();
+
+  dummy->_discrete = false;
+  representation.setTransferFunction(dummy);
+  representation.computeCurve();
+  representation.computeGradient();
+
+  delete dummy;
+  return EXIT_SUCCESS;
+}

+ 41 - 1
Libs/Core/ctkCheckableModelHelper.cpp

@@ -97,6 +97,11 @@ Qt::CheckState ctkCheckableModelHelperPrivate::checkState(
   const QModelIndex& index, bool *checkable)const
 {
   Q_Q(const ctkCheckableModelHelper);
+  if (!q->model())
+    {
+    qWarning() << "Model has not been set.";
+    return q->defaultCheckState();
+    }
   QVariant indexCheckState = index != q->rootIndex() ?
     q->model()->data(index, Qt::CheckStateRole):
     q->model()->headerData(0, q->orientation(), Qt::CheckStateRole);
@@ -108,7 +113,12 @@ void ctkCheckableModelHelperPrivate::setCheckState(
   const QModelIndex& modelIndex, Qt::CheckState newCheckState)
 {
   Q_Q(ctkCheckableModelHelper);
-  if (modelIndex != q->rootIndex())
+  if (!q->model())
+    {
+    qWarning() << "Model has not been set.";
+    return;
+    }
+  else if (modelIndex != q->rootIndex())
     {
     q->model()->setData(modelIndex, newCheckState, Qt::CheckStateRole);
     }
@@ -604,18 +614,33 @@ void ctkCheckableModelHelper::onRowsInserted(const QModelIndex &parentIndex,
 //-----------------------------------------------------------------------------
 bool ctkCheckableModelHelper::isHeaderCheckable(int section)const
 {
+  if (!this->model())
+    {
+    qWarning() << "ctkCheckableModelHelper::isHeaderCheckable : Model has not been set";
+    return (this->forceCheckability() && section == 0);
+    }
   return !this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).isNull();
 }
 
 //-----------------------------------------------------------------------------
 bool ctkCheckableModelHelper::isCheckable(const QModelIndex& index)const
 {
+  if (!this->model())
+    {
+    qWarning() << "ctkCheckableModelHelper::isCheckable : Model has not been set";
+    return (this->forceCheckability() && index.column() == 0);
+    }
   return !this->model()->data(index, Qt::CheckStateRole).isNull();
 }
 
 //-----------------------------------------------------------------------------
 Qt::CheckState ctkCheckableModelHelper::headerCheckState(int section)const
 {
+  if (!this->model())
+    {
+    qWarning() << "ctkCheckableModelHelper::headerCheckState : Model has not been set";
+    return this->defaultCheckState();
+    }
   return static_cast<Qt::CheckState>(
     this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).toInt());
 }
@@ -623,6 +648,11 @@ Qt::CheckState ctkCheckableModelHelper::headerCheckState(int section)const
 //-----------------------------------------------------------------------------
 Qt::CheckState ctkCheckableModelHelper::checkState(const QModelIndex& index)const
 {
+  if (!this->model())
+    {
+    qWarning() << "ctkCheckableModelHelper::checkState : Model has not been set";
+    return this->defaultCheckState();
+    }
   return static_cast<Qt::CheckState>(
     this->model()->data(index, Qt::CheckStateRole).toInt());
 }
@@ -631,6 +661,11 @@ Qt::CheckState ctkCheckableModelHelper::checkState(const QModelIndex& index)cons
 bool ctkCheckableModelHelper::headerCheckState(int section, Qt::CheckState& checkState)const
 {
   bool checkable = false;
+  if (!this->model())
+    {
+    qWarning() << "ctkCheckableModelHelper::headerCheckState : Model has not been set";
+    return (this->forceCheckability() && section == 0);
+    }
   checkState = static_cast<Qt::CheckState>(
     this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).toInt(&checkable));
   return checkable;
@@ -640,6 +675,11 @@ bool ctkCheckableModelHelper::headerCheckState(int section, Qt::CheckState& chec
 bool ctkCheckableModelHelper::checkState(const QModelIndex& index, Qt::CheckState& checkState)const
 {
   bool checkable = false;
+  if (!this->model())
+    {
+    qWarning() << "ctkCheckableModelHelper::checkState : Model has not been set";
+    return (this->forceCheckability() && index.column() == 0);
+    }
   checkState = static_cast<Qt::CheckState>(
     this->model()->data(index, Qt::CheckStateRole).toInt(&checkable));
   return checkable;