Blender V5.3
asset_library_service_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BLI_fileops.hh" /* For PATH_MAX (at least on Windows). */
8#include "BLI_path_utils.hh"
9#include "BLI_string.hh"
10
11#include "BKE_appdir.hh"
12#include "BKE_callbacks.hh"
13#include "BKE_global.hh"
14#include "BKE_gtest_base.hh"
15#include "BKE_main.hh"
16
17#include "DNA_asset_types.h"
18
19#include "CLG_log.h"
20
21#include "testing/testing.h"
22
24
25const UUID UUID_POSES_ELLIE("df60e1f6-2259-475b-93d9-69a1b4a8db78");
26
28 public:
32
33 void SetUp() override
34 {
35 const std::string test_files_dir = blender::tests::flags_test_asset_dir();
36 if (test_files_dir.empty()) {
37 FAIL();
38 }
39
40 /* G_MAIN is needed for variable expansion in asset library paths.
41 * IE {project_root}, {blend_name}, {blend_dir} etc.
42 */
44 G_MAIN = bmain;
45
46 asset_library_root_ = test_files_dir + SEP_STR + "asset_library";
48 }
49
50 void TearDown() override
51 {
53
54 if (!temp_library_path_.empty()) {
55 BLI_delete(temp_library_path_.c_str(), true, true);
57 }
58
60 G_MAIN = nullptr;
61 }
62
63 /* Register a temporary path, which will be removed at the end of the test.
64 * The returned path ends in a slash. */
66 {
67 BKE_tempdir_init(nullptr);
68 const CatalogFilePath tempdir = BKE_tempdir_session();
69 temp_library_path_ = tempdir + "test-temporary-path" + SEP_STR;
70 return temp_library_path_;
71 }
72
74 {
76 BLI_dir_create_recursive(path.c_str());
77 return path;
78 }
79};
80
82{
85 << "Calling twice without destroying in between should return the same instance.";
86
87 /* This should not crash. */
90
91 /* NOTE: there used to be a test for the opposite here, that after a call to
92 * AssetLibraryService::destroy() the above calls should return freshly allocated objects. This
93 * cannot be reliably tested by just pointer comparison, though. */
94}
95
97{
99
100 AssetLibrary *const lib = service->get_asset_library_on_disk_custom(__func__,
101 asset_library_root_);
102 AssetLibrary *const curfile_lib = service->get_asset_library_current_file();
103
104 EXPECT_EQ(lib, service->get_asset_library_on_disk_custom(__func__, asset_library_root_))
105 << "Calling twice without destroying in between should return the same instance.";
106 EXPECT_EQ(curfile_lib, service->get_asset_library_current_file())
107 << "Calling twice without destroying in between should return the same instance.";
108
109 /* NOTE: there used to be a test for the opposite here, that after a call to
110 * AssetLibraryService::destroy() the above calls should return freshly allocated objects. This
111 * cannot be reliably tested by just pointer comparison, though. */
112}
113
114TEST_F(AssetLibraryServiceTest, library_from_reference)
115{
117
118 AssetLibrary *const curfile_lib = service->get_asset_library_current_file();
119
122 EXPECT_EQ(curfile_lib, service->get_asset_library(nullptr, ref))
123 << "Getting the local (current file) reference without a main saved on disk should return "
124 "the current file library";
125
126 {
127 Main dummy_main{};
128 std::string dummy_filepath = asset_library_root_ + SEP + "dummy.blend";
129 STRNCPY(dummy_main.filepath, dummy_filepath.c_str());
130
131 AssetLibrary *custom_lib = service->get_asset_library_on_disk_custom(__func__,
132 asset_library_root_);
133 AssetLibrary *tmp_curfile_lib = service->get_asset_library(&dummy_main, ref);
134
135 /* Requested a current file library with a (fake) file saved in the same directory as a custom
136 * asset library. The resulting library should never match the custom asset library, even
137 * though the paths match. */
138
139 EXPECT_NE(custom_lib, tmp_curfile_lib)
140 << "Getting an asset library from a local (current file) library reference should never "
141 "match any custom asset library";
142 EXPECT_EQ(custom_lib->root_path(), tmp_curfile_lib->root_path());
143 }
144}
145
146TEST_F(AssetLibraryServiceTest, library_path_trailing_slashes)
147{
149
150 char asset_lib_no_slash[PATH_MAX];
151 char asset_lib_with_slash[PATH_MAX];
152 STRNCPY(asset_lib_no_slash, asset_library_root_.c_str());
153 STRNCPY(asset_lib_with_slash, asset_library_root_.c_str());
154
155 /* Ensure #asset_lib_no_slash has no trailing slash, regardless of what was passed on the CLI to
156 * the unit test. */
157 while (strlen(asset_lib_no_slash) &&
158 ELEM(asset_lib_no_slash[strlen(asset_lib_no_slash) - 1], SEP, ALTSEP))
159 {
160 asset_lib_no_slash[strlen(asset_lib_no_slash) - 1] = '\0';
161 }
162
163 BLI_path_slash_ensure(asset_lib_with_slash, PATH_MAX);
164
165 AssetLibrary *const lib_no_slash = service->get_asset_library_on_disk_custom(__func__,
166 asset_lib_no_slash);
167
168 EXPECT_EQ(lib_no_slash,
169 service->get_asset_library_on_disk_custom(__func__, asset_lib_with_slash))
170 << "With or without trailing slash shouldn't matter.";
171}
172
174{
176 AssetLibrary *const lib = service->get_asset_library_on_disk_custom(__func__,
177 asset_library_root_);
178 AssetCatalogService &cat_service = lib->catalog_service();
179
180 const UUID UUID_POSES_ELLIE("df60e1f6-2259-475b-93d9-69a1b4a8db78");
181 EXPECT_NE(nullptr, cat_service.find_catalog(UUID_POSES_ELLIE))
182 << "Catalogs should be loaded after getting an asset library from disk.";
183}
184
185TEST_F(AssetLibraryServiceTest, has_any_unsaved_catalogs)
186{
189 << "Empty AssetLibraryService should have no unsaved catalogs";
190
191 AssetLibrary *const lib = service->get_asset_library_on_disk_custom(__func__,
192 asset_library_root_);
193 AssetCatalogService &cat_service = lib->catalog_service();
195 << "Unchanged AssetLibrary should have no unsaved catalogs";
196
197 const UUID UUID_POSES_ELLIE("df60e1f6-2259-475b-93d9-69a1b4a8db78");
200 << "Deletion of catalogs via AssetCatalogService should not automatically tag as 'unsaved "
201 "changes'.";
202
203 const UUID UUID_POSES_RUZENA("79a4f887-ab60-4bd4-94da-d572e27d6aed");
204 AssetCatalog *cat = cat_service.find_catalog(UUID_POSES_RUZENA);
205 ASSERT_NE(nullptr, cat) << "Catalog " << UUID_POSES_RUZENA << " should be known";
206
207 cat_service.tag_has_unsaved_changes(cat);
209 << "Tagging as having unsaved changes of a single catalog service should result in unsaved "
210 "changes being reported.";
212}
213
214TEST_F(AssetLibraryServiceTest, has_any_unsaved_catalogs_after_write)
215{
216 const CatalogFilePath writable_dir = create_temp_path(); /* Has trailing slash. */
217 const CatalogFilePath original_cdf_file = asset_library_root_ + SEP_STR +
218 "blender_assets.cats.txt";
219 CatalogFilePath writable_cdf_file = writable_dir + AssetCatalogService::DEFAULT_CATALOG_FILENAME;
220 BLI_path_slash_native(writable_cdf_file.data());
221 ASSERT_EQ(0, BLI_copy(original_cdf_file.c_str(), writable_cdf_file.c_str()));
222
224 AssetLibrary *const lib = service->get_asset_library_on_disk_custom(__func__, writable_dir);
225
227 << "Unchanged AssetLibrary should have no unsaved catalogs";
228
229 AssetCatalogService &cat_service = lib->catalog_service();
230 AssetCatalog *cat = cat_service.find_catalog(UUID_POSES_ELLIE);
231
232 cat_service.tag_has_unsaved_changes(cat);
233
235 << "Tagging as having unsaved changes of a single catalog service should result in unsaved "
236 "changes being reported.";
238
239 cat_service.write_to_disk(writable_dir + "dummy_path.blend");
241 << "Written AssetCatalogService should have no unsaved catalogs";
243}
244
249TEST_F(AssetLibraryServiceTest, move_runtime_current_file_into_on_disk_library__empty_directory)
250{
252
253 AssetLibrary *runtime_lib = service->get_asset_library_current_file();
254 AssetCatalogService &runtime_catservice = runtime_lib->catalog_service();
255
256 /* Catalog created in the runtime lib that should be moved to the on-disk lib. */
257 AssetCatalog *catalog = runtime_catservice.create_catalog("Some/Catalog/Path");
258 runtime_catservice.undo_push();
259
260 {
262
263 EXPECT_EQ(nullptr, runtime_catservice.find_catalog(UUID_POSES_ELLIE))
264 << "Catalog not expected in the runtime asset library.";
265 }
266
267 {
268 Main dummy_main{};
269 std::string dummy_filepath = create_temp_path() + "dummy.blend";
270 STRNCPY(dummy_main.filepath, dummy_filepath.c_str());
271
273
276
277 /* Loads and merges the catalogs from disk. */
278 AssetLibrary *on_disk_lib = service->get_asset_library(&dummy_main, ref);
279 AssetCatalogService &on_disk_catservice = on_disk_lib->catalog_service();
280
281 /* Can only test the pointer equality here because the implementation keeps the runtime library
282 * alive until all its contents are moved to the on-disk library. Otherwise the allocator might
283 * choose the same address for the new on-disk library. Useful for testing, though not
284 * required. */
285 EXPECT_NE(on_disk_lib, runtime_lib);
286 EXPECT_EQ(on_disk_lib->root_path(), temp_library_path_);
287
288 /* Check if catalog was moved correctly. */
289 {
290 EXPECT_EQ(on_disk_catservice.find_catalog(catalog->catalog_id)->path, catalog->path);
291 /* Compare catalog by pointer. #move_runtime_current_file_into_on_disk_library() doesn't
292 * guarantee publicly that catalog pointers remain unchanged, but practically code might rely
293 * on it. Good to know if this breaks. */
294 EXPECT_EQ(on_disk_catservice.find_catalog(catalog->catalog_id), catalog);
295 /* No writing happened, just merging. */
296 EXPECT_TRUE(on_disk_catservice.find_catalog(catalog->catalog_id)->flags.has_unsaved_changes);
297 }
298
299 EXPECT_EQ(nullptr, runtime_catservice.find_catalog(UUID_POSES_ELLIE))
300 << "Catalog not expected in the on disk asset library.";
301
302 /* Check if undo stack was moved correctly. */
303 {
304 on_disk_catservice.undo();
305 const AssetCatalog *ellie_catalog = on_disk_catservice.find_catalog(UUID_POSES_ELLIE);
306 EXPECT_EQ(nullptr, ellie_catalog) << "This catalog should not be present after undo";
307 EXPECT_EQ(on_disk_catservice.find_catalog(catalog->catalog_id)->path, catalog->path)
308 << "This catalog should still be present after undo";
309 }
310
311 /* Force a new current file runtime library to be created. */
312 EXPECT_NE(service->get_asset_library_current_file(), on_disk_lib);
313 }
314}
315
322 move_runtime_current_file_into_on_disk_library__directory_with_catalogs)
323{
325
326 AssetLibrary *runtime_lib = service->get_asset_library_current_file();
327 AssetCatalogService &runtime_catservice = runtime_lib->catalog_service();
328
329 /* Catalog created in the runtime lib that should be moved to the on-disk lib. */
330 AssetCatalog *catalog = runtime_catservice.create_catalog("Some/Catalog/Path");
331 runtime_catservice.undo_push();
332
333 {
335
336 EXPECT_EQ(nullptr, runtime_catservice.find_catalog(UUID_POSES_ELLIE))
337 << "Catalog not expected in the runtime asset library.";
338 }
339
340 {
341 Main dummy_main{};
342 std::string dummy_filepath = asset_library_root_ + SEP + "dummy.blend";
343 STRNCPY(dummy_main.filepath, dummy_filepath.c_str());
344
346
349
350 /* Loads and merges the catalogs from disk. */
351 AssetLibrary *on_disk_lib = service->get_asset_library(&dummy_main, ref);
352 AssetCatalogService &on_disk_catservice = on_disk_lib->catalog_service();
353
354 EXPECT_NE(on_disk_lib, runtime_lib);
356 (asset_library_root_ + SEP).c_str()),
357 0);
358
359 /* Check if catalog was moved correctly. */
360 {
361 EXPECT_EQ(on_disk_catservice.find_catalog(catalog->catalog_id)->path, catalog->path);
362 /* Compare catalog by pointer. #move_runtime_current_file_into_on_disk_library() doesn't
363 * guarantee publicly that catalog pointers remain unchanged, but practically code might rely
364 * on it. Good to know if this breaks. */
365 EXPECT_EQ(on_disk_catservice.find_catalog(catalog->catalog_id), catalog);
366 /* No writing happened, just merging. */
367 EXPECT_TRUE(on_disk_catservice.find_catalog(catalog->catalog_id)->flags.has_unsaved_changes);
368 }
369
370 /* Check if catalogs have been merged in from disk correctly (by #get_asset_library()). */
371 {
372 const AssetCatalog *ellie_catalog = on_disk_catservice.find_catalog(UUID_POSES_ELLIE);
373 EXPECT_NE(nullptr, ellie_catalog)
374 << "Catalogs should be loaded after getting an asset library from disk.";
375 EXPECT_FALSE(ellie_catalog->flags.has_unsaved_changes);
376 }
377
378 /* Check if undo stack was moved correctly. */
379 {
380 on_disk_catservice.undo();
381 const AssetCatalog *ellie_catalog = on_disk_catservice.find_catalog(UUID_POSES_ELLIE);
382 EXPECT_EQ(nullptr, ellie_catalog) << "This catalog should not be present after undo";
383 EXPECT_EQ(on_disk_catservice.find_catalog(catalog->catalog_id)->path, catalog->path)
384 << "This catalog should still be present after undo";
385 }
386
387 /* Force a new current file runtime library to be created. */
388 EXPECT_NE(service->get_asset_library_current_file(), on_disk_lib);
389 }
390}
391
392} // namespace blender::asset_system::tests
#define G_MAIN
File and directory operations.
#define PATH_MAX
#define ELEM(...)
constexpr const char * c_str() const
AssetCatalog * find_catalog(CatalogID catalog_id) const
bool write_to_disk(const CatalogFilePath &blend_file_path)
AssetCatalog * create_catalog(const AssetCatalogPath &catalog_path)
void tag_has_unsaved_changes(AssetCatalog *edited_catalog=nullptr)
static const CatalogFilePath DEFAULT_CATALOG_FILENAME
struct blender::asset_system::AssetCatalog::Flags flags
AssetLibrary * get_asset_library(const Main *bmain, const AssetLibraryReference &library_reference)
static AssetLibrary * move_runtime_current_file_into_on_disk_library(const Main &bmain)
AssetLibrary * get_asset_library_on_disk_custom(StringRef name, StringRefNull root_path)
AssetCatalogService & catalog_service() const
#define EXPECT_FALSE(result)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
const UUID UUID_POSES_RUZENA("79a4f887-ab60-4bd4-94da-d572e27d6aed")
TEST_F(AssetCatalogPathTest, construction)
const UUID UUID_POSES_ELLIE("df60e1f6-2259-475b-93d9-69a1b4a8db78")
char * STRNCPY(char(&dst)[N], const char *src)
constexpr char ALTSEP
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
Main * BKE_main_new()
Definition main.cc:93
void BLI_path_slash_native(char *path) ATTR_NONNULL(1)
const char * BKE_tempdir_session() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1283
@ ASSET_LIBRARY_LOCAL
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) 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)
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
constexpr char SEP
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1245
char filepath[1024]
Definition BKE_main.hh:267
#define SEP_STR
Definition unit.cc:41