COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-graphics-value.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2011 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for the GUI GraphicalValue class"
9 
10 #include <QGridLayout>
11 #include <QLineEdit>
12 #include <QValidator>
13 #include <QtTest>
14 
15 #include <boost/assign/std/vector.hpp>
16 
17 #include "common/OptionT.hpp"
18 #include "common/OptionArray.hpp"
19 
20 #include "math/Consts.hpp"
21 
31 
33 
34 #include "test/ui/Application.hpp"
35 
37 
38 using namespace boost::assign; // for operator += ()
39 using namespace cf3;
40 using namespace cf3::common;
41 using namespace cf3::ui::graphics;
42 
44 
45 const QValidator * find_array_validator(const GraphicalArray* array)
46 {
47  // /!\ WARNING /!\
48  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
49  // OF THE GRAPHICAL LOOK OF GraphicalArray
50 
51  // array->layout()->itemAt(0)->widget() is the QGroupBox of the GraphicalArray
52  // we know it has a QGridLayout
53  QGridLayout * layout = dynamic_cast<QGridLayout*>(array->layout()->itemAt(0)->widget()->layout());
54  const QValidator * validator = nullptr;
55 
56  if( is_not_null(layout) )
57  {
58  QLineEdit * lineEdit = dynamic_cast<QLineEdit*>(layout->itemAtPosition(0, 0)->widget());
59 
60  if( is_null(lineEdit) )
61  std::cerr << "Failed to find the line edit." << std::endl;
62  else
63  validator = lineEdit->validator();
64  }
65  else
66  std::cerr << "Failed to find the layout or cast it to QGridLayout." << std::endl;
67 
68  return validator;
69 }
70 
72 
73 BOOST_AUTO_TEST_SUITE( uiGraphicsGraphicalValueSuite )
74 
75 
78 {
79  application();
80 
81  AssertionManager::instance().AssertionDumps = false;
82  AssertionManager::instance().AssertionThrows = true;
83  ExceptionManager::instance().ExceptionDumps = false;
84  ExceptionManager::instance().ExceptionOutputs = false;
85 }
86 
88 
89 BOOST_AUTO_TEST_CASE( create_from_option )
90 {
91  boost::shared_ptr<Option> option;
92  GraphicalValue * value = nullptr;
93 
94  //
95  // 1. null option, should get a null pointer
96  //
97  BOOST_CHECK_EQUAL( GraphicalValue::create_from_option(boost::shared_ptr< Option >()), (GraphicalValue*) nullptr );
98 
99  //
100  // 2. check bool, should get a GraphicalBool
101  //
102  option = boost::shared_ptr< OptionT<bool> >(new OptionT<bool>("OptBool", true));
103  value = GraphicalValue::create_from_option( option );
104  BOOST_CHECK( is_not_null(value) );
105  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalBool*>(value) ) );
106  delete value;
107  value = nullptr;
108 
109  //
110  // 3. check int, should get a GraphicalInt
111  //
112  option = boost::shared_ptr< OptionT<int> >(new OptionT<int>("OptInt", int(-156754)));
113  value = GraphicalValue::create_from_option( option );
114  BOOST_CHECK( is_not_null(value) );
115  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalInt*>(value) ) );
116  delete value;
117  value = nullptr;
118 
119  //
120  // 4. check Uint, should get a GraphicalInt
121  //
122  option = boost::shared_ptr< OptionT<Uint> >(new OptionT<Uint>("OptUint", Uint(42)));
123  value = GraphicalValue::create_from_option( option );
124  BOOST_CHECK( is_not_null(value) );
125  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalInt*>(value) ) );
126  delete value;
127  value = nullptr;
128 
129  //
130  // 5. check Uint, should get a GraphicalReal
131  //
132  option = boost::shared_ptr< OptionT<Real> >(new OptionT<Real>("OptReal", Real(3.141592)));
133  value = GraphicalValue::create_from_option( option );
134  BOOST_CHECK( is_not_null(value) );
135  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalDouble*>(value) ) );
136  delete value;
137  value = nullptr;
138 
139  //
140  // 6. check str::string, should get a GraphicalString
141  //
142  option = boost::shared_ptr< OptionT<std::string> >(new OptionT<std::string>("OptString", std::string()));
143  value = GraphicalValue::create_from_option( option );
144  BOOST_CHECK( is_not_null(value) );
145  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalString*>(value) ) );
146  delete value;
147  value = nullptr;
148 
149  //
150  // 7. check URI, should get a GraphicalUri
151  //
152  option = boost::shared_ptr< OptionURI >(new OptionURI("OptUri", URI()));
153  value = GraphicalValue::create_from_option( option );
154  BOOST_CHECK( is_not_null(value) );
155  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalUri*>(value) ) );
156  delete value;
157  value = nullptr;
158 }
159 
161 
162 BOOST_AUTO_TEST_CASE( create_from_option_array )
163 {
164  boost::shared_ptr<Option> option;
165  GraphicalValue * value = nullptr;
166  const QValidator * validator;
167 
168  //
169  // 1. null option, should get a null pointer
170  //
171  BOOST_CHECK_EQUAL( GraphicalValue::create_from_option(boost::shared_ptr< Option >()), (GraphicalValue*) nullptr );
172 
173  //
174  // 2. check bool, should get a GraphicalArray
175  //
176  option = boost::shared_ptr< OptionArray<bool> >(new OptionArray<bool>("OptBool", std::vector<bool>()));
177  value = GraphicalValue::create_from_option( option );
178 
179  // 2a. check that it is a GraphicalArray
180  BOOST_CHECK( is_not_null(value) );
181  BOOST_CHECK( is_not_null( dynamic_cast<GraphicalArray*>(value) ) );
182 
183  // 2b. check that a validator is present
184  validator = find_array_validator(dynamic_cast<GraphicalArray*>(value));
185  BOOST_CHECK_MESSAGE( is_not_null(validator),
186  "No validator found. Please check the warning above." );
187 
188  // 2c. check its a QRegExpValidator with the correct string
189  {
190  const QRegExpValidator * val = dynamic_cast<const QRegExpValidator*>(validator);
191  BOOST_CHECK( is_not_null(val) );
192  BOOST_CHECK_EQUAL( val->regExp().pattern().toStdString(),
193  std::string("(true)|(false)|(1)|(0)|(on)|(off)") );
194  }
195  delete value;
196  value = nullptr;
197 
198  //
199  // 3. check int, should get a GraphicalArray
200  //
201  option = boost::shared_ptr< OptionArray<int> >(new OptionArray<int>("OptInt", std::vector<int>()));
202  value = GraphicalValue::create_from_option( option );
203 
204  // 3a. check that it is a GraphicalArray
205  BOOST_CHECK( is_not_null(value) );
206  BOOST_CHECK( is_not_null( dynamic_cast<GraphicalArray*>(value) ) );
207 
208  // 3b. check that a validator is present
209  validator = find_array_validator(dynamic_cast<GraphicalArray*>(value));
210  BOOST_CHECK_MESSAGE( is_not_null(validator), "No validator found. Please check the warning above." );
211 
212  // 2c. check its a QIntValidator
213  {
214  const QIntValidator * val = dynamic_cast<const QIntValidator*>(validator);
215  BOOST_CHECK( is_not_null(val) );
216  }
217 
218  delete value;
219  value = nullptr;
220 
221  //
222  // 4. check Uint, should get a GraphicalArray
223  //
224  option = boost::shared_ptr< OptionArray<Uint> >(new OptionArray<Uint>("OptUint", std::vector<Uint>()));
225  value = GraphicalValue::create_from_option( option );
226 
227  // 4a. check that it is a GraphicalArray
228  BOOST_CHECK( is_not_null(value) );
229  BOOST_CHECK( is_not_null( dynamic_cast<GraphicalArray*>(value) ) );
230 
231  // 4b. check that a validator is present
232  validator = find_array_validator(dynamic_cast<GraphicalArray*>(value));
233  BOOST_CHECK_MESSAGE( is_not_null(validator), "No validator found. Please check the warning above." );
234 
235  // 4c. check its a QIntValidator and the bottom is 0
236  {
237  const QIntValidator * val = dynamic_cast<const QIntValidator*>(validator);
238  BOOST_CHECK( is_not_null(val) );
239  BOOST_CHECK_EQUAL( val->bottom(), 0);
240  }
241 
242  delete value;
243  value = nullptr;
244 
245  //
246  // 5. check Real, should get a GraphicalArray
247  //
248  option = boost::shared_ptr< OptionArray<Real> >(new OptionArray<Real>("OptReal", std::vector<Real>()));
249  value = GraphicalValue::create_from_option( option );
250 
251  // 5a. check that it is a GraphicalArray
252  BOOST_CHECK( is_not_null(value) );
253  BOOST_CHECK( is_not_null( dynamic_cast<GraphicalArray*>(value) ) );
254 
255  // 5b. check that a validator is present
256  validator = find_array_validator(dynamic_cast<GraphicalArray*>(value));
257  BOOST_CHECK_MESSAGE( is_not_null(validator), "No validator found. Please check the warning above." );
258 
259  // 5c. check its a QDoubleValidator
260  {
261  const QDoubleValidator * val = dynamic_cast<const QDoubleValidator*>(validator);
262  BOOST_CHECK( is_not_null(val) );
263  }
264 
265  delete value;
266  value = nullptr;
267 
268  //
269  // 6. check str::string, should get a GraphicalArray
270  //
271  option = boost::shared_ptr< OptionArray<std::string> >(new OptionArray<std::string>("OptString", std::vector<std::string>()));
272  value = GraphicalValue::create_from_option( option );
273 
274  // 6a. check that it is a GraphicalArray
275  BOOST_CHECK( is_not_null(value) );
276  BOOST_CHECK( is_not_null( dynamic_cast<GraphicalArray*>(value) ) );
277 
278  // 6b. check that *no* validator is present
279  validator = find_array_validator(dynamic_cast<GraphicalArray*>(value));
280  BOOST_CHECK_MESSAGE( is_null(validator), "Validator found. We should not have a validator here.");
281 
282  delete value;
283  value = nullptr;
284 
285  //
286  // 7. check URI, should get a GraphicalUriArray
287  //
288  option = boost::shared_ptr< OptionArray<URI> >(new OptionArray<URI>("OptString", std::vector<URI>()));
289  value = GraphicalValue::create_from_option( option );
290 
291  // check that is is a GraphicalUriArray
292  BOOST_CHECK( is_not_null(value) );
293  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalUriArray*>(value) ) );
294  delete value;
295  value = nullptr;
296 }
297 
299 
300 BOOST_AUTO_TEST_CASE( create_from_option_restr_values )
301 {
302  boost::shared_ptr<Option> option;
303  GraphicalValue * value = nullptr;
304 
305  //
306  // 1. check bool
307  //
308  option = boost::shared_ptr< OptionT<bool> >(new OptionT<bool>("OptBool", true));
309  option->restricted_list() += false;
310  value = GraphicalValue::create_from_option( option );
311  BOOST_CHECK( is_not_null(value) );
312  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalRestrictedList*>(value) ) );
313  delete value;
314  value = nullptr;
315 
316  //
317  // 2. check int
318  //
319  option = boost::shared_ptr< OptionT<int> >(new OptionT<int>("OptInt", int(-156754)));
320  option->restricted_list() += int(47687876);
321  value = GraphicalValue::create_from_option( option );
322  BOOST_CHECK( is_not_null(value) );
323  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalRestrictedList*>(value) ) );
324  delete value;
325  value = nullptr;
326 
327  //
328  // 3. check Uint
329  //
330  option = boost::shared_ptr< OptionT<Uint> >(new OptionT<Uint>("OptUint", Uint(42)));
331  option->restricted_list() += Uint(314);
332  value = GraphicalValue::create_from_option( option );
333  BOOST_CHECK( is_not_null(value) );
334  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalRestrictedList*>(value) ) );
335  delete value;
336  value = nullptr;
337 
338  //
339  // 4. check Real
340  //
341  option = boost::shared_ptr< OptionT<Real> >(new OptionT<Real>("OptReal", Real(3.141592)));
342  option->restricted_list() += Real(2.71);
343  value = GraphicalValue::create_from_option( option );
344  BOOST_CHECK( is_not_null(value) );
345  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalRestrictedList*>(value) ) );
346  delete value;
347  value = nullptr;
348 
349  //
350  // 5. check str::string
351  //
352  option = boost::shared_ptr< OptionT<std::string> >(new OptionT<std::string>("OptString", std::string()));
353  option->restricted_list() += std::string("Hello, World!");
354  value = GraphicalValue::create_from_option( option );
355  BOOST_CHECK( is_not_null(value) );
356  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalRestrictedList*>(value) ) );
357  delete value;
358  value = nullptr;
359 
360  //
361  // 6. check URI
362  //
363  option = boost::shared_ptr< OptionURI >(new OptionURI("OptUri", URI("http://www.google.com")));
364  option->restricted_list().push_back( URI("cpath:/") );
365  value = GraphicalValue::create_from_option( option );
366  BOOST_CHECK( is_not_null(value) );
367  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalRestrictedList*>(value) ) );
368  delete value;
369  value = nullptr;
370 }
371 
373 
374 BOOST_AUTO_TEST_CASE( create_from_option_array_restr_values )
375 {
376  boost::shared_ptr<Option> option;
377  GraphicalValue * value = nullptr;
378 
379  //
380  // 1. check bool
381  //
382  std::vector<bool> vectBool;
383  vectBool.push_back(true);
384  option = boost::shared_ptr< OptionArray<bool> >(new OptionArray<bool>("OptBool", vectBool));
385  option->restricted_list() += false;
386  BOOST_CHECK_NO_THROW(value = GraphicalValue::create_from_option( option ));
387  BOOST_CHECK( is_not_null(value) );
388  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalArrayRestrictedList*>(value) ) );
389  delete value;
390  value = nullptr;
391 
392  //
393  // 2. check int
394  //
395  std::vector<int> vectInt;
396  vectInt.push_back(-154786);
397  option = boost::shared_ptr< OptionArray<int> >(new OptionArray<int>("OptInt", vectInt));
398  option->restricted_list() += int(47687876);
399  value = GraphicalValue::create_from_option( option );
400  BOOST_CHECK( is_not_null(value) );
401  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalArrayRestrictedList*>(value) ) );
402  delete value;
403  value = nullptr;
404 
405  //
406  // 3. check Uint
407  //
408  std::vector<Uint> vectUint;
409  vectUint.push_back(42);
410  option = boost::shared_ptr< OptionArray<Uint> >(new OptionArray<Uint>("OptUint", vectUint));
411  option->restricted_list() += Uint(3654614);
412  value = GraphicalValue::create_from_option( option );
413  BOOST_CHECK( is_not_null(value) );
414  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalArrayRestrictedList*>(value) ) );
415  delete value;
416  value = nullptr;
417 
418  //
419  // 4. check Real
420  //
421  std::vector<Real> vectReal;
422  vectReal.push_back(3.141592);
423  option = boost::shared_ptr< OptionArray<Real> >(new OptionArray<Real>("OptReal", vectReal));
424  option->restricted_list() += Real(2.71);
425  value = GraphicalValue::create_from_option( option );
426  BOOST_CHECK( is_not_null(value) );
427  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalArrayRestrictedList*>(value) ) );
428  delete value;
429  value = nullptr;
430 
431  //
432  // 5. check str::string
433  //
434  std::vector<std::string> vectString;
435  vectString.push_back("Hello");
436  option = boost::shared_ptr< OptionArray<std::string> >(new OptionArray<std::string>("OptString", vectString));
437  option->restricted_list() += std::string(", World!");
438  value = GraphicalValue::create_from_option( option );
439  BOOST_CHECK( is_not_null(value) );
440  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalArrayRestrictedList*>(value) ) );
441  delete value;
442  value = nullptr;
443 
444  //
445  // 6. check URI
446  //
447  std::vector<URI> vectUri;
448  vectUri.push_back( URI("http://coolfluidsrv.vki.ac.be") );
449  option = boost::shared_ptr< OptionArray<URI> >(new OptionArray<URI>("OptUri", vectUri));
450  option->restricted_list() += URI("cpath:/");
451  value = GraphicalValue::create_from_option( option );
452  BOOST_CHECK( is_not_null(value) );
453  BOOST_CHECK( is_not_null(dynamic_cast<GraphicalArrayRestrictedList*>(value) ) );
454  delete value;
455  value = nullptr;
456 }
457 
459 
460 BOOST_AUTO_TEST_SUITE_END()
QApplication * application()
Definition: Application.hpp:11
bool is_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:151
Top-level namespace for coolfluid.
Definition: Action.cpp:18
BOOST_AUTO_TEST_CASE(init)
Basic Classes for Graphics applications used by CF.
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
Most basic kernel library.
Definition: Action.cpp:19
const QValidator * find_array_validator(const GraphicalArray *array)
bool is_not_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:147
Send comments to:
COOLFluiD Web Admin