-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectItemModel_test.cpp
More file actions
337 lines (275 loc) · 11.1 KB
/
ObjectItemModel_test.cpp
File metadata and controls
337 lines (275 loc) · 11.1 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <gtest/gtest.h>
#include <QApplication>
#include <QThread>
#include <QStandardItem>
#include "Manager.h"
#include "ObjectItemModel.h"
#include "SelectionSet.h"
#include "PrimitiveObject.h"
#include <OgreException.h>
#include "TestHelpers.h"
class ObjectItemModelTest : public ::testing::Test
{
protected:
void SetUp() override
{
Manager::kill();
QThread::msleep(50);
app = qobject_cast<QApplication*>(QCoreApplication::instance());
ASSERT_NE(app, nullptr);
if (!tryInitOgre()) {
GTEST_SKIP() << "Skipping: Ogre initialization failed";
}
createStandardOgreMaterials();
}
void TearDown() override
{
if (app)
{
app->processEvents();
}
}
private:
QApplication* app = nullptr;
};
// Test: Constructor creates model with correct header
TEST_F(ObjectItemModelTest, ConstructorCreatesModelWithCorrectHeader)
{
ObjectItemModel model;
// The constructor calls reloadSceneNode which sets header to "No Selection"
QVariant headerData = model.headerData(0, Qt::Horizontal, Qt::DisplayRole);
EXPECT_EQ(headerData.toString(), QString("No Selection"));
}
// Test: Constructor creates model with root item "Scene"
TEST_F(ObjectItemModelTest, ConstructorCreatesRootSceneItem)
{
ObjectItemModel model;
// The root item should exist and be named "Scene"
QModelIndex rootIndex = model.getRootIndex();
EXPECT_TRUE(rootIndex.isValid());
QString rootText = model.data(rootIndex, Qt::DisplayRole).toString();
EXPECT_EQ(rootText, QString("Scene"));
}
// Test: reloadSceneNode builds tree from scene graph
TEST_F(ObjectItemModelTest, ReloadSceneNodeBuildsTreeFromSceneGraph)
{
if (!canLoadMeshFiles()) { GTEST_SKIP() << "Skipping: entity creation not supported without render window"; }
// Create a primitive to add a node to the scene
PrimitiveObject::createCube("TestCube");
// Create the model - constructor calls reloadSceneNode which should pick up the existing node
ObjectItemModel model;
QModelIndex rootIndex = model.getRootIndex();
EXPECT_TRUE(rootIndex.isValid());
// The root item should have at least one child (the TestCube node)
int childCount = model.rowCount(rootIndex);
EXPECT_GE(childCount, 1);
// Verify the child node text contains the name
bool foundCube = false;
for (int i = 0; i < childCount; ++i)
{
QModelIndex childIndex = model.index(i, 0, rootIndex);
QString childText = model.data(childIndex, Qt::DisplayRole).toString();
if (childText.contains("TestCube"))
{
foundCube = true;
break;
}
}
EXPECT_TRUE(foundCube);
Manager::getSingleton()->destroySceneNode("TestCube");
}
// Test: reloadSceneNode rebuilds the tree (clears and repopulates)
TEST_F(ObjectItemModelTest, ReloadSceneNodeRebuildsTree)
{
if (!canLoadMeshFiles()) { GTEST_SKIP() << "Skipping: entity creation not supported without render window"; }
ObjectItemModel model;
QModelIndex rootIndex = model.getRootIndex();
EXPECT_TRUE(rootIndex.isValid());
int initialChildCount = model.rowCount(rootIndex);
// Add a node
PrimitiveObject::createCube("ReloadTestCube");
// Manually trigger reload (simulates what entityCreated signal does)
// The signal is already connected, but since PrimitiveObject::createCube
// also triggers entityCreated, let us check the model
// After the signal, the model should have rebuilt
QModelIndex newRootIndex = model.getRootIndex();
EXPECT_TRUE(newRootIndex.isValid());
int newChildCount = model.rowCount(newRootIndex);
EXPECT_GT(newChildCount, initialChildCount);
Manager::getSingleton()->destroySceneNode("ReloadTestCube");
}
// Test: newObjectNode adds nodes to the model
TEST_F(ObjectItemModelTest, NewObjectNodeAddsNodesToModel)
{
if (!canLoadMeshFiles()) { GTEST_SKIP() << "Skipping: entity creation not supported without render window"; }
ObjectItemModel model;
QModelIndex rootIndex = model.getRootIndex();
int initialChildCount = model.rowCount(rootIndex);
// Creating a primitive triggers sceneNodeCreated signal, which calls newObjectNode
PrimitiveObject::createCube("AddNodeTestCube");
// After signal processing, the model root index may have changed due to reload
// (entityCreated also triggers reloadSceneNode), so fetch the root again
QModelIndex updatedRootIndex = model.getRootIndex();
int updatedChildCount = model.rowCount(updatedRootIndex);
EXPECT_GT(updatedChildCount, initialChildCount);
// Verify the node is present in the tree
bool foundNode = false;
for (int i = 0; i < updatedChildCount; ++i)
{
QModelIndex childIndex = model.index(i, 0, updatedRootIndex);
QString childText = model.data(childIndex, Qt::DisplayRole).toString();
if (childText.contains("AddNodeTestCube"))
{
foundNode = true;
break;
}
}
EXPECT_TRUE(foundNode);
Manager::getSingleton()->destroySceneNode("AddNodeTestCube");
}
// Test: objectNodeRemoved removes nodes from the model
TEST_F(ObjectItemModelTest, ObjectNodeRemovedRemovesNodes)
{
if (!canLoadMeshFiles()) { GTEST_SKIP() << "Skipping: entity creation not supported without render window"; }
// Create a primitive first
PrimitiveObject::createCube("RemoveTestCube");
ObjectItemModel model;
QModelIndex rootIndex = model.getRootIndex();
int childCountBefore = model.rowCount(rootIndex);
EXPECT_GE(childCountBefore, 1);
// Destroy the scene node - this triggers sceneNodeDestroyed signal
// which calls objectNodeRemoved
Manager::getSingleton()->destroySceneNode("RemoveTestCube");
// After the signal, the model should be rebuilt (reloadSceneNode is triggered)
QModelIndex newRootIndex = model.getRootIndex();
int childCountAfter = model.rowCount(newRootIndex);
EXPECT_LT(childCountAfter, childCountBefore);
}
// Test: getRootIndex returns valid index
TEST_F(ObjectItemModelTest, GetRootIndexReturnsValidIndex)
{
ObjectItemModel model;
QModelIndex rootIndex = model.getRootIndex();
EXPECT_TRUE(rootIndex.isValid());
// The root index should point to the "Scene" item
QString rootText = model.data(rootIndex, Qt::DisplayRole).toString();
EXPECT_EQ(rootText, QString("Scene"));
}
// Test: getRootIndex row and column are valid
TEST_F(ObjectItemModelTest, GetRootIndexHasValidRowAndColumn)
{
ObjectItemModel model;
QModelIndex rootIndex = model.getRootIndex();
EXPECT_TRUE(rootIndex.isValid());
EXPECT_EQ(rootIndex.row(), 0);
EXPECT_EQ(rootIndex.column(), 0);
}
// Test: setHeaderText changes header
TEST_F(ObjectItemModelTest, SetHeaderTextChangesHeader)
{
ObjectItemModel model;
// Initially "No Selection"
QVariant headerBefore = model.headerData(0, Qt::Horizontal, Qt::DisplayRole);
EXPECT_EQ(headerBefore.toString(), QString("No Selection"));
// Change header text
model.setHeaderText("Custom Header");
QVariant headerAfter = model.headerData(0, Qt::Horizontal, Qt::DisplayRole);
EXPECT_EQ(headerAfter.toString(), QString("Custom Header"));
}
// Test: setHeaderText with empty string
TEST_F(ObjectItemModelTest, SetHeaderTextWithEmptyString)
{
ObjectItemModel model;
model.setHeaderText("");
QVariant headerData = model.headerData(0, Qt::Horizontal, Qt::DisplayRole);
EXPECT_EQ(headerData.toString(), QString(""));
}
// Test: Model stores node data in user roles
TEST_F(ObjectItemModelTest, ModelStoresNodeDataInUserRoles)
{
if (!canLoadMeshFiles()) { GTEST_SKIP() << "Skipping: entity creation not supported without render window"; }
PrimitiveObject::createCube("DataTestCube");
ObjectItemModel model;
QModelIndex rootIndex = model.getRootIndex();
int childCount = model.rowCount(rootIndex);
EXPECT_GE(childCount, 1);
// Find the cube node and verify it stores node data
for (int i = 0; i < childCount; ++i)
{
QModelIndex childIndex = model.index(i, 0, rootIndex);
QString childText = model.data(childIndex, Qt::DisplayRole).toString();
if (childText.contains("DataTestCube"))
{
// NODE_DATA is Qt::UserRole+1, the data should be a non-null pointer
QVariant nodeData = model.data(childIndex, Qt::UserRole + 1);
EXPECT_TRUE(nodeData.isValid());
void* ptr = nodeData.value<void*>();
EXPECT_NE(ptr, nullptr);
break;
}
}
Manager::getSingleton()->destroySceneNode("DataTestCube");
}
// Test: Entities are added as children of their scene node
TEST_F(ObjectItemModelTest, EntitiesAreChildrenOfSceneNode)
{
if (!canLoadMeshFiles()) { GTEST_SKIP() << "Skipping: entity creation not supported without render window"; }
PrimitiveObject::createCube("EntityTestCube");
ObjectItemModel model;
QModelIndex rootIndex = model.getRootIndex();
int childCount = model.rowCount(rootIndex);
EXPECT_GE(childCount, 1);
// Find the cube node and check it has entity children
for (int i = 0; i < childCount; ++i)
{
QModelIndex childIndex = model.index(i, 0, rootIndex);
QString childText = model.data(childIndex, Qt::DisplayRole).toString();
if (childText.contains("EntityTestCube"))
{
// The node should have at least one entity child (the mesh)
int entityCount = model.rowCount(childIndex);
EXPECT_GE(entityCount, 1);
// The entity child should contain "(Mesh)" in its text
bool foundMesh = false;
for (int j = 0; j < entityCount; ++j)
{
QModelIndex entityIndex = model.index(j, 0, childIndex);
QString entityText = model.data(entityIndex, Qt::DisplayRole).toString();
if (entityText.contains("(Mesh)"))
{
foundMesh = true;
break;
}
}
EXPECT_TRUE(foundMesh);
break;
}
}
Manager::getSingleton()->destroySceneNode("EntityTestCube");
}
// Test: Multiple nodes can be tracked in the model
TEST_F(ObjectItemModelTest, MultipleNodesTracked)
{
if (!canLoadMeshFiles()) { GTEST_SKIP() << "Skipping: entity creation not supported without render window"; }
PrimitiveObject::createCube("MultiCube1");
PrimitiveObject::createSphere("MultiSphere1");
ObjectItemModel model;
QModelIndex rootIndex = model.getRootIndex();
int childCount = model.rowCount(rootIndex);
EXPECT_GE(childCount, 2);
bool foundCube = false;
bool foundSphere = false;
for (int i = 0; i < childCount; ++i)
{
QModelIndex childIndex = model.index(i, 0, rootIndex);
QString childText = model.data(childIndex, Qt::DisplayRole).toString();
if (childText.contains("MultiCube1"))
foundCube = true;
if (childText.contains("MultiSphere1"))
foundSphere = true;
}
EXPECT_TRUE(foundCube);
EXPECT_TRUE(foundSphere);
Manager::getSingleton()->destroySceneNode("MultiCube1");
Manager::getSingleton()->destroySceneNode("MultiSphere1");
}