Skip to content

Commit f999fe8

Browse files
committed
Tests: Add SliderFloatRange2/SliderIntRange2 tests
Tests for the new SliderRange2 widgets (ocornut/imgui#9164): - Overlapping handles resolution by drag direction - Ctrl+Click text input with "..." separator - Ctrl+Click text input with " - " separator - Handles can't cross constraint - Integer range slider text input
1 parent 3fff435 commit f999fe8

File tree

1 file changed

+169
-1
lines changed

1 file changed

+169
-1
lines changed

imgui_test_suite/imgui_tests_widgets.cpp

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3584,7 +3584,7 @@ void RegisterTests_Widgets(ImGuiTestEngine* e)
35843584
ImGui::LogToClipboard();
35853585
#ifdef __GNUC__
35863586
#pragma GCC diagnostic push
3587-
#pragma GCC diagnostic ignored "-Wformat-overflow" // warning: ‘%s’ directive argument is null
3587+
#pragma GCC diagnostic ignored "-Wformat-overflow" // warning: �%s� directive argument is null
35883588
#endif
35893589
ImGui::Text("%s", (const char*)NULL);
35903590
ImGui::Text("%.*s", 3, (const char*)NULL);
@@ -5882,6 +5882,174 @@ void RegisterTests_Widgets(ImGuiTestEngine* e)
58825882
IM_CHECK_EQ(vars.Float1, 100.0f);
58835883
};
58845884

5885+
#if IMGUI_VERSION_NUM >= 19259
5886+
// ## Test SliderFloatRange2 overlapping handles resolution by drag direction
5887+
t = IM_REGISTER_TEST(e, "widgets", "widgets_slider_range2_overlap_resolve");
5888+
t->GuiFunc = [](ImGuiTestContext* ctx)
5889+
{
5890+
ImGuiTestGenericVars& vars = ctx->GenericVars;
5891+
ImGui::Begin("Test Window", NULL, ImGuiWindowFlags_NoSavedSettings);
5892+
ImGui::SliderFloatRange2("range", &vars.FloatArray[0], &vars.FloatArray[1], 0.0f, 1.0f);
5893+
ImGui::End();
5894+
};
5895+
t->TestFunc = [](ImGuiTestContext* ctx)
5896+
{
5897+
ImGuiTestGenericVars& vars = ctx->GenericVars;
5898+
5899+
// Test dragging right when overlapped
5900+
vars.FloatArray[0] = 0.5f;
5901+
vars.FloatArray[1] = 0.5f;
5902+
ctx->SetRef("Test Window");
5903+
ctx->ItemClick("range");
5904+
ctx->ItemDragWithDelta("range", ImVec2(50, 0));
5905+
IM_CHECK_EQ(vars.FloatArray[0], 0.5f); // Min stays
5906+
IM_CHECK_GT(vars.FloatArray[1], 0.5f); // Max increases (dragged right)
5907+
5908+
// Test dragging left when overlapped
5909+
vars.FloatArray[0] = 0.5f;
5910+
vars.FloatArray[1] = 0.5f;
5911+
ctx->ItemClick("range");
5912+
ctx->ItemDragWithDelta("range", ImVec2(-50, 0));
5913+
IM_CHECK_LT(vars.FloatArray[0], 0.5f); // Min decreases (dragged left)
5914+
IM_CHECK_EQ(vars.FloatArray[1], 0.5f); // Max stays
5915+
};
5916+
5917+
// ## Test SliderFloatRange2 Ctrl+Click text input with "..." separator
5918+
t = IM_REGISTER_TEST(e, "widgets", "widgets_slider_range2_text_input");
5919+
t->GuiFunc = [](ImGuiTestContext* ctx)
5920+
{
5921+
ImGuiTestGenericVars& vars = ctx->GenericVars;
5922+
ImGui::Begin("Test Window", NULL, ImGuiWindowFlags_NoSavedSettings);
5923+
ImGui::SliderFloatRange2("range", &vars.FloatArray[0], &vars.FloatArray[1], 0.0f, 1.0f);
5924+
ImGui::End();
5925+
};
5926+
t->TestFunc = [](ImGuiTestContext* ctx)
5927+
{
5928+
ImGuiTestGenericVars& vars = ctx->GenericVars;
5929+
vars.FloatArray[0] = 0.25f;
5930+
vars.FloatArray[1] = 0.75f;
5931+
5932+
ctx->SetRef("Test Window");
5933+
5934+
// Ctrl+Click to enter text input mode
5935+
ctx->KeyDown(ImGuiMod_Ctrl);
5936+
ctx->ItemClick("range");
5937+
ctx->KeyUp(ImGuiMod_Ctrl);
5938+
5939+
// Type new values with "..." separator
5940+
ctx->KeyCharsReplaceEnter("0.1...0.9");
5941+
5942+
IM_CHECK(ImAbs(vars.FloatArray[0] - 0.1f) < 0.01f);
5943+
IM_CHECK(ImAbs(vars.FloatArray[1] - 0.9f) < 0.01f);
5944+
};
5945+
5946+
// ## Test SliderFloatRange2 text input with " - " separator
5947+
t = IM_REGISTER_TEST(e, "widgets", "widgets_slider_range2_text_input_dash");
5948+
t->GuiFunc = [](ImGuiTestContext* ctx)
5949+
{
5950+
ImGuiTestGenericVars& vars = ctx->GenericVars;
5951+
ImGui::Begin("Test Window", NULL, ImGuiWindowFlags_NoSavedSettings);
5952+
ImGui::SliderFloatRange2("range", &vars.FloatArray[0], &vars.FloatArray[1], 0.0f, 1.0f);
5953+
ImGui::End();
5954+
};
5955+
t->TestFunc = [](ImGuiTestContext* ctx)
5956+
{
5957+
ImGuiTestGenericVars& vars = ctx->GenericVars;
5958+
vars.FloatArray[0] = 0.25f;
5959+
vars.FloatArray[1] = 0.75f;
5960+
5961+
ctx->SetRef("Test Window");
5962+
5963+
ctx->KeyDown(ImGuiMod_Ctrl);
5964+
ctx->ItemClick("range");
5965+
ctx->KeyUp(ImGuiMod_Ctrl);
5966+
5967+
// Type new values with " - " separator
5968+
ctx->KeyCharsReplaceEnter("0.2 - 0.8");
5969+
5970+
IM_CHECK(ImAbs(vars.FloatArray[0] - 0.2f) < 0.01f);
5971+
IM_CHECK(ImAbs(vars.FloatArray[1] - 0.8f) < 0.01f);
5972+
};
5973+
5974+
// ## Test SliderFloatRange2 handles can't cross
5975+
t = IM_REGISTER_TEST(e, "widgets", "widgets_slider_range2_no_cross");
5976+
t->GuiFunc = [](ImGuiTestContext* ctx)
5977+
{
5978+
ImGuiTestGenericVars& vars = ctx->GenericVars;
5979+
ImGui::Begin("Test Window", NULL, ImGuiWindowFlags_NoSavedSettings);
5980+
ImGui::SliderFloatRange2("range", &vars.FloatArray[0], &vars.FloatArray[1], 0.0f, 1.0f);
5981+
ImGui::End();
5982+
};
5983+
t->TestFunc = [](ImGuiTestContext* ctx)
5984+
{
5985+
ImGuiTestGenericVars& vars = ctx->GenericVars;
5986+
vars.FloatArray[0] = 0.4f;
5987+
vars.FloatArray[1] = 0.6f;
5988+
5989+
ctx->SetRef("Test Window");
5990+
5991+
// Try to drag min handle past max
5992+
ctx->ItemClick("range", 0, ImGuiTestOpFlags_MoveToEdgeL);
5993+
ctx->ItemDragWithDelta("range", ImVec2(200, 0));
5994+
5995+
// Min should be clamped to max
5996+
IM_CHECK_LE(vars.FloatArray[0], vars.FloatArray[1]);
5997+
};
5998+
5999+
// ## Test SliderIntRange2 text input
6000+
t = IM_REGISTER_TEST(e, "widgets", "widgets_slider_range2_int");
6001+
t->GuiFunc = [](ImGuiTestContext* ctx)
6002+
{
6003+
ImGuiTestGenericVars& vars = ctx->GenericVars;
6004+
ImGui::Begin("Test Window", NULL, ImGuiWindowFlags_NoSavedSettings);
6005+
ImGui::SliderIntRange2("range", &vars.IntArray[0], &vars.IntArray[1], 0, 100);
6006+
ImGui::End();
6007+
};
6008+
t->TestFunc = [](ImGuiTestContext* ctx)
6009+
{
6010+
ImGuiTestGenericVars& vars = ctx->GenericVars;
6011+
vars.IntArray[0] = 25;
6012+
vars.IntArray[1] = 75;
6013+
6014+
ctx->SetRef("Test Window");
6015+
6016+
ctx->KeyDown(ImGuiMod_Ctrl);
6017+
ctx->ItemClick("range");
6018+
ctx->KeyUp(ImGuiMod_Ctrl);
6019+
ctx->KeyCharsReplaceEnter("10...90");
6020+
6021+
IM_CHECK_EQ(vars.IntArray[0], 10);
6022+
IM_CHECK_EQ(vars.IntArray[1], 90);
6023+
};
6024+
6025+
// ## Test SliderFloat2 with ImGuiSliderFlags_Range flag
6026+
t = IM_REGISTER_TEST(e, "widgets", "widgets_slider_range2_flag");
6027+
t->GuiFunc = [](ImGuiTestContext* ctx)
6028+
{
6029+
ImGuiTestGenericVars& vars = ctx->GenericVars;
6030+
ImGui::Begin("Test Window", NULL, ImGuiWindowFlags_NoSavedSettings);
6031+
ImGui::SliderFloat2("range", vars.FloatArray, 0.0f, 1.0f, "%.3f", ImGuiSliderFlags_Range);
6032+
ImGui::End();
6033+
};
6034+
t->TestFunc = [](ImGuiTestContext* ctx)
6035+
{
6036+
ImGuiTestGenericVars& vars = ctx->GenericVars;
6037+
vars.FloatArray[0] = 0.25f;
6038+
vars.FloatArray[1] = 0.75f;
6039+
6040+
ctx->SetRef("Test Window");
6041+
6042+
// Test text input works via the flag approach
6043+
ctx->KeyDown(ImGuiMod_Ctrl);
6044+
ctx->ItemClick("range");
6045+
ctx->KeyUp(ImGuiMod_Ctrl);
6046+
ctx->KeyCharsReplaceEnter("0.1...0.9");
6047+
6048+
IM_CHECK(ImAbs(vars.FloatArray[0] - 0.1f) < 0.01f);
6049+
IM_CHECK(ImAbs(vars.FloatArray[1] - 0.9f) < 0.01f);
6050+
};
6051+
#endif
6052+
58856053
// ## Test tooltip positioning in various conditions.
58866054
t = IM_REGISTER_TEST(e, "widgets", "widgets_popup_positioning");
58876055
struct TooltipPosVars { ImVec2 Size = ImVec2(50, 50); };

0 commit comments

Comments
 (0)