Files
ViewDesignEngine/tests/plugin/test_official_ui_theme.cpp
T

148 lines
5.6 KiB
C++

// P4-43 / P4-59 官方 UI 主题插件测试(PluginManager + 真实 C ABI 动态加载)。
// 覆盖 dark/light/high_contrast/custom 主题、custom colors hex 校验、默认/未知主题、
// version/colors/fonts/line_widths 字段与确定性。
#include <gtest/gtest.h>
#include <string>
#include "vde/plugin/geometry_host_services.h"
#include "vde/plugin/plugin_manager.h"
#include "vde/plugin/vde_plugin_api.h"
#ifndef VDE_PLUGIN_DIRS
#define VDE_PLUGIN_DIRS ""
#endif
namespace {
using vde::plugin::PluginManager;
constexpr const char* kPluginId = "com.vde.official.ui.theme";
constexpr const char* kUiId = "ui.theme";
bool contains(const std::string& text, const std::string& needle) {
return text.find(needle) != std::string::npos;
}
class UiThemeFixture : public ::testing::Test {
protected:
void SetUp() override {
host_ = vde::plugin::make_geometry_host_services();
PluginManager& manager = PluginManager::instance();
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
if (VDE_PLUGIN_DIRS[0] != '\0') manager.add_search_path(VDE_PLUGIN_DIRS);
manager.scan_search_paths();
std::string error;
ASSERT_TRUE(manager.load(kPluginId, &error)) << error;
}
void TearDown() override {
PluginManager::instance().clear();
vde::plugin::release_geometry_host_services(&host_);
}
std::string theme_json(const std::string& options, bool* ok = nullptr) {
std::string json;
std::string error;
const bool result = PluginManager::instance().ui_theme_json(kUiId, options, &json,
&error);
last_error_ = error;
if (ok) *ok = result;
return json;
}
VdePluginHost host_{};
std::string last_error_;
};
TEST_F(UiThemeFixture, Metadata) {
const auto manifest = PluginManager::instance().manifest(kPluginId);
ASSERT_TRUE(manifest.has_value());
EXPECT_EQ(manifest->kind, vde::plugin::PluginKind::Ui);
EXPECT_EQ(manifest->abi_minor, 8);
EXPECT_EQ(PluginManager::instance().ui_plugins().size(), 1u);
EXPECT_EQ(PluginManager::instance().ui_plugins()[0].ui_id, kUiId);
}
TEST_F(UiThemeFixture, DefaultAndNamedThemes) {
bool ok = false;
const std::string default_json = theme_json("", &ok);
ASSERT_TRUE(ok) << last_error_;
EXPECT_TRUE(contains(default_json, "\"theme\":\"dark\"")) << default_json;
EXPECT_TRUE(contains(default_json, "\"version\":1")) << default_json;
EXPECT_TRUE(contains(default_json, "\"colors\"")) << default_json;
EXPECT_TRUE(contains(default_json, "\"fonts\"")) << default_json;
EXPECT_TRUE(contains(default_json, "\"line_widths\"")) << default_json;
EXPECT_TRUE(contains(default_json, "\"border\"")) << default_json;
EXPECT_TRUE(contains(default_json, "\"selection\"")) << default_json;
const std::string light = theme_json("{\"theme\":\"light\"}", &ok);
ASSERT_TRUE(ok) << last_error_;
EXPECT_TRUE(contains(light, "\"theme\":\"light\"")) << light;
EXPECT_TRUE(contains(light, "\"background\":\"#FFFFFF\"")) << light;
const std::string contrast = theme_json("{\"theme\":\"high_contrast\"}", &ok);
ASSERT_TRUE(ok) << last_error_;
EXPECT_TRUE(contains(contrast, "\"theme\":\"high_contrast\"")) << contrast;
EXPECT_TRUE(contains(contrast, "\"background\":\"#000000\"")) << contrast;
EXPECT_TRUE(contains(contrast, "\"accent\":\"#FFFF00\"")) << contrast;
}
TEST_F(UiThemeFixture, CustomThemeColors) {
const std::string options =
"{\"theme\":\"custom\",\"colors\":{\"background\":\"#112233\","
"\"foreground\":\"#AABBCC\",\"accent\":\"#FF0000\"}}";
bool ok = false;
const std::string json = theme_json(options, &ok);
ASSERT_TRUE(ok) << last_error_;
EXPECT_TRUE(contains(json, "\"theme\":\"custom\"")) << json;
EXPECT_TRUE(contains(json, "\"background\":\"#112233\"")) << json;
EXPECT_TRUE(contains(json, "\"foreground\":\"#AABBCC\"")) << json;
EXPECT_TRUE(contains(json, "\"accent\":\"#FF0000\"")) << json;
// 未提供的 border/selection 回退 dark 主题值
EXPECT_TRUE(contains(json, "\"border\":\"#3C3C3C\"")) << json;
EXPECT_TRUE(contains(json, "\"selection\":\"#264F78\"")) << json;
}
TEST_F(UiThemeFixture, InvalidThemeAndCustomColorsRejected) {
bool ok = true;
(void)theme_json("{\"theme\":\"custom\"}", &ok);
EXPECT_FALSE(ok);
EXPECT_FALSE(last_error_.empty());
// PluginManager 对插件负错误码统一包装为 "returned rc=...",具体原因由插件
// set_error 记录;这里断言失败与非空错误信息(插件直接 C ABI 调用见 smoke 验证)。
ok = true;
(void)theme_json("{\"theme\":\"custom\",\"colors\":{\"background\":\"#12345\"}}", &ok);
EXPECT_FALSE(ok);
EXPECT_FALSE(last_error_.empty());
ok = true;
(void)theme_json("{\"theme\":\"custom\",\"colors\":{\"accent\":\"GGGGGG\"}}", &ok);
EXPECT_FALSE(ok);
ok = true;
(void)theme_json("{\"theme\":\"neon\"}", &ok);
EXPECT_FALSE(ok);
EXPECT_FALSE(last_error_.empty());
}
TEST_F(UiThemeFixture, DeterministicOutput) {
bool ok = false;
const std::string first = theme_json("{\"theme\":\"light\"}", &ok);
ASSERT_TRUE(ok) << last_error_;
const std::string second = theme_json("{\"theme\":\"light\"}", &ok);
ASSERT_TRUE(ok) << last_error_;
EXPECT_EQ(first, second);
}
TEST_F(UiThemeFixture, UnknownUiIdRejected) {
std::string json;
std::string error;
EXPECT_FALSE(PluginManager::instance().ui_theme_json("ui.unknown", "", &json, &error));
EXPECT_TRUE(contains(error, "not registered")) << error;
}
} // namespace