COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-graphics-array.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 GraphicalArray class"
9 
10 #include <QHBoxLayout>
11 #include <QLineEdit>
12 #include <QListView>
13 #include <QPushButton>
14 #include <QSignalSpy>
15 #include <QStringListModel>
16 #include <QTest>
17 
18 #include "common/OptionArray.hpp"
19 #include "common/OptionT.hpp"
21 #include "common/URI.hpp"
22 
25 
26 #include "test/ui/Application.hpp"
27 
29 
30 using namespace cf3;
31 using namespace cf3::common;
32 using namespace cf3::ui::graphics;
33 
35 
36 QWidget * find_widget(const GraphicalArray* value, int row, int col)
37 {
38  // /!\ WARNING /!\
39  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
40  // TO THE GRAPHICAL LOOK OF GraphicalArray
41 
42  QGridLayout * layout = dynamic_cast<QGridLayout*>(value->layout()->itemAt(0)->widget()->layout());
43  QWidget * widget = nullptr;
44 
45  if( is_not_null(layout) )
46  widget = layout->itemAtPosition(row, col)->widget();
47  else
48  std::cerr << "Failed to find the layout or cast it to QGridLayout." << std::endl;
49 
50  return widget;
51 }
52 
54 
55 QLineEdit * find_line_edit(const GraphicalArray* value)
56 {
57  // /!\ WARNING /!\
58  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
59  // TO THE GRAPHICAL LOOK OF GraphicalArray
60 
61  QLineEdit * lineEdit = dynamic_cast<QLineEdit*>(find_widget(value, 0, 0));
62 
63  if( is_null(lineEdit) )
64  std::cerr << "Failed to find the line edit." << std::endl;
65 
66  return lineEdit;
67 }
68 
70 
71 QPushButton * find_remove_button(const GraphicalArray* value)
72 {
73  // /!\ WARNING /!\
74  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
75  // TO THE GRAPHICAL LOOK OF GraphicalArray
76 
77  QPushButton * button = dynamic_cast<QPushButton*>(find_widget(value, 1, 1));
78 
79  if( is_null(button) )
80  std::cerr << "Failed to find the remove button." << std::endl;
81 
82  return button;
83 }
84 
86 
87 
88 QListView * find_list_view(const GraphicalArray* value)
89 {
90  // /!\ WARNING /!\
91  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
92  // TO THE GRAPHICAL LOOK OF GraphicalArray
93 
94  QListView * listView = dynamic_cast<QListView*>(find_widget(value, 1, 0));
95 
96  if( is_null(listView) )
97  std::cerr << "Failed to find the list." << std::endl;
98 
99  return listView;
100 }
101 
103 
104 QStringListModel * find_model(const GraphicalArray* value)
105 {
106  // /!\ WARNING /!\
107  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
108  // TO THE GRAPHICAL LOOK OF GraphicalArray
109 
110  QListView * view = find_list_view(value);
111  QStringListModel * model = nullptr;
112 
113  if( is_not_null(view) )
114  {
115  model = dynamic_cast<QStringListModel*>( view->model() );
116 
117  if( is_null(model) )
118  std::cerr << "Failed to find the view model." << std::endl;
119  }
120 
121  return model;
122 }
123 
125 
126 BOOST_AUTO_TEST_SUITE( uiGraphicsGraphicalArraySuite )
127 
128 
131 {
132  application();
133 
135 
136  GraphicalArray * value = new GraphicalArray();
137 
142 
143  // check the find_* functions are valid with the current Graphical arrangement
144  BOOST_REQUIRE( is_not_null( find_line_edit(value) ) );
145  BOOST_REQUIRE( is_not_null( find_remove_button(value) ) );
146  BOOST_REQUIRE( is_not_null( find_list_view(value) ) );
147  BOOST_REQUIRE( is_not_null( find_model(value) ) );
148 
149  delete value;
150 }
151 
153 
154 BOOST_AUTO_TEST_CASE( constructor )
155 {
156  GraphicalArray * value = new GraphicalArray();
157  QStringListModel * model = find_model(value);
158 
159  // 1. value is empty, the line edit should be empty as well
160  BOOST_CHECK( is_not_null(model) );
161  BOOST_CHECK( model->stringList().empty() );
162 
163  delete value;
164 
165  QIntValidator * validator = new QIntValidator();
166  value = new GraphicalArray( validator );
167  QLineEdit * lineEdit = find_line_edit(value);
168 
169  // 2. validator objects should be the same
170  BOOST_CHECK( is_not_null(lineEdit) );
171  BOOST_CHECK_EQUAL( lineEdit->validator(), validator );
172 
173  delete value;
174  delete validator;
175 }
176 
178 
179 BOOST_AUTO_TEST_CASE( set_validator )
180 {
181  QValidator * validator = new QIntValidator();
182  GraphicalArray * value = new GraphicalArray( validator );
183  QLineEdit * lineEdit = find_line_edit(value);
184 
185  BOOST_CHECK( is_not_null(lineEdit) );
186 
187  // 1. validator objects should be the same
188  BOOST_CHECK_EQUAL( lineEdit->validator(), validator );
189 
190  // 2. try to set a null validator
191  value->set_validator(nullptr);
192  BOOST_CHECK_EQUAL( lineEdit->validator(), (QValidator*) nullptr );
193 
194  delete validator;
195  validator = new QDoubleValidator(nullptr);
196 
197  // 3. set a new validator
198  value->set_validator(validator);
199  BOOST_CHECK_EQUAL( lineEdit->validator(), validator );
200 
201 
202  delete validator;
203  delete value;
204 }
205 
207 
209 {
210  QString sep(";");
211  GraphicalArray * value = new GraphicalArray( new QIntValidator(), sep );
212  QLineEdit * lineEdit = find_line_edit(value);
213  QStringListModel * model = find_model(value);
214 
215  QStringList validList;
216  QStringList invalidList;
217  QStringList list;
218 
219  validList << "42" << "15465" << "-145314" << "42"; // duplicates should be kept
220  invalidList << "25" << "1" << "something" << "32" << "3.14" << "-54789";
221 
222  BOOST_CHECK( is_not_null(lineEdit) );
223 
224  //
225  // 1. check with strings
226  //
227  // 1a. list that only contains valid values
228  BOOST_CHECK( value->set_value( validList.join(sep) ) );
229 // BOOST_CHECK_EQUAL( model->stringList(), validList );
230  list = model->stringList();
231 
232  BOOST_CHECK_EQUAL( list.count(), validList.count() );
233 
234  for( int i = 0 ; i < list.count() ; ++i)
235  BOOST_CHECK_EQUAL( list.at(i).toStdString(), validList.at(i).toStdString() );
236 
237  // 1c. only one value
238  BOOST_CHECK( value->set_value( QString("1456789") ) );
239  BOOST_CHECK_EQUAL( model->stringList().count(), 1 );
240  BOOST_CHECK_EQUAL( model->stringList().at(0).toStdString(), std::string("1456789") );
241 
242  // 1b. list that contains some invalid values
243  BOOST_CHECK( !value->set_value( invalidList.join(sep) ) );
244  list = model->stringList();
245 
246  // "something" and "3.14" are invalid integers and so the value shouldn't have changed
247  BOOST_CHECK_EQUAL( model->stringList().count(), 1 );
248  BOOST_CHECK_EQUAL( model->stringList().at(0).toStdString(), std::string("1456789") );
249 
250  //
251  // 2. check with other types
252  //
253  BOOST_CHECK( !value->set_value(true) );
254  BOOST_CHECK_EQUAL( model->stringList().at(0).toStdString(), std::string("1456789") );
255 
256  BOOST_CHECK( !value->set_value(3.145) );
257  BOOST_CHECK_EQUAL( model->stringList().at(0).toStdString(), std::string("1456789") );
258 
259  delete value;
260 }
261 
263 
265 {
266  GraphicalArray * value = new GraphicalArray();
267  QStringListModel * model = find_model(value);
268  QStringList stringList;
269  QVariant theValue;
270  QStringList list;
271 
272  stringList << "hello" << "world" << "!";
273 
274  // the list should be empty
275  theValue = value->value();
276  BOOST_CHECK( theValue.type() == QVariant::StringList );
277 
278  list = theValue.toStringList();
279 
280  BOOST_CHECK_EQUAL( list.count(), 0 );
281 
282  for( int i = 0 ; i < list.count() ; ++i)
283  BOOST_CHECK_EQUAL( list.at(i).toStdString(), stringList.at(i).toStdString() );
284 
285  // set a stringlist, the list should have 3 items
286  model->setStringList( stringList );
287 
288  theValue = value->value();
289 
290  list = theValue.toStringList();
291 
292  BOOST_CHECK( theValue.type() == QVariant::StringList );
293 
294  BOOST_CHECK_EQUAL( list.count(), stringList.count() );
295 
296  for( int i = 0 ; i < list.count() ; ++i)
297  BOOST_CHECK_EQUAL( list.at(i).toStdString(), stringList.at(i).toStdString() );
298 
299  delete value;
300 }
301 
303 
304 BOOST_AUTO_TEST_CASE( signal_emitting )
305 {
306  GraphicalArray * value = new GraphicalArray();
307  QLineEdit * lineEdit = find_line_edit(value);
308  QStringListModel * model = find_model(value);
309  QSignalSpy spy(value, SIGNAL(value_changed()));
310 
311  //
312  // 1. through setValue()
313  //
314  value->set_value( QString("Hello World") ); // when the value is a string
315  value->set_value( QStringList() << "Hello" << "World" ); // when the value is a string list
316  value->set_value( 42 ); // when the value is not valid (no signal emitted)
317 
318  // 2 signals should have been emitted
319  BOOST_CHECK_EQUAL( spy.count(), 2 );
320 
321  spy.clear();
322 
323  // clear the model
324  model->setStringList( QStringList() );
325 
326  //
327  // 2. by simulating keyboard events
328  //
329  // note: when validating, we wait 25 ms (last parameter) to let the event being processed
330 // lineEdit->clear();
331 // value->show(); // make the value visible (it ignores keyboard events if not)
332 // lineEdit->setFocus(Qt::PopupFocusReason);
333 // QTest::keyClicks(lineEdit, "123" );
334 // QTest::keyClick(value, Qt::Key_Enter, Qt::NoModifier); // validate by pressing the 'ENTER' key on the keypad
335 // QTest::keyClicks(lineEdit, "156" );
336 // QTest::keyClicks(lineEdit, "456" );
337 // QTest::keyClick(value, Qt::Key_Return, Qt::NoModifier); // validate by pressing the 'Return' key
338 
339  // 2 signals should have been emitted (one per validation)
340 // BOOST_CHECK_EQUAL( spy.count(), 2 );
341 
342 // BOOST_CHECK_EQUAL( model->stringList().toStdString(), std::stringList() << "123" << "156456");
343 // BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string() );
344 
345  spy.clear();
346  //
347  // 3. when committing
348  //
349  value->commit();
350 
351  // 1 signal should have been emitted
352  BOOST_CHECK_EQUAL( spy.count(), 1 );
353 
354  delete value;
355 }
356 
358 
359 BOOST_AUTO_TEST_CASE( value_string )
360 {
361  GraphicalArray * value = new GraphicalArray(nullptr, ";");
362 
363  value->set_value( QString("Hello") );
364  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("Hello") );
365 
366  value->set_value( QStringList() << "Hello" << "World");
367  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("Hello;World") );
368 
369  delete value;
370 }
371 
373 
374 BOOST_AUTO_TEST_CASE( is_modified )
375 {
376  GraphicalArray * value = new GraphicalArray();
377  QStringListModel * model = find_model(value);
378 
379  // 1. initially, it's not modified
380  BOOST_CHECK( !value->is_modified() );
381 
382  // 2. change the value
383  model->setStringList( QStringList() << "Hello" << "World" );
384  BOOST_CHECK( value->is_modified() );
385 
386  // 3. change the value and commit
387  model->setStringList( QStringList() << "Hello" << "World" << "and" << "Happy" << "New" << "Year!" );
388  BOOST_CHECK( value->is_modified() );
389  value->commit();
390  BOOST_CHECK( !value->is_modified() );
391 
392  // 4. set the same value
393  model->setStringList( QStringList() << "Hello" << "World" << "and" << "Happy" << "New" << "Year!" );
394  BOOST_CHECK( !value->is_modified() );
395 
396  delete value;
397 }
398 
400 
401 BOOST_AUTO_TEST_CASE( remove_items )
402 {
403  GraphicalArray * value = new GraphicalArray();
404  QPushButton * button = find_remove_button(value);
405  QStringListModel * model = find_model(value);
406  QListView * view = find_list_view(value);
407  QItemSelectionModel::SelectionFlags flag = QItemSelectionModel::Select;
408 
409  model->setStringList( QStringList() << "Hello" << "World" << "and" << "Happy" << "New" << "Year!" );
410  value->show();
411 
412  view->selectionModel()->select( model->index(0), flag ); // select "Hello"
413  view->selectionModel()->select( model->index(1), flag ); // select "World"
414  view->selectionModel()->select( model->index(3), flag ); // select "Happy"
415  view->selectionModel()->select( model->index(5), flag ); // select "Year"
416 
417  // simulate a click on the 'Remove' button
418  QTest::mouseClick( button, Qt::LeftButton );
419 
420  QStringList values = model->stringList();
421 
422  BOOST_CHECK_EQUAL( values.count(), 2 );
423  BOOST_CHECK_EQUAL( values.at(0).toStdString(), std::string("and") );
424  BOOST_CHECK_EQUAL( values.at(1).toStdString(), std::string("New") );
425 
426  delete value;
427 }
428 
430 
431 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
bool AssertionThrows
assertions throw exceptions
Definition: Assertions.hpp:67
QLineEdit * find_line_edit(const GraphicalArray *value)
bool ExceptionOutputs
if exception contructor should output
Definition: Exception.hpp:32
static AssertionManager & instance()
Gets the instance of the manager.
Definition: Assertions.cpp:33
bool AssertionDumps
assertions dump backtraces
Definition: Assertions.hpp:65
QWidget * find_widget(const GraphicalArray *value, int row, int col)
virtual QVariant value() const
bool ExceptionDumps
if exception contructor should dump backtrace
Definition: Exception.hpp:34
Conversions from and to std::string.
void set_validator(QValidator *validator)
QListView * find_list_view(const GraphicalArray *value)
QStringListModel * find_model(const GraphicalArray *value)
QPushButton * find_remove_button(const GraphicalArray *value)
Uniform Resource Identifier (see http://en.wikipedia.org/wiki/Uniform_Resource_Identifier) ...
virtual bool set_value(const QVariant &path)
tuple model
Global confifuration.
BOOST_AUTO_TEST_CASE(init)
Top-level namespace for coolfluid.
Definition: Action.cpp:18
static ExceptionManager & instance()
Gets the instance of the manager.
Definition: Exception.cpp:34
static ThreadManager & instance()
Basic Classes for Graphics applications used by CF.
Most basic kernel library.
Definition: Action.cpp:19
bool is_not_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:147
Send comments to:
COOLFluiD Web Admin