Blender V5.3
lib_remap_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#include "testing/testing.h"
5
7#include "DNA_mesh_types.h"
8#include "DNA_node_types.h"
9#include "DNA_object_types.h"
10
11#include "BKE_context.hh"
12#include "BKE_global.hh"
13#include "BKE_gtest_base.hh"
14#include "BKE_idtype.hh"
15#include "BKE_lib_id.hh"
16#include "BKE_lib_remap.hh"
17#include "BKE_main.hh"
18#include "BKE_material.hh"
19#include "BKE_mesh.h"
20#include "BKE_node.hh"
21#include "BKE_object.hh"
22
23#include "NOD_defaults.hh"
24
25namespace blender {
26
27using namespace blender::bke::id;
28
29namespace bke::tests {
30
31namespace {
32class TestData {
33 public:
34 Main *bmain = nullptr;
35 bContext *C = nullptr;
36
37 TestData()
38 {
39 if (this->bmain == nullptr) {
40 this->bmain = BKE_main_new();
41 G.main = this->bmain;
42 }
43
44 if (this->C == nullptr) {
45 this->C = CTX_create();
46 CTX_data_main_set(this->C, this->bmain);
47 }
48 }
49
50 ~TestData()
51 {
52 if (this->bmain != nullptr) {
53 BKE_main_free(this->bmain);
54 this->bmain = nullptr;
55 G.main = nullptr;
56 }
57
58 if (this->C != nullptr) {
59 CTX_free(C);
60 this->C = nullptr;
61 }
62 }
63};
64
65class LibRemapTest : public BlenderGTestBase {};
66
67class MaterialTestData : public TestData {
68 public:
69 Material *material = nullptr;
70 bNodeTree *material_nodetree = nullptr;
71 MaterialTestData()
72 {
73 material = BKE_material_add(this->bmain, "Material");
74 this->material_nodetree = this->material->nodetree;
75 }
76};
77
78class MeshTestData : public TestData {
79 public:
80 Mesh *mesh = nullptr;
81 MeshTestData()
82 {
83 this->mesh = BKE_mesh_add(this->bmain, nullptr);
84 }
85};
86
87class TwoMeshesTestData : public MeshTestData {
88 public:
89 Mesh *other_mesh = nullptr;
90
91 TwoMeshesTestData()
92 {
93 this->other_mesh = BKE_mesh_add(this->bmain, nullptr);
94 }
95};
96
97class MeshObjectTestData : public MeshTestData {
98 public:
99 Object *object;
100 MeshObjectTestData()
101 {
102 this->object = BKE_object_add_only_object(this->bmain, OB_MESH, nullptr);
103 this->object->data = id_cast<ID *>(this->mesh);
104 }
105};
106} // namespace
107
108/* -------------------------------------------------------------------- */
111
112TEST_F(LibRemapTest, embedded_ids_can_not_be_remapped)
113{
114 MaterialTestData context;
115 bNodeTree *other_tree = BKE_id_new_nomain<bNodeTree>(nullptr);
116
117 ASSERT_NE(context.material, nullptr);
118 ASSERT_EQ(context.material_nodetree, context.material->nodetree);
119
120 BKE_libblock_remap(context.bmain, context.material_nodetree, other_tree, 0);
121
122 EXPECT_EQ(context.material_nodetree, context.material->nodetree);
123 EXPECT_NE(context.material->nodetree, other_tree);
124
125 BKE_id_free(nullptr, other_tree);
126}
127
128TEST_F(LibRemapTest, embedded_ids_can_not_be_deleted)
129{
130 MaterialTestData context;
131
132 ASSERT_NE(context.material_nodetree, nullptr);
133 ASSERT_EQ(context.material_nodetree, context.material->nodetree);
134
136 context.bmain, context.material_nodetree, nullptr, ID_REMAP_SKIP_NEVER_NULL_USAGE);
137
138 EXPECT_EQ(context.material_nodetree, context.material->nodetree);
139 EXPECT_NE(context.material->nodetree, nullptr);
140}
141
143
144/* -------------------------------------------------------------------- */
147
148TEST_F(LibRemapTest, delete_when_remap_to_self_not_allowed)
149{
150 TwoMeshesTestData context;
151
152 ASSERT_NE(context.mesh, nullptr);
153 ASSERT_NE(context.other_mesh, nullptr);
154 context.mesh->texcomesh = context.other_mesh;
155
156 BKE_libblock_remap(context.bmain, context.other_mesh, context.mesh, 0);
157
158 EXPECT_EQ(context.mesh->texcomesh, nullptr);
159}
160
162
163/* -------------------------------------------------------------------- */
166
167TEST_F(LibRemapTest, users_are_decreased_when_not_skipping_never_null)
168{
169 MeshObjectTestData context;
170
171 ASSERT_NE(context.object, nullptr);
172 ASSERT_EQ(context.object->data, id_cast<ID *>(context.mesh));
173 ASSERT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
174 ASSERT_EQ(context.mesh->id.us, 1);
175
176 /* This is an invalid situation, test case tests this in between value until we have a better
177 * solution. */
178 BKE_libblock_remap(context.bmain, context.mesh, nullptr, 0);
179 EXPECT_EQ(context.mesh->id.us, 0);
180 EXPECT_EQ(context.object->data, id_cast<ID *>(context.mesh));
181 EXPECT_NE(context.object->data, nullptr);
182 EXPECT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
183}
184
185TEST_F(LibRemapTest, users_are_same_when_skipping_never_null)
186{
187 MeshObjectTestData context;
188
189 ASSERT_NE(context.object, nullptr);
190 ASSERT_EQ(context.object->data, id_cast<ID *>(context.mesh));
191 ASSERT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
192 ASSERT_EQ(context.mesh->id.us, 1);
193
194 BKE_libblock_remap(context.bmain, context.mesh, nullptr, ID_REMAP_SKIP_NEVER_NULL_USAGE);
195 EXPECT_EQ(context.mesh->id.us, 1);
196 EXPECT_EQ(context.object->data, id_cast<ID *>(context.mesh));
197 EXPECT_NE(context.object->data, nullptr);
198 EXPECT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
199}
200
202
203/* -------------------------------------------------------------------- */
206
207TEST_F(LibRemapTest, do_not_delete_when_cannot_unset)
208{
209 MeshObjectTestData context;
210
211 ASSERT_NE(context.object, nullptr);
212 ASSERT_EQ(context.object->data, id_cast<ID *>(context.mesh));
213
214 BKE_libblock_remap(context.bmain, context.mesh, nullptr, ID_REMAP_SKIP_NEVER_NULL_USAGE);
215 EXPECT_EQ(context.object->data, id_cast<ID *>(context.mesh));
216 EXPECT_NE(context.object->data, nullptr);
217}
218
219TEST_F(LibRemapTest, force_never_null_usage)
220{
221 MeshObjectTestData context;
222
223 ASSERT_NE(context.object, nullptr);
224 ASSERT_EQ(context.object->data, id_cast<ID *>(context.mesh));
225
226 BKE_libblock_remap(context.bmain, context.mesh, nullptr, ID_REMAP_FORCE_NEVER_NULL_USAGE);
227 EXPECT_EQ(context.object->data, nullptr);
228}
229
230TEST_F(LibRemapTest, never_null_usage_flag_not_requested_on_delete)
231{
232 MeshObjectTestData context;
233
234 ASSERT_NE(context.object, nullptr);
235 ASSERT_EQ(context.object->data, id_cast<ID *>(context.mesh));
236 ASSERT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
237
238 /* Never null usage isn't requested so the flag should not be set. */
239 BKE_libblock_remap(context.bmain, context.mesh, nullptr, ID_REMAP_SKIP_NEVER_NULL_USAGE);
240 EXPECT_EQ(context.object->data, id_cast<ID *>(context.mesh));
241 EXPECT_NE(context.object->data, nullptr);
242 EXPECT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
243}
244
245TEST_F(LibRemapTest, never_null_usage_storage_requested_on_delete)
246{
247 MeshObjectTestData context;
248
249 ASSERT_NE(context.object, nullptr);
250 ASSERT_EQ(context.object->data, id_cast<ID *>(context.mesh));
251 ASSERT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
252
253 /* Never null usage is requested so the owner ID (the Object) should be added to the set. */
254 IDRemapper remapper;
255 remapper.add(&context.mesh->id, nullptr);
258
259 /* Never null usages un-assignment is not enforced (no #ID_REMAP_FORCE_NEVER_NULL_USAGE),
260 * so the object-data should still use the original mesh. */
261 EXPECT_EQ(context.object->data, id_cast<ID *>(context.mesh));
262 EXPECT_NE(context.object->data, nullptr);
263 EXPECT_TRUE(remapper.never_null_users().contains(&context.object->id));
264}
265
266TEST_F(LibRemapTest, never_null_usage_flag_not_requested_on_remap)
267{
268 MeshObjectTestData context;
269 Mesh *other_mesh = BKE_mesh_add(context.bmain, nullptr);
270
271 ASSERT_NE(context.object, nullptr);
272 ASSERT_EQ(context.object->data, id_cast<ID *>(context.mesh));
273 ASSERT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
274
275 /* Never null usage isn't requested so the flag should not be set. */
276 BKE_libblock_remap(context.bmain, context.mesh, other_mesh, ID_REMAP_SKIP_NEVER_NULL_USAGE);
277 EXPECT_EQ(context.object->data, id_cast<ID *>(other_mesh));
278 EXPECT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
279}
280
281TEST_F(LibRemapTest, never_null_usage_storage_requested_on_remap)
282{
283 MeshObjectTestData context;
284 Mesh *other_mesh = BKE_mesh_add(context.bmain, nullptr);
285
286 ASSERT_NE(context.object, nullptr);
287 ASSERT_EQ(context.object->data, id_cast<ID *>(context.mesh));
288 ASSERT_EQ(context.object->id.tag & ID_TAG_DOIT, 0);
289
290 /* Never null usage is requested, but the obdata is remapped to another Mesh, not to `nullptr`,
291 * so the `never_null_users` set should remain empty. */
292 IDRemapper remapper;
293 remapper.add(&context.mesh->id, &other_mesh->id);
296 EXPECT_EQ(context.object->data, id_cast<ID *>(other_mesh));
298}
299
301
302} // namespace bke::tests
303} // namespace blender
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool is_empty() const
Definition BLI_set.hh:614
void add(ID *old_id, ID *new_id)
const Set< ID * > & never_null_users() const
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
BMesh Iterators.
void void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, int remap_flags) ATTR_NONNULL(1
Definition lib_remap.cc:701
Main * BKE_main_new()
Definition main.cc:93
@ ID_REMAP_SKIP_NEVER_NULL_USAGE
@ ID_REMAP_FORCE_NEVER_NULL_USAGE
@ ID_REMAP_STORE_NEVER_NULL_USAGE
bContext * CTX_create()
Global G
Definition blender.cc:57
void CTX_free(bContext *C)
void BKE_id_free(Main *bmain, void *idv)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
void BKE_libblock_remap_multiple_locked(Main *bmain, bke::id::IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:654
void CTX_data_main_set(bContext *C, Main *bmain)
Material * BKE_material_add(Main *bmain, const char *name)
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
Mesh * BKE_mesh_add(Main *bmain, const char *name)