Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDRO_tests / TestViewer.cxx
index 4fc29d343263750c2d268e89d4594fd7ae39d505..96eaec9030fb8af7154b31e822d530c164c64bc6 100644 (file)
@@ -5,7 +5,9 @@
 #include <OCCViewer_ViewWindow.h>
 #include <AIS_InteractiveContext.hxx>
 #include <AIS_Shape.hxx>
+#include <TopoDS_Iterator.hxx>
 #include <QDir>
+#include <cppunit/TestAssert.h>
 
 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
@@ -44,21 +46,76 @@ Handle(AIS_InteractiveContext) context()
   return TestViewer::viewer()->getAISContext();
 }
 
-void TestViewer::show( const TopoDS_Shape& theShape, bool isFitAll )
+void TestViewer::show( const TopoDS_Shape& theShape, const QColor& theColor, int theMode )
 {
-  context()->EraseAll();
+  QColor aColor = theColor;
+  if( !aColor.isValid() )
+  {
+    // random color
+    int aHue = rand()%255;
+    aColor = QColor::fromHsl( aHue, 255, 128 );
+  }
+
+  double r = aColor.red() / 255.0;
+  double g = aColor.green() / 255.0;
+  double b = aColor.blue() / 255.0;
+
   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
-  context()->Display( aShape, AIS_Shaded, 0, Standard_False );
+  aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
+  aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
+  context()->Display( aShape, theMode, 0, Standard_False );
+}
+
+void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll )
+{
+  context()->EraseAll();
+  
+  if( theShape.ShapeType()==TopAbs_COMPOUND )
+  {
+    TopoDS_Iterator anIt( theShape );
+    for( ; anIt.More(); anIt.Next() )
+      show( anIt.Value(), QColor(), theMode );
+  }
+  else
+    show( theShape, QColor(), theMode );
 
   viewWindow()->onTopView();
   viewWindow()->onFitAll();
 }
 
-void TestViewer::dump( const TopoDS_Shape& theShape, const QString& theName )
+bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance = 0.0 )
 {
-  show( theShape );
-  QImage anImage = viewWindow()->dumpView();
+  if( theImage1.isNull() || theImage2.isNull() )
+    return theImage1.isNull() == theImage2.isNull();
+
+  if( theImage1.size() != theImage2.size() )
+    return false;
+
+  int aBytesCount = theImage1.byteCount();
+  const uchar *aBytes1 = theImage1.constBits();
+  const uchar *aBytes2 = theImage2.constBits();
+  for( int i=0; i<aBytesCount; i++ )
+    if( aBytes1[i] != aBytes2[i] )
+      return false;
+
+  return true;
+}
+
+bool TestViewer::AssertEqual( const QString& theUseCaseName )
+{
+  QImage anActualImage = viewWindow()->dumpView();
+
+  QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
+  anExpectedRefFilePath += "/" + theUseCaseName + ".png";
+  QImage anExpectedRefImage; 
+  anExpectedRefImage.load( anExpectedRefFilePath );
+
+  if( AreImagesEqual( anActualImage, anExpectedRefImage ) )
+    return true;
 
-  QString aPath = QDir::tempPath() + "/" + theName + ".png";
-  anImage.save( aPath );
+  QString aPath = QDir::tempPath() + "/" + theUseCaseName + ".png";
+  anActualImage.save( aPath );
+  std::string aMessage = "The viewer contents does not correspond to the reference image: " + theUseCaseName.toStdString();
+  CPPUNIT_FAIL( aMessage.c_str() );
+  return false;
 }