2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
* Copyright 2021 Blender Foundation. */
|
2021-11-26 11:06:23 +01:00
|
|
|
#include "testing/testing.h"
|
|
|
|
|
|
|
|
|
|
#include "CLG_log.h"
|
|
|
|
|
|
|
|
|
|
#include "BKE_bpath.h"
|
|
|
|
|
#include "BKE_idtype.h"
|
|
|
|
|
#include "BKE_lib_id.h"
|
|
|
|
|
#include "BKE_main.h"
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_listBase.h"
|
|
|
|
|
#include "DNA_movieclip_types.h"
|
|
|
|
|
#include "DNA_text_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_listbase.h"
|
2021-11-26 17:57:40 +01:00
|
|
|
#include "BLI_path_util.h"
|
2021-11-26 11:06:23 +01:00
|
|
|
#include "BLI_string.h"
|
|
|
|
|
|
|
|
|
|
namespace blender::bke::tests {
|
|
|
|
|
|
2021-11-26 21:34:48 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
|
# define ABSOLUTE_ROOT "C:" SEP_STR
|
|
|
|
|
#else
|
|
|
|
|
# define ABSOLUTE_ROOT SEP_STR
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-11-26 22:07:37 +01:00
|
|
|
#define RELATIVE_ROOT "//"
|
2021-11-26 21:34:48 +01:00
|
|
|
#define BASE_DIR ABSOLUTE_ROOT "blendfiles" SEP_STR
|
|
|
|
|
#define REBASE_DIR BASE_DIR "rebase" SEP_STR
|
|
|
|
|
|
|
|
|
|
#define BLENDFILE_NAME "bpath.blend"
|
|
|
|
|
#define BLENDFILE_PATH BASE_DIR BLENDFILE_NAME
|
|
|
|
|
|
|
|
|
|
#define TEXT_PATH_ITEM "texts" SEP_STR "text.txt"
|
|
|
|
|
#define TEXT_PATH_ABSOLUTE ABSOLUTE_ROOT TEXT_PATH_ITEM
|
2021-11-26 22:39:34 +01:00
|
|
|
#define TEXT_PATH_ABSOLUTE_MADE_RELATIVE RELATIVE_ROOT ".." SEP_STR TEXT_PATH_ITEM
|
2021-11-26 21:34:48 +01:00
|
|
|
#define TEXT_PATH_RELATIVE RELATIVE_ROOT TEXT_PATH_ITEM
|
|
|
|
|
#define TEXT_PATH_RELATIVE_MADE_ABSOLUTE BASE_DIR TEXT_PATH_ITEM
|
|
|
|
|
|
|
|
|
|
#define MOVIECLIP_PATH_ITEM "movieclips" SEP_STR "movieclip.avi"
|
|
|
|
|
#define MOVIECLIP_PATH_ABSOLUTE ABSOLUTE_ROOT MOVIECLIP_PATH_ITEM
|
2021-11-26 22:39:34 +01:00
|
|
|
#define MOVIECLIP_PATH_ABSOLUTE_MADE_RELATIVE RELATIVE_ROOT ".." SEP_STR MOVIECLIP_PATH_ITEM
|
2021-11-26 21:34:48 +01:00
|
|
|
#define MOVIECLIP_PATH_RELATIVE RELATIVE_ROOT MOVIECLIP_PATH_ITEM
|
|
|
|
|
#define MOVIECLIP_PATH_RELATIVE_MADE_ABSOLUTE BASE_DIR MOVIECLIP_PATH_ITEM
|
2021-11-26 11:06:23 +01:00
|
|
|
|
|
|
|
|
class BPathTest : public testing::Test {
|
|
|
|
|
public:
|
|
|
|
|
static void SetUpTestSuite()
|
|
|
|
|
{
|
|
|
|
|
CLG_init();
|
|
|
|
|
BKE_idtype_init();
|
|
|
|
|
}
|
|
|
|
|
static void TearDownTestSuite()
|
|
|
|
|
{
|
|
|
|
|
CLG_exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetUp() override
|
|
|
|
|
{
|
|
|
|
|
bmain = BKE_main_new();
|
2021-12-13 16:22:19 +11:00
|
|
|
STRNCPY(bmain->filepath, BLENDFILE_PATH);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
|
|
|
|
BKE_id_new(bmain, ID_TXT, nullptr);
|
|
|
|
|
BKE_id_new(bmain, ID_MC, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TearDown() override
|
|
|
|
|
{
|
|
|
|
|
BKE_main_free(bmain);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Main *bmain;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_F(BPathTest, rebase_on_relative)
|
|
|
|
|
{
|
2022-03-16 15:19:31 +11:00
|
|
|
/* Test on relative paths, should be modified. */
|
2021-11-26 11:06:23 +01:00
|
|
|
Text *text = reinterpret_cast<Text *>(bmain->texts.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
text->filepath = BLI_strdup(TEXT_PATH_RELATIVE);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
|
|
|
|
MovieClip *movie_clip = reinterpret_cast<MovieClip *>(bmain->movieclips.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
BLI_strncpy(movie_clip->filepath, MOVIECLIP_PATH_RELATIVE, sizeof(movie_clip->filepath));
|
2021-11-26 11:06:23 +01:00
|
|
|
|
2021-11-26 21:34:48 +01:00
|
|
|
BKE_bpath_relative_rebase(bmain, BASE_DIR, REBASE_DIR, nullptr);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
2021-11-26 21:34:48 +01:00
|
|
|
EXPECT_STREQ(text->filepath, RELATIVE_ROOT ".." SEP_STR TEXT_PATH_ITEM);
|
|
|
|
|
EXPECT_STREQ(movie_clip->filepath, RELATIVE_ROOT ".." SEP_STR MOVIECLIP_PATH_ITEM);
|
2021-11-26 11:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(BPathTest, rebase_on_absolute)
|
|
|
|
|
{
|
2022-03-16 15:19:31 +11:00
|
|
|
/* Test on absolute paths, should not be modified. */
|
2021-11-26 11:06:23 +01:00
|
|
|
Text *text = reinterpret_cast<Text *>(bmain->texts.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
text->filepath = BLI_strdup(TEXT_PATH_ABSOLUTE);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
|
|
|
|
MovieClip *movie_clip = reinterpret_cast<MovieClip *>(bmain->movieclips.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
BLI_strncpy(movie_clip->filepath, MOVIECLIP_PATH_ABSOLUTE, sizeof(movie_clip->filepath));
|
2021-11-26 11:06:23 +01:00
|
|
|
|
2021-11-26 21:34:48 +01:00
|
|
|
BKE_bpath_relative_rebase(bmain, BASE_DIR, REBASE_DIR, nullptr);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
2021-11-26 21:34:48 +01:00
|
|
|
EXPECT_STREQ(text->filepath, TEXT_PATH_ABSOLUTE);
|
|
|
|
|
EXPECT_STREQ(movie_clip->filepath, MOVIECLIP_PATH_ABSOLUTE);
|
2021-11-26 11:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(BPathTest, convert_to_relative)
|
|
|
|
|
{
|
|
|
|
|
Text *text = reinterpret_cast<Text *>(bmain->texts.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
text->filepath = BLI_strdup(TEXT_PATH_RELATIVE);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
|
|
|
|
MovieClip *movie_clip = reinterpret_cast<MovieClip *>(bmain->movieclips.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
BLI_strncpy(movie_clip->filepath, MOVIECLIP_PATH_ABSOLUTE, sizeof(movie_clip->filepath));
|
2021-11-26 11:06:23 +01:00
|
|
|
|
2021-11-26 21:34:48 +01:00
|
|
|
BKE_bpath_relative_convert(bmain, BASE_DIR, nullptr);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
2022-03-16 15:19:31 +11:00
|
|
|
/* Already relative path should not be modified. */
|
2021-11-26 21:34:48 +01:00
|
|
|
EXPECT_STREQ(text->filepath, TEXT_PATH_RELATIVE);
|
2022-03-16 15:19:31 +11:00
|
|
|
/* Absolute path should be modified. */
|
2021-11-26 21:34:48 +01:00
|
|
|
EXPECT_STREQ(movie_clip->filepath, MOVIECLIP_PATH_ABSOLUTE_MADE_RELATIVE);
|
2021-11-26 11:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(BPathTest, convert_to_absolute)
|
|
|
|
|
{
|
|
|
|
|
Text *text = reinterpret_cast<Text *>(bmain->texts.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
text->filepath = BLI_strdup(TEXT_PATH_RELATIVE);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
|
|
|
|
MovieClip *movie_clip = reinterpret_cast<MovieClip *>(bmain->movieclips.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
BLI_strncpy(movie_clip->filepath, MOVIECLIP_PATH_ABSOLUTE, sizeof(movie_clip->filepath));
|
2021-11-26 11:06:23 +01:00
|
|
|
|
2021-11-26 21:34:48 +01:00
|
|
|
BKE_bpath_absolute_convert(bmain, BASE_DIR, nullptr);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
2022-03-16 15:19:31 +11:00
|
|
|
/* Relative path should be modified. */
|
2021-11-26 21:34:48 +01:00
|
|
|
EXPECT_STREQ(text->filepath, TEXT_PATH_RELATIVE_MADE_ABSOLUTE);
|
2022-03-16 15:19:31 +11:00
|
|
|
/* Already absolute path should not be modified. */
|
2021-11-26 21:34:48 +01:00
|
|
|
EXPECT_STREQ(movie_clip->filepath, MOVIECLIP_PATH_ABSOLUTE);
|
2021-11-26 11:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(BPathTest, list_backup_restore)
|
|
|
|
|
{
|
|
|
|
|
Text *text = reinterpret_cast<Text *>(bmain->texts.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
text->filepath = BLI_strdup(TEXT_PATH_RELATIVE);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
|
|
|
|
MovieClip *movie_clip = reinterpret_cast<MovieClip *>(bmain->movieclips.first);
|
2021-11-26 21:34:48 +01:00
|
|
|
BLI_strncpy(movie_clip->filepath, MOVIECLIP_PATH_ABSOLUTE, sizeof(movie_clip->filepath));
|
2021-11-26 11:06:23 +01:00
|
|
|
|
Refactor BKE_bpath module.
The main goal of this refactor is to make BPath module use `IDTypeInfo`,
and move each ID-specific part of the `foreach_path` looper into their
own IDTypeInfo struct, using a new `foreach_path` callback.
Additionally, following improvements/cleanups are included:
* Attempt to get better, more consistent namings.
** In particular, move from `path_visitor` to more standard `foreach_path`.
* Update and extend documentation.
** API doc was moved to header, according to recent discussions on this
topic.
* Remove `BKE_bpath_relocate_visitor` from API, this is specific
callback that belongs in `lib_id.c` user code.
NOTE: This commit is expected to be 100% non-behavioral-change. This
implies that several potential further changes were only noted as
comments (like using a more generic solution for
`lib_id_library_local_paths`, addressing inconsistencies like path of
packed libraries always being skipped, regardless of the
`BKE_BPATH_FOREACH_PATH_SKIP_PACKED` `eBPathForeachFlag` flag value,
etc.).
NOTE: basic unittests were added to master already in
rBdcc500e5a265093bc9cc.
Reviewed By: brecht
Differential Revision: https://developer.blender.org/D13381
2021-11-29 14:20:58 +01:00
|
|
|
void *path_list_handle = BKE_bpath_list_backup(bmain, static_cast<eBPathForeachFlag>(0));
|
2021-11-26 11:06:23 +01:00
|
|
|
|
|
|
|
|
ListBase *path_list = reinterpret_cast<ListBase *>(path_list_handle);
|
|
|
|
|
EXPECT_EQ(BLI_listbase_count(path_list), 2);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(text->filepath);
|
2021-11-26 21:34:48 +01:00
|
|
|
text->filepath = BLI_strdup(TEXT_PATH_ABSOLUTE);
|
|
|
|
|
BLI_strncpy(movie_clip->filepath, MOVIECLIP_PATH_RELATIVE, sizeof(movie_clip->filepath));
|
2021-11-26 11:06:23 +01:00
|
|
|
|
Refactor BKE_bpath module.
The main goal of this refactor is to make BPath module use `IDTypeInfo`,
and move each ID-specific part of the `foreach_path` looper into their
own IDTypeInfo struct, using a new `foreach_path` callback.
Additionally, following improvements/cleanups are included:
* Attempt to get better, more consistent namings.
** In particular, move from `path_visitor` to more standard `foreach_path`.
* Update and extend documentation.
** API doc was moved to header, according to recent discussions on this
topic.
* Remove `BKE_bpath_relocate_visitor` from API, this is specific
callback that belongs in `lib_id.c` user code.
NOTE: This commit is expected to be 100% non-behavioral-change. This
implies that several potential further changes were only noted as
comments (like using a more generic solution for
`lib_id_library_local_paths`, addressing inconsistencies like path of
packed libraries always being skipped, regardless of the
`BKE_BPATH_FOREACH_PATH_SKIP_PACKED` `eBPathForeachFlag` flag value,
etc.).
NOTE: basic unittests were added to master already in
rBdcc500e5a265093bc9cc.
Reviewed By: brecht
Differential Revision: https://developer.blender.org/D13381
2021-11-29 14:20:58 +01:00
|
|
|
BKE_bpath_list_restore(bmain, static_cast<eBPathForeachFlag>(0), path_list_handle);
|
2021-11-26 11:06:23 +01:00
|
|
|
|
2021-11-26 21:34:48 +01:00
|
|
|
EXPECT_STREQ(text->filepath, TEXT_PATH_RELATIVE);
|
|
|
|
|
EXPECT_STREQ(movie_clip->filepath, MOVIECLIP_PATH_ABSOLUTE);
|
2021-11-26 11:06:23 +01:00
|
|
|
EXPECT_EQ(BLI_listbase_count(path_list), 0);
|
|
|
|
|
|
|
|
|
|
BKE_bpath_list_free(path_list_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace blender::bke::tests
|