Blender V4.3
asset_catalog_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
5#include "AS_asset_catalog.hh"
9
10#include "BKE_preferences.h"
11
12#include "BLI_fileops.h"
13#include "BLI_path_utils.hh"
14
15#include "DNA_asset_types.h"
16#include "DNA_userdef_types.h"
17
18#include "testing/testing.h"
19
21
23
24/* UUIDs from tests/data/asset_library/blender_assets.cats.txt */
25const bUUID UUID_ID_WITHOUT_PATH("e34dd2c5-5d2e-4668-9794-1db5de2a4f71");
26const bUUID UUID_POSES_ELLIE("df60e1f6-2259-475b-93d9-69a1b4a8db78");
27const bUUID UUID_POSES_ELLIE_WHITESPACE("b06132f6-5687-4751-a6dd-392740eb3c46");
28const bUUID UUID_POSES_ELLIE_TRAILING_SLASH("3376b94b-a28d-4d05-86c1-bf30b937130d");
29const bUUID UUID_POSES_ELLIE_BACKSLASHES("a51e17ae-34fc-47d5-ba0f-64c2c9b771f7");
30const bUUID UUID_POSES_RUZENA("79a4f887-ab60-4bd4-94da-d572e27d6aed");
31const bUUID UUID_POSES_RUZENA_HAND("81811c31-1a88-4bd7-bb34-c6fc2607a12e");
32const bUUID UUID_POSES_RUZENA_FACE("82162c1f-06cc-4d91-a9bf-4f72c104e348");
33const bUUID UUID_WITHOUT_SIMPLENAME("d7916a31-6ca9-4909-955f-182ca2b81fa3");
34const bUUID UUID_ANOTHER_RUZENA("00000000-d9fa-4b91-b704-e6af1f1339ef");
35
36/* UUIDs from tests/data/asset_library/modified_assets.cats.txt */
37const bUUID UUID_AGENT_47("c5744ba5-43f5-4f73-8e52-010ad4a61b34");
38
39/* Subclass that adds accessors such that protected fields can be used in tests. */
41 public:
43
44 explicit TestableAssetCatalogService(const CatalogFilePath &asset_library_root)
45 : AssetCatalogService(asset_library_root)
46 {
47 }
48
53
58
63
68
70 {
71 int64_t count = 0;
72 for (const auto &catalog_uptr : get_catalogs().values()) {
73 if (catalog_uptr->path == path) {
74 count++;
75 }
76 }
77 return count;
78 }
79};
80
82 protected:
83 /* Used by on_blendfile_save__from_memory_into_existing_asset_lib* test functions. */
84 void save_from_memory_into_existing_asset_lib(const bool should_top_level_cdf_exist)
85 {
86 const CatalogFilePath target_dir = create_temp_path(); /* Has trailing slash. */
87 const CatalogFilePath original_cdf_file = asset_library_root_ + SEP_STR +
88 "blender_assets.cats.txt";
89 const CatalogFilePath registered_asset_lib = target_dir + "my_asset_library" + SEP_STR;
90 const CatalogFilePath asset_lib_subdir = registered_asset_lib + "subdir" + SEP_STR;
91 CatalogFilePath cdf_toplevel = registered_asset_lib +
93 CatalogFilePath cdf_in_subdir = asset_lib_subdir +
95 BLI_path_slash_native(cdf_toplevel.data());
96 BLI_path_slash_native(cdf_in_subdir.data());
97
98 /* Set up a temporary asset library for testing. */
100 &U, "Test", registered_asset_lib.c_str());
101 ASSERT_NE(nullptr, asset_lib_pref);
102 ASSERT_TRUE(BLI_dir_create_recursive(asset_lib_subdir.c_str()));
103
104 if (should_top_level_cdf_exist) {
105 ASSERT_EQ(0, BLI_copy(original_cdf_file.c_str(), cdf_toplevel.c_str()));
106 }
107
108 /* Create an empty CDF to add complexity. It should not save to this, but to the top-level
109 * one. */
110 ASSERT_TRUE(BLI_file_touch(cdf_in_subdir.c_str()));
111 ASSERT_EQ(0, BLI_file_size(cdf_in_subdir.c_str()));
112
113 /* Create the catalog service without loading the already-existing CDF. */
115 const CatalogFilePath blendfilename = asset_lib_subdir + "some_file.blend";
116 const AssetCatalog *cat = service.create_catalog("some/catalog/path");
117
118 /* Mock that the blend file is written to the directory already containing a CDF. */
119 ASSERT_TRUE(service.write_to_disk(blendfilename));
120
121 /* Test that the CDF still exists in the expected location. */
122 EXPECT_TRUE(BLI_exists(cdf_toplevel.c_str()));
123 const CatalogFilePath backup_filename = cdf_toplevel + "~";
124 const bool backup_exists = BLI_exists(backup_filename.c_str());
125 EXPECT_EQ(should_top_level_cdf_exist, backup_exists)
126 << "Overwritten CDF should have been backed up.";
127
128 /* Test that the in-memory CDF has the expected file path. */
130 std::string native_cdf_path = cdf->file_path;
131 BLI_path_slash_native(native_cdf_path.data());
132 EXPECT_EQ(cdf_toplevel, native_cdf_path);
133
134 /* Test that the in-memory catalogs have been merged with the on-disk one. */
135 AssetCatalogService loaded_service(cdf_toplevel);
136 loaded_service.load_from_disk();
137 EXPECT_NE(nullptr, loaded_service.find_catalog(cat->catalog_id));
138
139 /* This catalog comes from a pre-existing CDF that should have been merged.
140 * However, if the file doesn't exist, so does the catalog. */
141 AssetCatalog *poses_ellie_catalog = loaded_service.find_catalog(UUID_POSES_ELLIE);
142 if (should_top_level_cdf_exist) {
143 EXPECT_NE(nullptr, poses_ellie_catalog);
144 }
145 else {
146 EXPECT_EQ(nullptr, poses_ellie_catalog);
147 }
148
149 /* Test that the "red herring" CDF has not been touched. */
150 EXPECT_EQ(0, BLI_file_size(cdf_in_subdir.c_str()));
151
152 BKE_preferences_asset_library_remove(&U, asset_lib_pref);
153 }
154};
155
156TEST_F(AssetCatalogTest, load_single_file)
157{
158 AssetCatalogService service(asset_library_root_);
159 service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
160
161 /* Test getting a non-existent catalog ID. */
163
164 /* Test getting an invalid catalog (without path definition). */
165 AssetCatalog *cat_without_path = service.find_catalog(UUID_ID_WITHOUT_PATH);
166 ASSERT_EQ(nullptr, cat_without_path);
167
168 /* Test getting a regular catalog. */
169 AssetCatalog *poses_ellie = service.find_catalog(UUID_POSES_ELLIE);
170 ASSERT_NE(nullptr, poses_ellie);
172 EXPECT_EQ("character/Ellie/poselib", poses_ellie->path.str());
173 EXPECT_EQ("POSES_ELLIE", poses_ellie->simple_name);
174
175 /* Test white-space stripping and support in the path. */
176 AssetCatalog *poses_whitespace = service.find_catalog(UUID_POSES_ELLIE_WHITESPACE);
177 ASSERT_NE(nullptr, poses_whitespace);
179 EXPECT_EQ("character/Ellie/poselib/white space", poses_whitespace->path.str());
180 EXPECT_EQ("POSES_ELLIE WHITESPACE", poses_whitespace->simple_name);
181
182 /* Test getting a UTF-8 catalog ID. */
183 AssetCatalog *poses_ruzena = service.find_catalog(UUID_POSES_RUZENA);
184 ASSERT_NE(nullptr, poses_ruzena);
185 EXPECT_EQ(UUID_POSES_RUZENA, poses_ruzena->catalog_id);
186 EXPECT_EQ("character/Ružena/poselib", poses_ruzena->path.str());
187 EXPECT_EQ("POSES_RUŽENA", poses_ruzena->simple_name);
188
189 /* Test getting a catalog that aliases an earlier-defined catalog. */
190 AssetCatalog *another_ruzena = service.find_catalog(UUID_ANOTHER_RUZENA);
191 ASSERT_NE(nullptr, another_ruzena);
192 EXPECT_EQ(UUID_ANOTHER_RUZENA, another_ruzena->catalog_id);
193 EXPECT_EQ("character/Ružena/poselib", another_ruzena->path.str());
194 EXPECT_EQ("Another Ružena", another_ruzena->simple_name);
195}
196
197TEST_F(AssetCatalogTest, load_catalog_path_backslashes)
198{
199 AssetCatalogService service(asset_library_root_);
200 service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
201
202 const AssetCatalog *found_by_id = service.find_catalog(UUID_POSES_ELLIE_BACKSLASHES);
203 ASSERT_NE(nullptr, found_by_id);
204 EXPECT_EQ(AssetCatalogPath("character/Ellie/backslashes"), found_by_id->path)
205 << "Backslashes should be normalized when loading from disk.";
206 EXPECT_EQ(StringRefNull("Windows For Life!"), found_by_id->simple_name);
207
208 const AssetCatalog *found_by_path = service.find_catalog_by_path("character/Ellie/backslashes");
209 EXPECT_EQ(found_by_id, found_by_path)
210 << "Catalog with backslashed path should be findable by the normalized path.";
211
212 EXPECT_EQ(nullptr, service.find_catalog_by_path("character\\Ellie\\backslashes"))
213 << "Nothing should be found when searching for backslashes.";
214}
215
216TEST_F(AssetCatalogTest, is_first_loaded_flag)
217{
218 AssetCatalogService service(asset_library_root_);
219 service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
220
221 AssetCatalog *new_cat = service.create_catalog("never/before/seen/path");
222 EXPECT_FALSE(new_cat->flags.is_first_loaded)
223 << "Adding a catalog at runtime should never mark it as 'first loaded'; "
224 "only loading from disk is allowed to do that.";
225
226 AssetCatalog *alias_cat = service.create_catalog("character/Ružena/poselib");
227 EXPECT_FALSE(alias_cat->flags.is_first_loaded)
228 << "Adding a new catalog with an already-loaded path should not mark it as 'first loaded'";
229
230 EXPECT_TRUE(service.find_catalog(UUID_POSES_ELLIE)->flags.is_first_loaded);
234
235 AssetCatalog *ruzena = service.find_catalog_by_path("character/Ružena/poselib");
237 << "The first-seen definition of a catalog should be returned";
238}
239
240TEST_F(AssetCatalogTest, find_catalog_by_path)
241{
242 TestableAssetCatalogService service(asset_library_root_);
243 service.load_from_disk(asset_library_root_ + SEP_STR +
245
246 AssetCatalog *catalog;
247
248 EXPECT_EQ(nullptr, service.find_catalog_by_path(""));
249 catalog = service.find_catalog_by_path("character/Ellie/poselib/white space");
250 EXPECT_NE(nullptr, catalog);
252 catalog = service.find_catalog_by_path("character/Ružena/poselib");
253 EXPECT_NE(nullptr, catalog);
255
256 /* "character/Ellie/poselib" is used by two catalogs. Check if it's using the first one. */
257 catalog = service.find_catalog_by_path("character/Ellie/poselib");
258 EXPECT_NE(nullptr, catalog);
260 EXPECT_NE(UUID_POSES_ELLIE_TRAILING_SLASH, catalog->catalog_id);
261}
262
263TEST_F(AssetCatalogTest, write_single_file)
264{
265 TestableAssetCatalogService service(asset_library_root_);
266 service.load_from_disk(asset_library_root_ + SEP_STR +
268
269 const CatalogFilePath save_to_path = use_temp_path() +
272 cdf->write_to_disk(save_to_path);
273
274 AssetCatalogService loaded_service(save_to_path);
275 loaded_service.load_from_disk();
276
277 /* Test that the expected catalogs are there. */
278 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE));
279 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE_WHITESPACE));
280 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE_TRAILING_SLASH));
281 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA));
282 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA_HAND));
283 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA_FACE));
284
285 /* Test that the invalid catalog definition wasn't copied. */
286 EXPECT_EQ(nullptr, loaded_service.find_catalog(UUID_ID_WITHOUT_PATH));
287
288 /* TODO(@sybren): test ordering of catalogs in the file. */
289}
290
291TEST_F(AssetCatalogTest, read_write_unicode_filepath)
292{
293 TestableAssetCatalogService service(asset_library_root_);
294 const CatalogFilePath load_from_path = asset_library_root_ + SEP_STR + "новый" + SEP_STR +
296 service.load_from_disk(load_from_path);
297
298 const CatalogFilePath save_to_path = use_temp_path() + "новый.cats.txt";
300 ASSERT_NE(nullptr, cdf) << "unable to load " << load_from_path;
301 EXPECT_TRUE(cdf->write_to_disk(save_to_path));
302
303 AssetCatalogService loaded_service(save_to_path);
304 loaded_service.load_from_disk();
305
306 /* Test that the file was loaded correctly. */
307 const bUUID materials_uuid("a2151dff-dead-4f29-b6bc-b2c7d6cccdb4");
308 const AssetCatalog *cat = loaded_service.find_catalog(materials_uuid);
309 ASSERT_NE(nullptr, cat);
310 EXPECT_EQ(materials_uuid, cat->catalog_id);
311 EXPECT_EQ(AssetCatalogPath("Материалы"), cat->path);
312 EXPECT_EQ("Russian Materials", cat->simple_name);
313}
314
315TEST_F(AssetCatalogTest, no_writing_empty_files)
316{
317 const CatalogFilePath temp_lib_root = create_temp_path();
318 AssetCatalogService service(temp_lib_root);
319 service.write_to_disk(temp_lib_root + "phony.blend");
320
321 const CatalogFilePath default_cdf_path = temp_lib_root +
323 EXPECT_FALSE(BLI_exists(default_cdf_path.c_str()));
324}
325
326/* Already loaded a CDF, saving to some unrelated directory. */
327TEST_F(AssetCatalogTest, on_blendfile_save__with_existing_cdf)
328{
329 const CatalogFilePath top_level_dir = create_temp_path(); /* Has trailing slash. */
330
331 /* Create a copy of the CDF in SVN, so we can safely write to it. */
332 const CatalogFilePath original_cdf_file = asset_library_root_ + SEP_STR +
333 "blender_assets.cats.txt";
334 const CatalogFilePath cdf_dirname = top_level_dir + "other_dir" + SEP_STR;
335 const CatalogFilePath cdf_filename = cdf_dirname + AssetCatalogService::DEFAULT_CATALOG_FILENAME;
336 ASSERT_TRUE(BLI_dir_create_recursive(cdf_dirname.c_str()));
337 ASSERT_EQ(0, BLI_copy(original_cdf_file.c_str(), cdf_filename.c_str()))
338 << "Unable to copy " << original_cdf_file << " to " << cdf_filename;
339
340 /* Load the CDF, add a catalog, and trigger a write. This should write to the loaded CDF. */
341 TestableAssetCatalogService service(cdf_filename);
342 service.load_from_disk();
343 const AssetCatalog *cat = service.create_catalog("some/catalog/path");
344
345 const CatalogFilePath blendfilename = top_level_dir + "subdir" + SEP_STR + "some_file.blend";
346 ASSERT_TRUE(service.write_to_disk(blendfilename));
347 EXPECT_EQ(cdf_filename, service.get_catalog_definition_file()->file_path);
348
349 /* Test that the CDF was created in the expected location. */
350 const CatalogFilePath backup_filename = cdf_filename + "~";
351 EXPECT_TRUE(BLI_exists(cdf_filename.c_str()));
352 EXPECT_TRUE(BLI_exists(backup_filename.c_str()))
353 << "Overwritten CDF should have been backed up.";
354
355 /* Test that the on-disk CDF contains the expected catalogs. */
356 AssetCatalogService loaded_service(cdf_filename);
357 loaded_service.load_from_disk();
358 EXPECT_NE(nullptr, loaded_service.find_catalog(cat->catalog_id))
359 << "Expected to see the newly-created catalog.";
360 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE))
361 << "Expected to see the already-existing catalog.";
362}
363
364/* Create some catalogs in memory, save to directory that doesn't contain anything else. */
365TEST_F(AssetCatalogTest, on_blendfile_save__from_memory_into_empty_directory)
366{
367 const CatalogFilePath target_dir = create_temp_path(); /* Has trailing slash. */
368
370 const AssetCatalog *cat = service.create_catalog("some/catalog/path");
371
372 const CatalogFilePath blendfilename = target_dir + "some_file.blend";
373 ASSERT_TRUE(service.write_to_disk(blendfilename));
374
375 /* Test that the CDF was created in the expected location. */
376 const CatalogFilePath expected_cdf_path = target_dir +
378 EXPECT_TRUE(BLI_exists(expected_cdf_path.c_str()));
379
380 /* Test that the in-memory CDF has been created, and contains the expected catalog. */
382 ASSERT_NE(nullptr, cdf);
383 EXPECT_TRUE(cdf->contains(cat->catalog_id));
384
385 /* Test that the on-disk CDF contains the expected catalog. */
386 AssetCatalogService loaded_service(expected_cdf_path);
387 loaded_service.load_from_disk();
388 EXPECT_NE(nullptr, loaded_service.find_catalog(cat->catalog_id));
389}
390
391/* Create some catalogs in memory, save to directory that contains a default CDF. */
392TEST_F(AssetCatalogTest, on_blendfile_save__from_memory_into_existing_cdf_and_merge)
393{
394 const CatalogFilePath target_dir = create_temp_path(); /* Has trailing slash. */
395 const CatalogFilePath original_cdf_file = asset_library_root_ + SEP_STR +
396 "blender_assets.cats.txt";
397 CatalogFilePath writable_cdf_file = target_dir + AssetCatalogService::DEFAULT_CATALOG_FILENAME;
398 BLI_path_slash_native(writable_cdf_file.data());
399 ASSERT_EQ(0, BLI_copy(original_cdf_file.c_str(), writable_cdf_file.c_str()));
400
401 /* Create the catalog service without loading the already-existing CDF. */
403 const AssetCatalog *cat = service.create_catalog("some/catalog/path");
404
405 /* Mock that the blend file is written to a subdirectory of the asset library. */
406 const CatalogFilePath blendfilename = target_dir + "some_file.blend";
407 ASSERT_TRUE(service.write_to_disk(blendfilename));
408
409 /* Test that the CDF still exists in the expected location. */
410 const CatalogFilePath backup_filename = writable_cdf_file + "~";
411 EXPECT_TRUE(BLI_exists(writable_cdf_file.c_str()));
412 EXPECT_TRUE(BLI_exists(backup_filename.c_str()))
413 << "Overwritten CDF should have been backed up.";
414
415 /* Test that the in-memory CDF has the expected file path. */
417 ASSERT_NE(nullptr, cdf);
418 EXPECT_EQ(writable_cdf_file, cdf->file_path);
419
420 /* Test that the in-memory catalogs have been merged with the on-disk one. */
421 AssetCatalogService loaded_service(writable_cdf_file);
422 loaded_service.load_from_disk();
423 EXPECT_NE(nullptr, loaded_service.find_catalog(cat->catalog_id));
424 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE));
425}
426
427/* Create some catalogs in memory, save to subdirectory of a registered asset library, where the
428 * subdirectory also contains a CDF. This should still write to the top-level dir of the asset
429 * library. */
431 on_blendfile_save__from_memory_into_existing_asset_lib_without_top_level_cdf)
432{
433 save_from_memory_into_existing_asset_lib(true);
434}
435
436/* Create some catalogs in memory, save to subdirectory of a registered asset library, where the
437 * subdirectory contains a CDF, but the top-level directory does not. This should still write to
438 * the top-level dir of the asset library. */
439TEST_F(AssetCatalogTest, on_blendfile_save__from_memory_into_existing_asset_lib)
440{
441 save_from_memory_into_existing_asset_lib(false);
442}
443
444TEST_F(AssetCatalogTest, create_first_catalog_from_scratch)
445{
446 /* Even from scratch a root directory should be known. */
447 const CatalogFilePath temp_lib_root = use_temp_path();
448 AssetCatalogService service;
449
450 /* Just creating the service should NOT create the path. */
451 EXPECT_FALSE(BLI_exists(temp_lib_root.c_str()));
452
453 AssetCatalog *cat = service.create_catalog("some/catalog/path");
454 ASSERT_NE(nullptr, cat);
455 EXPECT_EQ(cat->path, "some/catalog/path");
456 EXPECT_EQ(cat->simple_name, "some-catalog-path");
457
458 /* Creating a new catalog should not save anything to disk yet. */
459 EXPECT_FALSE(BLI_exists(temp_lib_root.c_str()));
460
461 /* Writing to disk should create the directory + the default file. */
462 service.write_to_disk(temp_lib_root + "phony.blend");
463 EXPECT_TRUE(BLI_is_dir(temp_lib_root.c_str()));
464
465 const CatalogFilePath definition_file_path = temp_lib_root + SEP_STR +
467 EXPECT_TRUE(BLI_is_file(definition_file_path.c_str()));
468
469 AssetCatalogService loaded_service(temp_lib_root);
470 loaded_service.load_from_disk();
471
472 /* Test that the expected catalog is there. */
473 AssetCatalog *written_cat = loaded_service.find_catalog(cat->catalog_id);
474 ASSERT_NE(nullptr, written_cat);
475 EXPECT_EQ(written_cat->catalog_id, cat->catalog_id);
476 EXPECT_EQ(written_cat->path, cat->path.str());
477}
478
479TEST_F(AssetCatalogTest, create_catalog_after_loading_file)
480{
481 const CatalogFilePath temp_lib_root = create_temp_path();
482
483 /* Copy the asset catalog definition files to a separate location, so that we can test without
484 * overwriting the test file in SVN. */
485 const CatalogFilePath default_catalog_path = asset_library_root_ + SEP_STR +
487 const CatalogFilePath writable_catalog_path = temp_lib_root +
489 ASSERT_EQ(0, BLI_copy(default_catalog_path.c_str(), writable_catalog_path.c_str()));
490 EXPECT_TRUE(BLI_is_dir(temp_lib_root.c_str()));
491 EXPECT_TRUE(BLI_is_file(writable_catalog_path.c_str()));
492
493 TestableAssetCatalogService service(temp_lib_root);
494 service.load_from_disk();
495 EXPECT_EQ(writable_catalog_path, service.get_catalog_definition_file()->file_path);
496 EXPECT_NE(nullptr, service.find_catalog(UUID_POSES_ELLIE)) << "expected catalogs to be loaded";
497
498 /* This should create a new catalog but not write to disk. */
499 const AssetCatalog *new_catalog = service.create_catalog("new/catalog");
500 const bUUID new_catalog_id = new_catalog->catalog_id;
501
502 /* Reload the on-disk catalog file. */
503 TestableAssetCatalogService loaded_service(temp_lib_root);
504 loaded_service.load_from_disk();
505 EXPECT_EQ(writable_catalog_path, loaded_service.get_catalog_definition_file()->file_path);
506
507 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE))
508 << "expected pre-existing catalogs to be kept in the file";
509 EXPECT_EQ(nullptr, loaded_service.find_catalog(new_catalog_id))
510 << "expecting newly added catalog to not yet be saved to " << temp_lib_root;
511
512 /* Write and reload the catalog file. */
513 service.write_to_disk(temp_lib_root + "phony.blend");
514 AssetCatalogService reloaded_service(temp_lib_root);
515 reloaded_service.load_from_disk();
516 EXPECT_NE(nullptr, reloaded_service.find_catalog(UUID_POSES_ELLIE))
517 << "expected pre-existing catalogs to be kept in the file";
518 EXPECT_NE(nullptr, reloaded_service.find_catalog(new_catalog_id))
519 << "expecting newly added catalog to exist in the file";
520}
521
522TEST_F(AssetCatalogTest, create_catalog_path_cleanup)
523{
524 AssetCatalogService service;
525 AssetCatalog *cat = service.create_catalog(" /some/path / ");
526
527 EXPECT_FALSE(BLI_uuid_is_nil(cat->catalog_id));
528 EXPECT_EQ("some/path", cat->path.str());
529 EXPECT_EQ("some-path", cat->simple_name);
530}
531
532TEST_F(AssetCatalogTest, create_catalog_simple_name)
533{
534 AssetCatalogService service;
535 AssetCatalog *cat = service.create_catalog(
536 "production/Spite Fright/Characters/Victora/Pose Library/Approved/Body Parts/Hands");
537
538 EXPECT_FALSE(BLI_uuid_is_nil(cat->catalog_id));
539 EXPECT_EQ("production/Spite Fright/Characters/Victora/Pose Library/Approved/Body Parts/Hands",
540 cat->path.str());
541 EXPECT_EQ("...ht-Characters-Victora-Pose Library-Approved-Body Parts-Hands", cat->simple_name);
542}
543
544TEST_F(AssetCatalogTest, delete_catalog_leaf)
545{
546 AssetCatalogService service(asset_library_root_);
547 service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
548
549 /* Delete a leaf catalog, i.e. one that is not a parent of another catalog.
550 * This keeps this particular test easy. */
553
554 /* Contains not only paths from the CDF but also the missing parents (implicitly defined
555 * catalogs). This is why a leaf catalog was deleted. */
556 std::vector<AssetCatalogPath> expected_paths{
557 "character",
558 "character/Ellie",
559 "character/Ellie/backslashes",
560 "character/Ellie/poselib",
561 "character/Ellie/poselib/tailslash",
562 "character/Ellie/poselib/white space",
563 "character/Ružena",
564 "character/Ružena/poselib",
565 "character/Ružena/poselib/face",
566 // "character/Ružena/poselib/hand", /* This is the deleted one. */
567 "path",
568 "path/without",
569 "path/without/simplename",
570 };
571
572 const AssetCatalogTree &tree = service.catalog_tree();
574}
575
576TEST_F(AssetCatalogTest, delete_catalog_parent_by_id)
577{
578 TestableAssetCatalogService service(asset_library_root_);
579 service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
580
581 /* Delete a parent catalog. */
583
584 /* The catalog should have been deleted, but its children should still be there. */
585 EXPECT_EQ(nullptr, service.find_catalog(UUID_POSES_RUZENA));
586 EXPECT_NE(nullptr, service.find_catalog(UUID_POSES_RUZENA_FACE));
587 EXPECT_NE(nullptr, service.find_catalog(UUID_POSES_RUZENA_HAND));
588}
589
590TEST_F(AssetCatalogTest, delete_catalog_parent_by_path)
591{
592 AssetCatalogService service(asset_library_root_);
593 service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
594
595 /* Create an extra catalog with the to-be-deleted path, and one with a child of that.
596 * This creates some duplicates that are bound to occur in production asset libraries as well.
597 */
598 const bUUID cat1_uuid = service.create_catalog("character/Ružena/poselib")->catalog_id;
599 const bUUID cat2_uuid = service.create_catalog("character/Ružena/poselib/body")->catalog_id;
600
601 /* Delete a parent catalog. */
602 service.prune_catalogs_by_path("character/Ružena/poselib");
603
604 /* The catalogs and their children should have been deleted. */
605 EXPECT_EQ(nullptr, service.find_catalog(UUID_POSES_RUZENA));
608 EXPECT_EQ(nullptr, service.find_catalog(cat1_uuid));
609 EXPECT_EQ(nullptr, service.find_catalog(cat2_uuid));
610
611 /* Contains not only paths from the CDF but also the missing parents (implicitly defined
612 * catalogs). This is why a leaf catalog was deleted. */
613 std::vector<AssetCatalogPath> expected_paths{
614 "character",
615 "character/Ellie",
616 "character/Ellie/backslashes",
617 "character/Ellie/poselib",
618 "character/Ellie/poselib/tailslash",
619 "character/Ellie/poselib/white space",
620 "character/Ružena",
621 "path",
622 "path/without",
623 "path/without/simplename",
624 };
625
626 const AssetCatalogTree &tree = service.catalog_tree();
628}
629
630TEST_F(AssetCatalogTest, delete_catalog_write_to_disk)
631{
632 TestableAssetCatalogService service(asset_library_root_);
633 service.load_from_disk(asset_library_root_ + SEP_STR +
635
637
638 const CatalogFilePath save_to_path = use_temp_path();
641
642 AssetCatalogService loaded_service(save_to_path);
643 loaded_service.load_from_disk();
644
645 /* Test that the expected catalogs are there, except the deleted one. */
646 EXPECT_EQ(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE));
647 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE_WHITESPACE));
648 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE_TRAILING_SLASH));
649 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA));
650 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA_HAND));
651 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA_FACE));
652}
653
654TEST_F(AssetCatalogTest, update_catalog_path)
655{
656 AssetCatalogService service(asset_library_root_);
657 service.load_from_disk(asset_library_root_ + SEP_STR +
659
660 const AssetCatalog *orig_cat = service.find_catalog(UUID_POSES_RUZENA);
661 const AssetCatalogPath orig_path = orig_cat->path;
662
663 service.update_catalog_path(UUID_POSES_RUZENA, "charlib/Ružena");
664
665 EXPECT_EQ(nullptr, service.find_catalog_by_path(orig_path))
666 << "The original (pre-rename) path should not be associated with a catalog any more.";
667
668 const AssetCatalog *renamed_cat = service.find_catalog(UUID_POSES_RUZENA);
669 ASSERT_NE(nullptr, renamed_cat);
670 ASSERT_EQ(orig_cat, renamed_cat) << "Changing the path should not reallocate the catalog.";
671 EXPECT_EQ(orig_cat->catalog_id, renamed_cat->catalog_id)
672 << "Changing the path should not change the catalog ID.";
673
674 EXPECT_EQ("charlib/Ružena", renamed_cat->path.str())
675 << "Changing the path should change the path. Surprise.";
676
677 EXPECT_EQ("charlib/Ružena/hand", service.find_catalog(UUID_POSES_RUZENA_HAND)->path.str())
678 << "Changing the path should update children.";
679 EXPECT_EQ("charlib/Ružena/face", service.find_catalog(UUID_POSES_RUZENA_FACE)->path.str())
680 << "Changing the path should update children.";
681}
682
683TEST_F(AssetCatalogTest, update_catalog_path_simple_name)
684{
685 AssetCatalogService service(asset_library_root_);
686 service.load_from_disk(asset_library_root_ + SEP_STR +
688 service.update_catalog_path(UUID_POSES_RUZENA, "charlib/Ružena");
689
690 /* This may not be valid forever; maybe at some point we'll expose the simple name to users &
691 * let them change it from the UI. Until then, automatically updating it is better, because
692 * otherwise all simple names would be "Catalog". */
693 EXPECT_EQ("charlib-Ružena", service.find_catalog(UUID_POSES_RUZENA)->simple_name)
694 << "Changing the path should update the simplename.";
695 EXPECT_EQ("charlib-Ružena-face", service.find_catalog(UUID_POSES_RUZENA_FACE)->simple_name)
696 << "Changing the path should update the simplename of children.";
697}
698
699TEST_F(AssetCatalogTest, update_catalog_path_longer_than_simplename)
700{
701 AssetCatalogService service(asset_library_root_);
702 service.load_from_disk(asset_library_root_ + SEP_STR +
704 const std::string new_path =
705 "this/is/a/very/long/path/that/exceeds/the/simple-name/length/of/assets";
706 ASSERT_GT(new_path.length(), sizeof(AssetMetaData::catalog_simple_name))
707 << "This test case should work with paths longer than AssetMetaData::catalog_simple_name";
708
709 service.update_catalog_path(UUID_POSES_RUZENA, new_path);
710
711 const std::string new_simple_name = service.find_catalog(UUID_POSES_RUZENA)->simple_name;
712 EXPECT_LT(new_simple_name.length(), sizeof(AssetMetaData::catalog_simple_name))
713 << "The new simple name should fit in the asset metadata.";
714 EXPECT_EQ("...very-long-path-that-exceeds-the-simple-name-length-of-assets", new_simple_name)
715 << "Changing the path should update the simplename.";
716 EXPECT_EQ("...long-path-that-exceeds-the-simple-name-length-of-assets-face",
718 << "Changing the path should update the simplename of children.";
719}
720
721TEST_F(AssetCatalogTest, update_catalog_path_add_slashes)
722{
723 AssetCatalogService service(asset_library_root_);
724 service.load_from_disk(asset_library_root_ + SEP_STR +
726
727 const AssetCatalog *orig_cat = service.find_catalog(UUID_POSES_RUZENA);
728 const AssetCatalogPath orig_path = orig_cat->path;
729
730 /* Original path is `character/Ružena/poselib`.
731 * This rename will also create a new catalog for `character/Ružena/poses`. */
732 service.update_catalog_path(UUID_POSES_RUZENA, "character/Ružena/poses/general");
733
734 EXPECT_EQ(nullptr, service.find_catalog_by_path(orig_path))
735 << "The original (pre-rename) path should not be associated with a catalog any more.";
736
737 const AssetCatalog *renamed_cat = service.find_catalog(UUID_POSES_RUZENA);
738 ASSERT_NE(nullptr, renamed_cat);
739 EXPECT_EQ(orig_cat->catalog_id, renamed_cat->catalog_id)
740 << "Changing the path should not change the catalog ID.";
741
742 EXPECT_EQ("character/Ružena/poses/general", renamed_cat->path.str())
743 << "When creating a new catalog by renaming + adding a slash, the renamed catalog should be "
744 "assigned the path passed to update_catalog_path()";
745
746 /* Test the newly created catalog. */
747 const AssetCatalog *new_cat = service.find_catalog_by_path("character/Ružena/poses");
748 ASSERT_NE(nullptr, new_cat) << "Renaming to .../X/Y should cause .../X to exist as well.";
749 EXPECT_EQ("character/Ružena/poses", new_cat->path.str());
750 EXPECT_EQ("character-Ružena-poses", new_cat->simple_name);
751 EXPECT_TRUE(new_cat->flags.has_unsaved_changes);
752
753 /* Test the children. */
754 EXPECT_EQ("character/Ružena/poses/general/hand",
756 << "Changing the path should update children.";
757 EXPECT_EQ("character/Ružena/poses/general/face",
759 << "Changing the path should update children.";
760}
761
762TEST_F(AssetCatalogTest, merge_catalog_files)
763{
764 const CatalogFilePath cdf_dir = create_temp_path();
765 const CatalogFilePath original_cdf_file = asset_library_root_ + SEP_STR +
766 "blender_assets.cats.txt";
767 const CatalogFilePath modified_cdf_file = asset_library_root_ + SEP_STR +
768 "modified_assets.cats.txt";
769 const CatalogFilePath temp_cdf_file = cdf_dir + "blender_assets.cats.txt";
770 ASSERT_EQ(0, BLI_copy(original_cdf_file.c_str(), temp_cdf_file.c_str()));
771
772 /* Load the unmodified, original CDF. */
773 TestableAssetCatalogService service(asset_library_root_);
774 service.load_from_disk(cdf_dir);
775
776 /* Copy a modified file, to mimic a situation where someone changed the
777 * CDF after we loaded it. */
778 ASSERT_EQ(0, BLI_copy(modified_cdf_file.c_str(), temp_cdf_file.c_str()));
779
780 /* Overwrite the modified file. This should merge the on-disk file with our catalogs.
781 * No catalog was marked as "has unsaved changes", so effectively this should not
782 * save anything, and reload what's on disk. */
783 service.write_to_disk(cdf_dir + "phony.blend");
784
785 AssetCatalogService loaded_service(cdf_dir);
786 loaded_service.load_from_disk();
787
788 /* Test that the expected catalogs are there. */
789 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE));
790 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA_FACE));
791 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_AGENT_47)); /* New in the modified file. */
792
793 /* Test that catalogs removed from modified CDF are gone. */
794 EXPECT_EQ(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE_WHITESPACE));
796 EXPECT_EQ(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA));
797 EXPECT_EQ(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA_HAND));
798
799 /* On-disk changed catalogs should have overridden in-memory not-changed ones. */
800 const AssetCatalog *ruzena_face = loaded_service.find_catalog(UUID_POSES_RUZENA_FACE);
801 EXPECT_EQ("character/Ružena/poselib/gezicht", ruzena_face->path.str());
802}
803
804TEST_F(AssetCatalogTest, refresh_catalogs_with_modification)
805{
806 const CatalogFilePath cdf_dir = create_temp_path();
807 const CatalogFilePath original_cdf_file = asset_library_root_ + SEP_STR +
808 "blender_assets.cats.txt";
809 const CatalogFilePath modified_cdf_file = asset_library_root_ + SEP_STR +
810 "catalog_reload_test.cats.txt";
811 const CatalogFilePath temp_cdf_file = cdf_dir + "blender_assets.cats.txt";
812 ASSERT_EQ(0, BLI_copy(original_cdf_file.c_str(), temp_cdf_file.c_str()));
813
814 /* Load the unmodified, original CDF. */
815 TestableAssetCatalogService service(asset_library_root_);
816 service.load_from_disk(cdf_dir);
817
818 /* === Perform changes that should be handled gracefully by the reloading code: */
819
820 /* 1. Delete a subtree of catalogs. */
822 /* 2. Rename a catalog. */
824 service.update_catalog_path(UUID_POSES_ELLIE_TRAILING_SLASH, "character/Ellie/test-value");
825
826 /* Copy a modified file, to mimic a situation where someone changed the
827 * CDF after we loaded it. */
828 ASSERT_EQ(0, BLI_copy(modified_cdf_file.c_str(), temp_cdf_file.c_str()));
829
830 AssetCatalog *const ellie_whitespace_before_reload = service.find_catalog(
832
833 /* This should merge the on-disk file with our catalogs. */
834 service.reload_catalogs();
835
836 /* === Test that the expected catalogs are there. */
837 EXPECT_NE(nullptr, service.find_catalog(UUID_POSES_ELLIE));
838 EXPECT_NE(nullptr, service.find_catalog(UUID_POSES_ELLIE_WHITESPACE));
839 EXPECT_NE(nullptr, service.find_catalog(UUID_POSES_ELLIE_TRAILING_SLASH));
840
841 /* === Test changes made to the CDF: */
842
843 /* Removed from the file. */
845 /* Added to the file. */
846 EXPECT_NE(nullptr, service.find_catalog(UUID_AGENT_47));
847 /* Path modified in file. */
848 AssetCatalog *ellie_whitespace_after_reload = service.find_catalog(UUID_POSES_ELLIE_WHITESPACE);
849 EXPECT_EQ(AssetCatalogPath("whitespace from file"), ellie_whitespace_after_reload->path);
850 EXPECT_NE(ellie_whitespace_after_reload, ellie_whitespace_before_reload);
851 /* Simple name modified in file. */
852 EXPECT_EQ(std::string("Hah simple name after all"),
854
855 /* === Test persistence of in-memory changes: */
856
857 /* This part of the tree we deleted, but still existed in the CDF. They should remain deleted
858 * after reloading: */
859 EXPECT_EQ(nullptr, service.find_catalog(UUID_POSES_RUZENA));
862
863 /* This catalog had its path changed in the test and in the CDF. The change from the test (i.e.
864 * the in-memory, yet-unsaved change) should persist. */
865 EXPECT_EQ(AssetCatalogPath("character/Ellie/test-value"),
867
868 /* Overwrite the modified file. This should merge the on-disk file with our catalogs, and clear
869 * the "has_unsaved_changes" flags. */
870 service.write_to_disk(cdf_dir + "phony.blend");
871
873 << "The catalogs whose path we changed should now be saved";
874 EXPECT_TRUE(service.get_deleted_catalogs().is_empty())
875 << "Deleted catalogs should not be remembered after saving.";
876}
877
879{
880 const CatalogFilePath cdf_dir = create_temp_path();
881 const CatalogFilePath original_cdf_file = asset_library_root_ + SEP_STR +
882 "blender_assets.cats.txt";
883 const CatalogFilePath writable_cdf_file = cdf_dir + SEP_STR + "blender_assets.cats.txt";
884 ASSERT_EQ(0, BLI_copy(original_cdf_file.c_str(), writable_cdf_file.c_str()));
885
886 /* Read a CDF, modify, and write it. */
887 TestableAssetCatalogService service(cdf_dir);
888 service.load_from_disk();
890 service.write_to_disk(cdf_dir + "phony.blend");
891
892 const CatalogFilePath backup_path = writable_cdf_file + "~";
893 ASSERT_TRUE(BLI_is_file(backup_path.c_str()));
894
895 AssetCatalogService loaded_service;
896 loaded_service.load_from_disk(backup_path);
897
898 /* Test that the expected catalogs are there, including the deleted one.
899 * This is the backup, after all. */
900 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE));
901 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE_WHITESPACE));
902 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_ELLIE_TRAILING_SLASH));
903 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA));
904 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA_HAND));
905 EXPECT_NE(nullptr, loaded_service.find_catalog(UUID_POSES_RUZENA_FACE));
906}
907
909{
910 const bUUID cat2_uuid("22222222-b847-44d9-bdca-ff04db1c24f5");
911 const bUUID cat4_uuid("11111111-b847-44d9-bdca-ff04db1c24f5"); /* Sorts earlier than above. */
912 const AssetCatalog cat1(BLI_uuid_generate_random(), "simple/path/child", "");
913 const AssetCatalog cat2(cat2_uuid, "simple/path", "");
914 const AssetCatalog cat3(BLI_uuid_generate_random(), "complex/path/...or/is/it?", "");
915 const AssetCatalog cat4(
916 cat4_uuid, "simple/path", "different ID, same path"); /* should be kept */
917 const AssetCatalog cat5(cat4_uuid, "simple/path", "same ID, same path"); /* disappears */
918
920 by_path.insert(&cat1);
921 by_path.insert(&cat2);
922 by_path.insert(&cat3);
923 by_path.insert(&cat4);
924 by_path.insert(&cat5);
925
926 AssetCatalogOrderedSet::const_iterator set_iter = by_path.begin();
927
928 EXPECT_EQ(1, by_path.count(&cat1));
929 EXPECT_EQ(1, by_path.count(&cat2));
930 EXPECT_EQ(1, by_path.count(&cat3));
931 EXPECT_EQ(1, by_path.count(&cat4));
932 ASSERT_EQ(4, by_path.size()) << "Expecting cat5 to not be stored in the set, as it duplicates "
933 "an already-existing path + UUID";
934
935 EXPECT_EQ(cat3.catalog_id, (*(set_iter++))->catalog_id); /* complex/path */
936 EXPECT_EQ(cat4.catalog_id, (*(set_iter++))->catalog_id); /* simple/path with 111.. ID */
937 EXPECT_EQ(cat2.catalog_id, (*(set_iter++))->catalog_id); /* simple/path with 222.. ID */
938 EXPECT_EQ(cat1.catalog_id, (*(set_iter++))->catalog_id); /* simple/path/child */
939
940 if (set_iter != by_path.end()) {
941 const AssetCatalog *next_cat = *set_iter;
942 FAIL() << "Did not expect more items in the set, had at least " << next_cat->catalog_id << ":"
943 << next_cat->path;
944 }
945}
946
947TEST_F(AssetCatalogTest, order_by_path_and_first_seen)
948{
949 AssetCatalogService service;
950 service.load_from_disk(asset_library_root_);
951
952 const bUUID first_seen_uuid("3d451c87-27d1-40fd-87fc-f4c9e829c848");
953 const bUUID first_sorted_uuid("00000000-0000-0000-0000-000000000001");
954 const bUUID last_sorted_uuid("ffffffff-ffff-ffff-ffff-ffffffffffff");
955
956 AssetCatalog first_seen_cat(first_seen_uuid, "simple/path/child", "");
957 const AssetCatalog first_sorted_cat(first_sorted_uuid, "simple/path/child", "");
958 const AssetCatalog last_sorted_cat(last_sorted_uuid, "simple/path/child", "");
959
960 /* Mimic that this catalog was first-seen when loading from disk. */
961 first_seen_cat.flags.is_first_loaded = true;
962
963 /* Just an assertion of the defaults; this is more to avoid confusing errors later on than an
964 * actual test of these defaults. */
965 ASSERT_FALSE(first_sorted_cat.flags.is_first_loaded);
966 ASSERT_FALSE(last_sorted_cat.flags.is_first_loaded);
967
969 by_path.insert(&first_seen_cat);
970 by_path.insert(&first_sorted_cat);
971 by_path.insert(&last_sorted_cat);
972
973 AssetCatalogOrderedSet::const_iterator set_iter = by_path.begin();
974
975 EXPECT_EQ(1, by_path.count(&first_seen_cat));
976 EXPECT_EQ(1, by_path.count(&first_sorted_cat));
977 EXPECT_EQ(1, by_path.count(&last_sorted_cat));
978 ASSERT_EQ(3, by_path.size());
979
980 EXPECT_EQ(first_seen_uuid, (*(set_iter++))->catalog_id);
981 EXPECT_EQ(first_sorted_uuid, (*(set_iter++))->catalog_id);
982 EXPECT_EQ(last_sorted_uuid, (*(set_iter++))->catalog_id);
983
984 if (set_iter != by_path.end()) {
985 const AssetCatalog *next_cat = *set_iter;
986 FAIL() << "Did not expect more items in the set, had at least " << next_cat->catalog_id << ":"
987 << next_cat->path;
988 }
989}
990
991TEST_F(AssetCatalogTest, create_missing_catalogs)
992{
993 TestableAssetCatalogService new_service;
994 new_service.create_catalog("path/with/missing/parents");
995
996 EXPECT_EQ(nullptr, new_service.find_catalog_by_path("path/with/missing"))
997 << "Missing parents should not be immediately created.";
998 EXPECT_EQ(nullptr, new_service.find_catalog_by_path("")) << "Empty path should never be valid";
999
1000 new_service.create_missing_catalogs();
1001
1002 EXPECT_NE(nullptr, new_service.find_catalog_by_path("path/with/missing"));
1003 EXPECT_NE(nullptr, new_service.find_catalog_by_path("path/with"));
1004 EXPECT_NE(nullptr, new_service.find_catalog_by_path("path"));
1005 EXPECT_EQ(nullptr, new_service.find_catalog_by_path(""))
1006 << "Empty path should never be valid, even when after missing catalogs";
1007}
1008
1009TEST_F(AssetCatalogTest, create_missing_catalogs_after_loading)
1010{
1011 TestableAssetCatalogService loaded_service(asset_library_root_);
1012 loaded_service.load_from_disk();
1013
1014 const AssetCatalog *cat_char = loaded_service.find_catalog_by_path("character");
1015 const AssetCatalog *cat_ellie = loaded_service.find_catalog_by_path("character/Ellie");
1016 const AssetCatalog *cat_ruzena = loaded_service.find_catalog_by_path("character/Ružena");
1017 ASSERT_NE(nullptr, cat_char) << "Missing parents should be created immediately after loading.";
1018 ASSERT_NE(nullptr, cat_ellie) << "Missing parents should be created immediately after loading.";
1019 ASSERT_NE(nullptr, cat_ruzena) << "Missing parents should be created immediately after loading.";
1020
1021 EXPECT_TRUE(cat_char->flags.has_unsaved_changes)
1022 << "Missing parents should be marked as having changes.";
1023 EXPECT_TRUE(cat_ellie->flags.has_unsaved_changes)
1024 << "Missing parents should be marked as having changes.";
1025 EXPECT_TRUE(cat_ruzena->flags.has_unsaved_changes)
1026 << "Missing parents should be marked as having changes.";
1027
1028 const AssetCatalogDefinitionFile *cdf = loaded_service.get_catalog_definition_file();
1029 ASSERT_NE(nullptr, cdf);
1030 EXPECT_TRUE(cdf->contains(cat_char->catalog_id)) << "Missing parents should be saved to a CDF.";
1031 EXPECT_TRUE(cdf->contains(cat_ellie->catalog_id)) << "Missing parents should be saved to a CDF.";
1032 EXPECT_TRUE(cdf->contains(cat_ruzena->catalog_id))
1033 << "Missing parents should be saved to a CDF.";
1034
1035 /* Check that each missing parent is only created once. The CDF contains multiple paths that
1036 * could trigger the creation of missing parents, so this test makes sense. */
1037 EXPECT_EQ(1, loaded_service.count_catalogs_with_path("character"));
1038 EXPECT_EQ(1, loaded_service.count_catalogs_with_path("character/Ellie"));
1039 EXPECT_EQ(1, loaded_service.count_catalogs_with_path("character/Ružena"));
1040}
1041
1042TEST_F(AssetCatalogTest, create_catalog_filter)
1043{
1044 AssetCatalogService service(asset_library_root_);
1045 service.load_from_disk();
1046
1047 /* Alias for the same catalog as the main one. */
1048 AssetCatalog *alias_ruzena = service.create_catalog("character/Ružena/poselib");
1049 /* Alias for a sub-catalog. */
1050 AssetCatalog *alias_ruzena_hand = service.create_catalog("character/Ružena/poselib/hand");
1051
1053
1054 /* Positive test for loaded-from-disk catalogs. */
1055 EXPECT_TRUE(filter.contains(UUID_POSES_RUZENA))
1056 << "Main catalog should be included in the filter.";
1057 EXPECT_TRUE(filter.contains(UUID_POSES_RUZENA_HAND))
1058 << "Sub-catalog should be included in the filter.";
1059 EXPECT_TRUE(filter.contains(UUID_POSES_RUZENA_FACE))
1060 << "Sub-catalog should be included in the filter.";
1061
1062 /* Positive test for newly-created catalogs. */
1063 EXPECT_TRUE(filter.contains(alias_ruzena->catalog_id))
1064 << "Alias of main catalog should be included in the filter.";
1065 EXPECT_TRUE(filter.contains(alias_ruzena_hand->catalog_id))
1066 << "Alias of sub-catalog should be included in the filter.";
1067
1068 /* Negative test for unrelated catalogs. */
1069 EXPECT_FALSE(filter.contains(BLI_uuid_nil())) << "Nil catalog should not be included.";
1070 EXPECT_FALSE(filter.contains(UUID_ID_WITHOUT_PATH));
1071 EXPECT_FALSE(filter.contains(UUID_POSES_ELLIE));
1072 EXPECT_FALSE(filter.contains(UUID_POSES_ELLIE_WHITESPACE));
1073 EXPECT_FALSE(filter.contains(UUID_POSES_ELLIE_TRAILING_SLASH));
1074 EXPECT_FALSE(filter.contains(UUID_WITHOUT_SIMPLENAME));
1075}
1076
1077TEST_F(AssetCatalogTest, create_catalog_filter_for_unknown_uuid)
1078{
1079 AssetCatalogService service;
1080 const bUUID unknown_uuid = BLI_uuid_generate_random();
1081
1082 AssetCatalogFilter filter = service.create_catalog_filter(unknown_uuid);
1083 EXPECT_TRUE(filter.contains(unknown_uuid));
1084
1085 EXPECT_FALSE(filter.contains(BLI_uuid_nil())) << "Nil catalog should not be included.";
1086 EXPECT_FALSE(filter.contains(UUID_POSES_ELLIE));
1087}
1088
1089TEST_F(AssetCatalogTest, create_catalog_filter_for_unassigned_assets)
1090{
1091 AssetCatalogService service;
1092
1094 EXPECT_TRUE(filter.contains(BLI_uuid_nil()));
1095 EXPECT_FALSE(filter.contains(UUID_POSES_ELLIE));
1096}
1097
1098TEST_F(AssetCatalogTest, cat_collection_deep_copy__empty)
1099{
1100 const AssetCatalogCollection empty;
1101 auto copy = empty.deep_copy();
1102 EXPECT_NE(&empty, copy.get());
1103}
1104
1106 public:
1108 {
1109 return catalogs_;
1110 }
1120 {
1121 catalog_definition_file_ = std::make_unique<AssetCatalogDefinitionFile>(file_path);
1123 }
1124};
1125
1126TEST_F(AssetCatalogTest, cat_collection_deep_copy__nonempty_nocdf)
1127{
1129 auto cat1 = std::make_unique<AssetCatalog>(UUID_POSES_RUZENA, "poses/Henrik", "");
1130 auto cat2 = std::make_unique<AssetCatalog>(UUID_POSES_RUZENA_FACE, "poses/Henrik/face", "");
1131 auto cat3 = std::make_unique<AssetCatalog>(UUID_POSES_RUZENA_HAND, "poses/Henrik/hands", "");
1132 cat3->flags.is_deleted = true;
1133
1134 AssetCatalog *cat1_ptr = cat1.get();
1135 AssetCatalog *cat3_ptr = cat3.get();
1136
1137 catcoll.get_catalogs().add_new(cat1->catalog_id, std::move(cat1));
1138 catcoll.get_catalogs().add_new(cat2->catalog_id, std::move(cat2));
1139 catcoll.get_deleted_catalogs().add_new(cat3->catalog_id, std::move(cat3));
1140
1141 auto copy = catcoll.deep_copy();
1142 EXPECT_NE(&catcoll, copy.get());
1143
1144 TestableAssetCatalogCollection *testcopy = reinterpret_cast<TestableAssetCatalogCollection *>(
1145 copy.get());
1146
1147 /* Test catalogs & deleted catalogs. */
1148 EXPECT_EQ(2, testcopy->get_catalogs().size());
1149 EXPECT_EQ(1, testcopy->get_deleted_catalogs().size());
1150
1151 ASSERT_TRUE(testcopy->get_catalogs().contains(UUID_POSES_RUZENA));
1152 ASSERT_TRUE(testcopy->get_catalogs().contains(UUID_POSES_RUZENA_FACE));
1153 ASSERT_TRUE(testcopy->get_deleted_catalogs().contains(UUID_POSES_RUZENA_HAND));
1154
1155 EXPECT_NE(nullptr, testcopy->get_catalogs().lookup(UUID_POSES_RUZENA));
1156 EXPECT_NE(cat1_ptr, testcopy->get_catalogs().lookup(UUID_POSES_RUZENA).get())
1157 << "AssetCatalogs should be actual copies.";
1158
1159 EXPECT_NE(nullptr, testcopy->get_deleted_catalogs().lookup(UUID_POSES_RUZENA_HAND));
1160 EXPECT_NE(cat3_ptr, testcopy->get_deleted_catalogs().lookup(UUID_POSES_RUZENA_HAND).get())
1161 << "AssetCatalogs should be actual copies.";
1162}
1163
1171
1172TEST_F(AssetCatalogTest, cat_collection_deep_copy__nonempty_cdf)
1173{
1175 auto cat1 = std::make_unique<AssetCatalog>(UUID_POSES_RUZENA, "poses/Henrik", "");
1176 auto cat2 = std::make_unique<AssetCatalog>(UUID_POSES_RUZENA_FACE, "poses/Henrik/face", "");
1177 auto cat3 = std::make_unique<AssetCatalog>(UUID_POSES_RUZENA_HAND, "poses/Henrik/hands", "");
1178 cat3->flags.is_deleted = true;
1179
1180 AssetCatalog *cat1_ptr = cat1.get();
1181 AssetCatalog *cat2_ptr = cat2.get();
1182 AssetCatalog *cat3_ptr = cat3.get();
1183
1184 catcoll.get_catalogs().add_new(cat1->catalog_id, std::move(cat1));
1185 catcoll.get_catalogs().add_new(cat2->catalog_id, std::move(cat2));
1186 catcoll.get_deleted_catalogs().add_new(cat3->catalog_id, std::move(cat3));
1187
1189 "path/to/somewhere.cats.txt");
1190 cdf->add_new(cat1_ptr);
1191 cdf->add_new(cat2_ptr);
1192 cdf->add_new(cat3_ptr);
1193
1194 /* Test CDF remapping. */
1195 auto copy = catcoll.deep_copy();
1196 TestableAssetCatalogCollection *testable_copy = static_cast<TestableAssetCatalogCollection *>(
1197 copy.get());
1198
1200 testable_copy->get_catalog_definition_file());
1201 EXPECT_EQ(testable_copy->get_catalogs().lookup(UUID_POSES_RUZENA).get(),
1202 cdf_copy->get_catalogs().lookup(UUID_POSES_RUZENA))
1203 << "AssetCatalog pointers should have been remapped to the copy.";
1204
1206 cdf_copy->get_catalogs().lookup(UUID_POSES_RUZENA_HAND))
1207 << "Deleted AssetCatalog pointers should have been remapped to the copy.";
1208}
1209
1210TEST_F(AssetCatalogTest, undo_redo_one_step)
1211{
1212 TestableAssetCatalogService service(asset_library_root_);
1213 service.load_from_disk();
1214
1215 EXPECT_FALSE(service.is_undo_possbile());
1216 EXPECT_FALSE(service.is_redo_possbile());
1217
1218 service.create_catalog("some/catalog/path");
1219 EXPECT_FALSE(service.is_undo_possbile())
1220 << "Undo steps should be created explicitly, and not after creating any catalog.";
1221
1222 service.undo_push();
1223 const bUUID other_catalog_id = service.create_catalog("other/catalog/path")->catalog_id;
1224 EXPECT_TRUE(service.is_undo_possbile())
1225 << "Undo should be possible after creating an undo snapshot.";
1226
1227 /* Undo the creation of the catalog. */
1228 service.undo();
1229 EXPECT_FALSE(service.is_undo_possbile())
1230 << "Undoing the only stored step should make it impossible to undo further.";
1231 EXPECT_TRUE(service.is_redo_possbile()) << "Undoing a step should make redo possible.";
1232 EXPECT_EQ(nullptr, service.find_catalog_by_path("other/catalog/path"))
1233 << "Undone catalog should not exist after undo.";
1234 EXPECT_NE(nullptr, service.find_catalog_by_path("some/catalog/path"))
1235 << "First catalog should still exist after undo.";
1236 EXPECT_FALSE(service.get_catalog_definition_file()->contains(other_catalog_id))
1237 << "The CDF should also not contain the undone catalog.";
1238
1239 /* Redo the creation of the catalog. */
1240 service.redo();
1241 EXPECT_TRUE(service.is_undo_possbile())
1242 << "Undoing and then redoing a step should make it possible to undo again.";
1243 EXPECT_FALSE(service.is_redo_possbile())
1244 << "Undoing and then redoing a step should make redo impossible.";
1245 EXPECT_NE(nullptr, service.find_catalog_by_path("other/catalog/path"))
1246 << "Redone catalog should exist after redo.";
1247 EXPECT_NE(nullptr, service.find_catalog_by_path("some/catalog/path"))
1248 << "First catalog should still exist after redo.";
1249 EXPECT_TRUE(service.get_catalog_definition_file()->contains(other_catalog_id))
1250 << "The CDF should contain the redone catalog.";
1251}
1252
1253TEST_F(AssetCatalogTest, undo_redo_more_complex)
1254{
1255 TestableAssetCatalogService service(asset_library_root_);
1256 service.load_from_disk();
1257
1258 service.undo_push();
1259 service.find_catalog(UUID_POSES_ELLIE_WHITESPACE)->simple_name = "Edited simple name";
1260
1261 service.undo_push();
1262 service.find_catalog(UUID_POSES_ELLIE)->path = "poselib/EllieWithEditedPath";
1263
1264 service.undo();
1265 service.undo();
1266
1267 service.undo_push();
1268 service.find_catalog(UUID_POSES_ELLIE)->simple_name = "Ellie Simple";
1269
1270 EXPECT_FALSE(service.is_redo_possbile())
1271 << "After storing an undo snapshot, the redo buffer should be empty.";
1272 EXPECT_TRUE(service.is_undo_possbile())
1273 << "After storing an undo snapshot, undoing should be possible";
1274
1275 EXPECT_EQ(service.find_catalog(UUID_POSES_ELLIE)->simple_name, "Ellie Simple"); /* Not undone. */
1277 "POSES_ELLIE WHITESPACE"); /* Undone. */
1278 EXPECT_EQ(service.find_catalog(UUID_POSES_ELLIE)->path, "character/Ellie/poselib"); /* Undone. */
1279}
1280
1281} // namespace blender::asset_system::tests
struct bUserAssetLibrary * BKE_preferences_asset_library_add(struct UserDef *userdef, const char *name, const char *dirpath) ATTR_NONNULL(1)
void BKE_preferences_asset_library_remove(struct UserDef *userdef, struct bUserAssetLibrary *library) ATTR_NONNULL()
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
File and directory operations.
bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:316
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:350
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:391
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:438
size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:215
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:433
void BLI_path_slash_native(char *path) ATTR_NONNULL(1)
bUUID BLI_uuid_nil(void)
Definition uuid.cc:73
bool BLI_uuid_is_nil(bUUID uuid)
Definition uuid.cc:79
bUUID BLI_uuid_generate_random(void)
Definition uuid.cc:24
unsigned int U
Definition btGjkEpa3.h:78
const Value & lookup(const Key &key) const
Definition BLI_map.hh:506
ValueIterator values() const
Definition BLI_map.hh:846
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:241
int64_t size() const
Definition BLI_map.hh:927
bool is_empty() const
Definition BLI_map.hh:937
bool contains(const Key &key) const
Definition BLI_map.hh:329
std::unique_ptr< AssetCatalogDefinitionFile > catalog_definition_file_
std::unique_ptr< AssetCatalogCollection > deep_copy() const
AssetCatalogFilter create_catalog_filter(CatalogID active_catalog_id) const
AssetCatalog * find_catalog_by_path(const AssetCatalogPath &path) 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 update_catalog_path(CatalogID catalog_id, const AssetCatalogPath &new_catalog_path)
static const CatalogFilePath DEFAULT_CATALOG_FILENAME
const AssetCatalogDefinitionFile * get_catalog_definition_file() const
struct blender::asset_system::AssetCatalog::Flags flags
void save_from_memory_into_existing_asset_lib(const bool should_top_level_cdf_exist)
static void expect_tree_items(const AssetCatalogTree &tree, const std::vector< AssetCatalogPath > &expected_paths)
AssetCatalogDefinitionFile * allocate_catalog_definition_file(StringRef file_path)
const OwningAssetCatalogMap & get_deleted_catalogs() const
TestableAssetCatalogService(const CatalogFilePath &asset_library_root)
const AssetCatalogDefinitionFile * get_catalog_definition_file()
int64_t count_catalogs_with_path(const CatalogFilePath &path)
KDTree_3d * tree
int count
const bUUID UUID_POSES_RUZENA_HAND("81811c31-1a88-4bd7-bb34-c6fc2607a12e")
TEST_F(AssetCatalogTest, load_single_file)
const bUUID UUID_ANOTHER_RUZENA("00000000-d9fa-4b91-b704-e6af1f1339ef")
const bUUID UUID_POSES_RUZENA_FACE("82162c1f-06cc-4d91-a9bf-4f72c104e348")
const bUUID UUID_POSES_ELLIE_TRAILING_SLASH("3376b94b-a28d-4d05-86c1-bf30b937130d")
const bUUID UUID_POSES_ELLIE_BACKSLASHES("a51e17ae-34fc-47d5-ba0f-64c2c9b771f7")
const bUUID UUID_POSES_ELLIE("df60e1f6-2259-475b-93d9-69a1b4a8db78")
const bUUID UUID_WITHOUT_SIMPLENAME("d7916a31-6ca9-4909-955f-182ca2b81fa3")
const bUUID UUID_POSES_RUZENA("79a4f887-ab60-4bd4-94da-d572e27d6aed")
const bUUID UUID_POSES_ELLIE_WHITESPACE("b06132f6-5687-4751-a6dd-392740eb3c46")
const bUUID UUID_ID_WITHOUT_PATH("e34dd2c5-5d2e-4668-9794-1db5de2a4f71")
const bUUID UUID_AGENT_47("c5744ba5-43f5-4f73-8e52-010ad4a61b34")
std::set< const AssetCatalog *, AssetCatalogLessThan > AssetCatalogOrderedSet
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)
__int64 int64_t
Definition stdint.h:89
char catalog_simple_name[64]
Universally Unique Identifier according to RFC4122.
#define SEP_STR
Definition unit.cc:39