COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-graphics-int.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 GraphicalInt class with int values"
9 
10 #include <QDoubleSpinBox>
11 #include <QHBoxLayout>
12 #include <QSignalSpy>
13 #include <QTest>
14 
15 #include "math/Consts.hpp"
16 
18 
19 #include "test/ui/Application.hpp"
20 
22 
23 using namespace cf3;
24 using namespace cf3::common;
25 using namespace cf3::math;
26 using namespace cf3::ui::graphics;
27 
29 
30 QDoubleSpinBox * find_spin_box(const GraphicalInt* value)
31 {
32  // /!\ WARNING /!\
33  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
34  // TO THE GRAPHICAL LOOK OF GraphicalInt
35 
36  QHBoxLayout * layout = dynamic_cast<QHBoxLayout*>(value->layout());
37  QDoubleSpinBox * spin_box = nullptr;
38 
39  if( is_not_null(layout) )
40  {
41  spin_box = dynamic_cast<QDoubleSpinBox*>(layout->itemAt(0)->widget());
42 
43  if( is_null(spin_box) )
44  std::cerr << "Failed to find the spin box." << std::endl;
45  }
46  else
47  std::cerr << "Failed to find the layout or cast it to QHBoxLayout." << std::endl;
48 
49  return spin_box;
50 }
51 
53 
54 BOOST_AUTO_TEST_SUITE( uiGraphicsGraphicalBoolSuite )
55 
56 
59 {
60  application();
61  GraphicalInt * value = new GraphicalInt(false);
62 
67 
68  BOOST_CHECK( is_not_null( find_spin_box(value) ) );
69 
70  delete value;
71 }
72 
74 
75 BOOST_AUTO_TEST_CASE( constructor )
76 {
77  GraphicalInt * value = new GraphicalInt(false);
78  QDoubleSpinBox * spin_box = find_spin_box(value);
79 
80  BOOST_CHECK( is_not_null(spin_box) );
81 
82  // 1. check the range
83  BOOST_CHECK_EQUAL( spin_box->minimum(), Consts::int_min() );
84  BOOST_CHECK_EQUAL( spin_box->maximum(), Consts::int_max() );
85 
86  // 2. value is empty, the line edit should be empty as well
87  BOOST_CHECK_EQUAL( int(spin_box->value()), 0 );
88 
89  delete value;
90  value = new GraphicalInt(false, 1456);
91  spin_box = find_spin_box(value);
92 
93  // 3. value is not empty
94  BOOST_CHECK( is_not_null(spin_box) );
95  BOOST_CHECK_EQUAL( int(spin_box->value()), 1456 );
96 
97  delete value;
98 }
99 
101 
103 {
104  GraphicalInt * value = new GraphicalInt(false);
105  QDoubleSpinBox * spin_box = find_spin_box(value);
106 
107  BOOST_CHECK( is_not_null(spin_box) );
108 
109  //
110  // 1. check with ints
111  //
112  BOOST_CHECK( value->set_value(-1456) );
113  BOOST_CHECK_EQUAL( int(spin_box->value()), -1456 );
114 
115  BOOST_CHECK( value->set_value(215468548) );
116  BOOST_CHECK_EQUAL( int(spin_box->value()), 215468548 );
117 
118  //
119  // 2. check with other types
120  //
121  BOOST_CHECK( !value->set_value(3.141592) );
122  BOOST_CHECK_EQUAL( int(spin_box->value()), 215468548 );
123 
124  BOOST_CHECK( !value->set_value(true) );
125  BOOST_CHECK_EQUAL( int(spin_box->value()), 215468548 );
126 
127  BOOST_CHECK( !value->set_value("789654123") );
128  BOOST_CHECK_EQUAL( int(spin_box->value()), 215468548 );
129 
130  delete value;
131 }
132 
134 
136 {
137  GraphicalInt * value = new GraphicalInt(false);
138  QDoubleSpinBox * spin_box = find_spin_box(value);
139  QVariant theValue;
140 
141  // get value when the line edit is empty
142  theValue = value->value();
143  BOOST_CHECK( theValue.type() == QVariant::Int );
144  BOOST_CHECK_EQUAL( theValue.toInt(), 0 );
145 
146  // get value when the line edit has a string
147  spin_box->setValue(488654);
148  theValue = value->value();
149  BOOST_CHECK( theValue.type() == QVariant::Int );
150  BOOST_CHECK_EQUAL( theValue.toInt(), 488654 );
151 
152  delete value;
153 }
154 
156 
157 BOOST_AUTO_TEST_CASE( signal_emitting )
158 {
159  GraphicalInt * value = new GraphicalInt(false);
160  QDoubleSpinBox * spin_box = find_spin_box(value);
161  QSignalSpy spy(value, SIGNAL(value_changed()));
162 
163  //
164  // 1. through setValue()
165  //
166  value->set_value(125464);
167  value->set_value(-876541);
168 
169  // 2 signals should have been emitted
170  BOOST_CHECK_EQUAL( spy.count(), 2 );
171 
172  spy.clear();
173 
174  //
175  // 2. by simulating keyboard events
176  //
177  spin_box->clear();
178  value->show(); // make the value visible (it ignores keyboard events if not)
179  QTest::keyClicks(spin_box, "-2014" );
180  QTest::keyClicks(spin_box, "357" );
181  QTest::keyClicks(spin_box, "aq45s2" );
182 
183  // 10 signals should have been emitted (one per character)
184  // (13 chars entered but 'a', 'q' and 's' were ignored)
185  BOOST_CHECK_EQUAL( spy.count(), 10 );
186 
187  spy.clear();
188  //
189  // 3. when committing
190  //
191  value->commit();
192 
193  // 1 signal should have been emitted
194  BOOST_CHECK_EQUAL( spy.count(), 1 );
195 
196  delete value;
197 }
198 
200 
201 BOOST_AUTO_TEST_CASE( value_string )
202 {
203  GraphicalInt * value = new GraphicalInt(false);
204 
205  value->set_value(78646);
206  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("78646") );
207 
208  value->set_value(165464);
209  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("165464") );
210 
211  delete value;
212 }
213 
215 
216 BOOST_AUTO_TEST_CASE( is_modified )
217 {
218  GraphicalInt * value = new GraphicalInt(false);
219  QDoubleSpinBox* spin_box = find_spin_box(value);
220 
221  // 1. initially, it's not modified
222  BOOST_CHECK( !value->is_modified() );
223 
224  // 2. change the value
225  spin_box->setValue(123464);
226  BOOST_CHECK( value->is_modified() );
227 
228  // 3. change the value and commit
229  spin_box->setValue(65454354);
230  BOOST_CHECK( value->is_modified() );
231  value->commit();
232  BOOST_CHECK( !value->is_modified() );
233 
234  // 4. put the same value
235  spin_box->setValue(65454354);
236  BOOST_CHECK( !value->is_modified() );
237 
238  delete value;
239 }
240 
242 
243 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
virtual QVariant value() const
Real int_min()
Definition of the minimum number representable with the chosen precision.
Definition: Consts.hpp:30
virtual bool set_value(const QVariant &value)
QDoubleSpinBox * find_spin_box(const GraphicalInt *value)
Real int_max()
Returns the maximum number representable with the chosen precision.
Definition: Consts.hpp:28
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
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