Blender V5.3
brush_test.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#include "BKE_brush.hh"
7#include "BKE_gtest_base.hh"
8#include "BKE_idtype.hh"
9#include "BKE_lib_id.hh"
10#include "BKE_main.hh"
11
12#include "BLI_listbase.hh"
13
14#include "DNA_brush_types.h"
15#include "DNA_material_types.h"
16#include "DNA_node_types.h"
17
18namespace blender {
19
21 public:
22 Main *bmain = nullptr;
23
24 void SetUp() override
25 {
27 }
28
29 void TearDown() override
30 {
32 }
33};
34
35static void check_id_and_name(const ID *a, const ID *b)
36{
37 EXPECT_NE(a, b) << "ID " << a->name << " and " << b->name << "should be different pointers";
38 EXPECT_EQ(a->us, 1) << "ID " << a->name << " should have 1 user";
39 EXPECT_EQ(b->us, 1) << "ID " << b->name << " should have 1 user";
40 EXPECT_STRNE(a->name, b->name);
41}
42
43static void check_embedded_copy(const ID *a, const ID *b)
44{
45 EXPECT_NE(a, b) << "ID " << a->name << " and " << b->name << "should be different pointers";
46 EXPECT_EQ(a->us, 0) << "ID " << a->name << " should have 0 users";
47 EXPECT_EQ(b->us, 0) << "ID " << b->name << " should have 0 users";
50}
51
52TEST_F(BrushTest, deep_copy)
53{
54 Brush *brush = BKE_brush_add(bmain, "UnitTestBrush", OB_MODE_SCULPT);
55 /* TODO: Ideally this shouldn't be needed, but BKE_brush_add generates an extra user. Remove this
56 * once that has been fixed. */
57 id_us_min(&brush->id);
58
59 /* Normal Linked Data */
60 brush->paint_curve = static_cast<PaintCurve *>(BKE_id_new(bmain, ID_PC, "UnitTestPaintCurve"));
61 brush->mtex.tex = static_cast<Tex *>(BKE_id_new(bmain, ID_TE, "UnitTestTexture"));
62 brush->mtex.tex->ima = static_cast<Image *>(BKE_id_new(bmain, ID_IM, "UnitTestImage"));
63
64 /* Embedded Data */
65 brush->mtex.tex->nodetree = BKE_id_new_nomain<bNodeTree>("UnitTestNodeTree");
66 brush->mtex.tex->nodetree->id.flag |= ID_FLAG_EMBEDDED_DATA;
67
68 Brush *duplicated_brush = BKE_brush_duplicate(
70
71 check_id_and_name(&brush->id, &duplicated_brush->id);
72 check_id_and_name(&brush->paint_curve->id, &duplicated_brush->paint_curve->id);
73 check_id_and_name(&brush->mtex.tex->id, &duplicated_brush->mtex.tex->id);
74 check_id_and_name(&brush->mtex.tex->ima->id, &duplicated_brush->mtex.tex->ima->id);
75
76 check_embedded_copy(&brush->mtex.tex->nodetree->id, &duplicated_brush->mtex.tex->nodetree->id);
77
78 EXPECT_TRUE(bmain->nodetrees.is_empty());
79}
80
81TEST_F(BrushTest, deep_copy_grease_pencil_brush)
82{
83 /* Grease pencil brushes potentially have more ID linked to them, hence a separate test */
84
85 Brush *brush = BKE_brush_add(bmain, "UnitTestBrush", OB_MODE_PAINT_GREASE_PENCIL);
86 /* TODO: Ideally this shouldn't be needed, but #BKE_brush_add generates an extra user.
87 * Remove this once that has been fixed. */
88 id_us_min(&brush->id);
89
90 /* Normal Linked Data */
91 brush->paint_curve = static_cast<PaintCurve *>(BKE_id_new(bmain, ID_PC, "UnitTestPaintCurve"));
92 brush->gpencil_settings->material = static_cast<Material *>(
93 BKE_id_new(bmain, ID_MA, "UnitTestMaterial"));
94 brush->gpencil_settings->material_alt = static_cast<Material *>(
95 BKE_id_new(bmain, ID_MA, "UnitTestMaterialAlt"));
96
97 Brush *duplicated_brush = BKE_brush_duplicate(
99
100 check_id_and_name(&brush->id, &duplicated_brush->id);
101 check_id_and_name(&brush->paint_curve->id, &duplicated_brush->paint_curve->id);
102 check_id_and_name(&brush->gpencil_settings->material->id,
103 &duplicated_brush->gpencil_settings->material->id);
104 check_id_and_name(&brush->gpencil_settings->material_alt->id,
105 &duplicated_brush->gpencil_settings->material_alt->id);
106
107 check_embedded_copy(&brush->gpencil_settings->material->nodetree->id,
108 &duplicated_brush->gpencil_settings->material->nodetree->id);
109 check_embedded_copy(&brush->gpencil_settings->material_alt->nodetree->id,
110 &duplicated_brush->gpencil_settings->material_alt->nodetree->id);
111
112 EXPECT_TRUE(bmain->nodetrees.is_empty());
113}
114
115} // namespace blender
void TearDown() override
Definition brush_test.cc:29
void SetUp() override
Definition brush_test.cc:24
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
BMesh Iterators.
Main * BKE_main_new()
Definition main.cc:93
Brush * BKE_brush_duplicate(Main *bmain, Brush *brush, eDupli_ID_Flags dupflag, uint duplicate_options)
Definition brush.cc:761
@ LIB_ID_DUPLICATE_IS_ROOT_ID
@ OB_MODE_PAINT_GREASE_PENCIL
TEST_F(BrushTest, deep_copy)
Definition brush_test.cc:52
static void check_embedded_copy(const ID *a, const ID *b)
Definition brush_test.cc:43
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
static void check_id_and_name(const ID *a, const ID *b)
Definition brush_test.cc:35
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:775
void id_us_min(ID *id)
Definition lib_id.cc:441
Brush * BKE_brush_add(Main *bmain, const char *name, eObjectMode ob_mode)
Definition brush.cc:695
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
struct BrushGpencilSettings * gpencil_settings
struct PaintCurve * paint_curve
char name[258]
Definition DNA_ID.h:433
short flag
Definition DNA_ID.h:439
struct Image * ima
struct bNodeTree * nodetree