Skip to content

Commit

Permalink
TestEngine: fix for menu-bar identifier in 1.91.8 wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed Jan 29, 2025
1 parent d927339 commit 53b9bc3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
2025/01/29:
- TestEngine: fixed issue using MenuXXX() functions with a relative path
when current SetRef() value is empty.
- TestEngine: fix for menu-bar changes in 1.91.8 wip.

2025/01/20:
- TestEngine: added ItemReadAsString() function helpers to facilitate
Expand Down
9 changes: 8 additions & 1 deletion imgui_test_engine/imgui_te_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3380,7 +3380,9 @@ bool ImGuiTestContext::TabBarCompareOrder(ImGuiTabBar* tab_bar, const char**
return true;
}


// Automatically insert "##MenuBar" between window and menus.
// Automatically open and navigate sub-menus
// FIXME: Currently assume that any path after the window are sub-menus.
void ImGuiTestContext::MenuAction(ImGuiTestAction action, ImGuiTestRef ref)
{
if (IsError())
Expand Down Expand Up @@ -3445,8 +3447,13 @@ void ImGuiTestContext::MenuAction(ImGuiTestAction action, ImGuiTestRef ref)
p = path_end;

const bool is_target_item = (p == path_end);
#if IMGUI_VERSION_NUM >= 19174
if (current_window->Flags & ImGuiWindowFlags_MenuBar)
buf.setf("//%s/##MenuBar/%.*s", current_window->Name, (int)(p - path), path); // Click menu in menu bar
#else
if (current_window->Flags & ImGuiWindowFlags_MenuBar)
buf.setf("//%s/##menubar/%.*s", current_window->Name, (int)(p - path), path); // Click menu in menu bar
#endif
else
buf.setf("//%s/%.*s", current_window->Name, (int)(p - path), path); // Click sub menu in its own window

Expand Down
18 changes: 9 additions & 9 deletions imgui_test_suite/imgui_tests_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,24 +1050,24 @@ void RegisterTests_Window(ImGuiTestEngine* e)
IM_CHECK_EQ(vars.SecondOpen, false);

// Test menu opening by hovering a menu item while a menu is already open.
ctx->ItemClick("##menubar/First"); // Click and open first menu.
ctx->ItemClick("##MenuBar/First"); // Click and open first menu.
IM_CHECK_EQ(vars.FirstOpen, true);
IM_CHECK_EQ(vars.SecondOpen, false);
ctx->MouseMove("##menubar/Second", ImGuiTestOpFlags_NoFocusWindow); // Hover and open second menu.
ctx->MouseMove("##MenuBar/Second", ImGuiTestOpFlags_NoFocusWindow); // Hover and open second menu.
IM_CHECK_EQ(vars.FirstOpen, false);
IM_CHECK_EQ(vars.SecondOpen, true);
ctx->MouseMove("##menubar/First", ImGuiTestOpFlags_NoFocusWindow); // Hover and open first menu again.
ctx->MouseMove("##MenuBar/First", ImGuiTestOpFlags_NoFocusWindow); // Hover and open first menu again.
IM_CHECK_EQ(vars.FirstOpen, true);
IM_CHECK_EQ(vars.SecondOpen, false);

// Test closing a menu by clicking menu item that opens this menu (#3496).
//IM_SUSPEND_TESTFUNC();
ctx->ItemClick("##menubar/First", 0, ImGuiTestOpFlags_NoFocusWindow); // Click it again to close.
ctx->ItemClick("##MenuBar/First", 0, ImGuiTestOpFlags_NoFocusWindow); // Click it again to close.
IM_CHECK_EQ(vars.FirstOpen, false);
IM_CHECK_EQ(vars.SecondOpen, false);

// Test closing open menu by clicking popup window body.
ctx->ItemClick("##menubar/First"); // Click and open first menu.
ctx->ItemClick("##MenuBar/First"); // Click and open first menu.
ctx->MouseSetViewport(popup);
ctx->MouseMoveToPos(popup->Rect().GetBR() - ImVec2(20.0f, 20.0f)); // Clicking window outside of menu closes it.
ctx->MouseClick();
Expand All @@ -1085,7 +1085,7 @@ void RegisterTests_Window(ImGuiTestEngine* e)
ctx->WindowMove(popup->ID, window->Rect().GetTR());

// Test closing a menu by clicking popup's parent window body.
ctx->ItemClick("##menubar/First"); // Click and open first menu.
ctx->ItemClick("##MenuBar/First"); // Click and open first menu.
ctx->MouseSetViewport(window);
ctx->MouseMoveToPos(window->Rect().GetBL() + ImVec2(20.0f, -20.0f)); // Clicking parent window of menu popup closes it.
ctx->MouseClick();
Expand All @@ -1105,7 +1105,7 @@ void RegisterTests_Window(ImGuiTestEngine* e)
ctx->ItemClick("//Test Window/Open Menu Popup"); // Reopen popup if it was closed.

// Test closing a menu by clicking on empty space.
ctx->ItemClick("##menubar/First"); // Click and reopen first menu.
ctx->ItemClick("##MenuBar/First"); // Click and reopen first menu.
ctx->MouseClickOnVoid(); // Clicking outside of menu closes it.
#if IMGUI_BROKEN_TESTS
IM_CHECK_EQ(vars.FirstOpen, false);
Expand Down Expand Up @@ -1422,7 +1422,7 @@ void RegisterTests_Window(ImGuiTestEngine* e)
IM_CHECK(window3 != NULL && popup2 != NULL);
IM_CHECK_EQ(g.NavWindow, window3);
IM_CHECK_EQ(popup2->Active, true);
ctx->ItemClick(Str30f("//%s/##menubar/File", popup2->Name).c_str());// FIXME: MenuClick() does not work well with menus inside of a popup.
ctx->ItemClick(Str30f("//%s/##MenuBar/File", popup2->Name).c_str());// FIXME: MenuClick() does not work well with menus inside of a popup.
#ifdef IMGUI_HAS_DOCK
ctx->DockInto("Window3", "Interrupts", ImGuiDir_None, false, ImGuiTestOpFlags_NoFocusWindow | ImGuiTestOpFlags_NoError);
IM_CHECK(ctx->WindowIsUndockedOrStandalone(window3)); // Can not dock into windows that belong to same begin stack, but are below parent popup
Expand Down Expand Up @@ -6551,7 +6551,7 @@ void RegisterTests_TestEngine(ImGuiTestEngine* e)
{
ctx->SetRef("Test Window");
ctx->WindowResize("", ImVec2(50, 200));
ctx->ItemClick("##menubar/Button");
ctx->MenuClick("Button");
ctx->WindowResize("", ImVec2(50, 200));
ctx->MenuClick("SECOND_MENU/Item");
};
Expand Down
6 changes: 3 additions & 3 deletions imgui_test_suite/imgui_tests_nav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ void RegisterTests_Nav(ImGuiTestEngine* e)
IM_CHECK(g.NavLayer == ImGuiNavLayer_Main);
ctx->KeyPress(ImGuiMod_Alt);
IM_CHECK(g.NavLayer == ImGuiNavLayer_Menu);
const char* menu_item_1 = step == 3 ? "#COLLAPSE" : "##menubar/File";
const char* menu_item_2 = step == 3 ? "#CLOSE" : "##menubar/Edit";
const char* menu_item_1 = step == 3 ? "#COLLAPSE" : "##MenuBar/File";
const char* menu_item_2 = step == 3 ? "#CLOSE" : "##MenuBar/Edit";
IM_CHECK_EQ(g.NavId, ctx->GetID(menu_item_1));
ctx->KeyPress(ImGuiKey_RightArrow);
IM_CHECK_EQ(g.NavId, ctx->GetID(menu_item_2));
Expand Down Expand Up @@ -1417,7 +1417,7 @@ void RegisterTests_Nav(ImGuiTestEngine* e)
ctx->LogDebug("Test variant: in menu");
vars.ShowWindows = vars.SetFocus = vars.MenuLayer = true;
ctx->Yield(2);
IM_CHECK_EQ(g.NavId, ctx->GetID("Window/##menubar/Item 2"));
IM_CHECK_EQ(g.NavId, ctx->GetID("Window/##MenuBar/Item 2"));
IM_CHECK(g.NavLayer == ImGuiNavLayer_Menu);
#endif
};
Expand Down
27 changes: 14 additions & 13 deletions imgui_test_suite/imgui_tests_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3628,15 +3628,16 @@ void RegisterTests_Widgets(ImGuiTestEngine* e)
};
t->TestFunc = [](ImGuiTestContext* ctx)
{
// This would likely work with MenuClick("Menu") but going lower-level to ensure we don't mess with focus.
auto& vars = ctx->GetVars<WindowMenuReopenVars>();
ctx->SetRef("Test Window");
ctx->ItemClick("##menubar/Menu", 0, ImGuiTestOpFlags_NoFocusWindow);
ctx->ItemClick("##MenuBar/Menu", 0, ImGuiTestOpFlags_NoFocusWindow);
IM_CHECK(vars.MenuIsVisible);
vars.MenuWasOnceNotVisible = false;
ctx->ItemClick("##menubar/Menu", 0, ImGuiTestOpFlags_NoFocusWindow);
ctx->ItemClick("##MenuBar/Menu", 0, ImGuiTestOpFlags_NoFocusWindow);
IM_CHECK(vars.MenuIsVisible == false);
IM_CHECK(vars.MenuWasOnceNotVisible);
ctx->ItemClick("##menubar/Menu", 0, ImGuiTestOpFlags_NoFocusWindow);
ctx->ItemClick("##MenuBar/Menu", 0, ImGuiTestOpFlags_NoFocusWindow);
IM_CHECK(vars.MenuIsVisible);

ctx->SetRef("//$FOCUSED");
Expand Down Expand Up @@ -3828,9 +3829,9 @@ void RegisterTests_Widgets(ImGuiTestEngine* e)
ctx->SetRef("Test Window");

struct { const char* OpenButton; const char* MenuBase; } test_data[] = {
{ NULL, "##menubar" }, // Test from a window menu bar.
{ "Open Popup", "##menubar" }, // Test from popup menu bar.
{ "Open Modal", "##menubar" }, // Test from modal menu bar.
{ NULL, "##MenuBar" }, // Test from a window menu bar.
{ "Open Popup", "##MenuBar" }, // Test from popup menu bar.
{ "Open Modal", "##MenuBar" }, // Test from modal menu bar.
{ "Open Menu" , "" }, // Test from popup menu.
};

Expand Down Expand Up @@ -3892,7 +3893,7 @@ void RegisterTests_Widgets(ImGuiTestEngine* e)
IM_CHECK_NE(g.OpenPopupStack.back().OpenParentId, popup_parent_id);

// 3. Now do same as 1. except in reverse order.
ctx->MouseMove("##menubar/Examples", ImGuiTestOpFlags_NoFocusWindow | ImGuiTestOpFlags_NoCheckHoveredId);
ctx->MouseMove("##MenuBar/Examples", ImGuiTestOpFlags_NoFocusWindow | ImGuiTestOpFlags_NoCheckHoveredId);
IM_CHECK(g.OpenPopupStack.Size == 0);

// 4. Clicking another menuset should finally open it.
Expand Down Expand Up @@ -4135,7 +4136,7 @@ void RegisterTests_Widgets(ImGuiTestEngine* e)
{
ImGuiContext& g = *ImGui::GetCurrentContext();

ctx->ItemClick("##MainMenuBar##menubar/Menu 1");
ctx->MenuClick("##MainMenuBar/Menu 1");

// Click doesn't affect g.NavId which is null at this point

Expand All @@ -4147,18 +4148,18 @@ void RegisterTests_Widgets(ImGuiTestEngine* e)
ctx->KeyPress(ImGuiKey_LeftArrow);
IM_CHECK_EQ(g.NavId, ctx->GetID("##Menu_00/Sub Menu 1"));
ctx->KeyPress(ImGuiKey_LeftArrow);
IM_CHECK_EQ(g.NavId, ctx->GetID("##MainMenuBar##menubar/Menu 1"));
IM_CHECK_EQ(g.NavId, ctx->GetID("##MainMenuBar##MenuBar/Menu 1"));

ctx->MouseMove("##MainMenuBar##menubar/Menu 2"); // FIXME-TESTS: Workaround so TestEngine can find again Menu 1
ctx->MouseMove("##MainMenuBar##MenuBar/Menu 2"); // FIXME-TESTS: Workaround so TestEngine can find again Menu 1

// Test forwarding of nav event to parent when there's no match in the current menu
// Going from "Menu 1/Sub Menu 1/Item 1" to "Menu 2"
ctx->ItemClick("##MainMenuBar##menubar/Menu 1");
ctx->ItemClick("##MainMenuBar##MenuBar/Menu 1");
ctx->KeyPress(ImGuiKey_DownArrow);
ctx->KeyPress(ImGuiKey_RightArrow);
IM_CHECK_EQ(g.NavId, ctx->GetID("##Menu_01/Item 1"));
ctx->KeyPress(ImGuiKey_RightArrow);
IM_CHECK_EQ(g.NavId, ctx->GetID("##MainMenuBar##menubar/Menu 2"));
IM_CHECK_EQ(g.NavId, ctx->GetID("##MainMenuBar##MenuBar/Menu 2"));

ctx->KeyPress(ImGuiKey_DownArrow);
IM_CHECK_EQ(g.NavId, ctx->GetID("##Menu_00/Sub Menu 2-1"));
Expand Down Expand Up @@ -4228,7 +4229,7 @@ void RegisterTests_Widgets(ImGuiTestEngine* e)
#if IMGUI_BROKEN_TESTS
ctx->MenuClick("Table/InputText2"); // FIXME-TESTS: This doesn't work because "../Table" in MenuAction doesn't resolve to an item, and even if it did it would try to click it.
#else
ctx->ItemClick("##menubar/Table/InputText2");
ctx->ItemClick("##MenuBar/Table/InputText2");
#endif
IM_CHECK_EQ(g.NavLayer, ImGuiNavLayer_Menu);
ctx->KeyPress(ImGuiKey_Escape); // Deactivate InputText
Expand Down

0 comments on commit 53b9bc3

Please sign in to comment.