Salome HOME
refs #1327: debug of automatic tests
[modules/hydro.git] / src / HYDRO_tests / test_HYDROData_OperationsFactory.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <test_HYDROData_OperationsFactory.h>
20
21 #include <HYDROData_Document.h>
22 #include <HYDROData_Image.h>
23 #include <HYDROData_PolylineXY.h>
24 #include <HYDROData_OperationsFactory.h>
25
26 #include <ImageComposer_CropOperator.h>
27
28 #include <gp_XY.hxx>
29
30 #include <QPainter>
31
32 void test_HYDROData_OperationsFactory::testCreate()
33 {
34   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
35   
36   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
37   CPPUNIT_ASSERT(aFactory);
38   Handle(HYDROData_Image) anImage = aFactory->CreateImage(aDoc, NULL);
39   CPPUNIT_ASSERT(!anImage.IsNull());
40   CPPUNIT_ASSERT(anImage->Image().isNull());
41   
42   aDoc->Close();
43 }
44
45 static QImage TestImage() {
46   QImage aPic(50, 40, QImage::Format_RGB32);
47   aPic.fill(Qt::white);
48   QPainter aPainter(&aPic);
49   aPainter.drawEllipse(6, 7, 38, 30);
50   aPainter.drawLine(0, 40, 10, 0);
51   aPainter.drawLine(10, 0, 25, 35);
52   aPainter.drawLine(25, 35, 40, 0);
53   aPainter.drawLine(40, 0, 50, 40);
54   return aPic;
55 }
56
57 void test_HYDROData_OperationsFactory::testCrop()
58 {
59   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
60   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
61   
62   // prepare the original image and crop-path
63   Handle(HYDROData_Image) anOriImage = 
64     Handle(HYDROData_Image)::DownCast( aDoc->CreateObject( KIND_IMAGE ) );
65
66   QImage aTestImage = TestImage();
67   anOriImage->SetImage( aTestImage );
68
69   Handle(HYDROData_PolylineXY) aCropPolyline = 
70     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
71
72   aCropPolyline->AddSection( "", HYDROData_PolylineXY::SECTION_POLYLINE, true );
73
74   aCropPolyline->AddPoint( 0, HYDROData_PolylineXY::Point( 25, 0  ) );
75   aCropPolyline->AddPoint( 0, HYDROData_PolylineXY::Point( 0,  20 ) );
76   aCropPolyline->AddPoint( 0, HYDROData_PolylineXY::Point( 25, 40 ) );
77   aCropPolyline->AddPoint( 0, HYDROData_PolylineXY::Point( 50, 20 ) );
78
79   // prepare Composer Operation
80   ImageComposer_Operator* aCropOp = 
81     aFactory->Operator( ImageComposer_CropOperator::Type() );
82   
83   CPPUNIT_ASSERT( aCropOp );
84
85   aCropOp->setArgs( Qt::red );
86
87   // create crop - image 
88   Handle(HYDROData_Image) aCropImage = aFactory->CreateImage( aDoc, aCropOp );
89   CPPUNIT_ASSERT( !aCropImage.IsNull() );
90
91   aCropImage->AppendReference( anOriImage );
92   aCropImage->AppendReference( aCropPolyline );
93   aCropImage->Update();
94
95   // check crop operation was performed
96   CPPUNIT_ASSERT( !aCropImage->Image().isNull() );
97   CPPUNIT_ASSERT( aCropImage->Image() != aTestImage );
98   
99   aDoc->Close();
100 }