forked from paceholder/nodeeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFlowScene.cpp
More file actions
228 lines (171 loc) · 7.38 KB
/
TestFlowScene.cpp
File metadata and controls
228 lines (171 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "ApplicationSetup.hpp"
#include "Stringify.hpp"
#include "StubNodeDataModel.hpp"
#include <QtNodes/FlowScene>
#include <QtNodes/Node>
#include <QtNodes/NodeDataModel>
#include <catch2/catch.hpp>
#include <functional>
#include <memory>
#include <utility>
#include <vector>
using QtNodes::Connection;
using QtNodes::DataModelRegistry;
using QtNodes::FlowScene;
using QtNodes::Node;
using QtNodes::NodeData;
using QtNodes::NodeDataModel;
using QtNodes::NodeDataType;
using QtNodes::PortIndex;
using QtNodes::PortType;
TEST_CASE("FlowScene triggers connections created or deleted", "[gui]")
{
struct MockDataModel : StubNodeDataModel
{
unsigned int nPorts(PortType) const override { return 1; }
void inputConnectionCreated(Connection const &) override { inputCreatedCalledCount++; }
void inputConnectionDeleted(Connection const &) override { inputDeletedCalledCount++; }
void outputConnectionCreated(Connection const &) override { outputCreatedCalledCount++; }
void outputConnectionDeleted(Connection const &) override { outputDeletedCalledCount++; }
int inputCreatedCalledCount = 0;
int inputDeletedCalledCount = 0;
int outputCreatedCalledCount = 0;
int outputDeletedCalledCount = 0;
void resetCallCounts()
{
inputCreatedCalledCount = 0;
inputDeletedCalledCount = 0;
outputCreatedCalledCount = 0;
outputDeletedCalledCount = 0;
}
};
auto setup = applicationSetup();
FlowScene scene;
Node &fromNode = scene.createNode(std::make_unique<MockDataModel>());
Node &toNode = scene.createNode(std::make_unique<MockDataModel>());
Node &unrelatedNode = scene.createNode(std::make_unique<MockDataModel>());
auto &fromNgo = fromNode.nodeGraphicsObject();
auto &toNgo = toNode.nodeGraphicsObject();
auto &unrelatedNgo = unrelatedNode.nodeGraphicsObject();
fromNgo.setPos(0, 0);
toNgo.setPos(200, 20);
unrelatedNgo.setPos(-100, -100);
auto &from = dynamic_cast<MockDataModel &>(*fromNode.nodeDataModel());
auto &to = dynamic_cast<MockDataModel &>(*toNode.nodeDataModel());
auto &unrelated = dynamic_cast<MockDataModel &>(*unrelatedNode.nodeDataModel());
SECTION("creating half a connection (not finishing the connection)")
{
auto connection = scene.createConnection(PortType::Out, fromNode, 0);
CHECK(from.inputCreatedCalledCount == 0);
CHECK(from.outputCreatedCalledCount == 0);
CHECK(to.inputCreatedCalledCount == 0);
CHECK(to.outputCreatedCalledCount == 0);
CHECK(unrelated.inputCreatedCalledCount == 0);
CHECK(unrelated.outputCreatedCalledCount == 0);
scene.deleteConnection(*connection);
}
struct Creation
{
std::string name;
std::function<std::shared_ptr<Connection>()> createConnection;
};
Creation sceneCreation{"scene.createConnection",
[&] { return scene.createConnection(toNode, 0, fromNode, 0); }};
Creation partialCreation{"scene.createConnection-by partial", [&] {
auto connection = scene.createConnection(PortType::Out,
fromNode,
0);
connection->setNodeToPort(toNode, PortType::In, 0);
return connection;
}};
struct Deletion
{
std::string name;
std::function<void(Connection &connection)> deleteConnection;
};
Deletion sceneDeletion{"scene.deleteConnection",
[&](Connection &c) { scene.deleteConnection(c); }};
Deletion partialDragDeletion{"scene-deleteConnectionByDraggingOff", [&](Connection &c) {
PortIndex portIndex = c.getPortIndex(PortType::In);
Node *node = c.getNode(PortType::In);
node->nodeState().getEntries(PortType::In)[portIndex].clear();
c.clearNode(PortType::In);
}};
SECTION("creating a connection")
{
std::vector<Creation> cases({sceneCreation, partialCreation});
for (Creation const &create : cases) {
SECTION(create.name)
{
auto connection = create.createConnection();
CHECK(from.inputCreatedCalledCount == 0);
CHECK(from.outputCreatedCalledCount == 1);
CHECK(to.inputCreatedCalledCount == 1);
CHECK(to.outputCreatedCalledCount == 0);
CHECK(unrelated.inputCreatedCalledCount == 0);
CHECK(unrelated.outputCreatedCalledCount == 0);
scene.deleteConnection(*connection);
}
}
}
SECTION("deleting a connection")
{
std::vector<Deletion> cases({sceneDeletion, partialDragDeletion});
for (auto const &deletion : cases) {
SECTION("deletion: " + deletion.name)
{
Connection &connection = *sceneCreation.createConnection();
from.resetCallCounts();
to.resetCallCounts();
deletion.deleteConnection(connection);
// Here the Connection reference becomes dangling
CHECK(from.inputDeletedCalledCount == 0);
CHECK(from.outputDeletedCalledCount == 1);
CHECK(to.inputDeletedCalledCount == 1);
CHECK(to.outputDeletedCalledCount == 0);
CHECK(unrelated.inputDeletedCalledCount == 0);
CHECK(unrelated.outputDeletedCalledCount == 0);
}
}
}
}
TEST_CASE("FlowScene's DataModelRegistry outlives nodes and connections", "[asan][gui]")
{
class MockDataModel : public StubNodeDataModel
{
public:
MockDataModel(int *const &incrementOnDestruction)
: incrementOnDestruction(incrementOnDestruction)
{}
~MockDataModel() { (*incrementOnDestruction)++; }
// The reference ensures that we point into the memory that would be free'd
// if the DataModelRegistry doesn't outlive this node
int *const &incrementOnDestruction;
};
struct MockDataModelCreator
{
MockDataModelCreator(int *shouldBeAliveWhenAssignedTo)
: shouldBeAliveWhenAssignedTo(shouldBeAliveWhenAssignedTo)
{}
auto operator()() const
{
return std::make_unique<MockDataModel>(shouldBeAliveWhenAssignedTo);
}
int *shouldBeAliveWhenAssignedTo;
};
int modelsDestroyed = 0;
// Introduce a new scope, so that modelsDestroyed will be alive even after the
// FlowScene is destroyed.
{
auto setup = applicationSetup();
auto registry = std::make_shared<DataModelRegistry>();
registry->registerModel(MockDataModelCreator(&modelsDestroyed));
modelsDestroyed = 0;
FlowScene scene(std::move(registry));
auto &node = scene.createNode(scene.registry().create("name"));
// On destruction, if this `node` outlives its MockDataModelCreator,
// (if it outlives the DataModelRegistry), then we trigger undefined
// behavior through use-after-free. ASAN will catch that.
}
CHECK(modelsDestroyed == 1);
}