Blender V4.5
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 "DNA_asset_types.h"
11
12#include "BLI_string.h"
13
14#include "../intern/utils.hh"
15
16#include "testing/testing.h"
17
19
25 public:
27 {
29
31 ref.type = type;
32 return service->get_asset_library(nullptr, ref);
33 }
34
36 {
37 std::unique_ptr<AssetMetaData> dummy_metadata = std::make_unique<AssetMetaData>();
38 return *library
39 .add_external_asset(relative_path, "Some asset name", 0, std::move(dummy_metadata))
40 .lock();
41 }
42};
43
44TEST_F(AssetRepresentationTest, weak_reference__current_file)
45{
46 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
47 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
48
49 {
50 AssetWeakReference weak_ref = asset.make_weak_reference();
52 EXPECT_EQ(weak_ref.asset_library_identifier, nullptr);
53 EXPECT_STREQ(weak_ref.relative_asset_identifier, "path/to/an/asset");
54 }
55}
56
57TEST_F(AssetRepresentationTest, weak_reference__custom_library)
58{
60 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
61 asset_library_root_);
62 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
63
64 {
65 AssetWeakReference weak_ref = asset.make_weak_reference();
67 EXPECT_STREQ(weak_ref.asset_library_identifier, "My custom lib");
68 EXPECT_STREQ(weak_ref.relative_asset_identifier, "path/to/an/asset");
69 }
70}
71
72TEST_F(AssetRepresentationTest, weak_reference__compare)
73{
74 {
77 EXPECT_EQ(a, b);
78
79 /* Arbitrary individual member changes to test how it affects the comparison. */
80 b.asset_library_identifier = "My lib";
81 EXPECT_NE(a, b);
82 a.asset_library_identifier = "My lib";
83 EXPECT_EQ(a, b);
85 EXPECT_NE(a, b);
86 b.asset_library_type = ASSET_LIBRARY_LOCAL;
87 EXPECT_NE(a, b);
88 b.asset_library_type = ASSET_LIBRARY_ESSENTIALS;
89 EXPECT_EQ(a, b);
91 EXPECT_NE(a, b);
92 b.relative_asset_identifier = "Bar";
93 EXPECT_NE(a, b);
95 EXPECT_EQ(a, b);
96
97 /* Make the destructor work. */
98 a.asset_library_identifier = b.asset_library_identifier = nullptr;
99 a.relative_asset_identifier = b.relative_asset_identifier = nullptr;
100 }
101
102 {
105 a.asset_library_identifier = "My custom lib";
106 a.relative_asset_identifier = "path/to/an/asset";
107
109 EXPECT_NE(a, b);
110
111 b.asset_library_type = ASSET_LIBRARY_LOCAL;
112 b.asset_library_identifier = "My custom lib";
113 b.relative_asset_identifier = "path/to/an/asset";
114 EXPECT_EQ(a, b);
115
116 /* Make the destructor work. */
117 a.asset_library_identifier = b.asset_library_identifier = nullptr;
118 a.relative_asset_identifier = b.relative_asset_identifier = nullptr;
119 }
120
121 {
123 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
124 asset_library_root_);
125 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
126
127 AssetWeakReference weak_ref = asset.make_weak_reference();
128 AssetWeakReference other;
130 other.asset_library_identifier = "My custom lib";
131 other.relative_asset_identifier = "path/to/an/asset";
132 EXPECT_EQ(weak_ref, other);
133
134 other.relative_asset_identifier = "";
135 EXPECT_NE(weak_ref, other);
136 other.relative_asset_identifier = nullptr;
137 EXPECT_NE(weak_ref, other);
138
139 /* Make the destructor work. */
140 other.asset_library_identifier = nullptr;
141 other.relative_asset_identifier = nullptr;
142 }
143
144 /* Same but comparing windows and unix style paths. */
145 {
147 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
148 asset_library_root_);
149 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
150
151 AssetWeakReference weak_ref = asset.make_weak_reference();
152 AssetWeakReference other;
154 other.asset_library_identifier = "My custom lib";
155 other.relative_asset_identifier = "path\\to\\an\\asset";
156 EXPECT_EQ(weak_ref, other);
157
158 other.relative_asset_identifier = "";
159 EXPECT_NE(weak_ref, other);
160 other.relative_asset_identifier = nullptr;
161 EXPECT_NE(weak_ref, other);
162
163 /* Make the destructor work. */
164 other.asset_library_identifier = nullptr;
165 other.relative_asset_identifier = nullptr;
166 }
167}
168
169TEST_F(AssetRepresentationTest, weak_reference__resolve_to_full_path__current_file)
170{
172 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
173 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
174
175 AssetWeakReference weak_ref = asset.make_weak_reference();
176
177 std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(weak_ref);
178 EXPECT_EQ(resolved_path, "");
179}
180
181/* #AssetLibraryService::resolve_asset_weak_reference_to_full_path(). */
182TEST_F(AssetRepresentationTest, weak_reference__resolve_to_full_path__custom_library)
183{
185 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
186 asset_library_root_);
187 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
188
189 AssetWeakReference weak_ref = asset.make_weak_reference();
190
191 std::string expected_path = utils::normalize_path(asset_library_root_ + "/" + "path/") +
192 "to/an/asset";
193 std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(weak_ref);
194
195 EXPECT_EQ(BLI_path_cmp(resolved_path.c_str(), expected_path.c_str()), 0);
196}
197
199 weak_reference__resolve_to_full_path__custom_library__windows_pathsep)
200{
202 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
203 asset_library_root_);
204 AssetRepresentation &asset = add_dummy_asset(*library, "path\\to\\an\\asset");
205
206 AssetWeakReference weak_ref = asset.make_weak_reference();
207
208 std::string expected_path = utils::normalize_path(asset_library_root_ + "\\" + "path\\") +
209 "to\\an\\asset";
210 std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(weak_ref);
211
212 EXPECT_EQ(BLI_path_cmp(resolved_path.c_str(), expected_path.c_str()), 0);
213}
214
215/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
216TEST_F(AssetRepresentationTest, weak_reference__resolve_to_exploded_path__current_file)
217{
219 AssetLibrary *library = get_builtin_library_from_type(ASSET_LIBRARY_LOCAL);
220 AssetRepresentation &asset = add_dummy_asset(*library, "path/to/an/asset");
221
222 AssetWeakReference weak_ref = asset.make_weak_reference();
223
224 std::string expected_full_path = utils::normalize_path("path/to/an/asset", 5);
225 std::optional<AssetLibraryService::ExplodedPath> resolved_path =
227
228 EXPECT_EQ(*resolved_path->full_path, expected_full_path);
229 EXPECT_EQ(resolved_path->dir_component, "");
230 EXPECT_EQ(resolved_path->group_component, "path");
231 /* ID names may contain slashes. */
232 EXPECT_EQ(resolved_path->name_component, "to/an/asset");
233}
234
235/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
236TEST_F(AssetRepresentationTest, weak_reference__resolve_to_exploded_path__custom_library)
237{
239 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
240 asset_library_root_);
241 AssetRepresentation &asset = add_dummy_asset(*library, "some.blend/Material/asset/name");
242
243 AssetWeakReference weak_ref = asset.make_weak_reference();
244
245 std::string expected_full_path = utils::normalize_path(asset_library_root_ +
246 "/some.blend/Material/") +
247 "asset/name";
248 std::optional<AssetLibraryService::ExplodedPath> resolved_path =
250
251 EXPECT_EQ(BLI_path_cmp(resolved_path->full_path->c_str(), expected_full_path.c_str()), 0);
252 EXPECT_EQ(BLI_path_cmp_normalized(std::string(resolved_path->dir_component).c_str(),
253 std::string(asset_library_root_ + "/some.blend").c_str()),
254 0);
255 EXPECT_EQ(resolved_path->group_component, "Material");
256 /* ID names may contain slashes. */
257 EXPECT_EQ(resolved_path->name_component, "asset/name");
258}
259
260/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
262 weak_reference__resolve_to_exploded_path__custom_library__windows_pathsep)
263{
265 AssetLibrary *const library = service->get_asset_library_on_disk_custom("My custom lib",
266 asset_library_root_);
267 AssetRepresentation &asset = add_dummy_asset(*library, "some.blend\\Material\\asset/name");
268
269 AssetWeakReference weak_ref = asset.make_weak_reference();
270
271 std::string expected_full_path = utils::normalize_path(asset_library_root_ +
272 "\\some.blend\\Material\\") +
273 "asset/name";
274 std::optional<AssetLibraryService::ExplodedPath> resolved_path =
276
277 EXPECT_EQ(BLI_path_cmp(resolved_path->full_path->c_str(), expected_full_path.c_str()), 0);
278 EXPECT_EQ(BLI_path_cmp_normalized(std::string(resolved_path->dir_component).c_str(),
279 std::string(asset_library_root_ + "\\some.blend").c_str()),
280 0);
281 EXPECT_EQ(resolved_path->group_component, "Material");
282 /* ID names may contain slashes. */
283 EXPECT_EQ(resolved_path->name_component, "asset/name");
284}
285
286} // 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)
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