Blender V5.3
asset_representation_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
7
9
10#include "BKE_lib_id.hh"
11#include "BKE_main.hh"
12
13#if defined(WIN32)
14# include "BLI_string.hh"
15#endif
16
17#include "DNA_asset_types.h"
18#include "DNA_object_types.h"
19
21
22#include "../intern/utils.hh"
23
24#include "testing/testing.h"
25
27
33 public:
35 {
37
39 ref.type = type;
40 return service->get_asset_library(nullptr, ref);
41 }
42
44 {
45 std::unique_ptr<AssetMetaData> dummy_metadata = std::make_unique<AssetMetaData>();
46 return *library
47 .add_external_on_disk_asset(
48 relative_path, "Some asset name", 0, std::move(dummy_metadata))
49 .lock();
50 }
51
53 {
54 /* Ensure ID is marked as asset (no-op if already marked). */
56
57 return *library.add_local_id_asset(id).lock();
58 }
59};
60
61TEST_F(AssetRepresentationTest, library_relative_identifier__id_name_change)
62{
63 Main *bmain = BKE_main_new();
64 Object *object = BKE_id_new<Object>(bmain, "Before rename");
65
66 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
67
68 AssetRepresentation &asset = add_dummy_id_asset(*library, object->id);
69
70 EXPECT_EQ(asset.library_relative_identifier(), "Object" SEP_STR "Before rename");
71
72 BKE_id_rename(*bmain, object->id, "Renamed!");
73 EXPECT_EQ(asset.library_relative_identifier(), "Object" SEP_STR "Renamed!");
74
75 BKE_id_rename(*bmain, object->id, "Name/With\\Slashes/");
76 EXPECT_EQ(asset.library_relative_identifier(), "Object" SEP_STR "Name/With\\Slashes/");
77
78 BKE_main_free(bmain);
79}
80
81TEST_F(AssetRepresentationTest, weak_reference__current_file)
82{
83 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
84 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
85
86 {
87 AssetWeakReference weak_ref = asset.make_weak_reference();
89 EXPECT_EQ(weak_ref.asset_library_identifier, nullptr);
90 EXPECT_STREQ(weak_ref.relative_asset_identifier, "path/to/an/asset");
91 }
92}
93
94TEST_F(AssetRepresentationTest, weak_reference__custom_library)
95{
97 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
98 asset_library_root_);
99 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
100
101 {
102 AssetWeakReference weak_ref = asset.make_weak_reference();
104 EXPECT_STREQ(weak_ref.asset_library_identifier, "My custom lib");
105 EXPECT_STREQ(weak_ref.relative_asset_identifier, "path/to/an/asset");
106 }
107}
108
109/* Test if new weak references the ID name changes. */
110TEST_F(AssetRepresentationTest, weak_reference__id_name_change)
111{
112 Main *bmain = BKE_main_new();
113 Object *object = BKE_id_new<Object>(bmain, "Before rename");
114
115 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
116
117 AssetRepresentation &asset = add_dummy_id_asset(*library, object->id);
118
119 {
120 AssetWeakReference weak_ref = asset.make_weak_reference();
122 EXPECT_STREQ(weak_ref.asset_library_identifier, nullptr);
123 EXPECT_STREQ(weak_ref.relative_asset_identifier, "Object" SEP_STR "Before rename");
124 }
125
126 BKE_id_rename(*bmain, object->id, "Renamed!");
127 {
128 AssetWeakReference weak_ref = asset.make_weak_reference();
130 EXPECT_STREQ(weak_ref.asset_library_identifier, nullptr);
131 EXPECT_STREQ(weak_ref.relative_asset_identifier, "Object" SEP_STR "Renamed!");
132 }
133
134 BKE_id_rename(*bmain, object->id, "Name/With\\Slashes/");
135 {
136 AssetWeakReference weak_ref = asset.make_weak_reference();
138 EXPECT_STREQ(weak_ref.asset_library_identifier, nullptr);
139 EXPECT_STREQ(weak_ref.relative_asset_identifier, "Object" SEP_STR "Name/With\\Slashes/");
140 }
141
142 BKE_main_free(bmain);
143}
144
145TEST_F(AssetRepresentationTest, weak_reference__compare)
146{
147 {
150 EXPECT_EQ(a, b);
151
152 /* Arbitrary individual member changes to test how it affects the comparison. */
153 b.asset_library_identifier = "My lib";
154 /* Asset library identifier should be ignored unless the type is #ASSET_LIBRARY_CUSTOM. */
155 EXPECT_EQ(a, b);
156 a.asset_library_identifier = "My lib";
157 EXPECT_EQ(a, b);
159 EXPECT_NE(a, b);
160 b.asset_library_type = ASSET_LIBRARY_LOCAL;
161 EXPECT_NE(a, b);
162 b.asset_library_type = ASSET_LIBRARY_ESSENTIALS;
163 EXPECT_EQ(a, b);
165 EXPECT_NE(a, b);
166 b.relative_asset_identifier = "Bar";
167 EXPECT_NE(a, b);
169 EXPECT_EQ(a, b);
170
171 /* Make the destructor work. */
172 a.asset_library_identifier = b.asset_library_identifier = nullptr;
173 a.relative_asset_identifier = b.relative_asset_identifier = nullptr;
174 }
175
176 {
179 a.asset_library_identifier = "My custom lib";
180 a.relative_asset_identifier = "path/to/an/asset";
181
183 EXPECT_NE(a, b);
184
185 b.asset_library_type = ASSET_LIBRARY_LOCAL;
186 b.asset_library_identifier = "My custom lib";
187 b.relative_asset_identifier = "path/to/an/asset";
188 EXPECT_EQ(a, b);
189
190 /* Make the destructor work. */
191 a.asset_library_identifier = b.asset_library_identifier = nullptr;
192 a.relative_asset_identifier = b.relative_asset_identifier = nullptr;
193 }
194
195 {
197 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
198 asset_library_root_);
199 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
200
201 AssetWeakReference weak_ref = asset.make_weak_reference();
202 AssetWeakReference other;
204 other.asset_library_identifier = "My custom lib";
205 other.relative_asset_identifier = "path/to/an/asset";
206 EXPECT_EQ(weak_ref, other);
207
208 other.relative_asset_identifier = "";
209 EXPECT_NE(weak_ref, other);
210 other.relative_asset_identifier = nullptr;
211 EXPECT_NE(weak_ref, other);
212
213 /* Make the destructor work. */
214 other.asset_library_identifier = nullptr;
215 other.relative_asset_identifier = nullptr;
216 }
217
218 /* Same but comparing windows and unix style paths. */
219 {
221 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
222 asset_library_root_);
223 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
224
225 AssetWeakReference weak_ref = asset.make_weak_reference();
226 AssetWeakReference other;
228 other.asset_library_identifier = "My custom lib";
229 other.relative_asset_identifier = "path\\to\\an\\asset";
230 EXPECT_EQ(weak_ref, other);
231
232 other.relative_asset_identifier = "";
233 EXPECT_NE(weak_ref, other);
234 other.relative_asset_identifier = nullptr;
235 EXPECT_NE(weak_ref, other);
236
237 /* Make the destructor work. */
238 other.asset_library_identifier = nullptr;
239 other.relative_asset_identifier = nullptr;
240 }
241}
242
243TEST_F(AssetRepresentationTest, weak_reference__resolve_to_full_path__current_file)
244{
246 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
247 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
248
249 AssetWeakReference weak_ref = asset.make_weak_reference();
250
251 std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(weak_ref);
252 EXPECT_EQ(resolved_path, "");
253}
254
255/* #AssetLibraryService::resolve_asset_weak_reference_to_full_path(). */
256TEST_F(AssetRepresentationTest, weak_reference__resolve_to_full_path__custom_library)
257{
259 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
260 asset_library_root_);
261 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
262
263 AssetWeakReference weak_ref = asset.make_weak_reference();
264
265 std::string expected_path = utils::resolve_path(asset_library_root_ + "/" + "path/") +
266 "to/an/asset";
267 std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(weak_ref);
268
269 EXPECT_EQ(BLI_path_cmp(resolved_path.c_str(), expected_path.c_str()), 0);
270}
271
273 weak_reference__resolve_to_full_path__custom_library__windows_pathsep)
274{
276 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
277 asset_library_root_);
278 AssetRepresentation &asset = add_dummy_asset(*library, "path\\to\\an\\asset");
279
280 AssetWeakReference weak_ref = asset.make_weak_reference();
281
282 std::string expected_path = utils::resolve_path(asset_library_root_ + "\\" + "path\\") +
283 "to\\an\\asset";
284 std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(weak_ref);
285
286 EXPECT_EQ(BLI_path_cmp(resolved_path.c_str(), expected_path.c_str()), 0);
287}
288
289/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
290TEST_F(AssetRepresentationTest, weak_reference__resolve_to_exploded_path__current_file)
291{
293 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
294 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
295
296 AssetWeakReference weak_ref = asset.make_weak_reference();
297
298 std::string expected_full_path = utils::resolve_path("path/to/an/asset", 5);
299 std::optional<AssetLibraryService::ExplodedPath> resolved_path =
301
302 EXPECT_EQ(*resolved_path->full_path, expected_full_path);
303 EXPECT_EQ(resolved_path->dir_component, "");
304 EXPECT_EQ(resolved_path->group_component, "path");
305 /* ID names may contain slashes. */
306 EXPECT_EQ(resolved_path->name_component, "to/an/asset");
307}
308
309/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
310TEST_F(AssetRepresentationTest, weak_reference__resolve_to_exploded_path__custom_library)
311{
313 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
314 asset_library_root_);
315 AssetRepresentation &asset = add_dummy_asset(*library, "some.blend/Material/asset/name");
316
317 AssetWeakReference weak_ref = asset.make_weak_reference();
318
319 std::string expected_full_path = utils::resolve_path(asset_library_root_ +
320 "/some.blend/Material/") +
321 "asset/name";
322 std::optional<AssetLibraryService::ExplodedPath> resolved_path =
324
325 EXPECT_EQ(BLI_path_cmp(resolved_path->full_path->c_str(), expected_full_path.c_str()), 0);
326 EXPECT_EQ(BLI_path_cmp_normalized(std::string(resolved_path->dir_component).c_str(),
327 std::string(asset_library_root_ + "/some.blend").c_str()),
328 0);
329 EXPECT_EQ(resolved_path->group_component, "Material");
330 /* ID names may contain slashes. */
331 EXPECT_EQ(resolved_path->name_component, "asset/name");
332}
333
334/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
336 weak_reference__resolve_to_exploded_path__custom_library__windows_pathsep)
337{
339 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
340 asset_library_root_);
341 AssetRepresentation &asset = add_dummy_asset(*library, "some.blend\\Material\\asset/name");
342
343 AssetWeakReference weak_ref = asset.make_weak_reference();
344
345 std::string expected_full_path = utils::resolve_path(asset_library_root_ +
346 "\\some.blend\\Material\\") +
347 "asset/name";
348 std::optional<AssetLibraryService::ExplodedPath> resolved_path =
350
351 EXPECT_EQ(BLI_path_cmp(resolved_path->full_path->c_str(), expected_full_path.c_str()), 0);
352 EXPECT_EQ(BLI_path_cmp_normalized(std::string(resolved_path->dir_component).c_str(),
353 std::string(asset_library_root_ + "\\some.blend").c_str()),
354 0);
355 EXPECT_EQ(resolved_path->group_component, "Material");
356 /* ID names may contain slashes. */
357 EXPECT_EQ(resolved_path->name_component, "asset/name");
358}
359
360} // namespace blender::asset_system::tests
Main runtime representation of an asset.
#define BLI_path_cmp
Object is a sort of wrapper for general info.
AssetLibrary * get_asset_library(const Main *bmain, const AssetLibraryReference &library_reference)
std::optional< ExplodedPath > resolve_asset_weak_reference_to_exploded_path(const AssetWeakReference &asset_reference)
std::string resolve_asset_weak_reference_to_full_path(const AssetWeakReference &asset_reference)
AssetLibrary * get_asset_library_on_disk_custom(StringRef name, StringRefNull root_path)
AssetRepresentation & add_dummy_id_asset(AssetLibrary &library, ID &id)
AssetLibrary * get_builtin_library_from_type(eAssetLibraryType type)
AssetRepresentation & add_dummy_asset(AssetLibrary &library, StringRef relative_path)
#define EXPECT_NE(result, expect)
TEST_F(AssetCatalogPathTest, construction)
std::string resolve_path(StringRefNull path, int64_t max_len)
Main * BKE_main_new()
Definition main.cc:93
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_LOCAL
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
IDNewNameResult BKE_id_rename(Main &bmain, ID &id, StringRefNull name, const IDNewNameMode mode=IDNewNameMode::RenameExistingNever)
Definition lib_id.cc:2654
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
eAssetLibraryType asset_library_type
#define SEP_STR
Definition unit.cc:41