From a661e2e6b1d2e5ccf1d4dbe58715cb458e31251e Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 24 Jul 2024 14:55:33 -0400 Subject: [PATCH] add MasterEditor::RegisterTests --- src/app/editor/master_editor.cc | 69 +++++++++++++++++++++++++++++++++ src/app/editor/master_editor.h | 17 +++++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/app/editor/master_editor.cc b/src/app/editor/master_editor.cc index 31f7a622..b53d2cbb 100644 --- a/src/app/editor/master_editor.cc +++ b/src/app/editor/master_editor.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include "absl/status/status.h" #include "app/core/common.h" @@ -421,6 +422,7 @@ void MasterEditor::DrawYazeMenu() { DrawFileMenu(); DrawEditMenu(); DrawViewMenu(); + DrawTestMenu(); DrawProjectMenu(); DrawHelpMenu(); @@ -672,6 +674,73 @@ void MasterEditor::DrawViewMenu() { } } +void MasterEditor::RegisterTests(ImGuiTestEngine* e) { + test_engine = e; + ImGuiTest* t = nullptr; + + t = IM_REGISTER_TEST(e, "master_editor", "open_rom"); + t->GuiFunc = [](ImGuiTestContext* ctx) { + IM_UNUSED(ctx); + ImGui::Begin("Test Window", nullptr, ImGuiWindowFlags_NoSavedSettings); + ImGui::Text("Hello, automation world"); + ImGui::Button("Click Me"); + if (ImGui::TreeNode("Node")) { + static bool b = false; + ImGui::Checkbox("Checkbox", &b); + ImGui::TreePop(); + } + ImGui::End(); + }; + t->TestFunc = [](ImGuiTestContext* ctx) { + ctx->SetRef("Test Window"); + ctx->ItemClick("Click Me"); + ctx->ItemOpen("Node"); // Optional as ItemCheck("Node/Checkbox") can do it + ctx->ItemCheck("Node/Checkbox"); + ctx->ItemUncheck("Node/Checkbox"); + }; + + t = IM_REGISTER_TEST(e, "master_editor", "use_variables"); + struct TestVars2 { + int MyInt = 42; + }; + t->SetVarsDataType(); + t->GuiFunc = [](ImGuiTestContext* ctx) { + TestVars2& vars = ctx->GetVars(); + ImGui::Begin("Test Window", nullptr, ImGuiWindowFlags_NoSavedSettings); + ImGui::SliderInt("Slider", &vars.MyInt, 0, 1000); + ImGui::End(); + }; + t->TestFunc = [](ImGuiTestContext* ctx) { + TestVars2& vars = ctx->GetVars(); + ctx->SetRef("Test Window"); + + IM_CHECK_EQ(vars.MyInt, 42); + ctx->ItemInputValue("Slider", 123); + IM_CHECK_EQ(vars.MyInt, 123); + }; + + t = IM_REGISTER_TEST(e, "master_editor", "open_metrics"); + t->GuiFunc = [](ImGuiTestContext* ctx) { + IM_UNUSED(ctx); + ImGui::ShowMetricsWindow(); + }; + t->TestFunc = [](ImGuiTestContext* ctx) { + ctx->SetRef("Dear ImGui Metrics"); + }; +} + +void MasterEditor::DrawTestMenu() { + static bool show_tests_ = false; + + if (BeginMenu("Tests")) { + MenuItem("Run Tests", nullptr, &show_tests_); + EndMenu(); + } + + if (show_tests_) + ImGuiTestEngine_ShowTestEngineWindows(test_engine, &show_tests_); +} + void MasterEditor::DrawProjectMenu() { static bool show_resource_label_manager = false; diff --git a/src/app/editor/master_editor.h b/src/app/editor/master_editor.h index 0be96af5..b3732116 100644 --- a/src/app/editor/master_editor.h +++ b/src/app/editor/master_editor.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "absl/status/status.h" #include "app/core/common.h" @@ -35,6 +36,16 @@ namespace yaze { namespace app { + +namespace core { +class GuiTestable { + public: + virtual void RegisterTests(ImGuiTestEngine* e) = 0; + + ImGuiTestEngine* test_engine; +}; +} // namespace core + namespace editor { /** @@ -57,7 +68,8 @@ namespace editor { */ class MasterEditor : public SharedRom, public context::GfxContext, - public core::ExperimentFlags { + public core::ExperimentFlags, + public core::GuiTestable { public: MasterEditor() { current_editor_ = &overworld_editor_; @@ -75,6 +87,8 @@ class MasterEditor : public SharedRom, auto emulator() -> emu::Emulator& { return emulator_; } auto quit() { return quit_; } + void RegisterTests(ImGuiTestEngine* e) override; + private: void ManageActiveEditors(); void ManageKeyboardShortcuts(); @@ -89,6 +103,7 @@ class MasterEditor : public SharedRom, void DrawFileMenu(); void DrawEditMenu(); void DrawViewMenu(); + void DrawTestMenu(); void DrawProjectMenu(); void DrawHelpMenu();