COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-graphics-double.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 GraphicalDouble class"
9 
10 #include <QLineEdit>
11 #include <QHBoxLayout>
12 #include <QSignalSpy>
13 #include <QTest>
14 
16 
17 #include "test/ui/Application.hpp"
18 
20 
21 using namespace cf3;
22 using namespace cf3::common;
23 using namespace cf3::ui::graphics;
24 
26 
27 QLineEdit * find_line_edit(const GraphicalDouble* value)
28 {
29  // /!\ WARNING /!\
30  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
31  // OF THE GRAPHICAL LOOK OF GraphicalDouble
32 
33  QHBoxLayout * layout = dynamic_cast<QHBoxLayout*>(value->layout());
34  QLineEdit * lineEdit = nullptr;
35 
36  if( is_not_null(layout) )
37  {
38  lineEdit = dynamic_cast<QLineEdit*>(layout->itemAt(0)->widget());
39 
40  if( is_null(lineEdit) )
41  std::cerr << "Failed to find the line edit." << std::endl;
42  }
43  else
44  std::cerr << "Failed to find the layout or cast it to QHBoxLayout." << std::endl;
45 
46  return lineEdit;
47 }
48 
50 
51 BOOST_AUTO_TEST_SUITE( uiGraphicsGraphicalBoolSuite )
52 
53 
56 {
57  application();
58  GraphicalDouble * value = new GraphicalDouble();
59 
64 
65  BOOST_CHECK( is_not_null( find_line_edit(value) ) );
66 
67  delete value;
68 }
69 
71 
72 BOOST_AUTO_TEST_CASE( constructor )
73 {
74  GraphicalDouble * value = new GraphicalDouble();
75  QLineEdit * lineEdit = find_line_edit(value);
76 
77  // 1. value is 0.0 (note: converting 0.0 to string gives "0")
78  BOOST_CHECK( is_not_null(lineEdit) );
79  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("0") );
80 
81  delete value;
82  value = new GraphicalDouble(3.151492);
83  lineEdit = find_line_edit(value);
84 
85  // 2. value is pi
86  BOOST_CHECK( is_not_null(lineEdit) );
87  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("3.151492") );
88 
89  delete value;
90 }
91 
93 
95 {
96  GraphicalDouble * value = new GraphicalDouble();
97  QLineEdit * lineEdit = find_line_edit(value);
98 
99  BOOST_CHECK( is_not_null(lineEdit) );
100 
101  //
102  // 1. check with doubles
103  //
104  BOOST_CHECK( value->set_value(3.14159265) );
105  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("3.14159265") );
106 
107  BOOST_CHECK( value->set_value(-2.71) );
108  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("-2.71") );
109 
110  //
111  // 2. check with other types
112  //
113  BOOST_CHECK( value->set_value(12) );
114  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("12") ); // value does not change
115 
116  BOOST_CHECK( !value->set_value("Hello") );
117  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("12") ); // value does not change
118 
119  BOOST_CHECK( !value->set_value(true) );
120  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("12") ); // value does not change
121 
122  BOOST_CHECK( !value->set_value("1.6a45") ); // with an invalid character
123  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("12") ); // value does not change
124 
125  BOOST_CHECK( value->set_value("1.6E-45") ); // try scientific notation
126  BOOST_CHECK_EQUAL( lineEdit->text().toStdString(), std::string("1.6E-45") );
127 
128 
129  delete value;
130 }
131 
133 
135 {
136  GraphicalDouble * value = new GraphicalDouble();
137  QLineEdit * lineEdit = find_line_edit(value);
138  QVariant theValue;
139 
140  // 1. the value is 0
141  theValue = value->value();
142  BOOST_CHECK( theValue.type() == QVariant::Double );
143  BOOST_CHECK_EQUAL( theValue.toDouble(), 0.0 );
144 
145  // 2. set another value
146  lineEdit->setText("-2.71828183");
147  theValue = value->value();
148  BOOST_CHECK( theValue.type() == QVariant::Double );
149  BOOST_CHECK_EQUAL( theValue.toDouble(), -2.71828183 );
150 
151  // 3. try with scientific notation
152  lineEdit->setText("1.6E45");
153  theValue = value->value();
154  BOOST_CHECK( theValue.type() == QVariant::Double );
155  BOOST_CHECK_EQUAL( theValue.toDouble(), 1.6e+45 );
156 
157 
158  delete value;
159 }
160 
162 
163 BOOST_AUTO_TEST_CASE( signal_emitting )
164 {
165  GraphicalDouble * value = new GraphicalDouble();
166  QLineEdit * lineEdit = find_line_edit(value);
167  QSignalSpy spy(value, SIGNAL(value_changed()));
168 
169  //
170  // 1. through setValue()
171  //
172  value->set_value(3.14159265);
173  value->set_value(2.71);
174 
175  // 2 signals should have been emitted
176  BOOST_CHECK_EQUAL( spy.count(), 2 );
177 
178  spy.clear();
179 
180  //
181  // 2. by simulating keyboard events
182  //
183  lineEdit->clear();
184  value->show(); // make the value visible (it ignores keyboard events if not)
185  QTest::keyClicks(lineEdit, "-3.141-51a6e4512r4+5w6" );
186 
187  // 18 signals should have been emitted
188  // (24 chars where entered, but only "-3.141516e4512456" were accepted)
189  BOOST_CHECK_EQUAL( spy.count(), 18 );
190 
191  spy.clear();
192  //
193  // 3. when committing
194  //
195  value->commit();
196 
197  // 1 signal should have been emitted
198  BOOST_CHECK_EQUAL( spy.count(), 1 );
199 
200  delete value;
201 }
202 
204 
205 BOOST_AUTO_TEST_CASE( value_string )
206 {
207  GraphicalDouble * value = new GraphicalDouble();
208 
209  value->set_value(-3.14159265);
210  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("-3.14159265") );
211 
212  value->set_value(1.6E-98);
213  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("1.6e-98") );
214 
215  delete value;
216 }
217 
219 
220 BOOST_AUTO_TEST_CASE( is_modified )
221 {
222  GraphicalDouble * value = new GraphicalDouble();
223  QLineEdit* lineEdit = find_line_edit(value);
224 
225  // 1. initially, it's not modified
226  BOOST_CHECK( !value->is_modified() );
227 
228  // 2. change the value
229  lineEdit->setText("12.45");
230  BOOST_CHECK( value->is_modified() );
231 
232  // 3. change the value and commit
233  lineEdit->setText("-5.879");
234  BOOST_CHECK( value->is_modified() );
235  value->commit();
236  BOOST_CHECK( !value->is_modified() );
237 
238  delete value;
239 }
240 
242 
243 BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(init)
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
virtual QVariant value() const
bool ExceptionDumps
if exception contructor should dump backtrace
Definition: Exception.hpp:34
QLineEdit * find_line_edit(const GraphicalDouble *value)
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.
Most basic kernel library.
Definition: Action.cpp:19
bool is_not_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:147
virtual bool set_value(const QVariant &value)
Send comments to:
COOLFluiD Web Admin