Blender V5.3
lib_query_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#include "DNA_world_types.h"
11
12#include "RNA_define.hh"
13
14#include "BKE_collection.hh"
15#include "BKE_context.hh"
16#include "BKE_global.hh"
17#include "BKE_gtest_base.hh"
18#include "BKE_idprop.hh"
19#include "BKE_idtype.hh"
20#include "BKE_lib_id.hh"
21#include "BKE_lib_query.hh"
22#include "BKE_main.hh"
23#include "BKE_material.hh"
24#include "BKE_mesh.h"
25#include "BKE_node.hh"
26#include "BKE_object.hh"
27#include "BKE_scene.hh"
28
29#include "NOD_defaults.hh"
30
31#include "BLI_index_range.hh"
32#include "BLI_set.hh"
33
34namespace blender::bke::tests {
35
36namespace {
37class TestData {
38 public:
39 Main *bmain = nullptr;
40 bContext *C = nullptr;
41
42 TestData()
43 {
44 if (this->bmain == nullptr) {
45 this->bmain = BKE_main_new();
46 G.main = this->bmain;
47 }
48
49 if (this->C == nullptr) {
50 this->C = CTX_create();
51 CTX_data_main_set(this->C, this->bmain);
52 }
53 }
54
55 ~TestData()
56 {
57 if (this->bmain != nullptr) {
58 BKE_main_free(this->bmain);
59 this->bmain = nullptr;
60 G.main = nullptr;
61 }
62
63 if (this->C != nullptr) {
64 CTX_free(this->C);
65 this->C = nullptr;
66 }
67 }
68};
69
70class LibQueryTest : public bke::BlenderGTestBase {};
71
72class WholeIDTestData : public TestData {
73 public:
74 Scene *scene = nullptr;
75 Object *object = nullptr;
76 Object *target = nullptr;
77 Mesh *mesh = nullptr;
78 Material *material = nullptr;
79
80 WholeIDTestData(const bool generate_cached_bmain_relations = false)
81 {
82 this->scene = BKE_scene_add(this->bmain, "IDLibQueryScene");
83 CTX_data_scene_set(this->C, this->scene);
84
85 this->object = BKE_object_add_only_object(this->bmain, OB_MESH, "IDLibQueryObject");
86 this->target = BKE_object_add_only_object(this->bmain, OB_EMPTY, "IDLibQueryTarget");
87
88 this->mesh = BKE_mesh_add(this->bmain, "IDLibQueryMesh");
89 this->object->data = id_cast<ID *>(this->mesh);
90
91 BKE_collection_object_add(this->bmain, this->scene->master_collection, this->object);
92 BKE_collection_object_add(this->bmain, this->scene->master_collection, this->target);
93
94 if (generate_cached_bmain_relations) {
95 BKE_main_relations_create(this->bmain, 0);
96 }
97 }
98};
99
100class IDSubDataTestData : public WholeIDTestData {
101 public:
102 bNode *node = nullptr;
103
104 IDSubDataTestData(const bool generate_cached_bmain_relations = false)
105 {
106 /* Add a material that contains an embedded nodetree and assign a custom property to one of
107 * its nodes. */
108 this->material = BKE_material_add(this->bmain, "Material");
109 nodes::node_tree_shader_default(this->C, this->bmain, &this->material->id);
110
112 this->bmain, this->object, this->material, this->object->actcol, BKE_MAT_ASSIGN_OBJECT);
113
114 this->node = this->material->nodetree->nodes.first();
115
116 this->node->prop = bke::idprop::create_group("Node Custom Properties").release();
117 IDP_AddToGroup(this->node->prop,
118 bke::idprop::create("ID Pointer", &this->target->id).release());
119
120 if (generate_cached_bmain_relations) {
121 BKE_main_relations_create(this->bmain, 0);
122 }
123 }
124 ~IDSubDataTestData()
125 {
126 BKE_id_free(this->bmain, &this->material->id);
127 }
128};
129} // namespace
130
131/* -------------------------------------------------------------------- */
134
135TEST_F(LibQueryTest, libquery_basic)
136{
137 /* Behavior should remain strictly identical, between foreach_id iterations over 'raw' ID data,
138 * or the cached relationships stored in Main::relations. */
139 WholeIDTestData context_raw = {false};
140 WholeIDTestData context_cached = {true};
141
142 for (const WholeIDTestData *context_p : {&context_raw, &context_cached}) {
143 const WholeIDTestData &context = *context_p;
144
145 ASSERT_NE(context.scene, nullptr);
146 ASSERT_NE(context.object, nullptr);
147 ASSERT_NE(context.target, nullptr);
148 ASSERT_NE(context.mesh, nullptr);
149
150 /* Reset all ID user-count to 0. */
151 ID *id_iter;
152 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
153 id_iter->us = 0;
154 }
156
157 /* Set an invalid user-count value to IDs directly used by the scene.
158 * This includes these used by its embedded IDs, like the master collection, and the scene
159 * itself (through the loop-back pointers of embedded IDs to their owner). */
160 auto set_count = [](LibraryIDLinkCallbackData *cb_data) -> int {
161 if (*(cb_data->id_pointer)) {
162 (*(cb_data->id_pointer))->us = 42;
163 }
164 return IDWALK_RET_NOP;
165 };
167 context.bmain, &context.scene->id, set_count, nullptr, IDWALK_READONLY);
168 EXPECT_EQ(context.scene->id.us, 42);
169 EXPECT_EQ(context.object->id.us, 42);
170 EXPECT_EQ(context.target->id.us, 42);
171 EXPECT_EQ(context.mesh->id.us, 0);
172
173 /* Clear object's obdata mesh pointer. */
174 auto clear_mesh_pointer = [&context](LibraryIDLinkCallbackData *cb_data) -> int {
175 if (*(cb_data->id_pointer) == &context.mesh->id) {
176 *(cb_data->id_pointer) = nullptr;
177 }
178 return IDWALK_RET_NOP;
179 };
181 context.bmain, &context.object->id, clear_mesh_pointer, nullptr, IDWALK_NOP);
182 EXPECT_EQ(context.object->data, nullptr);
183
184#if 0 /* Does not work. */
185 /* Modifying data when IDWALK_READONLY is set is forbidden. */
186 context.object->data = context.mesh;
187 EXPECT_BLI_ASSERT(BKE_library_foreach_ID_link(context.bmain,
188 &context.scene->id,
189 clear_mesh_pointer,
190 &context.test_data,
192 "");
193#endif
194 }
195}
196
197TEST_F(LibQueryTest, libquery_recursive)
198{
199 /* Behavior should remain strictly identical, between foreach_id iterations over 'raw' ID data,
200 * or the cached relationships stored in Main::relations. */
201 IDSubDataTestData context_raw = {false};
202 IDSubDataTestData context_cached = {true};
203
204 for (const IDSubDataTestData *context_p : {&context_raw, &context_cached}) {
205 const IDSubDataTestData &context = *context_p;
206
207 EXPECT_NE(context.scene, nullptr);
208 EXPECT_NE(context.object, nullptr);
209 EXPECT_NE(context.target, nullptr);
210 EXPECT_NE(context.mesh, nullptr);
211
212 /* Reset all ID user-count to 0. */
213 ID *id_iter;
214 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
215 id_iter->us = 0;
216 }
218
219 /* Set an invalid user-count value to all IDs used by the scene, recursively.
220 * Here, it should mean all IDs in Main, including the scene itself
221 * (because of the loop-back pointer from the embedded master collection to its scene owner).
222 */
223 auto set_count = [](LibraryIDLinkCallbackData *cb_data) -> int {
224 if (*(cb_data->id_pointer)) {
225 (*(cb_data->id_pointer))->us = 42;
226 }
227 return IDWALK_RET_NOP;
228 };
230 context.bmain, &context.scene->id, set_count, nullptr, IDWALK_RECURSE);
231 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
232 EXPECT_EQ(id_iter->us, 42);
233 }
235
236 /* Reset all ID user-count to 0. */
237 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
238 id_iter->us = 0;
239 }
241
242 /* Recompute valid user counts for all IDs used by the scene, recursively. */
243 auto compute_count = [](LibraryIDLinkCallbackData *cb_data) -> int {
244 if (*(cb_data->id_pointer) && (cb_data->cb_flag & IDWALK_CB_USER) != 0) {
245 (*(cb_data->id_pointer))->us++;
246 }
247 return IDWALK_RET_NOP;
248 };
250 context.bmain, &context.scene->id, compute_count, nullptr, IDWALK_RECURSE);
251 EXPECT_EQ(context.scene->id.us, 0);
252 EXPECT_EQ(context.object->id.us, 1);
253 /* Scene's master collection, and scene's compositor node IDProperty. Note that object
254 * constraint is _not_ a reference-counting usage. */
255 EXPECT_EQ(context.target->id.us, 2);
256 EXPECT_EQ(context.mesh->id.us, 1);
257 }
258}
259
260TEST_F(LibQueryTest, libquery_subdata)
261{
262 /* Behavior should remain strictly identical, between foreach_id iterations over 'raw' ID data,
263 * or the cached relationships stored in Main::relations. */
264 IDSubDataTestData context_raw = {false};
265 IDSubDataTestData context_cached = {true};
266
267 for (const IDSubDataTestData *context_p : {&context_raw, &context_cached}) {
268 const IDSubDataTestData &context = *context_p;
269
270 ASSERT_NE(context.scene, nullptr);
271 ASSERT_NE(context.object, nullptr);
272 ASSERT_NE(context.target, nullptr);
273 ASSERT_NE(context.mesh, nullptr);
274 ASSERT_NE(context.material, nullptr);
275
276 /* Reset all ID user-count to 0. */
277 ID *id_iter;
278 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
279 id_iter->us = 0;
280 }
282
283 /* Set an invalid user-count value to all IDs used by one of the material's nodes. */
284 auto set_count = [](LibraryIDLinkCallbackData *cb_data) -> int {
285 if (*(cb_data->id_pointer)) {
286 (*(cb_data->id_pointer))->us = 42;
287 }
288 return IDWALK_RET_NOP;
289 };
290 auto node_foreach_id = [&context](LibraryForeachIDData *data) {
291 bke::node_node_foreach_id(context.node, data);
292 };
293
294 BKE_library_foreach_subdata_id(context.bmain,
295 &context.material->id,
296 &context.material->nodetree->id,
297 node_foreach_id,
298 set_count,
299 nullptr,
300 IDWALK_NOP);
301
302 EXPECT_EQ(context.scene->id.us, 0);
303 EXPECT_EQ(context.object->id.us, 0);
304 /* The material's node-tree input node IDProperty uses the target object. */
305 EXPECT_EQ(context.target->id.us, 42);
306 EXPECT_EQ(context.mesh->id.us, 0);
307 }
308}
309
311
312/* -------------------------------------------------------------------- */
315
316class UnusedIDsTestData : public TestData {
317 public:
318 Library *library = nullptr;
319 Scene *scene = nullptr;
320 World *world = nullptr;
321 Object *object = nullptr;
322 Object *target = nullptr;
323 Mesh *mesh = nullptr;
324
325 /* IDs that must never be detected as unused. */
327 /* IDs that are 'immediately' unused (i.e. have `0` users refcounting). */
329 /* IDs that must only be detected as unused in the recursive search. */
331
333 {
334 /* Used data. */
335 this->library = BKE_id_new<Library>(this->bmain, "IDLibQueryLibrary");
336 used_ids.add(&this->library->id);
337 id_us_ensure_real(&this->library->id);
338
339 this->scene = BKE_scene_add(this->bmain, "IDLibQueryScene");
340 used_ids.add(&this->scene->id);
341 id_us_ensure_real(&this->scene->id);
342 CTX_data_scene_set(this->C, this->scene);
343 this->world = BKE_id_new<World>(this->bmain, "IDLibQueryWorld");
344 used_ids.add(&this->world->id);
345 scene->world = this->world;
346
347 this->object = BKE_object_add_only_object(this->bmain, OB_MESH, "IDLibQueryObject");
348 used_ids.add(&this->object->id);
349 this->target = BKE_object_add_only_object(this->bmain, OB_EMPTY, "IDLibQueryTarget");
350 used_ids.add(&this->target->id);
351
352 this->mesh = BKE_id_new_in_lib<Mesh>(this->bmain, this->library, "IDLibQueryMesh");
353 used_ids.add(&this->mesh->id);
354 this->object->data = id_cast<ID *>(this->mesh);
355
356 BKE_collection_object_add(this->bmain, this->scene->master_collection, this->object);
357 BKE_collection_object_add(this->bmain, this->scene->master_collection, this->target);
358
359 /* Unused data. */
360 /* 'dummy' unused data. */
361 Mesh *me0 = BKE_id_new_in_lib<Mesh>(this->bmain, this->library, "IDLibQueryMeshUnused");
362 unused_ids.add(&me0->id);
363 World *world0 = BKE_id_new<World>(this->bmain, "IDLibQueryWorldUnused");
364 unused_ids.add(&world0->id);
365
366 /* 'Fake User' IDs are always considered as used. */
367 Object *ob0 = BKE_object_add_only_object(this->bmain, OB_EMPTY, "IDLibQueryObjectUnused");
368 id_fake_user_set(&ob0->id);
369 used_ids.add(&ob0->id);
370
371 /* Libraries are never considered as unused by this code (`IDTYPE_FLAGS_NEVER_UNUSED`). */
372 Library *li0 = BKE_id_new<Library>(this->bmain, "IDLibQueryLibraryUnused");
373 used_ids.add(&li0->id);
374 id_us_ensure_real(&li0->id);
375
376 auto make_islands = [this](Library *lib_1, Library *lib_2) -> void {
377 /* Disconnected sub-graphs of IDs. */
378 Mesh *me1 = BKE_id_new_in_lib<Mesh>(this->bmain, lib_2, "IDLibQueryMeshUnused_Island_1");
380 Mesh *me2 = BKE_id_new_in_lib<Mesh>(this->bmain, lib_1, "IDLibQueryMeshUnused2_Island_1");
383 this->bmain, lib_2, "IDLibQueryWorldUnused_Island_1");
384 unused_recursive_ids.add(&world1->id);
386 this->bmain, lib_1, "IDLibQueryObjectUnused_Island_1");
387 ob1->type = OB_MESH;
389 /* Create the internal links keeping this isolated group 'used' from a refcounting
390 * perspective. */
391 ob1->data = id_cast<ID *>(me1);
392 IDProperty *ob1_idp = IDP_EnsureProperties(&ob1->id);
393 IDP_AddToGroup(ob1_idp, bke::idprop::create("Ob1ToMesh1", &me1->id).release());
394 IDP_AddToGroup(ob1_idp, bke::idprop::create("Ob1ToMesh2", &me2->id).release());
395 IDProperty *me2_idp = IDP_EnsureProperties(&me2->id);
396 IDP_AddToGroup(me2_idp, bke::idprop::create("Mesh1ToWorld1", &world1->id).release());
397 IDProperty *world1_idp = IDP_EnsureProperties(&world1->id);
398 IDP_AddToGroup(world1_idp, bke::idprop::create("World1ToOb1", &ob1->id).release());
399 };
400 /* Disconnected graph of all local IDs. */
401 make_islands(nullptr, nullptr);
402 /* Disconnected graph of all linked IDs. */
403 make_islands(this->library, this->library);
404 /* Disconnected graph of mixed local and linked IDs. */
405 make_islands(nullptr, this->library);
406
407 /* Ensure all ID refcountings are valid. */
408 BKE_main_id_refcount_recompute(this->bmain, false);
409 }
410};
411
412TEST_F(LibQueryTest, libquery_unused_ids_direct)
413{
414 UnusedIDsTestData context;
415
416 ASSERT_NE(context.library, nullptr);
417 ASSERT_NE(context.scene, nullptr);
418 ASSERT_NE(context.world, nullptr);
419 ASSERT_NE(context.object, nullptr);
420 ASSERT_NE(context.target, nullptr);
421 ASSERT_NE(context.mesh, nullptr);
422
423 ID *id_iter;
424 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
425 EXPECT_TRUE(context.used_ids.contains(id_iter) || context.unused_ids.contains(id_iter) ||
426 context.unused_recursive_ids.contains(id_iter));
427 }
429
430 /* "Detect nothing" case. */
432
434 for (const int64_t index : IndexRange(INDEX_ID_MAX)) {
435 EXPECT_EQ(data.num_local[index], 0);
436 EXPECT_EQ(data.num_linked[index], 0);
437 EXPECT_EQ(data.num_total[index], 0);
438 }
439 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
440 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) == 0);
441 }
443
444 /* "Detect local directly unused IDs" case. */
445 data = {};
446 data.do_local_ids = true;
447
449
450 for (const int64_t index : IndexRange(INDEX_ID_MAX)) {
451 EXPECT_EQ(data.num_linked[index], 0);
452 }
453 EXPECT_EQ(data.num_local[INDEX_ID_WO], 1);
454 EXPECT_EQ(data.num_local[INDEX_ID_WO], data.num_total[INDEX_ID_WO]);
455 EXPECT_EQ(data.num_local[INDEX_ID_NULL], 1);
456 EXPECT_EQ(data.num_local[INDEX_ID_NULL], data.num_total[INDEX_ID_NULL]);
457
458 for (ID *id_iter : context.unused_ids) {
459 EXPECT_TRUE(((id_iter->tag & ID_TAG_DOIT) != 0) != ID_IS_LINKED(id_iter));
460 }
461 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
462 if (!ID_IS_LINKED(id_iter) && context.unused_ids.contains(id_iter)) {
463 continue;
464 }
465 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) == 0);
466 }
468
469 /* "Detect linked directly unused IDs" case. */
470 data = {};
471 data.do_linked_ids = true;
472
474
475 for (const int64_t index : IndexRange(INDEX_ID_MAX)) {
476 EXPECT_EQ(data.num_local[index], 0);
477 }
478 EXPECT_EQ(data.num_linked[INDEX_ID_ME], 1);
479 EXPECT_EQ(data.num_linked[INDEX_ID_ME], data.num_total[INDEX_ID_ME]);
480 EXPECT_EQ(data.num_linked[INDEX_ID_NULL], 1);
481 EXPECT_EQ(data.num_linked[INDEX_ID_NULL], data.num_total[INDEX_ID_NULL]);
482
483 for (ID *id_iter : context.unused_ids) {
484 EXPECT_TRUE(((id_iter->tag & ID_TAG_DOIT) != 0) == ID_IS_LINKED(id_iter));
485 }
486 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
487 if (ID_IS_LINKED(id_iter) && context.unused_ids.contains(id_iter)) {
488 continue;
489 }
490 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) == 0);
491 }
493
494 /* "Detect local & linked directly unused IDs" case. */
495 data = {};
496 data.do_local_ids = true;
497 data.do_linked_ids = true;
498
500
501 EXPECT_EQ(data.num_local[INDEX_ID_WO], 1);
502 EXPECT_EQ(data.num_local[INDEX_ID_WO], data.num_total[INDEX_ID_WO]);
503 EXPECT_EQ(data.num_local[INDEX_ID_NULL], 1);
504 EXPECT_EQ(data.num_linked[INDEX_ID_ME], 1);
505 EXPECT_EQ(data.num_linked[INDEX_ID_ME], data.num_total[INDEX_ID_ME]);
506 EXPECT_EQ(data.num_linked[INDEX_ID_NULL], 1);
507 EXPECT_EQ(data.num_local[INDEX_ID_NULL] + data.num_linked[INDEX_ID_NULL],
508 data.num_total[INDEX_ID_NULL]);
509
510 for (ID *id_iter : context.unused_ids) {
511 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) != 0);
512 }
513 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
514 if (context.unused_ids.contains(id_iter)) {
515 continue;
516 }
517 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) == 0);
518 }
520}
521
522TEST_F(LibQueryTest, libquery_unused_ids_recursive)
523{
524 UnusedIDsTestData context;
525
526 ASSERT_NE(context.library, nullptr);
527 ASSERT_NE(context.scene, nullptr);
528 ASSERT_NE(context.world, nullptr);
529 ASSERT_NE(context.object, nullptr);
530 ASSERT_NE(context.target, nullptr);
531 ASSERT_NE(context.mesh, nullptr);
532
533 ID *id_iter;
534 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
535 EXPECT_TRUE(context.used_ids.contains(id_iter) || context.unused_ids.contains(id_iter) ||
536 context.unused_recursive_ids.contains(id_iter));
537 }
539
540 /* "Detect nothing" case. */
542 data.do_recursive = true;
543
545 for (const int64_t index : IndexRange(INDEX_ID_MAX)) {
546 EXPECT_EQ(data.num_local[index], 0);
547 EXPECT_EQ(data.num_linked[index], 0);
548 EXPECT_EQ(data.num_total[index], 0);
549 }
550 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
551 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) == 0);
552 }
554
555 /* "Detect all local unused IDs" case. */
556 data = {};
557 data.do_recursive = true;
558 data.do_local_ids = true;
559
561
562 for (const int64_t index : IndexRange(INDEX_ID_MAX)) {
563 EXPECT_EQ(data.num_linked[index], 0);
564 }
565 /* Data from the directly unused IDs, and the disconnected graphs. Note that the mixed graph is
566 * not detected here, as some of its IDs are linked, therefore not considered, which generates
567 * valid usages for the local IDs of that group. */
568 EXPECT_EQ(data.num_local[INDEX_ID_WO], 2);
569 EXPECT_EQ(data.num_local[INDEX_ID_WO], data.num_total[INDEX_ID_WO]);
570 EXPECT_EQ(data.num_local[INDEX_ID_ME], 2);
571 EXPECT_EQ(data.num_local[INDEX_ID_ME], data.num_total[INDEX_ID_ME]);
572 EXPECT_EQ(data.num_local[INDEX_ID_OB], 1);
573 EXPECT_EQ(data.num_local[INDEX_ID_OB], data.num_total[INDEX_ID_OB]);
574 EXPECT_EQ(data.num_local[INDEX_ID_NULL], 5);
575 EXPECT_EQ(data.num_local[INDEX_ID_NULL], data.num_total[INDEX_ID_NULL]);
576 for (ID *id_iter : context.unused_ids) {
577 /* All directly unused local IDs must be tagged, linked ones must remain untagged. */
578 EXPECT_TRUE(((id_iter->tag & ID_TAG_DOIT) != 0) != ID_IS_LINKED(id_iter));
579 }
580 for (ID *id_iter : context.unused_recursive_ids) {
581 /* All unused linked IDs must not be tagged. Some local ones also remain untagged. */
582 EXPECT_TRUE(((id_iter->tag & ID_TAG_DOIT) == 0) || !ID_IS_LINKED(id_iter));
583 }
584 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
585 if (!ID_IS_LINKED(id_iter) && context.unused_ids.contains(id_iter)) {
586 continue;
587 }
588 if (!ID_IS_LINKED(id_iter) && context.unused_recursive_ids.contains(id_iter)) {
589 continue;
590 }
591 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) == 0);
592 }
594
595 /* "Detect all linked unused IDs" case. */
596 data = {};
597 data.do_recursive = true;
598 data.do_linked_ids = true;
599
601
602 for (const int64_t index : IndexRange(INDEX_ID_MAX)) {
603 EXPECT_EQ(data.num_local[index], 0);
604 }
605 /* Data from the directly unused IDs, and the disconnected graphs. Note that the mixed graph is
606 * not detected here, as some of its IDs are local, therefore not considered, which generates
607 * valid usages for the linked IDs of that group. */
608 EXPECT_EQ(data.num_linked[INDEX_ID_WO], 1);
609 EXPECT_EQ(data.num_linked[INDEX_ID_WO], data.num_total[INDEX_ID_WO]);
610 EXPECT_EQ(data.num_linked[INDEX_ID_ME], 3);
611 EXPECT_EQ(data.num_linked[INDEX_ID_ME], data.num_total[INDEX_ID_ME]);
612 EXPECT_EQ(data.num_linked[INDEX_ID_OB], 1);
613 EXPECT_EQ(data.num_linked[INDEX_ID_OB], data.num_total[INDEX_ID_OB]);
614 EXPECT_EQ(data.num_linked[INDEX_ID_NULL], 5);
615 EXPECT_EQ(data.num_linked[INDEX_ID_NULL], data.num_total[INDEX_ID_NULL]);
616 for (ID *id_iter : context.unused_ids) {
617 /* All directly unused linked IDs must be tagged, local ones must remain untagged. */
618 EXPECT_TRUE(((id_iter->tag & ID_TAG_DOIT) != 0) == ID_IS_LINKED(id_iter));
619 }
620 for (ID *id_iter : context.unused_recursive_ids) {
621 /* All unused local IDs must not be tagged. Some linked ones also remain untagged. */
622 EXPECT_TRUE(((id_iter->tag & ID_TAG_DOIT) == 0) || ID_IS_LINKED(id_iter));
623 }
624 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
625 if (ID_IS_LINKED(id_iter) && context.unused_ids.contains(id_iter)) {
626 continue;
627 }
628 if (ID_IS_LINKED(id_iter) && context.unused_recursive_ids.contains(id_iter)) {
629 continue;
630 }
631 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) == 0);
632 }
634
635 /* "Detect all local & linked unused IDs" case. */
636 data = {};
637 data.do_recursive = true;
638 data.do_local_ids = true;
639 data.do_linked_ids = true;
640
642
643 /* Data from the directly unused IDs, and the disconnected graphs. Here IDs from all three
644 * disconnected graphs must be tagged. */
645 EXPECT_EQ(data.num_local[INDEX_ID_WO], 2);
646 EXPECT_EQ(data.num_local[INDEX_ID_ME], 3);
647 EXPECT_EQ(data.num_local[INDEX_ID_OB], 2);
648 EXPECT_EQ(data.num_local[INDEX_ID_NULL], 7);
649 EXPECT_EQ(data.num_linked[INDEX_ID_WO], 2);
650 EXPECT_EQ(data.num_linked[INDEX_ID_ME], 4);
651 EXPECT_EQ(data.num_linked[INDEX_ID_OB], 1);
652 EXPECT_EQ(data.num_linked[INDEX_ID_NULL], 7);
653
654 EXPECT_EQ(data.num_local[INDEX_ID_WO] + data.num_linked[INDEX_ID_WO],
655 data.num_total[INDEX_ID_WO]);
656 EXPECT_EQ(data.num_local[INDEX_ID_ME] + data.num_linked[INDEX_ID_ME],
657 data.num_total[INDEX_ID_ME]);
658 EXPECT_EQ(data.num_local[INDEX_ID_OB] + data.num_linked[INDEX_ID_OB],
659 data.num_total[INDEX_ID_OB]);
660 EXPECT_EQ(data.num_local[INDEX_ID_NULL] + data.num_linked[INDEX_ID_NULL],
661 data.num_total[INDEX_ID_NULL]);
662
663 for (ID *id_iter : context.unused_ids) {
664 /* All directly unused IDs must be tagged. */
665 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) != 0);
666 }
667 for (ID *id_iter : context.unused_recursive_ids) {
668 /* All indirectly unused IDs must be tagged. */
669 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) != 0);
670 }
671 FOREACH_MAIN_ID_BEGIN (context.bmain, id_iter) {
672 if (context.unused_ids.contains(id_iter)) {
673 continue;
674 }
675 if (context.unused_recursive_ids.contains(id_iter)) {
676 continue;
677 }
678 EXPECT_TRUE((id_iter->tag & ID_TAG_DOIT) == 0);
679 }
681}
682
684
685} // namespace blender::bke::tests
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define INDEX_ID_MAX
Definition DNA_ID.h:1362
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
constexpr IndexRange()=default
bool add(const Key &key)
Definition BLI_set.hh:248
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
void node_node_foreach_id(bNode *node, LibraryForeachIDData *data)
Definition node.cc:404
void node_tree_shader_default(const bContext *C, Main *bmain, ID *id)
__int64 int64_t
Definition BKE_text.h:90
void BKE_lib_query_unused_ids_tag(Main *bmain, int tag, LibQueryUnusedIDsData &parameters)
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
Main * BKE_main_new()
Definition main.cc:93
void BKE_object_material_assign(Main *bmain, Object *ob, Material *ma, short act, int assign_type)
void id_fake_user_set(ID *id)
Definition lib_id.cc:471
bContext * CTX_create()
void BKE_main_id_refcount_recompute(Main *bmain, bool do_linked_only)
Definition lib_id.cc:2131
Global G
Definition blender.cc:57
void BKE_library_foreach_subdata_id(Main *bmain, ID *owner_id, ID *self_id, FunctionRef< void(LibraryForeachIDData *data)> subdata_foreach_id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, const LibraryForeachIDFlag flag)
Definition lib_query.cc:469
BMesh const char void * data
@ BKE_MAT_ASSIGN_OBJECT
Scene * BKE_scene_add(Main *bmain, const char *name)
Definition scene.cc:2200
void CTX_free(bContext *C)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
void BKE_main_relations_create(Main *bmain, short flag)
Definition main.cc:723
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 id_us_ensure_real(ID *id)
Definition lib_id.cc:388
@ INDEX_ID_ME
Definition DNA_ID.h:1327
@ INDEX_ID_WO
Definition DNA_ID.h:1316
@ INDEX_ID_OB
Definition DNA_ID.h:1341
@ INDEX_ID_NULL
Definition DNA_ID.h:1359
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
IDProperty * IDP_EnsureProperties(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1005
void * BKE_id_new_in_lib(Main *bmain, std::optional< Library * > owner_library, short type, const char *name)
Definition lib_id.cc:1594
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)
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:449
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void CTX_data_scene_set(bContext *C, Scene *scene)
struct World * world
struct Collection * master_collection