refactor(tests): reorganize tests directory to match engine structure
- Created tests/Core/Asset/ with tests for IResource, ResourceTypes, ResourceHandle, ResourceCache, ResourceDependencyGraph, ResourceGUID
- Created tests/Core/IO/ with tests for IResourceLoader, ResourcePath, ResourceFileSystem, FileArchive, ResourcePackage
- Reorganized tests/Resources/ into subdirectories: Texture/, Mesh/, Material/, Shader/, AudioClip/
- Added CMakeLists.txt for each new test subdirectory
- Fixed Material.h missing ResourceManager.h include (lost during engine refactor)
- tests/Core/CMakeLists.txt updated to include Asset/ and IO/ subdirectories
2026-03-24 15:44:13 +08:00
|
|
|
#include <gtest/gtest.h>
|
2026-04-02 03:03:36 +08:00
|
|
|
#include <XCEngine/Core/Asset/AssetDatabase.h>
|
|
|
|
|
#include <XCEngine/Core/Asset/AssetRef.h>
|
|
|
|
|
#include <XCEngine/Core/Asset/ResourceManager.h>
|
2026-04-02 22:34:25 +08:00
|
|
|
#include <XCEngine/Resources/Texture/TextureImportSettings.h>
|
refactor(tests): reorganize tests directory to match engine structure
- Created tests/Core/Asset/ with tests for IResource, ResourceTypes, ResourceHandle, ResourceCache, ResourceDependencyGraph, ResourceGUID
- Created tests/Core/IO/ with tests for IResourceLoader, ResourcePath, ResourceFileSystem, FileArchive, ResourcePackage
- Reorganized tests/Resources/ into subdirectories: Texture/, Mesh/, Material/, Shader/, AudioClip/
- Added CMakeLists.txt for each new test subdirectory
- Fixed Material.h missing ResourceManager.h include (lost during engine refactor)
- tests/Core/CMakeLists.txt updated to include Asset/ and IO/ subdirectories
2026-03-24 15:44:13 +08:00
|
|
|
#include <XCEngine/Resources/Texture/TextureLoader.h>
|
|
|
|
|
#include <XCEngine/Core/Asset/ResourceTypes.h>
|
|
|
|
|
#include <XCEngine/Core/Containers/Array.h>
|
2026-04-02 03:03:36 +08:00
|
|
|
|
|
|
|
|
#include <chrono>
|
2026-03-26 16:22:24 +08:00
|
|
|
#include <filesystem>
|
2026-04-02 03:03:36 +08:00
|
|
|
#include <fstream>
|
|
|
|
|
#include <thread>
|
refactor(tests): reorganize tests directory to match engine structure
- Created tests/Core/Asset/ with tests for IResource, ResourceTypes, ResourceHandle, ResourceCache, ResourceDependencyGraph, ResourceGUID
- Created tests/Core/IO/ with tests for IResourceLoader, ResourcePath, ResourceFileSystem, FileArchive, ResourcePackage
- Reorganized tests/Resources/ into subdirectories: Texture/, Mesh/, Material/, Shader/, AudioClip/
- Added CMakeLists.txt for each new test subdirectory
- Fixed Material.h missing ResourceManager.h include (lost during engine refactor)
- tests/Core/CMakeLists.txt updated to include Asset/ and IO/ subdirectories
2026-03-24 15:44:13 +08:00
|
|
|
|
|
|
|
|
using namespace XCEngine::Resources;
|
|
|
|
|
using namespace XCEngine::Containers;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2026-03-26 16:22:24 +08:00
|
|
|
std::string GetTextureFixturePath(const char* fileName) {
|
|
|
|
|
return (std::filesystem::path(XCENGINE_TEST_FIXTURES_DIR) / "Resources" / "Texture" / fileName).string();
|
|
|
|
|
}
|
|
|
|
|
|
refactor(tests): reorganize tests directory to match engine structure
- Created tests/Core/Asset/ with tests for IResource, ResourceTypes, ResourceHandle, ResourceCache, ResourceDependencyGraph, ResourceGUID
- Created tests/Core/IO/ with tests for IResourceLoader, ResourcePath, ResourceFileSystem, FileArchive, ResourcePackage
- Reorganized tests/Resources/ into subdirectories: Texture/, Mesh/, Material/, Shader/, AudioClip/
- Added CMakeLists.txt for each new test subdirectory
- Fixed Material.h missing ResourceManager.h include (lost during engine refactor)
- tests/Core/CMakeLists.txt updated to include Asset/ and IO/ subdirectories
2026-03-24 15:44:13 +08:00
|
|
|
TEST(TextureLoader, GetResourceType) {
|
|
|
|
|
TextureLoader loader;
|
|
|
|
|
EXPECT_EQ(loader.GetResourceType(), ResourceType::Texture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(TextureLoader, GetSupportedExtensions) {
|
|
|
|
|
TextureLoader loader;
|
|
|
|
|
auto extensions = loader.GetSupportedExtensions();
|
|
|
|
|
EXPECT_GE(extensions.Size(), 1u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(TextureLoader, CanLoad) {
|
|
|
|
|
TextureLoader loader;
|
|
|
|
|
EXPECT_TRUE(loader.CanLoad("test.png"));
|
|
|
|
|
EXPECT_TRUE(loader.CanLoad("test.jpg"));
|
|
|
|
|
EXPECT_TRUE(loader.CanLoad("test.tga"));
|
|
|
|
|
EXPECT_FALSE(loader.CanLoad("test.txt"));
|
|
|
|
|
EXPECT_FALSE(loader.CanLoad("test.obj"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(TextureLoader, LoadInvalidPath) {
|
|
|
|
|
TextureLoader loader;
|
|
|
|
|
LoadResult result = loader.Load("invalid/path/texture.png");
|
|
|
|
|
EXPECT_FALSE(result);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 16:22:24 +08:00
|
|
|
TEST(TextureLoader, LoadValidBmpTexture) {
|
|
|
|
|
TextureLoader loader;
|
|
|
|
|
const std::string path = GetTextureFixturePath("checker.bmp");
|
|
|
|
|
|
|
|
|
|
LoadResult result = loader.Load(path.c_str());
|
|
|
|
|
ASSERT_TRUE(result);
|
|
|
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
|
|
|
|
|
|
auto* texture = static_cast<Texture*>(result.resource);
|
|
|
|
|
EXPECT_EQ(texture->GetWidth(), 2u);
|
|
|
|
|
EXPECT_EQ(texture->GetHeight(), 2u);
|
|
|
|
|
EXPECT_EQ(texture->GetTextureType(), TextureType::Texture2D);
|
|
|
|
|
EXPECT_EQ(texture->GetFormat(), TextureFormat::RGBA8_UNORM);
|
|
|
|
|
EXPECT_EQ(texture->GetPixelDataSize(), 16u);
|
|
|
|
|
|
|
|
|
|
delete texture;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 22:34:25 +08:00
|
|
|
TEST(TextureLoader, LoadValidBmpTextureAsSRGBWhenRequested) {
|
|
|
|
|
TextureLoader loader;
|
|
|
|
|
TextureImportSettings settings;
|
|
|
|
|
settings.SetSRGB(true);
|
|
|
|
|
|
|
|
|
|
const std::string path = GetTextureFixturePath("checker.bmp");
|
|
|
|
|
LoadResult result = loader.Load(path.c_str(), &settings);
|
|
|
|
|
ASSERT_TRUE(result);
|
|
|
|
|
ASSERT_NE(result.resource, nullptr);
|
|
|
|
|
|
|
|
|
|
auto* texture = static_cast<Texture*>(result.resource);
|
|
|
|
|
EXPECT_EQ(texture->GetWidth(), 2u);
|
|
|
|
|
EXPECT_EQ(texture->GetHeight(), 2u);
|
|
|
|
|
EXPECT_EQ(texture->GetFormat(), TextureFormat::RGBA8_SRGB);
|
|
|
|
|
|
|
|
|
|
delete texture;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 03:03:36 +08:00
|
|
|
TEST(TextureLoader, AssetDatabaseCreatesTextureArtifactAndReusesItWithoutReimport) {
|
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
|
|
|
|
|
const fs::path projectRoot = fs::temp_directory_path() / "xc_texture_library_cache_test";
|
|
|
|
|
const fs::path assetsDir = projectRoot / "Assets";
|
|
|
|
|
const fs::path texturePath = assetsDir / "checker.bmp";
|
|
|
|
|
|
|
|
|
|
fs::remove_all(projectRoot);
|
|
|
|
|
fs::create_directories(assetsDir);
|
|
|
|
|
fs::copy_file(GetTextureFixturePath("checker.bmp"), texturePath, fs::copy_options::overwrite_existing);
|
|
|
|
|
|
|
|
|
|
AssetDatabase database;
|
|
|
|
|
database.Initialize(projectRoot.string().c_str());
|
|
|
|
|
|
|
|
|
|
AssetDatabase::ResolvedAsset firstResolve;
|
|
|
|
|
ASSERT_TRUE(database.EnsureArtifact("Assets/checker.bmp", ResourceType::Texture, firstResolve));
|
|
|
|
|
ASSERT_TRUE(firstResolve.exists);
|
|
|
|
|
ASSERT_TRUE(firstResolve.artifactReady);
|
|
|
|
|
EXPECT_TRUE(fs::exists(projectRoot / "Assets" / "checker.bmp.meta"));
|
|
|
|
|
EXPECT_TRUE(fs::exists(projectRoot / "Library" / "SourceAssetDB" / "assets.db"));
|
|
|
|
|
EXPECT_TRUE(fs::exists(projectRoot / "Library" / "ArtifactDB" / "artifacts.db"));
|
|
|
|
|
EXPECT_TRUE(fs::exists(firstResolve.artifactMainPath.CStr()));
|
|
|
|
|
|
|
|
|
|
std::ifstream metaFile(projectRoot / "Assets" / "checker.bmp.meta");
|
|
|
|
|
ASSERT_TRUE(metaFile.is_open());
|
|
|
|
|
std::string metaText((std::istreambuf_iterator<char>(metaFile)), std::istreambuf_iterator<char>());
|
|
|
|
|
EXPECT_NE(metaText.find("guid:"), std::string::npos);
|
|
|
|
|
metaFile.close();
|
|
|
|
|
|
|
|
|
|
AssetRef assetRef;
|
|
|
|
|
ASSERT_TRUE(database.TryGetAssetRef("Assets/checker.bmp", ResourceType::Texture, assetRef));
|
|
|
|
|
EXPECT_TRUE(assetRef.IsValid());
|
|
|
|
|
|
|
|
|
|
const auto originalArtifactWriteTime = fs::last_write_time(firstResolve.artifactMainPath.CStr());
|
|
|
|
|
std::this_thread::sleep_for(50ms);
|
|
|
|
|
|
|
|
|
|
AssetDatabase::ResolvedAsset secondResolve;
|
|
|
|
|
ASSERT_TRUE(database.EnsureArtifact("Assets/checker.bmp", ResourceType::Texture, secondResolve));
|
|
|
|
|
EXPECT_EQ(firstResolve.artifactMainPath, secondResolve.artifactMainPath);
|
|
|
|
|
EXPECT_EQ(originalArtifactWriteTime, fs::last_write_time(secondResolve.artifactMainPath.CStr()));
|
|
|
|
|
|
|
|
|
|
database.Shutdown();
|
|
|
|
|
fs::remove_all(projectRoot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(TextureLoader, ResourceManagerLoadsTextureByAssetRefFromProjectAssets) {
|
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
|
|
ResourceManager& manager = ResourceManager::Get();
|
|
|
|
|
manager.Initialize();
|
|
|
|
|
|
|
|
|
|
const fs::path projectRoot = fs::temp_directory_path() / "xc_texture_asset_ref_test";
|
|
|
|
|
const fs::path assetsDir = projectRoot / "Assets";
|
|
|
|
|
const fs::path texturePath = assetsDir / "checker.bmp";
|
|
|
|
|
|
|
|
|
|
fs::remove_all(projectRoot);
|
|
|
|
|
fs::create_directories(assetsDir);
|
|
|
|
|
fs::copy_file(GetTextureFixturePath("checker.bmp"), texturePath, fs::copy_options::overwrite_existing);
|
|
|
|
|
|
|
|
|
|
manager.SetResourceRoot(projectRoot.string().c_str());
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
const auto firstHandle = manager.Load<Texture>("Assets/checker.bmp");
|
|
|
|
|
ASSERT_TRUE(firstHandle.IsValid());
|
|
|
|
|
EXPECT_EQ(firstHandle->GetWidth(), 2u);
|
|
|
|
|
EXPECT_EQ(firstHandle->GetHeight(), 2u);
|
|
|
|
|
|
|
|
|
|
AssetRef assetRef;
|
|
|
|
|
ASSERT_TRUE(manager.TryGetAssetRef("Assets/checker.bmp", ResourceType::Texture, assetRef));
|
|
|
|
|
EXPECT_TRUE(assetRef.IsValid());
|
|
|
|
|
|
|
|
|
|
manager.UnloadAll();
|
|
|
|
|
|
|
|
|
|
const auto secondHandle = manager.Load<Texture>(assetRef);
|
|
|
|
|
ASSERT_TRUE(secondHandle.IsValid());
|
|
|
|
|
EXPECT_EQ(secondHandle->GetWidth(), 2u);
|
|
|
|
|
EXPECT_EQ(secondHandle->GetHeight(), 2u);
|
|
|
|
|
EXPECT_EQ(secondHandle->GetPath(), "Assets/checker.bmp");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
manager.SetResourceRoot("");
|
|
|
|
|
manager.Shutdown();
|
|
|
|
|
fs::remove_all(projectRoot);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 19:44:59 +08:00
|
|
|
TEST(TextureLoader, ResourceManagerLoadsLibraryArtifactTextureWithoutReimportingIt) {
|
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
|
|
ResourceManager& manager = ResourceManager::Get();
|
|
|
|
|
manager.Initialize();
|
|
|
|
|
|
|
|
|
|
const fs::path projectRoot = fs::temp_directory_path() / "xc_texture_library_artifact_direct_load_test";
|
|
|
|
|
const fs::path assetsDir = projectRoot / "Assets";
|
|
|
|
|
const fs::path texturePath = assetsDir / "checker.bmp";
|
|
|
|
|
|
|
|
|
|
fs::remove_all(projectRoot);
|
|
|
|
|
fs::create_directories(assetsDir);
|
|
|
|
|
fs::copy_file(GetTextureFixturePath("checker.bmp"), texturePath, fs::copy_options::overwrite_existing);
|
|
|
|
|
|
|
|
|
|
manager.SetResourceRoot(projectRoot.string().c_str());
|
|
|
|
|
const AssetImportService::ImportStatusSnapshot bootstrapStatus = manager.GetProjectAssetImportStatus();
|
|
|
|
|
EXPECT_TRUE(bootstrapStatus.HasValue());
|
|
|
|
|
EXPECT_TRUE(bootstrapStatus.success);
|
|
|
|
|
EXPECT_EQ(std::string(bootstrapStatus.operation.CStr()), "Bootstrap Project");
|
|
|
|
|
EXPECT_GT(bootstrapStatus.startedAtMs, 0u);
|
|
|
|
|
EXPECT_GE(bootstrapStatus.completedAtMs, bootstrapStatus.startedAtMs);
|
|
|
|
|
EXPECT_EQ(bootstrapStatus.durationMs, bootstrapStatus.completedAtMs - bootstrapStatus.startedAtMs);
|
|
|
|
|
|
|
|
|
|
AssetDatabase database;
|
|
|
|
|
database.Initialize(projectRoot.string().c_str());
|
|
|
|
|
|
|
|
|
|
AssetDatabase::ResolvedAsset resolvedAsset;
|
|
|
|
|
ASSERT_TRUE(database.EnsureArtifact("Assets/checker.bmp", ResourceType::Texture, resolvedAsset));
|
|
|
|
|
ASSERT_TRUE(resolvedAsset.artifactReady);
|
|
|
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
|
const fs::path relativeArtifactPath =
|
|
|
|
|
fs::relative(fs::path(resolvedAsset.artifactMainPath.CStr()), projectRoot, ec);
|
|
|
|
|
ASSERT_FALSE(ec);
|
|
|
|
|
ASSERT_FALSE(relativeArtifactPath.empty());
|
|
|
|
|
|
|
|
|
|
const auto textureHandle = manager.Load<Texture>(relativeArtifactPath.generic_string().c_str());
|
|
|
|
|
ASSERT_TRUE(textureHandle.IsValid());
|
|
|
|
|
EXPECT_EQ(textureHandle->GetWidth(), 2u);
|
|
|
|
|
EXPECT_EQ(textureHandle->GetHeight(), 2u);
|
|
|
|
|
EXPECT_EQ(textureHandle->GetPath(), relativeArtifactPath.generic_string().c_str());
|
|
|
|
|
const AssetImportService::ImportStatusSnapshot postLoadStatus = manager.GetProjectAssetImportStatus();
|
|
|
|
|
EXPECT_TRUE(postLoadStatus.HasValue());
|
|
|
|
|
EXPECT_EQ(std::string(postLoadStatus.operation.CStr()), "Bootstrap Project");
|
|
|
|
|
EXPECT_GT(postLoadStatus.startedAtMs, 0u);
|
|
|
|
|
EXPECT_GE(postLoadStatus.completedAtMs, postLoadStatus.startedAtMs);
|
|
|
|
|
EXPECT_EQ(postLoadStatus.durationMs, postLoadStatus.completedAtMs - postLoadStatus.startedAtMs);
|
|
|
|
|
|
|
|
|
|
database.Shutdown();
|
|
|
|
|
manager.SetResourceRoot("");
|
|
|
|
|
manager.Shutdown();
|
|
|
|
|
fs::remove_all(projectRoot);
|
|
|
|
|
}
|
|
|
|
|
|
refactor(tests): reorganize tests directory to match engine structure
- Created tests/Core/Asset/ with tests for IResource, ResourceTypes, ResourceHandle, ResourceCache, ResourceDependencyGraph, ResourceGUID
- Created tests/Core/IO/ with tests for IResourceLoader, ResourcePath, ResourceFileSystem, FileArchive, ResourcePackage
- Reorganized tests/Resources/ into subdirectories: Texture/, Mesh/, Material/, Shader/, AudioClip/
- Added CMakeLists.txt for each new test subdirectory
- Fixed Material.h missing ResourceManager.h include (lost during engine refactor)
- tests/Core/CMakeLists.txt updated to include Asset/ and IO/ subdirectories
2026-03-24 15:44:13 +08:00
|
|
|
} // namespace
|