Files
XCEngine/tests/Resources/Texture/test_texture_loader.cpp

154 lines
5.5 KiB
C++

#include <gtest/gtest.h>
#include <XCEngine/Core/Asset/AssetDatabase.h>
#include <XCEngine/Core/Asset/AssetRef.h>
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <XCEngine/Resources/Texture/TextureLoader.h>
#include <XCEngine/Core/Asset/ResourceTypes.h>
#include <XCEngine/Core/Containers/Array.h>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <thread>
using namespace XCEngine::Resources;
using namespace XCEngine::Containers;
namespace {
std::string GetTextureFixturePath(const char* fileName) {
return (std::filesystem::path(XCENGINE_TEST_FIXTURES_DIR) / "Resources" / "Texture" / fileName).string();
}
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);
}
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;
}
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);
}
} // namespace