COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-graphics-string.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 <QLineEdit>
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 QLineEdit * find_line_edit(const GraphicalString* value)
31 {
32  // /!\ WARNING /!\
33  // THE FOLLOWING CODE IS EXTREMELY SENSITIVE TO ANY MODIFICATION
34  // OF THE GRAPHICAL LOOK OF GraphicalString
35 
36  QHBoxLayout * layout = dynamic_cast<QHBoxLayout*>(value->layout());
37  QLineEdit * lineEdit = nullptr;
38 
39  if( is_not_null(layout) )
40  {
41  lineEdit = dynamic_cast<QLineEdit*>(layout->itemAt(0)->widget());
42 
43  if( is_null(lineEdit) )
44  QWARN("Failed to find the line edit.");
45  }
46  else
47  QWARN("Failed to find the layout or cast it to QHBoxLayout.");
48 
49  return lineEdit;
50 }
51 
53 
54 BOOST_AUTO_TEST_SUITE( uiGraphicsGraphicalBoolSuite )
55 
56 
59 {
60  application();
61  GraphicalString * value = new GraphicalString();
62 
67 
68  BOOST_CHECK( is_not_null( find_line_edit(value) ) );
69 
70  delete value;
71 }
72 
74 
75 BOOST_AUTO_TEST_CASE( constructor )
76 {
77  GraphicalString * value = new GraphicalString();
78  QLineEdit * lineEdit = find_line_edit(value);
79 
80  // 1. value is empty, the line edit should be empty as well
81  QVERIFY( is_not_null(lineEdit) );
82  QCOMPARE( lineEdit->text().toStdString(), std::string() );
83 
84  delete value;
85  value = new GraphicalString("Hello, World!");
86  lineEdit = find_line_edit(value);
87 
88  // 2. value is not empty
89  QVERIFY( is_not_null(lineEdit) );
90  QCOMPARE( lineEdit->text().toStdString(), std::string("Hello, World!") );
91 
92  delete value;
93 }
94 
96 
98 {
99  GraphicalString * value = new GraphicalString();
100  QLineEdit * lineEdit = find_line_edit(value);
101 
102  QVERIFY( is_not_null(lineEdit) );
103 
104  //
105  // 1. check with strings
106  //
107  QVERIFY( value->set_value("Hello") );
108  QCOMPARE( lineEdit->text().toStdString(), std::string("Hello") );
109 
110  QVERIFY( value->set_value("World") );
111  QCOMPARE( lineEdit->text().toStdString(), std::string("World") );
112 
113  //
114  // 2. check with other types (this works since all primitive
115  // types can be implicitly converted to QString by Qt)
116  //
117  QVERIFY( value->set_value(12) );
118  QCOMPARE( lineEdit->text().toStdString(), std::string("12") );
119 
120  QVERIFY( value->set_value(3.141592) );
121  QCOMPARE( lineEdit->text().toStdString(), std::string("3.141592") );
122 
123  QVERIFY( value->set_value(true) );
124  QCOMPARE( lineEdit->text().toStdString(), std::string("true") );
125 
126  delete value;
127 }
128 
130 
132 {
133  GraphicalString * value = new GraphicalString();
134  QLineEdit * lineEdit = find_line_edit(value);
135  QVariant theValue;
136 
137  // get value when the line edit is empty
138  theValue = value->value();
139  QVERIFY( theValue.type() == QVariant::String );
140  QCOMPARE( theValue.toString().toStdString(), std::string() );
141 
142  // get value when the line edit has a string
143  lineEdit->setText("This is a sample text.");
144  theValue = value->value();
145  QVERIFY( theValue.type() == QVariant::String );
146  QCOMPARE( theValue.toString().toStdString(), std::string("This is a sample text.") );
147 
148  delete value;
149 }
150 
152 
153 BOOST_AUTO_TEST_CASE( signal_emitting )
154 {
155  GraphicalString * value = new GraphicalString();
156  QLineEdit * lineEdit = find_line_edit(value);
157  QSignalSpy spy(value, SIGNAL(value_changed()));
158 
159  //
160  // 1. through setValue()
161  //
162  value->set_value("Hello");
163  value->set_value("World!");
164 
165  // 2 signals should have been emitted
166  QCOMPARE( spy.count(), 2 );
167 
168  spy.clear();
169 
170  //
171  // 2. by simulating keyboard events
172  //
173  value->show(); // make the value visible (it ignores keyboard events if not)
174  QTest::keyClicks(lineEdit, "Hello " );
175  QTest::keyClicks(lineEdit, "World " );
176  QTest::keyClicks(lineEdit, "And others ;)" );
177 
178  // 25 signals should have been emitted (one per character)
179  QCOMPARE( spy.count(), 25 );
180 
181  spy.clear();
182  //
183  // 3. when committing
184  //
185  value->commit();
186 
187  // 1 signal should have been emitted
188  QCOMPARE( spy.count(), 1 );
189 
190  delete value;
191 }
192 
194 
195 BOOST_AUTO_TEST_CASE( value_string )
196 {
197  GraphicalString * value = new GraphicalString();
198 
199  value->set_value("Hello");
200  QCOMPARE( value->value_string().toStdString(), std::string("Hello") );
201 
202  value->set_value("World");
203  QCOMPARE( value->value_string().toStdString(), std::string("World") );
204 
205  delete value;
206 }
207 
209 
210 BOOST_AUTO_TEST_CASE( is_modified )
211 {
212  GraphicalString * value = new GraphicalString();
213  QLineEdit* lineEdit = find_line_edit(value);
214 
215  // 1. initially, it's not modified
216  QVERIFY( !value->is_modified() );
217 
218  // 2. change the value
219  lineEdit->setText("This is a sample text.");
220  QVERIFY( value->is_modified() );
221 
222  // 3. change the value and commit
223  lineEdit->setText("This is another one.");
224  QVERIFY( value->is_modified() );
225  value->commit();
226  QVERIFY( !value->is_modified() );
227 
228  delete value;
229 }
230 
232 
233 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)
virtual QVariant value() const
QLineEdit * find_line_edit(const GraphicalString *value)
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.
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