COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-graphics-restricted-list.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 GraphicalRestrictedList class"
9 
10 #include <QComboBox>
11 #include <QHBoxLayout>
12 #include <QSignalSpy>
13 
14 #include "common/OptionArray.hpp"
15 #include "common/OptionT.hpp"
17 #include "common/URI.hpp"
18 
20 
21 #include "test/ui/Application.hpp"
22 
24 
25 using namespace cf3;
26 using namespace cf3::common;
27 using namespace cf3::ui::graphics;
28 
30 
31 QComboBox * find_combo_box(const GraphicalRestrictedList* value)
32 {
33  // /!\ WARNING /!\
34  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
35  // TO THE GRAPHICAL LOOK OF GraphicalRestrictedList
36 
37  QHBoxLayout * layout = dynamic_cast<QHBoxLayout*>(value->layout());
38  QComboBox * comboBox = nullptr;
39 
40  if( is_not_null(layout) )
41  {
42  comboBox = dynamic_cast<QComboBox*>(layout->itemAt(0)->widget());
43 
44  if( is_null(comboBox) )
45  std::cerr << "Failed to find the line edit." << std::endl;
46  }
47  else
48  std::cerr << "Failed to find the layout or cast it to QHBoxLayout." << std::endl;
49 
50  return comboBox;
51 }
52 
54 
55 BOOST_AUTO_TEST_SUITE( uiGraphicsGraphicalRestrictedListSuite )
56 
57 
60 {
61  application();
63 
68 
69  BOOST_CHECK( is_not_null( find_combo_box(value) ) );
70 
71  delete value;
72 }
73 
75 
76 BOOST_AUTO_TEST_CASE( constructor )
77 {
79  QComboBox * combo_box = find_combo_box(value);
80  boost::shared_ptr<Option> opt( new OptionT<std::string>("opt", std::string("Hello") ) );
81 
82  // 1. empty option
83  BOOST_CHECK( is_not_null(combo_box) );
84  BOOST_CHECK_EQUAL( combo_box->count(), 0 );
85 
86  delete value;
87 
88  // 2. valid option with an empty list of restricted list
89  value = new GraphicalRestrictedList(opt);
90  combo_box = find_combo_box(value);
91  BOOST_CHECK( is_not_null(combo_box) );
92  BOOST_CHECK_EQUAL( combo_box->count(), 0 );
93 
94  delete value;
95 
96  // 3. option is not empty
97  opt->restricted_list().push_back( std::string("World") );
98  opt->restricted_list().push_back( std::string("Third restricted value") );
99  value = new GraphicalRestrictedList(opt);
100  combo_box = find_combo_box(value);
101 
102  BOOST_CHECK( is_not_null(combo_box) );
103  BOOST_CHECK_EQUAL( combo_box->count(), 2 );
104 
105  delete value;
106 }
107 
109 
111 {
112  boost::shared_ptr<Option> opt( new OptionT<std::string>("opt", std::string("Hello") ) );
113  opt->restricted_list().push_back( std::string("World") );
114  opt->restricted_list().push_back( std::string("Hello") );
115  opt->restricted_list().push_back( std::string("Third restricted value") );
116 
118  QComboBox * comboBox = find_combo_box(value);
119 
120  BOOST_CHECK( is_not_null(comboBox) );
121 
122  //
123  // 1. check with strings
124  //
125  BOOST_CHECK( value->set_value("Hello") );
126  BOOST_CHECK_EQUAL( comboBox->currentText().toStdString(), std::string("Hello") );
127 
128  BOOST_CHECK( value->set_value("World") );
129  BOOST_CHECK_EQUAL( comboBox->currentText().toStdString(), std::string("World") );
130 
131  BOOST_CHECK( !value->set_value("something") ); // value does not exist in the list
132  BOOST_CHECK_EQUAL( comboBox->currentText().toStdString(), std::string("World") ); // current text should be the same
133 
134  //
135  // 2. check with other types
136  //
137  BOOST_CHECK( !value->set_value(12) );
138  BOOST_CHECK_EQUAL( comboBox->currentText().toStdString(), std::string("World") );
139 
140  BOOST_CHECK( !value->set_value(3.141592) );
141  BOOST_CHECK_EQUAL( comboBox->currentText().toStdString(), std::string("World") );
142 
143  BOOST_CHECK( !value->set_value(true) );
144  BOOST_CHECK_EQUAL( comboBox->currentText().toStdString(), std::string("World") );
145 
146  delete value;
147 }
148 
150 
152 {
153  boost::shared_ptr<Option> opt( new OptionT<std::string>("opt", std::string("Hello") ) );
154  opt->restricted_list().push_back( std::string("Hello") );
155  opt->restricted_list().push_back( std::string("World") );
156  opt->restricted_list().push_back( std::string("Third restricted value") );
157 
159  QComboBox * comboBox = find_combo_box(value);
160  QVariant theValue;
161 
162  // get value when the line edit is empty
163  theValue = value->value();
164  BOOST_CHECK( theValue.type() == QVariant::String );
165  BOOST_CHECK_EQUAL( theValue.toString().toStdString(), std::string("Hello") );
166 
167 
168  // get value when the line edit has a string
169  comboBox->setCurrentIndex(2); // select the third value
170  theValue = value->value();
171  BOOST_CHECK( theValue.type() == QVariant::String );
172  BOOST_CHECK_EQUAL( theValue.toString().toStdString(), std::string("Third restricted value") );
173 
174  delete value;
175 }
176 
178 
179 BOOST_AUTO_TEST_CASE( signal_emitting )
180 {
181  boost::shared_ptr<Option> opt( new OptionT<std::string>("opt", std::string("Hello") ) );
182  opt->restricted_list().push_back( std::string("Hello") );
183  opt->restricted_list().push_back( std::string("World") );
184  opt->restricted_list().push_back( std::string("Third restricted value") );
185 
187  QComboBox * comboBox = find_combo_box(value);
188  QSignalSpy spy(value, SIGNAL(value_changed()));
189 
190  //
191  // 1. through setValue()
192  //
193  value->set_value( QString("World") );
194  value->set_value( QString("Hello") );
195 
196  // 2 signals should have been emitted
197  BOOST_CHECK_EQUAL( spy.count(), 2 );
198 
199  spy.clear();
200 
201  //
202  // 2. by changing the index of the combo box
203  //
204  value->show(); // make the value visible (it ignores keyboard events if not)
205  comboBox->setCurrentIndex(2);
206  comboBox->setCurrentIndex(0);
207  comboBox->setCurrentIndex(1);
208 
209  // 3 signals should have been emitted
210  BOOST_CHECK_EQUAL( spy.count(), 3 );
211 
212  spy.clear();
213  //
214  // 3. when committing
215  //
216  value->commit();
217 
218  // 1 signal should have been emitted
219  BOOST_CHECK_EQUAL( spy.count(), 1 );
220 
221  delete value;
222 }
223 
225 
226 BOOST_AUTO_TEST_CASE( value_string )
227 {
228  boost::shared_ptr<Option> opt( new OptionT<std::string>("opt", std::string("Hello") ) );
229  opt->restricted_list().push_back( std::string("Hello") );
230  opt->restricted_list().push_back( std::string("World") );
231  opt->restricted_list().push_back( std::string("Third restricted value") );
232 
234 
235  value->set_value("Hello");
236  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("Hello") );
237 
238  value->set_value("World");
239  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("World") );
240 
241  delete value;
242 }
243 
245 
246 BOOST_AUTO_TEST_CASE( is_modified )
247 {
248  boost::shared_ptr<Option> opt( new OptionT<std::string>("opt", std::string("Hello") ) );
249  opt->restricted_list().push_back( std::string("World") );
250  opt->restricted_list().push_back( std::string("Third restricted value") );
251 
253  QComboBox* comboBox = find_combo_box(value);
254 
255  // 1. initially, it's not modified
256  BOOST_CHECK( !value->is_modified() );
257 
258  // 2. change the value
259  comboBox->setCurrentIndex(2);
260  BOOST_CHECK( value->is_modified() );
261 
262  // 3. change the value and commit
263  comboBox->setCurrentIndex(1);
264  BOOST_CHECK( value->is_modified() );
265  value->commit();
266  BOOST_CHECK( !value->is_modified() );
267 
268  delete value;
269 }
270 
272 
273 BOOST_AUTO_TEST_CASE( set_restricted_list )
274 {
275  boost::shared_ptr<Option> opt( new OptionT<std::string>("opt", std::string("Hello") ) );
276  opt->restricted_list().push_back( std::string("World") );
277  opt->restricted_list().push_back( std::string("Third restricted value") );
278 
280  QComboBox* comboBox = find_combo_box(value);
281  QStringList newList;
282 
283  newList << "Here" << "is" << "a new" << "list";
284 
285  comboBox->setCurrentIndex(2);
286 
287  value->set_restricted_list( newList );
288 
289  BOOST_CHECK_EQUAL( comboBox->count(), newList.count() );
290  BOOST_CHECK_EQUAL( comboBox->itemText(0).toStdString(), newList.at(0).toStdString() );
291  BOOST_CHECK_EQUAL( comboBox->itemText(1).toStdString(), newList.at(1).toStdString() );
292  BOOST_CHECK_EQUAL( comboBox->itemText(2).toStdString(), newList.at(2).toStdString() );
293  BOOST_CHECK_EQUAL( comboBox->itemText(3).toStdString(), newList.at(3).toStdString() );
294 
295  BOOST_CHECK_EQUAL( comboBox->currentIndex(), 0 );
296 }
297 
299 
300 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
bool AssertionDumps
assertions dump backtraces
Definition: Assertions.hpp:65
QComboBox * find_combo_box(const GraphicalRestrictedList *value)
bool ExceptionDumps
if exception contructor should dump backtrace
Definition: Exception.hpp:34
Conversions from and to std::string.
Uniform Resource Identifier (see http://en.wikipedia.org/wiki/Uniform_Resource_Identifier) ...
Top-level namespace for coolfluid.
Definition: Action.cpp:18
static ExceptionManager & instance()
Gets the instance of the manager.
Definition: Exception.cpp:34
Basic Classes for Graphics applications used by CF.
virtual bool set_value(const QVariant &value)
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