#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace XCEngine::Resources; using namespace XCEngine::Containers; namespace Core = XCEngine::Core; 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(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, 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(result.resource); EXPECT_EQ(texture->GetWidth(), 2u); EXPECT_EQ(texture->GetHeight(), 2u); EXPECT_EQ(texture->GetFormat(), TextureFormat::RGBA8_SRGB); 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" / "assets.db")); EXPECT_TRUE(fs::exists(projectRoot / "Library" / "artifacts.db")); EXPECT_TRUE(fs::exists(firstResolve.artifactMainPath.CStr())); const fs::path firstArtifactPath(firstResolve.artifactMainPath.CStr()); ASSERT_TRUE(firstArtifactPath.has_filename()); ASSERT_TRUE(firstArtifactPath.has_parent_path()); EXPECT_EQ(firstArtifactPath.parent_path().filename().string(), firstArtifactPath.stem().string().substr(0, 2)); std::ifstream metaFile(projectRoot / "Assets" / "checker.bmp.meta"); ASSERT_TRUE(metaFile.is_open()); std::string metaText((std::istreambuf_iterator(metaFile)), std::istreambuf_iterator()); 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("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(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); } 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(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); } TEST(TextureLoader, LoadTextureArtifactFromContainerEntryPath) { namespace fs = std::filesystem; const fs::path projectRoot = fs::temp_directory_path() / "xc_texture_container_entry_path_test"; fs::remove_all(projectRoot); fs::create_directories(projectRoot); ArtifactContainerWriter writer; TextureArtifactHeader header; header.textureType = static_cast(TextureType::Texture2D); header.textureFormat = static_cast(TextureFormat::RGBA8_UNORM); header.width = 2; header.height = 2; header.depth = 1; header.mipLevels = 1; header.arraySize = 1; header.pixelDataSize = 16; ArtifactContainerEntry textureEntry; textureEntry.name = "texture_0.xctex"; textureEntry.resourceType = ResourceType::Texture; textureEntry.payload.Resize(sizeof(TextureArtifactHeader) + static_cast(header.pixelDataSize)); std::memcpy(textureEntry.payload.Data(), &header, sizeof(TextureArtifactHeader)); Core::uint8* pixelBytes = textureEntry.payload.Data() + sizeof(TextureArtifactHeader); const Core::uint8 pixelData[16] = { 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 255, 255 }; std::memcpy(pixelBytes, pixelData, sizeof(pixelData)); writer.AddEntry(std::move(textureEntry)); const String containerPath = (projectRoot / "Library" / "Artifacts" / "ab" / "artifact.xcmodel").string().c_str(); String errorMessage; ASSERT_TRUE(writer.WriteToFile(containerPath, &errorMessage)) << errorMessage.CStr(); TextureLoader loader; const String virtualPath = BuildArtifactContainerEntryPath(containerPath, "texture_0.xctex"); LoadResult result = loader.Load(virtualPath); ASSERT_TRUE(result) << result.errorMessage.CStr(); ASSERT_NE(result.resource, nullptr); auto* texture = static_cast(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); EXPECT_EQ(texture->GetPath(), virtualPath); delete texture; fs::remove_all(projectRoot); } } // namespace