COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-graphics-uri.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 GraphicalString class"
9 
10 #include <QComboBox>
11 #include <QLineEdit>
12 #include <QHBoxLayout>
13 #include <QSignalSpy>
14 #include <QTest>
15 
16 #include "math/Consts.hpp"
17 
19 
20 #include "test/ui/Application.hpp"
21 
23 
24 using namespace cf3;
25 using namespace cf3::common;
26 using namespace cf3::math;
27 using namespace cf3::ui::graphics;
28 
30 
31 QWidget * find_widget(const GraphicalUri* value, int index)
32 {
33  // /!\ WARNING /!\
34  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
35  // OF THE GRAPHICAL LOOK OF GraphicalUri
36 
37  QHBoxLayout * layout = dynamic_cast<QHBoxLayout*>(value->layout());
38  QWidget * widget = nullptr;
39 
40  if( is_not_null(layout) )
41  widget = layout->itemAt(index)->widget();
42  else
43  std::cerr << "Failed to find the layout or cast it to QHBoxLayout." << std::endl;
44 
45  return widget;
46 }
47 
49 
50 QLineEdit * find_line_edit(const GraphicalUri* value)
51 {
52  // /!\ WARNING /!\
53  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
54  // OF THE GRAPHICAL LOOK OF GraphicalUri
55 
56  QLineEdit * lineEdit = dynamic_cast<QLineEdit*>(find_widget(value, 1));
57 
58  if( is_null(lineEdit) )
59  std::cerr << "Failed to find the line edit." << std::endl;
60 
61  return lineEdit;
62 }
63 
65 
66 QComboBox * find_combo_box(const GraphicalUri* value)
67 {
68  // /!\ WARNING /!\
69  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
70  // OF THE GRAPHICAL LOOK OF GraphicalUri
71 
72  QComboBox * comboBox = dynamic_cast<QComboBox*>(find_widget(value, 0));
73 
74  if( is_null(comboBox) )
75  std::cerr << "Failed to find the combo box." << std::endl;
76 
77  return comboBox;
78 }
79 
81 
82 BOOST_AUTO_TEST_SUITE( uiGraphicsGraphicalBoolSuite )
83 
84 
87 {
88  application();
89  GraphicalUri * value = new GraphicalUri();
90 
95 
96  BOOST_CHECK( is_not_null( find_line_edit(value) ) );
97  BOOST_CHECK( is_not_null( find_combo_box(value) ) );
98 
99  delete value;
100 }
101 
103 
104 BOOST_AUTO_TEST_CASE( constructor )
105 {
106  GraphicalUri * value = new GraphicalUri();
107  QLineEdit * lineEdit = find_line_edit(value);
108 
109  // 1. value is empty, the line edit should be empty as well
110  BOOST_CHECK( is_not_null(lineEdit) );
111  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string() );
112 
113  delete value;
114  boost::shared_ptr<OptionURI> option(new OptionURI("Option", URI("cpath:/")));
115  value = new GraphicalUri(option);
116  lineEdit = find_line_edit(value);
117 
118  // 2. value is not empty
119  BOOST_CHECK( is_not_null(lineEdit) );
120  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("cpath:/") );
121 
122  delete value;
123 }
124 
126 
127 BOOST_AUTO_TEST_CASE( set_schemes )
128 {
129  boost::shared_ptr<OptionURI> option(new OptionURI("Option", URI("cpath:/")));
130  GraphicalUri * value = new GraphicalUri(option);
131  QComboBox * comboBox = find_combo_box(value);
132  std::vector<URI::Scheme::Type> schemes;
133 
134  BOOST_CHECK( is_not_null(comboBox) );
135 
136  // 1. all protocols are used by default
137  BOOST_CHECK_EQUAL( comboBox->count(), 3);
138  BOOST_CHECK_EQUAL( comboBox->itemText(0).toStdString(), std::string("cpath") );
139  BOOST_CHECK_EQUAL( comboBox->itemText(1).toStdString(), std::string("file") );
140  BOOST_CHECK_EQUAL( comboBox->itemText(2).toStdString(), std::string("http") );
141 
142  // 2. some protocols
143  schemes.push_back(URI::Scheme::CPATH);
144  schemes.push_back(URI::Scheme::FILE);
145 
146  value->set_schemes(schemes);
147  BOOST_CHECK_EQUAL( comboBox->count(), 2);
148  BOOST_CHECK_EQUAL( comboBox->itemText(0).toStdString(), std::string("cpath") );
149  BOOST_CHECK_EQUAL( comboBox->itemText(1).toStdString(), std::string("file") );
150 
151  // 3. some protocols where one appears twice
152  schemes.clear();
153  schemes.push_back(URI::Scheme::CPATH);
154  schemes.push_back(URI::Scheme::FILE);
155  schemes.push_back(URI::Scheme::CPATH);
156 
157  value->set_schemes(schemes);
158  BOOST_CHECK_EQUAL( comboBox->count(), 2);
159  BOOST_CHECK_EQUAL( comboBox->itemText(0).toStdString(), std::string("cpath") );
160  BOOST_CHECK_EQUAL( comboBox->itemText(1).toStdString(), std::string("file") );
161 
162  // 4. giving an empty vector should enable all schemes
163  schemes.clear();
164  value->set_schemes(schemes);
165 
166  BOOST_CHECK_EQUAL( comboBox->count(), 3);
167  BOOST_CHECK_EQUAL( comboBox->itemText(0).toStdString(), std::string("cpath") );
168  BOOST_CHECK_EQUAL( comboBox->itemText(1).toStdString(), std::string("file") );
169  BOOST_CHECK_EQUAL( comboBox->itemText(2).toStdString(), std::string("http") );
170 
171  delete value;
172 }
173 
175 
177 {
178  boost::shared_ptr<OptionURI> option(new OptionURI("Option", URI("cpath:/")));
179  GraphicalUri * value = new GraphicalUri(option);
180  QLineEdit * lineEdit = find_line_edit(value);
181 
182  BOOST_CHECK( is_not_null(lineEdit) );
183 
184  //
185  // 1. check with strings
186  //
187  BOOST_CHECK( value->set_value("cpath:/Component") );
188  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("cpath:/Component") );
189 
190  BOOST_CHECK( value->set_value("coolfluidsrv.vki.ac.be") );
191  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("cpath:coolfluidsrv.vki.ac.be") );
192 
193  //
194  // 2. check with other types
195  //
196  BOOST_CHECK( !value->set_value(12) );
197  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("cpath:coolfluidsrv.vki.ac.be") );
198 
199  BOOST_CHECK( !value->set_value(-421) );
200  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("cpath:coolfluidsrv.vki.ac.be") );
201 
202  BOOST_CHECK( !value->set_value(3.141592) );
203  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("cpath:coolfluidsrv.vki.ac.be") );
204 
205  BOOST_CHECK( !value->set_value(true) );
206  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("cpath:coolfluidsrv.vki.ac.be") );
207 
208  delete value;
209 }
210 
212 
214 {
215  boost::shared_ptr<OptionURI> option(new OptionURI("Option", URI("cpath:/")));
216  GraphicalUri * value = new GraphicalUri(option);
217  QLineEdit * lineEdit = find_line_edit(value);
218  QComboBox * comboBox = find_combo_box(value);
219  QVariant theValue;
220 
221  value->show();
222 
223  // change the scheme
224  comboBox->setCurrentIndex( comboBox->findText("file") );
225 
226  // 1. get value when the scheme exists in the line edit
227  theValue = value->value();
228  BOOST_CHECK( theValue.type() == QVariant::String );
229  BOOST_CHECK_EQUAL( theValue.toString().toStdString(), std::string("cpath:/") );
230 
231  // 2. get value when the scheme is determined by the combo box
232  lineEdit->setText("/etc/fstab");
233  theValue = value->value();
234  BOOST_CHECK( theValue.type() == QVariant::String );
235  BOOST_CHECK_EQUAL( theValue.toString().toStdString(), std::string("file:/etc/fstab") );
236 
237  // change the scheme again
238  comboBox->setCurrentIndex( comboBox->findText("http") );
239 
240  lineEdit->setText("coolfluidsrv.vki.ac.be");
241  theValue = value->value();
242  BOOST_CHECK( theValue.type() == QVariant::String );
243  BOOST_CHECK_EQUAL( theValue.toString().toStdString(), std::string("http:coolfluidsrv.vki.ac.be") );
244 
245  // 3. line edit is empty, should get an empty string
246  lineEdit->setText("");
247  theValue = value->value();
248  BOOST_CHECK( theValue.type() == QVariant::String );
249  BOOST_CHECK_EQUAL( theValue.toString().toStdString(), std::string() );
250 
251  delete value;
252 }
253 
255 
256 BOOST_AUTO_TEST_CASE( signal_emitting )
257 {
258  GraphicalUri * value = new GraphicalUri();
259  QLineEdit * lineEdit = find_line_edit(value);
260  QSignalSpy spy(value, SIGNAL(value_changed()));
261 
262  //
263  // 1. through setValue()
264  //
265  value->set_value("cpath:/");
266  value->set_value("file:/etc/fstab");
267  value->set_value(12);
268 
269  // 2 signals should have been emitted
270  BOOST_CHECK_EQUAL( spy.count(), 2 );
271 
272  spy.clear();
273 
274  //
275  // 2. by simulating keyboard events
276  //
277  lineEdit->clear();
278  value->show(); // make the value visible (it ignores keyboard events if not)
279  QTest::keyClicks(lineEdit, "cpath:" );
280  QTest::keyClicks(lineEdit, "//Path" );
281  QTest::keyClicks(lineEdit, "/To/A/Component" );
282 
283  // 32 signals should have been emitted (one per character)
284  BOOST_CHECK_EQUAL( spy.count(), 28 );
285 
286  spy.clear();
287  //
288  // 3. when committing
289  //
290  value->commit();
291 
292  // 1 signal should have been emitted
293  BOOST_CHECK_EQUAL( spy.count(), 1 );
294 
295  delete value;
296 }
297 
299 
300 BOOST_AUTO_TEST_CASE( value_string )
301 {
302  GraphicalUri * value = new GraphicalUri();
303 
304  value->set_value("cpath:/");
305  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("cpath:/") );
306 
307  value->set_value("http://coolfluidsrv.vki.ac.be");
308  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("http://coolfluidsrv.vki.ac.be") );
309 
310  delete value;
311 }
312 
314 
315 BOOST_AUTO_TEST_CASE( is_modified )
316 {
317  GraphicalUri * value = new GraphicalUri();
318  QLineEdit* lineEdit = find_line_edit(value);
319 
320  // 1. initially, it's not modified
321  BOOST_CHECK( !value->is_modified() );
322 
323  // 2. change the value
324  lineEdit->setText("cpath:/");
325  BOOST_CHECK( value->is_modified() );
326 
327  // 3. change the value and commit
328  lineEdit->setText("cpath:/Component");
329  BOOST_CHECK( value->is_modified() );
330  value->commit();
331  BOOST_CHECK( !value->is_modified() );
332 
333  // 4. set the same value
334  lineEdit->setText("cpath:/Component");
335  BOOST_CHECK( !value->is_modified() );
336 
337  delete value;
338 }
339 
341 
342 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
bool ExceptionOutputs
if exception contructor should output
Definition: Exception.hpp:32
static AssertionManager & instance()
Gets the instance of the manager.
Definition: Assertions.cpp:33
Basic Classes for Mathematical applications used by COOLFluiD.
bool AssertionDumps
assertions dump backtraces
Definition: Assertions.hpp:65
bool ExceptionDumps
if exception contructor should dump backtrace
Definition: Exception.hpp:34
BOOST_AUTO_TEST_CASE(init)
QWidget * find_widget(const GraphicalUri *value, int index)
Top-level namespace for coolfluid.
Definition: Action.cpp:18
static ExceptionManager & instance()
Gets the instance of the manager.
Definition: Exception.cpp:34
virtual bool set_value(const QVariant &path)
Basic Classes for Graphics applications used by CF.
QComboBox * find_combo_box(const GraphicalUri *value)
void set_schemes(const std::vector< common::URI::Scheme::Type > &list)
virtual QVariant value() const
Most basic kernel library.
Definition: Action.cpp:19
bool is_not_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:147
QLineEdit * find_line_edit(const GraphicalUri *value)
Send comments to:
COOLFluiD Web Admin