Blender V5.3
node_iterator_tests.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#include "testing/testing.h"
5
6/* Allow using `Scene->nodetree` because it's still relevant for backward compatibility. */
7#define DNA_DEPRECATED_ALLOW
9#include "DNA_scene_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_main.hh"
16#include "BKE_material.hh"
17#include "BKE_node.hh"
18#include "BKE_scene.hh"
19
20#include "ED_node_c.hh"
21
22#include "RNA_define.hh"
23
24#include "NOD_defaults.hh"
25
26namespace blender::nodes::tests {
27
28namespace {
29class NodeTest : public bke::BlenderGTestBase {
30
31 protected:
32 struct IteratorResult {
33 Vector<bNodeTree *> node_trees;
34 Vector<ID *> ids;
35 };
36
37 IteratorResult get_node_trees(Main *bmain)
38 {
39 IteratorResult iter_result;
40
41 FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
42 iter_result.node_trees.append(ntree);
43 iter_result.ids.append(id);
44 }
46
47 return iter_result;
48 };
49};
50
51class TestData {
52 public:
53 Main *bmain = nullptr;
54 bContext *C = nullptr;
55
56 TestData()
57 {
58 if (bmain == nullptr) {
59 bmain = BKE_main_new();
60 G.main = bmain;
61 }
62
63 if (C == nullptr) {
64 C = CTX_create();
65 CTX_data_main_set(C, bmain);
66 }
67 }
68
69 ~TestData()
70 {
71 if (bmain != nullptr) {
72 BKE_main_free(bmain);
73 bmain = nullptr;
74 G.main = nullptr;
75 }
76
77 if (C != nullptr) {
78 CTX_free(C);
79 C = nullptr;
80 }
81 }
82};
83} // namespace
84
85TEST_F(NodeTest, tree_iterator_empty)
86{
87 TestData context;
88
89 IteratorResult iter_result = this->get_node_trees(context.bmain);
90
91 EXPECT_EQ(iter_result.node_trees.size(), 0);
92 EXPECT_EQ(iter_result.ids.size(), 0);
93}
94
95TEST_F(NodeTest, tree_iterator_1_mat)
96{
97 TestData context;
98
99 Material *material = BKE_material_add(context.bmain, "Material");
100 nodes::node_tree_shader_default(context.C, context.bmain, &material->id);
101
102 IteratorResult iter_result = this->get_node_trees(context.bmain);
103
104 ASSERT_EQ(iter_result.node_trees.size(), 1);
105 ASSERT_EQ(iter_result.ids.size(), 1);
106
107 EXPECT_EQ(GS(iter_result.ids[0]->name), ID_MA);
108}
109
110TEST_F(NodeTest, tree_iterator_scene_no_tree)
111{
112 TestData context;
113
114 Material *material = BKE_material_add(context.bmain, "Material");
115 nodes::node_tree_shader_default(context.C, context.bmain, &material->id);
116
117 BKE_scene_add(context.bmain, "Scene");
118
119 IteratorResult iter_result = this->get_node_trees(context.bmain);
120
121 ASSERT_EQ(iter_result.node_trees.size(), 1);
122 ASSERT_EQ(iter_result.ids.size(), 1);
123
124 EXPECT_EQ(GS(iter_result.ids[0]->name), ID_MA);
125}
126
127TEST_F(NodeTest, tree_iterator_1mat_1scene)
128{
129 TestData context;
130 const char SCENE_NAME[MAX_ID_NAME] = "Scene for testing";
131
132 Material *material = BKE_material_add(context.bmain, "Material");
133 nodes::node_tree_shader_default(context.C, context.bmain, &material->id);
134
135 Scene *scene = BKE_scene_add(context.bmain, SCENE_NAME);
136 /* Embedded compositing trees are deprecated, but still relevant for versioning/backward
137 * compatibility. */
139 context.bmain, &scene->id, "compositing nodetree", "CompositorNodeTree");
140
141 IteratorResult iter_result = this->get_node_trees(context.bmain);
142
143 ASSERT_EQ(iter_result.node_trees.size(), 2);
144 ASSERT_EQ(iter_result.ids.size(), 2);
145
146 EXPECT_EQ(GS(iter_result.ids[1]->name), ID_MA);
147 EXPECT_EQ(GS(iter_result.ids[0]->name), ID_SCE);
148 EXPECT_STREQ(iter_result.ids[0]->name + 2, SCENE_NAME);
149
150 /* `scene->nodetree` is not managed by the scene anymore, i.e. `scene_free_data()` doesn't free
151 * its embedded node-trees, so we need to free it manually here. */
153 MEM_delete(scene->nodetree);
154 scene->nodetree = nullptr;
155}
156
157TEST_F(NodeTest, tree_iterator_1mat_3scenes)
158{
159 TestData context;
160 const char SCENE_NAME_1[MAX_ID_NAME] = "Scene 1";
161 const char SCENE_NAME_2[MAX_ID_NAME] = "Scene 2";
162 const char SCENE_NAME_3[MAX_ID_NAME] = "Scene 3";
163 const char NTREE_NAME[MAX_NAME] = "Test Composisiting Nodetree";
164 /* Name is hard-coded in #nodes::node_tree_shader_default(). */
165 const char MATERIAL_NTREE_NAME[MAX_NAME] = "Shader Nodetree";
166
167 Material *material = BKE_material_add(context.bmain, "Material");
168 nodes::node_tree_shader_default(context.C, context.bmain, &material->id);
169
170 BKE_scene_add(context.bmain, SCENE_NAME_1);
171 /* Note: no node tree for scene 1. */
172
173 Scene *scene2 = BKE_scene_add(context.bmain, SCENE_NAME_2);
175 context.bmain, &scene2->id, NTREE_NAME, "CompositorNodeTree");
176
177 BKE_scene_add(context.bmain, SCENE_NAME_3);
178 /* Also no node tree for scene 3. */
179
180 IteratorResult iter_result = this->get_node_trees(context.bmain);
181
182 ASSERT_EQ(iter_result.node_trees.size(), 2);
183 ASSERT_EQ(iter_result.ids.size(), 2);
184
185 /* Expect that scenes with no node-trees don't have side effects for node trees. */
186 EXPECT_EQ(GS(iter_result.ids[0]->name), ID_SCE);
187 EXPECT_STREQ(iter_result.ids[0]->name + 2, SCENE_NAME_2);
188 EXPECT_STREQ(iter_result.node_trees[0]->id.name + 2, NTREE_NAME);
189
190 EXPECT_EQ(GS(iter_result.ids[1]->name), ID_MA);
191 EXPECT_STREQ(iter_result.node_trees[1]->id.name + 2, MATERIAL_NTREE_NAME);
192
193 /* `scene->nodetree` is not managed by the scene anymore, i.e. `scene_free_data()` doesn't free
194 * its embedded node-trees, so we need to free it manually here. */
196 MEM_delete(scene2->nodetree);
197 scene2->nodetree = nullptr;
198}
199
200TEST_F(NodeTest, tree_iterator_1mat_1scene_2compositing_trees)
201{
202 TestData context;
203 const char SCENE_NAME_1[MAX_ID_NAME - 2] = "Scene 1";
204 const char NTREE_NAME_1[MAX_ID_NAME - 2] = "Test Composisiting Node Tree 1";
205 const char NTREE_NAME_2[MAX_ID_NAME - 2] = "Test Composisiting Node Tree 2";
206 const char MATERIAL_NTREE_NAME[MAX_NAME] = "Shader Nodetree";
207
208 Material *material = BKE_material_add(context.bmain, "Material");
209 nodes::node_tree_shader_default(context.C, context.bmain, &material->id);
210
211 BKE_scene_add(context.bmain, SCENE_NAME_1);
212
213 bke::node_tree_add_tree(context.bmain, NTREE_NAME_1, "CompositorNodeTree");
214 bke::node_tree_add_tree(context.bmain, NTREE_NAME_2, "CompositorNodeTree");
215
216 IteratorResult iter_result = this->get_node_trees(context.bmain);
217
218 ASSERT_EQ(iter_result.node_trees.size(), 3);
219 ASSERT_EQ(iter_result.ids.size(), 3);
220
221 /* Iterator should return 2 compositing node trees and no scene node tree. */
222 EXPECT_EQ(GS(iter_result.ids[0]->name), ID_NT);
223 EXPECT_STREQ(iter_result.ids[0]->name + 2, NTREE_NAME_1);
224 EXPECT_FALSE((iter_result.ids[0]->flag & ID_FLAG_EMBEDDED_DATA));
225
226 EXPECT_EQ(GS(iter_result.ids[1]->name), ID_NT);
227 EXPECT_STREQ(iter_result.ids[1]->name + 2, NTREE_NAME_2);
228 EXPECT_FALSE((iter_result.ids[1]->flag & ID_FLAG_EMBEDDED_DATA));
229
230 EXPECT_EQ(GS(iter_result.ids[2]->name), ID_MA);
231 EXPECT_STREQ(iter_result.node_trees[2]->id.name + 2, MATERIAL_NTREE_NAME);
232}
233
234} // namespace blender::nodes::tests
General operations, lookup, etc. for materials.
#define FOREACH_NODETREE_END
Definition BKE_node.hh:936
#define FOREACH_NODETREE_BEGIN(bmain, _nodetree, _id)
Definition BKE_node.hh:926
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define MAX_NAME
Definition DNA_defs.h:63
#define C
Definition RandGen.cpp:29
#define EXPECT_FALSE(result)
#define G(x, y, z)
bNodeTree * node_tree_add_tree_embedded(Main *bmain, ID *owner_id, StringRefNull name, StringRefNull idname)
Definition node.cc:5005
bNodeTree * node_tree_add_tree(Main *bmain, StringRef name, StringRef idname)
Definition node.cc:4992
void node_tree_free_embedded_tree(bNodeTree *ntree)
Definition node.cc:5231
TEST_F(BundleTest, DefaultBundle)
void node_tree_shader_default(const bContext *C, Main *bmain, ID *id)
Main * BKE_main_new()
Definition main.cc:93
bContext * CTX_create()
Scene * BKE_scene_add(Main *bmain, const char *name)
Definition scene.cc:2200
void CTX_free(bContext *C)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:775
void CTX_data_main_set(bContext *C, Main *bmain)
Material * BKE_material_add(Main *bmain, const char *name)
DNA_DEPRECATED struct bNodeTree * nodetree