#include "CppUnitTest.h" #include "StellarX.h" #include "SxEventLoopInternal.h" #include "SxNativeInputInternal.h" #include "SxTextInternal.h" #include #include #include #include #include #include using namespace Microsoft::VisualStudio::CppUnitTestFramework; namespace { // 轻量测试控件只实现生命周期和事件所需的最小行为,避免把所有权测试绑定到真实绘图。 class FakeControl final : public Control { public: FakeControl() : Control(0, 0, 10, 10) { id = "Fake"; } void draw() override { dirty = false; } bool handleEvent(const ExMessage&) override { return false; } bool model() const override { return false; } }; std::filesystem::path MakeTempLogPath(const char* name) { // 使用进程号隔离并行测试产生的日志路径;测试结束后由用例主动清理。 return std::filesystem::temp_directory_path() / (std::string("stellarx-") + name + "-" + std::to_string(GetCurrentProcessId()) + ".log"); } } namespace StellarXNativeTests { // 验证句柄身份和回调期间的延迟变更不会破坏容器遍历或对象生命周期。 TEST_CLASS(OwnershipTests) { public: TEST_METHOD(HandlesAreUnique) { // 句柄必须与对象地址解耦,才能在对象被销毁后安全地表示原对象身份。 auto first = std::make_unique(); auto second = std::make_unique(); Assert::AreNotEqual(first->getHandle().value, second->getHandle().value); } TEST_METHOD(CallbackCanRemoveCurrentControlSafely) { // 回调删除自身后,当前事件栈仍会继续返回,但不能再次访问已释放控件。 Window window(200, 120, 1); auto button = std::make_unique