COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-graphics-bool.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 GraphicalBool class"
9 
10 #include <QCheckBox>
11 #include <QHBoxLayout>
12 #include <QSignalSpy>
13 #include <QTest>
14 
15 #include "common/OptionArray.hpp"
16 #include "common/OptionT.hpp"
18 #include "common/URI.hpp"
19 
21 
22 #include "test/ui/Application.hpp"
23 
25 
26 using namespace cf3;
27 using namespace cf3::common;
28 using namespace cf3::ui::graphics;
29 
31 
32 QCheckBox * find_check_box(const GraphicalBool* value)
33 {
34  // /!\ WARNING /!\
35  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
36  // TO THE GRAPHICAL LOOK OF GraphicalBool
37 
38  QHBoxLayout * layout = dynamic_cast<QHBoxLayout*>(value->layout());
39  QCheckBox * checkBox = nullptr;
40 
41  if( is_not_null(layout) )
42  {
43  checkBox = dynamic_cast<QCheckBox*>(layout->itemAt(0)->widget());
44 
45  if( is_null(checkBox) )
46  std::cerr << "Failed to find the check box." << std::endl;
47  }
48  else
49  std::cerr << "Failed to find the layout or cast it to QHBoxLayout." << std::endl;
50 
51  return checkBox;
52 }
53 
55 
56 BOOST_AUTO_TEST_SUITE( uiGraphicsGraphicalBoolSuite )
57 
58 
61 {
62  application();
63  GraphicalBool * value = new GraphicalBool();
64 
69 
70  BOOST_CHECK( is_not_null( find_check_box(value) ) );
71 
72  delete value;
73 }
74 
76 
77 BOOST_AUTO_TEST_CASE( constructor )
78 {
79  GraphicalBool * value = new GraphicalBool(false);
80  QCheckBox * checkbox = find_check_box(value);
81 
82  // 1. value is false, the checkbox should be unchecked
83  BOOST_CHECK( is_not_null(checkbox) );
84  BOOST_CHECK( !checkbox->isChecked() );
85 
86  delete value;
87  value = new GraphicalBool(true);
88  checkbox = find_check_box(value);
89 
90  // 2. value is true, the checkbox should be checked
91  BOOST_CHECK( is_not_null(checkbox) );
92  BOOST_CHECK( checkbox->isChecked() );
93 
94  delete value;
95 }
96 
98 
100 {
101  GraphicalBool * value = new GraphicalBool(false);
102  QCheckBox * checkbox = find_check_box(value);
103 
104  BOOST_CHECK( is_not_null(checkbox) );
105 
106  //
107  // 1. check with bool values
108  //
109  BOOST_CHECK( value->set_value(true) );
110  BOOST_CHECK( checkbox->isChecked() );
111  BOOST_CHECK( value->original_value().toBool() );
112 
113  BOOST_CHECK( value->set_value(false) );
114  BOOST_CHECK( !checkbox->isChecked() );
115  BOOST_CHECK( !value->original_value().toBool() );
116 
117  //
118  // 2. check with strings (those supported by cf3::common::from_str<bool>())
119  //
120  BOOST_CHECK( value->set_value("true") );
121  BOOST_CHECK( checkbox->isChecked() );
122 
123  BOOST_CHECK( value->set_value("false") );
124  BOOST_CHECK( !checkbox->isChecked() );
125 
126  BOOST_CHECK( value->set_value("on") );
127  BOOST_CHECK( checkbox->isChecked() );
128 
129  BOOST_CHECK( value->set_value("off") );
130  BOOST_CHECK( !checkbox->isChecked() );
131 
132  BOOST_CHECK( value->set_value("1") );
133  BOOST_CHECK( checkbox->isChecked() );
134 
135  BOOST_CHECK( value->set_value("0") );
136  BOOST_CHECK( !checkbox->isChecked() );
137 
138  BOOST_CHECK_THROW( value->set_value("ThisIsNotABoolValue"), ParsingFailed );
139  BOOST_CHECK( !checkbox->isChecked() ); // state should not have changed
140 
141  //
142  // 3. check with other types
143  //
144  BOOST_CHECK( !value->set_value(12) );
145  BOOST_CHECK( !checkbox->isChecked() );
146 
147  BOOST_CHECK( !value->set_value(3.141592) );
148  BOOST_CHECK( !checkbox->isChecked() );
149 
150  BOOST_CHECK( !value->set_value(-456) );
151  BOOST_CHECK( !checkbox->isChecked() );
152 
153  delete value;
154 }
155 
157 
159 {
160  GraphicalBool * value = new GraphicalBool(false);
161  QCheckBox * checkbox = find_check_box(value);
162  QVariant isChecked;
163 
164  // get value when the check box is checked
165  checkbox->setChecked(true);
166  isChecked = value->value();
167  BOOST_CHECK( isChecked.type() == QVariant::Bool );
168  BOOST_CHECK( isChecked.toBool() );
169 
170  // get value when the check box is not checked
171  checkbox->setChecked(false);
172  isChecked = value->value();
173  BOOST_CHECK( isChecked.type() == QVariant::Bool );
174  BOOST_CHECK( !isChecked.toBool() );
175 
176  delete value;
177 }
178 
180 
181 BOOST_AUTO_TEST_CASE( signal_emitting )
182 {
183  GraphicalBool * value = new GraphicalBool(false);
184  QCheckBox * checkbox = find_check_box(value);
185  QSignalSpy spy(value, SIGNAL(value_changed()));
186 
187  //
188  // 1. check/uncheck through setValue()
189  //
190  value->set_value(true);
191  value->set_value(false);
192 
193  // 2 signal should have been emitted
194  BOOST_CHECK_EQUAL( spy.count(), 2 );
195 
196  spy.clear();
197 
198  //
199  // 2. check/uncheck by simulating a mouse click
200  //
201  value->show(); // make the value visible (it ignores mouse events if not)
202  QTest::mouseClick(checkbox, Qt::LeftButton);
203  QTest::mouseClick(checkbox, Qt::LeftButton);
204 
205  // 2 signal should have been emitted as well
206  BOOST_CHECK_EQUAL( spy.count(), 2 );
207 
208  spy.clear();
209  //
210  // 3. when committing
211  //
212  value->commit();
213 
214  // 1 signal should have been emitted
215  BOOST_CHECK_EQUAL( spy.count(), 1 );
216 
217  delete value;
218 }
219 
221 
222 BOOST_AUTO_TEST_CASE( value_string )
223 {
224  GraphicalBool * value = new GraphicalBool(false);
225 
226  value->set_value(true);
227  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("true") );
228 
229  value->set_value(false);
230  BOOST_CHECK_EQUAL( value->value_string().toStdString(), std::string("false") );
231 
232  delete value;
233 }
234 
236 
237 BOOST_AUTO_TEST_CASE( is_modified )
238 {
239  GraphicalBool * value = new GraphicalBool(false);
240  QCheckBox * checkbox = find_check_box(value);
241 
242  // 1. initially, it's not modified
243  BOOST_CHECK( !value->is_modified() );
244 
245  // 2. change the value
246  checkbox->setChecked(true);
247  BOOST_CHECK( value->is_modified() );
248 
249  // 3. set the same value again
250  value->set_value(true);
251  BOOST_CHECK( !value->is_modified() );
252 
253  // 4. change the value and commit
254  checkbox->setChecked(false);
255  BOOST_CHECK( value->is_modified() );
256  value->commit();
257  BOOST_CHECK( !value->is_modified() );
258 
259  delete value;
260 }
261 
263 
264 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
BOOST_AUTO_TEST_CASE(init)
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) ...
virtual QVariant value() const
Top-level namespace for coolfluid.
Definition: Action.cpp:18
virtual bool set_value(const QVariant &value)
static ExceptionManager & instance()
Gets the instance of the manager.
Definition: Exception.cpp:34
Basic Classes for Graphics applications used by CF.
QCheckBox * find_check_box(const GraphicalBool *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