Skip to content

Commit 5776d80

Browse files
committed
test: improve component coverage
1 parent f91be20 commit 5776d80

2 files changed

Lines changed: 207 additions & 210 deletions

File tree

tests/ui/test_knob.cpp

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ static inline void advance_frame() {
2222
ImGui::Begin("TestWindow");
2323
}
2424

25+
static ImGuiID get_popup_item_id(const char* popup_id_substr, const char* item_id_str) {
26+
ImGuiContext& g = *GImGui;
27+
std::cout << "DEBUG: Searching for " << popup_id_substr << ", item: " << item_id_str << "\n";
28+
for (int i = 0; i < g.Windows.Size; i++) {
29+
std::cout << "DEBUG: Window name: " << g.Windows[i]->Name << "\n";
30+
if (strstr(g.Windows[i]->Name, popup_id_substr)) {
31+
ImGuiID id = g.Windows[i]->GetID(item_id_str);
32+
std::cout << "DEBUG: Match found! ID is " << id << "\n";
33+
return id;
34+
}
35+
}
36+
std::cout << "DEBUG: No match found!\n";
37+
return 0;
38+
}
39+
40+
2541
TEST_F(PresetTest, test_knob_component_comprehensive) {
2642
ScopedImGuiContext imgui;
2743

@@ -630,7 +646,6 @@ TEST_F(PresetTest, KnobComponent_PopupSliderInteraction) {
630646
ImGui::SetNextWindowSize(ImVec2(1024, 768));
631647
ImGui::Begin("TestWindow");
632648

633-
634649
float val = 50.0f;
635650
float commit_old = 0.0f;
636651
float commit_new = 0.0f;
@@ -651,25 +666,25 @@ TEST_F(PresetTest, KnobComponent_PopupSliderInteraction) {
651666
advance_frame();
652667

653668
// Frame 3: Activate the slider inside popup
654-
ImGui::PushID("Popup_KPopSlider");
655-
ImGuiID slider_id = ImGui::GetID("##edit");
656-
ImGui::PopID();
669+
ImGuiID slider_id = get_popup_item_id("Popup_KPopSlider", "##edit");
657670

658671
ImGuiContext& g = *GImGui;
659672
g.ActiveId = slider_id;
660-
g.ActiveIdSource = ImGuiInputSource_Mouse;
673+
g.ActiveIdSource = ImGuiInputSource_Keyboard;
661674
g.ActiveIdIsJustActivated = true;
662-
props.value = 60.0f; // edit value
663675

676+
ImGui::OpenPopup("Popup_KPopSlider");
664677
KnobComponent::render("KPopSlider", props, 1.0f, center);
665678
advance_frame();
666679

667680
// Frame 4: Deactivate the slider with edit flag set
668-
g.ActiveIdPreviousFrame = slider_id;
669681
g.ActiveId = 0;
682+
g.ActiveIdPreviousFrame = slider_id;
670683
g.ActiveIdPreviousFrameHasBeenEditedBefore = true;
671684
props.value = 70.0f; // final value
685+
val = 70.0f;
672686

687+
ImGui::OpenPopup("Popup_KPopSlider");
673688
KnobComponent::render("KPopSlider", props, 1.0f, center);
674689
advance_frame();
675690

@@ -694,29 +709,25 @@ TEST_F(PresetTest, KnobComponent_PopupResetButton) {
694709
props.on_value_committed = [&](float o, float n) { commit_old = o; commit_new = n; };
695710

696711
ImVec2 center(200, 200);
697-
ImGuiIO& io = ImGui::GetIO();
698712

699713
// Frame 1: Render normally
700714
KnobComponent::render("KPopReset", props, 1.0f, center);
701715
advance_frame();
702716

703-
// Frame 2: Right click to open popup
704-
io.MousePos = center;
705-
io.MouseClicked[1] = true;
717+
// Frame 2: Open popup manually
718+
ImGui::OpenPopup("Popup_KPopReset");
706719
KnobComponent::render("KPopReset", props, 1.0f, center);
707720
advance_frame();
708-
io.MouseClicked[1] = false;
709721

710-
// Frame 3: Click Reset button
711-
io.MousePos = ImVec2(250, 260);
712-
io.MouseDown[0] = true;
713-
io.MouseClicked[0] = true;
714-
KnobComponent::render("KPopReset", props, 1.0f, center);
715-
advance_frame();
722+
// Frame 3: Trigger Reset button click programmatically
723+
ImGuiID reset_id = get_popup_item_id("Popup_KPopReset", "Reset");
716724

717-
// Frame 4: Release button
718-
io.MouseDown[0] = false;
719-
io.MouseClicked[0] = false;
725+
ImGuiContext& g = *GImGui;
726+
g.NavActivateId = reset_id;
727+
g.NavActivateDownId = reset_id;
728+
g.NavActivatePressedId = reset_id;
729+
730+
ImGui::OpenPopup("Popup_KPopReset");
720731
KnobComponent::render("KPopReset", props, 1.0f, center);
721732
advance_frame();
722733

@@ -746,76 +757,68 @@ TEST_F(PresetTest, KnobComponent_PopupMidiLearnItems) {
746757
props.midi_info = "";
747758

748759
ImVec2 center(200, 200);
749-
ImGuiIO& io = ImGui::GetIO();
760+
ImGuiContext& g = *GImGui;
750761

751-
// Frame 1: Render normally
762+
// --- 1. Test "MIDI Learn Parameter" ---
763+
ImGui::OpenPopup("Popup_KMidi");
752764
KnobComponent::render("KMidi", props, 1.0f, center);
753765
advance_frame();
754766

755-
// 1. Click "MIDI Learn Parameter"
756-
io.MousePos = center;
757-
io.MouseClicked[1] = true;
758-
KnobComponent::render("KMidi", props, 1.0f, center);
759-
advance_frame();
760-
io.MouseClicked[1] = false;
767+
ImGuiID learn_p_id = get_popup_item_id("Popup_KMidi", "MIDI Learn Parameter");
761768

762-
io.MousePos = ImVec2(220, 300);
763-
io.MouseDown[0] = true;
764-
io.MouseClicked[0] = true;
769+
g.NavActivateId = learn_p_id;
770+
g.NavActivateDownId = learn_p_id;
771+
g.NavActivatePressedId = learn_p_id;
772+
773+
ImGui::OpenPopup("Popup_KMidi");
765774
KnobComponent::render("KMidi", props, 1.0f, center);
766775
advance_frame();
767-
io.MouseDown[0] = false;
768-
io.MouseClicked[0] = false;
769-
advance_frame();
770776

771-
// 2. Click "MIDI Learn Bypass Toggle"
772-
io.MousePos = center;
773-
io.MouseClicked[1] = true;
777+
// --- 2. Test "MIDI Learn Bypass Toggle" ---
778+
ImGui::OpenPopup("Popup_KMidi");
774779
KnobComponent::render("KMidi", props, 1.0f, center);
775780
advance_frame();
776-
io.MouseClicked[1] = false;
777781

778-
io.MousePos = ImVec2(220, 325);
779-
io.MouseDown[0] = true;
780-
io.MouseClicked[0] = true;
782+
ImGuiID learn_b_id = get_popup_item_id("Popup_KMidi", "MIDI Learn Bypass Toggle");
783+
784+
g.NavActivateId = learn_b_id;
785+
g.NavActivateDownId = learn_b_id;
786+
g.NavActivatePressedId = learn_b_id;
787+
788+
ImGui::OpenPopup("Popup_KMidi");
781789
KnobComponent::render("KMidi", props, 1.0f, center);
782790
advance_frame();
783-
io.MouseDown[0] = false;
784-
io.MouseClicked[0] = false;
785-
advance_frame();
786791

787-
// 3. Click "Remove MIDI Mapping" and "Remove Bypass Mapping"
792+
// --- 3. Test "Remove MIDI Mapping" ---
788793
props.midi_info = "CC 7";
789-
io.MousePos = center;
790-
io.MouseClicked[1] = true;
794+
ImGui::OpenPopup("Popup_KMidi");
791795
KnobComponent::render("KMidi", props, 1.0f, center);
792796
advance_frame();
793-
io.MouseClicked[1] = false;
794797

795-
io.MousePos = ImVec2(220, 300);
796-
io.MouseDown[0] = true;
797-
io.MouseClicked[0] = true;
798+
ImGuiID remove_p_id = get_popup_item_id("Popup_KMidi", "Remove MIDI Mapping");
799+
800+
g.NavActivateId = remove_p_id;
801+
g.NavActivateDownId = remove_p_id;
802+
g.NavActivatePressedId = remove_p_id;
803+
804+
ImGui::OpenPopup("Popup_KMidi");
798805
KnobComponent::render("KMidi", props, 1.0f, center);
799806
advance_frame();
800-
io.MouseDown[0] = false;
801-
io.MouseClicked[0] = false;
802-
advance_frame();
803807

804-
// Open again for bypass mapping clear
805-
io.MousePos = center;
806-
io.MouseClicked[1] = true;
808+
// --- 4. Test "Remove Bypass Mapping" ---
809+
ImGui::OpenPopup("Popup_KMidi");
807810
KnobComponent::render("KMidi", props, 1.0f, center);
808811
advance_frame();
809-
io.MouseClicked[1] = false;
810812

811-
io.MousePos = ImVec2(220, 345);
812-
io.MouseDown[0] = true;
813-
io.MouseClicked[0] = true;
813+
ImGuiID remove_b_id = get_popup_item_id("Popup_KMidi", "Remove Bypass Mapping");
814+
815+
g.NavActivateId = remove_b_id;
816+
g.NavActivateDownId = remove_b_id;
817+
g.NavActivatePressedId = remove_b_id;
818+
819+
ImGui::OpenPopup("Popup_KMidi");
814820
KnobComponent::render("KMidi", props, 1.0f, center);
815821
advance_frame();
816-
io.MouseDown[0] = false;
817-
io.MouseClicked[0] = false;
818-
advance_frame();
819822

820823
ImGui::End();
821824

0 commit comments

Comments
 (0)