Skip to content

Commit

Permalink
tests: Add graphical unit-tests for the keypad
Browse files Browse the repository at this point in the history
also some basic interaction like clicking on keys

Signed-off-by: Sergio Martins <[email protected]>
  • Loading branch information
iamsergio committed Nov 7, 2024
1 parent b3b1052 commit e1b6859
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ui/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ if(ENABLE_IMGUI_TEST_ENGINE)
add_imgui_test(qa_popup_menu)
add_imgui_test(qa_dialog)
add_imgui_test(qa_filtercomboboxes)
add_imgui_test(qa_keypad)

endif()
81 changes: 81 additions & 0 deletions src/ui/test/qa_keypad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "ImGuiTestApp.hpp"
#include "components/Keypad.hpp"
#include "imgui.h"

#include "imgui_test_engine/imgui_te_context.h"
#include "imgui_test_engine/imgui_te_internal.h"

#include <boost/ut.hpp>

using namespace boost;

class TestApp : public DigitizerUi::test::ImGuiTestApp {
private:
using DigitizerUi::test::ImGuiTestApp::ImGuiTestApp;

void registerTests() override {
struct TestState {
bool edited = false;
int value = 0;
};

ImGuiTest* t = IM_REGISTER_TEST(engine(), "keypad", "keypad visual test");
t->SetVarsDataType<TestState>();

t->GuiFunc = [](ImGuiTestContext* ctx) {
ImGui::Begin("Test Window", NULL, ImGuiWindowFlags_NoSavedSettings);
ImGui::SetWindowSize(ImVec2(300, 300));

auto& vars = ctx->GetVars<TestState>();
if (DigitizerUi::components::InputKeypad<>::edit("label", &vars.value)) {
// do not override with false every frame
vars.edited = true;
}

ImGui::End();
};

t->TestFunc = [](ImGuiTestContext* ctx) {
ut::test("keypad visual test") = [ctx] {
ImGuiContext& g = *GImGui;

// keypad isn't visible yet
ut::expect(g.OpenPopupStack.empty());

ctx->SetRef("Test Window");
ctx->ItemClick("label");

ut::expect(g.OpenPopupStack.size() == 1);
auto keypadWindow = ImGui::FindWindowByID(g.OpenPopupStack[0].Window->ID);

captureScreenshot(*ctx, keypadWindow->ID);

// nothing edited yet
auto& vars = ctx->GetVars<TestState>();
ut::expect(vars.value == 0);
ut::expect(!vars.edited);

// KeyPads buttons are actually in a child window
auto subWindowInfo = ctx->WindowInfo("//KeypadX/drawKeypad Input");
ctx->SetRef(subWindowInfo.Window->ID);

// press some buttons
ctx->ItemClick("9");
ctx->ItemClick("Enter");
ut::expect(vars.value == 9);
ut::expect(vars.edited);

// no keypad now
ut::expect(g.OpenPopupStack.empty());
};
};
}
};

int main(int argc, char* argv[]) {
auto options = DigitizerUi::test::TestOptions::fromArgs(argc, argv);
options.screenshotPrefix = "keypad";

TestApp app(options);
return app.runTests() ? 0 : 1;
}

0 comments on commit e1b6859

Please sign in to comment.