COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ui-core-nlink.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 ui NLink class"
9 
10 #include <QModelIndex>
11 #include <QSignalSpy>
12 
14 
15 #include "ui/core/NGeneric.hpp"
16 #include "ui/core/NLink.hpp"
17 #include "ui/core/NRoot.hpp"
18 #include "ui/core/NTree.hpp"
19 #include "ui/core/TreeThread.hpp"
21 
23 
24 using namespace cf3;
25 using namespace cf3::common;
26 using namespace cf3::common::XML;
27 using namespace cf3::ui::core;
28 
29 Q_DECLARE_METATYPE(QModelIndex)
30 
31 
34 BOOST_AUTO_TEST_SUITE( uiCoreNLinkSuite )
35 
37 
39 {
40  application();
41 
42  ThreadManager::instance().tree();
43 
48 }
49 
51 
53 {
54  boost::shared_ptr< NRoot > root(new NRoot("Root"));
55  boost::shared_ptr< NGeneric > target(new NGeneric("Target", "MyType"));
56 
57  boost::shared_ptr< NLink > l1(new NLink("Link1"));
58  boost::shared_ptr< NLink > l2(new NLink("Link2"));
59 
60  root->add_node(target);
61  root->add_node(l1);
62 
63  l1->set_target_path("cpath:/Target");
64 
65  BOOST_CHECK_EQUAL(l1->tool_tip().toStdString(), std::string("Target: /Target"));
66  BOOST_CHECK_EQUAL(l2->tool_tip().toStdString(), std::string("Target: <No target>"));
67 
68  root->remove_node( "Link1" );
69  root->remove_node( "Target" );
70 }
71 
73 
74 BOOST_AUTO_TEST_CASE( target_path )
75 {
76  boost::shared_ptr< NRoot > root(new NRoot("Root"));
77  boost::shared_ptr< NGeneric > target(new NGeneric("Target", "MyType"));
78 
79  boost::shared_ptr< NLink > l1(new NLink("Link1"));
80  boost::shared_ptr< NLink > l2(new NLink("Link2"));
81 
82  root->add_node(target);
83  root->add_node(l1);
84 
85  l1->set_target_path("cpath:/Target");
86 
87  BOOST_CHECK_EQUAL( l1->target_path().string(), std::string("cpath:/Target"));
88  BOOST_CHECK_EQUAL( l2->target_path().string(), std::string(""));
89 
90  root->remove_node( "Link1" );
91  root->remove_node( "Target" );
92 }
93 
95 
96 BOOST_AUTO_TEST_CASE( go_to_target )
97 {
98  // QModelIndex needs to be registered. See QSignalSpy class doc.
99  qRegisterMetaType<QModelIndex>("QModelIndex");
100 
101  QModelIndex index;
102  SignalFrame frame;
103  Handle< NRoot > root = ThreadManager::instance().tree().root();
104  boost::shared_ptr< NGeneric > target(new NGeneric("Target", "MyType"));
105  boost::shared_ptr< NGeneric > wrongTargetParent(new NGeneric("WrongTargetParent", "MyType")); // not part of the tree
106  boost::shared_ptr< NGeneric > wrongTarget(new NGeneric("WrongTarget", "MyType"));
107  wrongTargetParent->add_component(wrongTarget);
108  Handle< NTree > tree = NTree::global();
109  boost::shared_ptr< NLink > link(new NLink("link"));
110  QSignalSpy spy(tree.get(), SIGNAL(current_index_changed(QModelIndex,QModelIndex)));
111 
112  root->add_node(target);
113  root->add_node(link);
114 
115  tree->set_current_index(tree->index(0, 0));
116 
117  // 1. The link has no target
118  BOOST_CHECK_THROW( link->go_to_target(frame) , ValueNotFound );
119 
120  // 2. target does not belong to the tree
121  link->set_target_node(wrongTarget->handle<NGeneric>());
122  BOOST_CHECK_THROW( link->go_to_target(frame) , ValueNotFound );
123 
124  // 3. everything is OK
125  spy.clear();
126  link->set_target_node(target->handle<NGeneric>());
127  index = tree->index_from_path("cpath:/Target");
128  BOOST_REQUIRE_NO_THROW(link->go_to_target(frame));
129 
130  BOOST_CHECK_EQUAL(spy.count(), 1);
131 
132  // check that the correct index was selected
133  BOOST_CHECK_EQUAL(qvariant_cast<QModelIndex>(spy.at(0).at(0)).internalPointer(), index.internalPointer());
134 
135  root->remove_node( "link" );
136  root->remove_node( "Target" );
137 }
138 
140 
141 BOOST_AUTO_TEST_CASE( set_target_path )
142 {
143  Handle< NTree > tree = NTree::global();
144  boost::shared_ptr< NLink > link(new NLink("link"));
145 
146  tree->tree_root()->add_node(link);
147 
148  // 1. path does not exist, assertion should fail
149  BOOST_CHECK_THROW( link->set_target_path("cpath:/Unexisting/Component"), InvalidURI );
150 
151  // 2. everything is ok
152  BOOST_REQUIRE_NO_THROW( link->set_target_path("cpath:/UI/Log") );
153  BOOST_CHECK_EQUAL( link->target_path().string(), std::string("cpath:/UI/Log") );
154 
155  tree->tree_root()->remove_node("link");
156 }
157 
159 
160 BOOST_AUTO_TEST_CASE( set_target_node )
161 {
162  Handle< NTree > tree = NTree::global();
163  boost::shared_ptr< NLink > link(new NLink("link"));
164  boost::shared_ptr< NGeneric > target(new NGeneric("Target", "MyType"));
165  Handle< NGeneric > emptyTarget;
166 
167  tree->tree_root()->add_node(link);
168  tree->tree_root()->add_node(target);
169 
170  // 1. give an empty target
171  BOOST_CHECK_THROW( link->set_target_node( emptyTarget ), FailedAssertion );
172 
173  // 2. everything is ok
174  BOOST_REQUIRE_NO_THROW( link->set_target_node( target->handle<NGeneric>() ) );
175  BOOST_CHECK_EQUAL( link->target_path().string(), std::string("cpath:/Target") );
176 
177  tree->tree_root()->remove_node("link");
178  tree->tree_root()->remove_node("Target");
179 }
180 
183 
184 BOOST_AUTO_TEST_SUITE_END()
QApplication * application()
Definition: Application.hpp:11
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Implementation of QAbstractItemModel::index().
Definition: NTree.cpp:500
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
void add_node(boost::shared_ptr< CNode > node)
Adds a sub-node.
Definition: CNode.cpp:452
Classes that implement the XML protocol for use in COOLFluiD.
Definition: Component.hpp:43
Basic Classes for client-core library used by coolfluid-client application.
Definition: CNode.cpp:57
bool AssertionDumps
assertions dump backtraces
Definition: Assertions.hpp:65
bool ExceptionDumps
if exception contructor should dump backtrace
Definition: Exception.hpp:34
tuple root
Definition: coolfluid.py:24
Handle< NRoot > tree_root() const
Gives the current root.
Definition: NTree.cpp:75
Manages a set of maps.
Definition: SignalFrame.hpp:31
void set_current_index(const QModelIndex &newIndex)
Sets the current index.
Definition: NTree.cpp:82
Top-level namespace for coolfluid.
Definition: Action.cpp:18
static ExceptionManager & instance()
Gets the instance of the manager.
Definition: Exception.cpp:34
Client root. This class is wrapper for cf3::common::Root class on the client side. A NRoot object may never have any child. Add them to the internal Root componenent instead. It can be obtained by calling root() method.
Definition: NRoot.hpp:34
Client generic component.
Definition: NGeneric.hpp:29
Exception thrown when a string does not construct a valid path.
Definition: URI.hpp:27
Most basic kernel library.
Definition: Action.cpp:19
void remove_node(const QString &node_name)
Removes a sub-node.
Definition: CNode.cpp:463
T * get() const
Raw pointer to the stored value, or null if there is none.
Definition: Handle.hpp:65
QModelIndex index_from_path(const cf3::common::URI &path) const
Retrieves a node index from its path.
Definition: NTree.cpp:302
Send comments to:
COOLFluiD Web Admin