Salome HOME
refs #1341: debug of automatic tests
[modules/hydro.git] / src / HYDRO_tests / test_HYDROGUI_BathymetryPrs.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_HYDROGUI_BathymetryPrs.h>
20 #include <HYDROGUI_ShapeBathymetry.h>
21 #include <TestViewer.h>
22 #include <AIS_ColorScale.hxx>
23 #include <OCCViewer_ViewWindow.h>
24 #include <OCCViewer_ViewFrame.h>
25 #include <OCCViewer_ViewPort3d.h>
26 #include <QTest>
27
28 extern QString REF_DATA_PATH;
29 const double EPS = 1E-3;
30
31 void test_HYDROGUI_BathymetryPrs::importTestBath( const Handle(HYDROData_Document)& theDoc )
32 {
33   myBath = Handle(HYDROData_Bathymetry)::DownCast( theDoc->CreateObject( KIND_BATHYMETRY ) );
34
35   QString fname = (REF_DATA_PATH + "/bathy.xyz");
36   CPPUNIT_ASSERT( myBath->ImportFromFiles( QStringList() << fname ) );
37
38   HYDROData_Bathymetry::AltitudePoints anAltitudePoints = myBath->GetAltitudePoints();
39   CPPUNIT_ASSERT_EQUAL( 14781, (int)anAltitudePoints.size() );
40 }
41
42 void test_HYDROGUI_BathymetryPrs::createBathPrs()
43 {
44   myBathPrs = new HYDROGUI_ShapeBathymetry( 0, TestViewer::context(), myBath );
45   myBathPrs->Build();
46   myBathPrs->getAISObjects()[0]->SetAutoHilight( Standard_False );
47
48   double min, max;
49   myBathPrs->GetRange( min, max );
50   CPPUNIT_ASSERT_DOUBLES_EQUAL( 175.56, min, EPS );
51   CPPUNIT_ASSERT_DOUBLES_EQUAL( 185.65, max, EPS );
52 }
53
54 void test_HYDROGUI_BathymetryPrs::updateColors()
55 {
56   double min, max;
57   myBathPrs->GetRange( min, max );
58
59   TestViewer::colorScale()->SetRange( min, max );
60   myBathPrs->UpdateWithColorScale( TestViewer::colorScale() );
61 }
62
63 void test_HYDROGUI_BathymetryPrs::test_presentation()
64 {
65   TestViewer::eraseAll( true, true );
66   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
67   
68   importTestBath( aDoc );
69   TestViewer::showColorScale( false );
70
71   createBathPrs();
72   updateColors();
73
74   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
75   CPPUNIT_ASSERT( !bprs.IsNull() );
76
77   TestViewer::show( bprs, 0, 0, true, "bathy_prs" );
78   CPPUNIT_ASSERT_IMAGES
79   //QTest::qWait( 50000 );
80
81   aDoc->Close();
82   TestViewer::eraseAll( true, true );
83 }
84
85 void select( int x1, int y1, int x2, int y2 )
86 {
87   QPoint p1( x1, y1 ), p2( x2, y2 );
88   QWidget* w = TestViewer::viewWindow()->getViewPort();
89   QTest::mousePress( w, Qt::LeftButton, Qt::NoModifier, p1 );
90   QTest::mouseEvent( QTest::MouseMove, w, Qt::LeftButton, Qt::NoModifier, p2 );
91   QTest::mouseRelease( w, Qt::LeftButton, Qt::NoModifier, p2 );
92   qApp->processEvents();
93 }
94
95 void test_HYDROGUI_BathymetryPrs::test_selection()
96 {
97   TestViewer::eraseAll( true, true );
98   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
99
100   importTestBath( aDoc );
101   TestViewer::showColorScale( false );
102
103   createBathPrs();
104   updateColors();
105
106   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
107   CPPUNIT_ASSERT( !bprs.IsNull() );
108
109   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
110   CPPUNIT_ASSERT_IMAGES;
111
112   select( 150, 5, 350, 140 );
113   TestViewer::setKey( "bathy_selection" );
114   CPPUNIT_ASSERT_IMAGES
115
116   select( 5, 5, 6, 6 );
117   TestViewer::setKey( "bathy_prs" );
118   CPPUNIT_ASSERT_IMAGES
119
120   //QTest::qWait( 50000 );
121
122   aDoc->Close();
123   TestViewer::eraseAll( true, true );
124 }
125
126 void test_HYDROGUI_BathymetryPrs::test_rescale_by_selection()
127 {
128   TestViewer::eraseAll( true, true );
129   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
130
131   importTestBath( aDoc );
132   TestViewer::showColorScale( false );
133
134   createBathPrs();
135   updateColors();
136
137   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
138   CPPUNIT_ASSERT( !bprs.IsNull() );
139
140   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
141   CPPUNIT_ASSERT_IMAGES;
142
143   // 1. Rescale by selection
144   select( 150, 100, 350, 125 );
145   myBathPrs->RescaleBySelection();
146   updateColors(); // In HYDRO GUI it should be done by displayer
147
148   //QTest::qWait( 50000 );
149
150   double min, max;
151   myBathPrs->GetRange( min, max );
152   CPPUNIT_ASSERT_DOUBLES_EQUAL( 177.78, min, EPS );
153   CPPUNIT_ASSERT_DOUBLES_EQUAL( 182.70, max, EPS );
154
155   TestViewer::colorScale()->GetRange( min, max );
156   CPPUNIT_ASSERT_DOUBLES_EQUAL( 177.78, min, EPS );
157   CPPUNIT_ASSERT_DOUBLES_EQUAL( 182.70, max, EPS );
158
159   TestViewer::setKey( "bathy_rescaled_selection" );
160   CPPUNIT_ASSERT_IMAGES;
161
162   //QTest::qWait( 50000 );
163
164   // 2. User rescale
165   myBathPrs->Rescale( 180, 181 );
166   updateColors(); // In HYDRO GUI it should be done by displayer
167
168   myBathPrs->GetRange( min, max );
169   CPPUNIT_ASSERT_DOUBLES_EQUAL( 180.0, min, EPS );
170   CPPUNIT_ASSERT_DOUBLES_EQUAL( 181.0, max, EPS );
171
172   TestViewer::colorScale()->GetRange( min, max );
173   CPPUNIT_ASSERT_DOUBLES_EQUAL( 180.0, min, EPS );
174   CPPUNIT_ASSERT_DOUBLES_EQUAL( 181.0, max, EPS );
175
176   TestViewer::setKey( "bathy_rescaled_user" );
177   CPPUNIT_ASSERT_IMAGES;
178
179   //QTest::qWait( 50000 );
180
181   aDoc->Close();
182   TestViewer::eraseAll( true, true );
183 }
184
185 void test_HYDROGUI_BathymetryPrs::test_rescale_by_visible()
186 {
187   TestViewer::eraseAll( true, true );
188   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
189
190   importTestBath( aDoc );
191   TestViewer::showColorScale( false );
192
193   createBathPrs();
194   updateColors();
195
196   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
197   CPPUNIT_ASSERT( !bprs.IsNull() );
198
199   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
200   CPPUNIT_ASSERT_IMAGES;
201
202   // Change aspect to see a part of bathymetry
203   OCCViewer_ViewPort3d* vp = TestViewer::viewWindow()->getViewPort();
204   Handle(V3d_View) v = vp->getView();
205   v->SetProj( 1, 1, 1 );
206   vp->fitRect( QRect( 500, 380, 100, 70 ) );
207   qApp->processEvents();
208
209   // Rescale to the visible part of bathymetry
210   myBathPrs->RescaleByVisible( TestViewer::viewWindow() );
211   updateColors(); // In HYDRO GUI it should be done by displayer
212   vp->fitAll(); // Necessary to see the whole presentation
213
214   // Check parameters
215   double min, max;
216   myBathPrs->GetRange( min, max );
217   CPPUNIT_ASSERT_DOUBLES_EQUAL( 176.18, min, EPS );
218   CPPUNIT_ASSERT_DOUBLES_EQUAL( 179.55, max, EPS );
219
220   TestViewer::colorScale()->GetRange( min, max );
221   CPPUNIT_ASSERT_DOUBLES_EQUAL( 176.18, min, EPS );
222   CPPUNIT_ASSERT_DOUBLES_EQUAL( 179.55, max, EPS );
223
224   TestViewer::setKey( "bathy_rescaled_visible" );
225   CPPUNIT_ASSERT_IMAGES;
226
227   //QTest::qWait( 50000 );
228   aDoc->Close();
229   TestViewer::eraseAll( true, true );
230 }
231
232 void test_HYDROGUI_BathymetryPrs::test_text_presentation()
233 {
234   TestViewer::eraseAll( true, true );
235   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
236
237   importTestBath( aDoc );
238   TestViewer::showColorScale( false );
239
240   createBathPrs();
241   updateColors();
242
243   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
244   CPPUNIT_ASSERT( !bprs.IsNull() );
245
246   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
247   CPPUNIT_ASSERT_IMAGES;
248
249   // Assign text labels
250   int x1 = 150, y1 = 100, x2 = 200, y2 = 150;
251   select( x1, y1, x2, y2 );
252   myBathPrs->TextLabels( true );
253   OCCViewer_ViewPort3d* vp = TestViewer::viewWindow()->getViewPort();
254   vp->fitRect( QRect( x1, y1, x2-x1, y2-y1 ) );
255
256   qApp->processEvents();
257
258   TestViewer::setKey( "bathy_text_labels" );
259   CPPUNIT_ASSERT_IMAGES;
260
261   // Disable text labels
262   myBathPrs->TextLabels( false );
263   vp->fitAll();
264   qApp->processEvents();
265   TestViewer::setKey( "bathy_prs" );
266   CPPUNIT_ASSERT_IMAGES;
267
268   // Special case: flag=false + non-empty selection
269   select( x1, y1, x2, y2 );
270   myBathPrs->TextLabels( false );
271   vp->fitAll();
272   qApp->processEvents();
273   TestViewer::setKey( "bathy_prs" );
274   CPPUNIT_ASSERT_IMAGES;
275
276
277   //QTest::qWait( 50000 );
278
279   aDoc->Close();
280   TestViewer::eraseAll( true, true );
281 }
282
283 void test_HYDROGUI_BathymetryPrs::test_rescale_default()
284 {
285   TestViewer::eraseAll( true, true );
286   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
287
288   importTestBath( aDoc );
289   TestViewer::showColorScale( false );
290
291   createBathPrs();
292   updateColors();
293
294   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
295   CPPUNIT_ASSERT( !bprs.IsNull() );
296
297   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
298   CPPUNIT_ASSERT_IMAGES;
299
300   double min, max;
301
302   // 1. User rescale
303   myBathPrs->Rescale( 180, 181 );
304   updateColors(); // In HYDRO GUI it should be done by displayer
305
306   myBathPrs->GetRange( min, max );
307   CPPUNIT_ASSERT_DOUBLES_EQUAL( 180.0, min, EPS );
308   CPPUNIT_ASSERT_DOUBLES_EQUAL( 181.0, max, EPS );
309
310   TestViewer::colorScale()->GetRange( min, max );
311   CPPUNIT_ASSERT_DOUBLES_EQUAL( 180.0, min, EPS );
312   CPPUNIT_ASSERT_DOUBLES_EQUAL( 181.0, max, EPS );
313
314   TestViewer::setKey( "bathy_rescaled_user" );
315   CPPUNIT_ASSERT_IMAGES;
316
317   //QTest::qWait( 50000 );
318
319   // 2. Default rescale
320   myBathPrs->RescaleDefault();
321   updateColors(); // In HYDRO GUI it should be done by displayer
322
323   myBathPrs->GetRange( min, max );
324   CPPUNIT_ASSERT_DOUBLES_EQUAL( 175.56, min, EPS );
325   CPPUNIT_ASSERT_DOUBLES_EQUAL( 185.65, max, EPS );
326
327   TestViewer::colorScale()->GetRange( min, max );
328   CPPUNIT_ASSERT_DOUBLES_EQUAL( 175.56, min, EPS );
329   CPPUNIT_ASSERT_DOUBLES_EQUAL( 185.65, max, EPS );
330
331   TestViewer::setKey( "bathy_prs" );
332   CPPUNIT_ASSERT_IMAGES;
333
334   //QTest::qWait( 50000 );
335
336   aDoc->Close();
337   TestViewer::eraseAll( true, true );
338 }
339
340 void test_HYDROGUI_BathymetryPrs::test_fit_on_selected()
341 {
342   TestViewer::eraseAll( true, true );
343   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
344
345   importTestBath( aDoc );
346   TestViewer::showColorScale( false );
347
348   createBathPrs();
349   updateColors();
350
351   Handle(AIS_InteractiveObject) bprs = myBathPrs->getAISObjects()[0];
352   CPPUNIT_ASSERT( !bprs.IsNull() );
353
354   TestViewer::show( bprs, 0, 1, true, "bathy_prs" );
355   CPPUNIT_ASSERT_IMAGES;
356
357   // Fit selected points on bathymetry
358   int x1 = 100, y1 = 50, x2 = 250, y2 = 200;
359   select( x1, y1, x2, y2 );
360   TestViewer::viewWindow()->onFitSelection();
361   TestViewer::setKey( "bathy_prs_fit_selected" );
362   CPPUNIT_ASSERT_IMAGES;
363
364   //QTest::qWait( 50000 );
365
366   aDoc->Close();
367   TestViewer::eraseAll( true, true );
368 }