2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2020 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2021-10-18 12:33:53 +02:00
|
|
|
|
|
|
|
#include "asset_library_service.hh"
|
|
|
|
|
2021-11-01 14:44:18 +01:00
|
|
|
#include "BLI_fileops.h" /* For PATH_MAX (at least on Windows). */
|
2021-10-19 15:22:40 +02:00
|
|
|
#include "BLI_path_util.h"
|
2023-09-01 21:37:11 +02:00
|
|
|
#include "BLI_string.h"
|
2021-10-19 15:22:40 +02:00
|
|
|
|
2021-10-19 18:07:22 +02:00
|
|
|
#include "BKE_appdir.h"
|
2021-10-25 14:21:35 +02:00
|
|
|
#include "BKE_callbacks.h"
|
Asset System: New core type to represent assets (`AssetRepresenation`)
Introduces a new `AssetRepresentation` type, as a runtime only container
to hold asset information. It is supposed to become _the_ main way to
represent and refer to assets in the asset system, see T87235. It can
store things like the asset name, asset traits, preview and other asset
metadata.
Technical documentation:
https://wiki.blender.org/wiki/Source/Architecture/Asset_System/Back_End#Asset_Representation.
By introducing a proper asset representation type, we do an important
step away from the previous, non-optimal representation of assets as
files in the file browser backend, and towards the asset system as
backend. It should replace the temporary & hacky `AssetHandle` design in
the near future. Note that the loading of asset data still happens
through the file browser backend, check the linked to Wiki page for more
information on that.
As a side-effect, asset metadata isn't stored in file browser file
entries when browsing with link/append anymore. Don't think this was
ever used, but scripts may have accessed this. Can be brought back if
there's a need for it.
2022-11-09 18:34:31 +01:00
|
|
|
#include "BKE_main.h"
|
|
|
|
|
|
|
|
#include "DNA_asset_types.h"
|
2021-10-19 18:07:22 +02:00
|
|
|
|
2021-10-18 12:33:53 +02:00
|
|
|
#include "CLG_log.h"
|
|
|
|
|
|
|
|
#include "testing/testing.h"
|
|
|
|
|
2022-11-14 12:41:55 +01:00
|
|
|
namespace blender::asset_system::tests {
|
2021-10-18 12:33:53 +02:00
|
|
|
|
2021-10-22 16:29:31 +02:00
|
|
|
const bUUID UUID_POSES_ELLIE("df60e1f6-2259-475b-93d9-69a1b4a8db78");
|
|
|
|
|
2021-10-18 12:33:53 +02:00
|
|
|
class AssetLibraryServiceTest : public testing::Test {
|
|
|
|
public:
|
|
|
|
CatalogFilePath asset_library_root_;
|
2021-10-19 18:07:22 +02:00
|
|
|
CatalogFilePath temp_library_path_;
|
2021-10-18 12:33:53 +02:00
|
|
|
|
|
|
|
static void SetUpTestSuite()
|
|
|
|
{
|
|
|
|
CLG_init();
|
2021-10-25 14:21:35 +02:00
|
|
|
BKE_callback_global_init();
|
2021-10-18 12:33:53 +02:00
|
|
|
}
|
|
|
|
static void TearDownTestSuite()
|
|
|
|
{
|
|
|
|
CLG_exit();
|
2021-10-25 14:21:35 +02:00
|
|
|
BKE_callback_global_finalize();
|
2021-10-18 12:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetUp() override
|
|
|
|
{
|
|
|
|
const std::string test_files_dir = blender::tests::flags_test_asset_dir();
|
|
|
|
if (test_files_dir.empty()) {
|
|
|
|
FAIL();
|
|
|
|
}
|
2022-10-17 11:50:02 +02:00
|
|
|
asset_library_root_ = test_files_dir + SEP_STR + "asset_library";
|
2021-10-19 18:07:22 +02:00
|
|
|
temp_library_path_ = "";
|
2021-10-18 12:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override
|
|
|
|
{
|
|
|
|
AssetLibraryService::destroy();
|
2021-10-19 18:07:22 +02:00
|
|
|
|
|
|
|
if (!temp_library_path_.empty()) {
|
|
|
|
BLI_delete(temp_library_path_.c_str(), true, true);
|
|
|
|
temp_library_path_ = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register a temporary path, which will be removed at the end of the test.
|
|
|
|
* The returned path ends in a slash. */
|
|
|
|
CatalogFilePath use_temp_path()
|
|
|
|
{
|
|
|
|
BKE_tempdir_init("");
|
|
|
|
const CatalogFilePath tempdir = BKE_tempdir_session();
|
2022-10-17 11:50:02 +02:00
|
|
|
temp_library_path_ = tempdir + "test-temporary-path" + SEP_STR;
|
2021-10-19 18:07:22 +02:00
|
|
|
return temp_library_path_;
|
|
|
|
}
|
|
|
|
|
|
|
|
CatalogFilePath create_temp_path()
|
|
|
|
{
|
|
|
|
CatalogFilePath path = use_temp_path();
|
|
|
|
BLI_dir_create_recursive(path.c_str());
|
|
|
|
return path;
|
2021-10-18 12:33:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(AssetLibraryServiceTest, get_destroy)
|
|
|
|
{
|
|
|
|
AssetLibraryService *const service = AssetLibraryService::get();
|
|
|
|
EXPECT_EQ(service, AssetLibraryService::get())
|
|
|
|
<< "Calling twice without destroying in between should return the same instance.";
|
|
|
|
|
|
|
|
/* This should not crash. */
|
|
|
|
AssetLibraryService::destroy();
|
|
|
|
AssetLibraryService::destroy();
|
2021-10-18 15:45:02 +02:00
|
|
|
|
2021-11-29 23:10:05 +01:00
|
|
|
/* NOTE: there used to be a test for the opposite here, that after a call to
|
2021-10-18 17:29:24 +02:00
|
|
|
* AssetLibraryService::destroy() the above calls should return freshly allocated objects. This
|
|
|
|
* cannot be reliably tested by just pointer comparison, though. */
|
2021-10-18 12:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AssetLibraryServiceTest, library_pointers)
|
|
|
|
{
|
|
|
|
AssetLibraryService *service = AssetLibraryService::get();
|
2023-03-30 12:25:42 +02:00
|
|
|
|
|
|
|
AssetLibrary *const lib = service->get_asset_library_on_disk_custom(__func__,
|
|
|
|
asset_library_root_);
|
2021-10-18 12:33:53 +02:00
|
|
|
AssetLibrary *const curfile_lib = service->get_asset_library_current_file();
|
|
|
|
|
2023-03-30 12:25:42 +02:00
|
|
|
EXPECT_EQ(lib, service->get_asset_library_on_disk_custom(__func__, asset_library_root_))
|
2021-10-18 12:33:53 +02:00
|
|
|
<< "Calling twice without destroying in between should return the same instance.";
|
|
|
|
EXPECT_EQ(curfile_lib, service->get_asset_library_current_file())
|
|
|
|
<< "Calling twice without destroying in between should return the same instance.";
|
|
|
|
|
2021-11-29 23:10:05 +01:00
|
|
|
/* NOTE: there used to be a test for the opposite here, that after a call to
|
2021-10-18 17:29:24 +02:00
|
|
|
* AssetLibraryService::destroy() the above calls should return freshly allocated objects. This
|
|
|
|
* cannot be reliably tested by just pointer comparison, though. */
|
2021-10-18 12:33:53 +02:00
|
|
|
}
|
|
|
|
|
Asset System: New core type to represent assets (`AssetRepresenation`)
Introduces a new `AssetRepresentation` type, as a runtime only container
to hold asset information. It is supposed to become _the_ main way to
represent and refer to assets in the asset system, see T87235. It can
store things like the asset name, asset traits, preview and other asset
metadata.
Technical documentation:
https://wiki.blender.org/wiki/Source/Architecture/Asset_System/Back_End#Asset_Representation.
By introducing a proper asset representation type, we do an important
step away from the previous, non-optimal representation of assets as
files in the file browser backend, and towards the asset system as
backend. It should replace the temporary & hacky `AssetHandle` design in
the near future. Note that the loading of asset data still happens
through the file browser backend, check the linked to Wiki page for more
information on that.
As a side-effect, asset metadata isn't stored in file browser file
entries when browsing with link/append anymore. Don't think this was
ever used, but scripts may have accessed this. Can be brought back if
there's a need for it.
2022-11-09 18:34:31 +01:00
|
|
|
TEST_F(AssetLibraryServiceTest, library_from_reference)
|
|
|
|
{
|
|
|
|
AssetLibraryService *service = AssetLibraryService::get();
|
2023-03-30 12:25:42 +02:00
|
|
|
|
|
|
|
AssetLibrary *const lib = service->get_asset_library_on_disk_custom(__func__,
|
|
|
|
asset_library_root_);
|
Asset System: New core type to represent assets (`AssetRepresenation`)
Introduces a new `AssetRepresentation` type, as a runtime only container
to hold asset information. It is supposed to become _the_ main way to
represent and refer to assets in the asset system, see T87235. It can
store things like the asset name, asset traits, preview and other asset
metadata.
Technical documentation:
https://wiki.blender.org/wiki/Source/Architecture/Asset_System/Back_End#Asset_Representation.
By introducing a proper asset representation type, we do an important
step away from the previous, non-optimal representation of assets as
files in the file browser backend, and towards the asset system as
backend. It should replace the temporary & hacky `AssetHandle` design in
the near future. Note that the loading of asset data still happens
through the file browser backend, check the linked to Wiki page for more
information on that.
As a side-effect, asset metadata isn't stored in file browser file
entries when browsing with link/append anymore. Don't think this was
ever used, but scripts may have accessed this. Can be brought back if
there's a need for it.
2022-11-09 18:34:31 +01:00
|
|
|
AssetLibrary *const curfile_lib = service->get_asset_library_current_file();
|
|
|
|
|
|
|
|
AssetLibraryReference ref{};
|
|
|
|
ref.type = ASSET_LIBRARY_LOCAL;
|
|
|
|
EXPECT_EQ(curfile_lib, service->get_asset_library(nullptr, ref))
|
|
|
|
<< "Getting the local (current file) reference without a main saved on disk should return "
|
|
|
|
"the current file library";
|
|
|
|
|
|
|
|
Main dummy_main{};
|
2022-11-09 22:50:18 +01:00
|
|
|
std::string dummy_filepath = asset_library_root_ + SEP + "dummy.blend";
|
2023-05-09 04:50:37 +02:00
|
|
|
STRNCPY(dummy_main.filepath, dummy_filepath.c_str());
|
Asset System: New core type to represent assets (`AssetRepresenation`)
Introduces a new `AssetRepresentation` type, as a runtime only container
to hold asset information. It is supposed to become _the_ main way to
represent and refer to assets in the asset system, see T87235. It can
store things like the asset name, asset traits, preview and other asset
metadata.
Technical documentation:
https://wiki.blender.org/wiki/Source/Architecture/Asset_System/Back_End#Asset_Representation.
By introducing a proper asset representation type, we do an important
step away from the previous, non-optimal representation of assets as
files in the file browser backend, and towards the asset system as
backend. It should replace the temporary & hacky `AssetHandle` design in
the near future. Note that the loading of asset data still happens
through the file browser backend, check the linked to Wiki page for more
information on that.
As a side-effect, asset metadata isn't stored in file browser file
entries when browsing with link/append anymore. Don't think this was
ever used, but scripts may have accessed this. Can be brought back if
there's a need for it.
2022-11-09 18:34:31 +01:00
|
|
|
EXPECT_EQ(lib, service->get_asset_library(&dummy_main, ref))
|
|
|
|
<< "Getting the local (current file) reference with a main saved on disk should return "
|
|
|
|
"the an asset library for this directory";
|
|
|
|
}
|
|
|
|
|
2021-10-19 15:22:40 +02:00
|
|
|
TEST_F(AssetLibraryServiceTest, library_path_trailing_slashes)
|
|
|
|
{
|
|
|
|
AssetLibraryService *service = AssetLibraryService::get();
|
|
|
|
|
|
|
|
char asset_lib_no_slash[PATH_MAX];
|
|
|
|
char asset_lib_with_slash[PATH_MAX];
|
|
|
|
STRNCPY(asset_lib_no_slash, asset_library_root_.c_str());
|
|
|
|
STRNCPY(asset_lib_with_slash, asset_library_root_.c_str());
|
|
|
|
|
|
|
|
/* Ensure #asset_lib_no_slash has no trailing slash, regardless of what was passed on the CLI to
|
|
|
|
* the unit test. */
|
|
|
|
while (strlen(asset_lib_no_slash) &&
|
|
|
|
ELEM(asset_lib_no_slash[strlen(asset_lib_no_slash) - 1], SEP, ALTSEP))
|
|
|
|
{
|
|
|
|
asset_lib_no_slash[strlen(asset_lib_no_slash) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
2022-10-30 05:34:02 +01:00
|
|
|
BLI_path_slash_ensure(asset_lib_with_slash, PATH_MAX);
|
2021-10-19 15:22:40 +02:00
|
|
|
|
2023-03-30 12:25:42 +02:00
|
|
|
AssetLibrary *const lib_no_slash = service->get_asset_library_on_disk_custom(__func__,
|
|
|
|
asset_lib_no_slash);
|
2021-10-19 15:22:40 +02:00
|
|
|
|
2023-03-30 12:25:42 +02:00
|
|
|
EXPECT_EQ(lib_no_slash,
|
|
|
|
service->get_asset_library_on_disk_custom(__func__, asset_lib_with_slash))
|
2021-10-19 15:22:40 +02:00
|
|
|
<< "With or without trailing slash shouldn't matter.";
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:33:53 +02:00
|
|
|
TEST_F(AssetLibraryServiceTest, catalogs_loaded)
|
|
|
|
{
|
|
|
|
AssetLibraryService *const service = AssetLibraryService::get();
|
2023-03-30 12:25:42 +02:00
|
|
|
AssetLibrary *const lib = service->get_asset_library_on_disk_custom(__func__,
|
|
|
|
asset_library_root_);
|
2021-10-18 12:33:53 +02:00
|
|
|
AssetCatalogService *const cat_service = lib->catalog_service.get();
|
|
|
|
|
|
|
|
const bUUID UUID_POSES_ELLIE("df60e1f6-2259-475b-93d9-69a1b4a8db78");
|
|
|
|
EXPECT_NE(nullptr, cat_service->find_catalog(UUID_POSES_ELLIE))
|
|
|
|
<< "Catalogs should be loaded after getting an asset library from disk.";
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:07:22 +02:00
|
|
|
TEST_F(AssetLibraryServiceTest, has_any_unsaved_catalogs)
|
|
|
|
{
|
|
|
|
AssetLibraryService *const service = AssetLibraryService::get();
|
|
|
|
EXPECT_FALSE(service->has_any_unsaved_catalogs())
|
|
|
|
<< "Empty AssetLibraryService should have no unsaved catalogs";
|
|
|
|
|
2023-03-30 12:25:42 +02:00
|
|
|
AssetLibrary *const lib = service->get_asset_library_on_disk_custom(__func__,
|
|
|
|
asset_library_root_);
|
2021-10-19 18:07:22 +02:00
|
|
|
AssetCatalogService *const cat_service = lib->catalog_service.get();
|
|
|
|
EXPECT_FALSE(service->has_any_unsaved_catalogs())
|
|
|
|
<< "Unchanged AssetLibrary should have no unsaved catalogs";
|
|
|
|
|
|
|
|
const bUUID UUID_POSES_ELLIE("df60e1f6-2259-475b-93d9-69a1b4a8db78");
|
|
|
|
cat_service->prune_catalogs_by_id(UUID_POSES_ELLIE);
|
|
|
|
EXPECT_FALSE(service->has_any_unsaved_catalogs())
|
2021-10-22 16:29:31 +02:00
|
|
|
<< "Deletion of catalogs via AssetCatalogService should not automatically tag as 'unsaved "
|
|
|
|
"changes'.";
|
|
|
|
|
|
|
|
const bUUID UUID_POSES_RUZENA("79a4f887-ab60-4bd4-94da-d572e27d6aed");
|
|
|
|
AssetCatalog *cat = cat_service->find_catalog(UUID_POSES_RUZENA);
|
|
|
|
ASSERT_NE(nullptr, cat) << "Catalog " << UUID_POSES_RUZENA << " should be known";
|
2021-10-19 18:07:22 +02:00
|
|
|
|
2021-10-22 16:29:31 +02:00
|
|
|
cat_service->tag_has_unsaved_changes(cat);
|
2021-10-19 18:07:22 +02:00
|
|
|
EXPECT_TRUE(service->has_any_unsaved_catalogs())
|
|
|
|
<< "Tagging as having unsaved changes of a single catalog service should result in unsaved "
|
|
|
|
"changes being reported.";
|
2021-10-22 16:29:31 +02:00
|
|
|
EXPECT_TRUE(cat->flags.has_unsaved_changes);
|
2021-10-19 18:07:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AssetLibraryServiceTest, has_any_unsaved_catalogs_after_write)
|
|
|
|
{
|
|
|
|
const CatalogFilePath writable_dir = create_temp_path(); /* Has trailing slash. */
|
2022-10-17 11:50:02 +02:00
|
|
|
const CatalogFilePath original_cdf_file = asset_library_root_ + SEP_STR +
|
|
|
|
"blender_assets.cats.txt";
|
2021-10-19 18:07:22 +02:00
|
|
|
CatalogFilePath writable_cdf_file = writable_dir + AssetCatalogService::DEFAULT_CATALOG_FILENAME;
|
|
|
|
BLI_path_slash_native(writable_cdf_file.data());
|
|
|
|
ASSERT_EQ(0, BLI_copy(original_cdf_file.c_str(), writable_cdf_file.c_str()));
|
|
|
|
|
|
|
|
AssetLibraryService *const service = AssetLibraryService::get();
|
2023-03-30 12:25:42 +02:00
|
|
|
AssetLibrary *const lib = service->get_asset_library_on_disk_custom(__func__, writable_dir);
|
2021-10-19 18:07:22 +02:00
|
|
|
|
|
|
|
EXPECT_FALSE(service->has_any_unsaved_catalogs())
|
|
|
|
<< "Unchanged AssetLibrary should have no unsaved catalogs";
|
|
|
|
|
|
|
|
AssetCatalogService *const cat_service = lib->catalog_service.get();
|
2021-10-22 16:29:31 +02:00
|
|
|
AssetCatalog *cat = cat_service->find_catalog(UUID_POSES_ELLIE);
|
|
|
|
|
|
|
|
cat_service->tag_has_unsaved_changes(cat);
|
2021-10-19 18:07:22 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(service->has_any_unsaved_catalogs())
|
|
|
|
<< "Tagging as having unsaved changes of a single catalog service should result in unsaved "
|
|
|
|
"changes being reported.";
|
2021-10-22 16:29:31 +02:00
|
|
|
EXPECT_TRUE(cat->flags.has_unsaved_changes);
|
2021-10-19 18:07:22 +02:00
|
|
|
|
|
|
|
cat_service->write_to_disk(writable_dir + "dummy_path.blend");
|
|
|
|
EXPECT_FALSE(service->has_any_unsaved_catalogs())
|
|
|
|
<< "Written AssetCatalogService should have no unsaved catalogs";
|
2021-10-22 16:29:31 +02:00
|
|
|
EXPECT_FALSE(cat->flags.has_unsaved_changes);
|
2021-10-19 18:07:22 +02:00
|
|
|
}
|
|
|
|
|
2022-11-14 12:41:55 +01:00
|
|
|
} // namespace blender::asset_system::tests
|