Blender V4.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
5#include "BLI_string.h"
6
9
11
12#include "BKE_asset.hh"
13
14#include "DNA_asset_types.h"
15
16#include "../intern/utils.hh"
17
18#include "testing/testing.h"
19
21
27 public:
29 {
31
33 ref.type = type;
34 return service->get_asset_library(nullptr, ref);
35 }
36
38 {
39 std::unique_ptr<AssetMetaData> dummy_metadata = std::make_unique<AssetMetaData>();
40 return *library
41 .add_external_asset(relative_path, "Some asset name", 0, std::move(dummy_metadata))
42 .lock();
43 }
44};
45
46TEST_F(AssetRepresentationTest, weak_reference__current_file)
47{
48 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
49 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
50
51 {
52 AssetWeakReference weak_ref = asset.make_weak_reference();
54 EXPECT_EQ(weak_ref.asset_library_identifier, nullptr);
55 EXPECT_STREQ(weak_ref.relative_asset_identifier, "path/to/an/asset");
56 }
57}
58
59TEST_F(AssetRepresentationTest, weak_reference__custom_library)
60{
62 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
63 asset_library_root_);
64 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
65
66 {
67 AssetWeakReference weak_ref = asset.make_weak_reference();
69 EXPECT_STREQ(weak_ref.asset_library_identifier, "My custom lib");
70 EXPECT_STREQ(weak_ref.relative_asset_identifier, "path/to/an/asset");
71 }
72}
73
74TEST_F(AssetRepresentationTest, weak_reference__compare)
75{
76 {
79 EXPECT_EQ(a, b);
80
81 /* Arbitrary individual member changes to test how it affects the comparison. */
82 b.asset_library_identifier = "My lib";
83 EXPECT_NE(a, b);
84 a.asset_library_identifier = "My lib";
85 EXPECT_EQ(a, b);
86 a.asset_library_type = ASSET_LIBRARY_ESSENTIALS;
87 EXPECT_NE(a, b);
88 b.asset_library_type = ASSET_LIBRARY_LOCAL;
89 EXPECT_NE(a, b);
90 b.asset_library_type = ASSET_LIBRARY_ESSENTIALS;
91 EXPECT_EQ(a, b);
92 a.relative_asset_identifier = "Foo";
93 EXPECT_NE(a, b);
94 b.relative_asset_identifier = "Bar";
95 EXPECT_NE(a, b);
96 a.relative_asset_identifier = "Bar";
97 EXPECT_EQ(a, b);
98
99 /* Make the destructor work. */
100 a.asset_library_identifier = b.asset_library_identifier = nullptr;
101 a.relative_asset_identifier = b.relative_asset_identifier = nullptr;
102 }
103
104 {
107 a.asset_library_identifier = "My custom lib";
108 a.relative_asset_identifier = "path/to/an/asset";
109
111 EXPECT_NE(a, b);
112
113 b.asset_library_type = ASSET_LIBRARY_LOCAL;
114 b.asset_library_identifier = "My custom lib";
115 b.relative_asset_identifier = "path/to/an/asset";
116 EXPECT_EQ(a, b);
117
118 /* Make the destructor work. */
119 a.asset_library_identifier = b.asset_library_identifier = nullptr;
120 a.relative_asset_identifier = b.relative_asset_identifier = nullptr;
121 }
122
123 {
125 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
126 asset_library_root_);
127 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
128
129 AssetWeakReference weak_ref = asset.make_weak_reference();
130 AssetWeakReference other;
132 other.asset_library_identifier = "My custom lib";
133 other.relative_asset_identifier = "path/to/an/asset";
134 EXPECT_EQ(weak_ref, other);
135
136 other.relative_asset_identifier = "";
137 EXPECT_NE(weak_ref, other);
138 other.relative_asset_identifier = nullptr;
139 EXPECT_NE(weak_ref, other);
140
141 /* Make the destructor work. */
142 other.asset_library_identifier = nullptr;
143 other.relative_asset_identifier = nullptr;
144 }
145
146 /* Same but comparing windows and unix style paths. */
147 {
149 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
150 asset_library_root_);
151 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
152
153 AssetWeakReference weak_ref = asset.make_weak_reference();
154 AssetWeakReference other;
156 other.asset_library_identifier = "My custom lib";
157 other.relative_asset_identifier = "path\\to\\an\\asset";
158 EXPECT_EQ(weak_ref, other);
159
160 other.relative_asset_identifier = "";
161 EXPECT_NE(weak_ref, other);
162 other.relative_asset_identifier = nullptr;
163 EXPECT_NE(weak_ref, other);
164
165 /* Make the destructor work. */
166 other.asset_library_identifier = nullptr;
167 other.relative_asset_identifier = nullptr;
168 }
169}
170
171TEST_F(AssetRepresentationTest, weak_reference__resolve_to_full_path__current_file)
172{
174 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
175 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
176
177 AssetWeakReference weak_ref = asset.make_weak_reference();
178
179 std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(weak_ref);
180 EXPECT_EQ(resolved_path, "");
181}
182
183/* #AssetLibraryService::resolve_asset_weak_reference_to_full_path(). */
184TEST_F(AssetRepresentationTest, weak_reference__resolve_to_full_path__custom_library)
185{
187 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
188 asset_library_root_);
189 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
190
191 AssetWeakReference weak_ref = asset.make_weak_reference();
192
193 std::string expected_path = utils::normalize_path(asset_library_root_ + "/" + "path/") +
194 "to/an/asset";
195 std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(weak_ref);
196
197 EXPECT_EQ(BLI_path_cmp(resolved_path.c_str(), expected_path.c_str()), 0);
198}
199
201 weak_reference__resolve_to_full_path__custom_library__windows_pathsep)
202{
204 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
205 asset_library_root_);
206 AssetRepresentation &asset = add_dummy_asset(*library, "path\\to\\an\\asset");
207
208 AssetWeakReference weak_ref = asset.make_weak_reference();
209
210 std::string expected_path = utils::normalize_path(asset_library_root_ + "\\" + "path\\") +
211 "to\\an\\asset";
212 std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(weak_ref);
213
214 EXPECT_EQ(BLI_path_cmp(resolved_path.c_str(), expected_path.c_str()), 0);
215}
216
217/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
218TEST_F(AssetRepresentationTest, weak_reference__resolve_to_exploded_path__current_file)
219{
221 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
222 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
223
224 AssetWeakReference weak_ref = asset.make_weak_reference();
225
226 std::string expected_full_path = utils::normalize_path("path/to/an/asset", 5);
227 std::optional<AssetLibraryService::ExplodedPath> resolved_path =
229
230 EXPECT_EQ(*resolved_path->full_path, expected_full_path);
231 EXPECT_EQ(resolved_path->dir_component, "");
232 EXPECT_EQ(resolved_path->group_component, "path");
233 /* ID names may contain slashes. */
234 EXPECT_EQ(resolved_path->name_component, "to/an/asset");
235}
236
237/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
238TEST_F(AssetRepresentationTest, weak_reference__resolve_to_exploded_path__custom_library)
239{
241 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
242 asset_library_root_);
243 AssetRepresentation &asset = add_dummy_asset(*library, "some.blend/Material/asset/name");
244
245 AssetWeakReference weak_ref = asset.make_weak_reference();
246
247 std::string expected_full_path = utils::normalize_path(asset_library_root_ +
248 "/some.blend/Material/") +
249 "asset/name";
250 std::optional<AssetLibraryService::ExplodedPath> resolved_path =
252
253 EXPECT_EQ(BLI_path_cmp(resolved_path->full_path->c_str(), expected_full_path.c_str()), 0);
254 EXPECT_EQ(BLI_path_cmp_normalized(std::string(resolved_path->dir_component).c_str(),
255 std::string(asset_library_root_ + "/some.blend").c_str()),
256 0);
257 EXPECT_EQ(resolved_path->group_component, "Material");
258 /* ID names may contain slashes. */
259 EXPECT_EQ(resolved_path->name_component, "asset/name");
260}
261
262/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
264 weak_reference__resolve_to_exploded_path__custom_library__windows_pathsep)
265{
267 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
268 asset_library_root_);
269 AssetRepresentation &asset = add_dummy_asset(*library, "some.blend\\Material\\asset/name");
270
271 AssetWeakReference weak_ref = asset.make_weak_reference();
272
273 std::string expected_full_path = utils::normalize_path(asset_library_root_ +
274 "\\some.blend\\Material\\") +
275 "asset/name";
276 std::optional<AssetLibraryService::ExplodedPath> resolved_path =
278
279 EXPECT_EQ(BLI_path_cmp(resolved_path->full_path->c_str(), expected_full_path.c_str()), 0);
280 EXPECT_EQ(BLI_path_cmp_normalized(std::string(resolved_path->dir_component).c_str(),
281 std::string(asset_library_root_ + "\\some.blend").c_str()),
282 0);
283 EXPECT_EQ(resolved_path->group_component, "Material");
284 /* ID names may contain slashes. */
285 EXPECT_EQ(resolved_path->name_component, "asset/name");
286}
287
288} // namespace blender::asset_system::tests
Main runtime representation of an asset.
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
#define BLI_path_cmp
eAssetLibraryType
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_LOCAL
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)
std::weak_ptr< AssetRepresentation > add_external_asset(StringRef relative_asset_path, StringRef name, int id_type, std::unique_ptr< AssetMetaData > metadata)
AssetLibrary * get_builtin_library_from_type(eAssetLibraryType type)
AssetRepresentation & add_dummy_asset(AssetLibrary &library, StringRef relative_path)
local_group_size(16, 16) .push_constant(Type b
TEST_F(AssetCatalogTest, load_single_file)
std::string normalize_path(StringRefNull path, int64_t max_len)
const char * relative_asset_identifier
const char * asset_library_identifier