Files
XCEngine/tests/NewEditor/test_imgui_text_atlas_provider.cpp

91 lines
3.0 KiB
C++
Raw Normal View History

2026-04-05 04:55:25 +08:00
#include <gtest/gtest.h>
#include "XCUIBackend/ImGuiTextAtlasProvider.h"
#include <imgui.h>
#include <cstdint>
namespace {
using XCEngine::Editor::XCUIBackend::IXCUITextAtlasProvider;
using XCEngine::Editor::XCUIBackend::ImGuiTextAtlasProvider;
class ImGuiContextScope {
public:
ImGuiContextScope() {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
}
~ImGuiContextScope() {
ImGui::DestroyContext();
}
};
void BuildDefaultFontAtlas() {
ImGuiIO& io = ImGui::GetIO();
if (io.Fonts->Fonts.empty()) {
io.Fonts->AddFontDefault();
}
unsigned char* pixels = nullptr;
int width = 0;
int height = 0;
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
}
TEST(ImGuiTextAtlasProviderTest, ReturnsEmptyResultsWhenNoContextIsAvailable) {
ImGuiTextAtlasProvider provider = {};
IXCUITextAtlasProvider::AtlasTextureView atlasView = {};
IXCUITextAtlasProvider::FontInfo fontInfo = {};
IXCUITextAtlasProvider::BakedFontInfo bakedFontInfo = {};
IXCUITextAtlasProvider::GlyphInfo glyphInfo = {};
EXPECT_EQ(provider.GetContext(), nullptr);
EXPECT_FALSE(provider.GetAtlasTextureView(IXCUITextAtlasProvider::PixelFormat::RGBA32, atlasView));
EXPECT_EQ(provider.GetFontCount(), 0u);
EXPECT_FALSE(provider.GetDefaultFont().IsValid());
EXPECT_FALSE(provider.GetFontInfo({}, fontInfo));
EXPECT_FALSE(provider.GetBakedFontInfo({}, 14.0f, bakedFontInfo));
EXPECT_FALSE(provider.FindGlyph({}, 14.0f, 'A', glyphInfo));
}
TEST(ImGuiTextAtlasProviderTest, ExposesAtlasAndGlyphDataFromExplicitContext) {
ImGuiContextScope contextScope;
BuildDefaultFontAtlas();
ImGuiTextAtlasProvider provider(ImGui::GetCurrentContext());
IXCUITextAtlasProvider::AtlasTextureView atlasView = {};
ASSERT_TRUE(provider.GetAtlasTextureView(IXCUITextAtlasProvider::PixelFormat::RGBA32, atlasView));
EXPECT_TRUE(atlasView.IsValid());
EXPECT_EQ(atlasView.format, IXCUITextAtlasProvider::PixelFormat::RGBA32);
EXPECT_GT(atlasView.atlasStorageKey, 0u);
EXPECT_GT(atlasView.pixelDataKey, 0u);
const IXCUITextAtlasProvider::FontHandle defaultFont = provider.GetDefaultFont();
ASSERT_TRUE(defaultFont.IsValid());
ASSERT_GE(provider.GetFontCount(), 1u);
IXCUITextAtlasProvider::FontInfo fontInfo = {};
ASSERT_TRUE(provider.GetFontInfo(defaultFont, fontInfo));
EXPECT_TRUE(fontInfo.handle.IsValid());
EXPECT_GT(fontInfo.nominalSize, 0.0f);
IXCUITextAtlasProvider::BakedFontInfo bakedFontInfo = {};
ASSERT_TRUE(provider.GetBakedFontInfo(defaultFont, 0.0f, bakedFontInfo));
EXPECT_GT(bakedFontInfo.lineHeight, 0.0f);
EXPECT_GT(bakedFontInfo.rasterizerDensity, 0.0f);
IXCUITextAtlasProvider::GlyphInfo glyphInfo = {};
ASSERT_TRUE(provider.FindGlyph(defaultFont, 0.0f, 'A', glyphInfo));
EXPECT_EQ(glyphInfo.requestedCodepoint, static_cast<std::uint32_t>('A'));
EXPECT_GT(glyphInfo.advanceX, 0.0f);
EXPECT_GE(glyphInfo.u1, glyphInfo.u0);
EXPECT_GE(glyphInfo.v1, glyphInfo.v0);
}
} // namespace