From 89ca146e97372df471b7c4d2044e31e27a8ee477 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sat, 18 Oct 2025 10:07:26 +0200 Subject: [PATCH] Adds signal testing to UI elements Extends UI element tests to include signal emission verification. This ensures that UI elements correctly emit signals upon user interaction, improving the reliability and responsiveness of the UI. Adds QSignalSpy to track signal emissions for various UI components like buttons, checkboxes, comboboxes, items, sliders, textboxes and thermos. --- tests/CMakeLists.txt | 3 ++- tests/Test.hpp | 3 +-- tests/UI_button.cpp | 8 ++++++++ tests/UI_checkbox.cpp | 8 ++++++++ tests/UI_combobox.cpp | 8 ++++++++ tests/UI_dial.cpp | 2 ++ tests/UI_item.cpp | 13 +++++++++++++ tests/UI_slider.cpp | 5 +++++ tests/UI_textbox.cpp | 16 +++++++++++++++- tests/UI_thermo.cpp | 2 ++ 10 files changed, 64 insertions(+), 4 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bfed2ae..a3e8cd1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,6 @@ # Third Party find_package(GTest QUIET) -find_package(Qt6 REQUIRED COMPONENTS Core) +find_package(Qt6 REQUIRED COMPONENTS Core Test) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) @@ -23,6 +23,7 @@ if (GTest_FOUND) magic_enum::magic_enum GTest::gtest_main Qt::Core + Qt::Test ) target_include_directories(tests diff --git a/tests/Test.hpp b/tests/Test.hpp index 5be6cc5..b2b1ca0 100644 --- a/tests/Test.hpp +++ b/tests/Test.hpp @@ -1,5 +1,4 @@ #include "ui.hpp" #include "gtest/gtest.h" +#include #include - -#define UNUSED(x) (void)(x) diff --git a/tests/UI_button.cpp b/tests/UI_button.cpp index 3ea2636..8884e1a 100644 --- a/tests/UI_button.cpp +++ b/tests/UI_button.cpp @@ -10,3 +10,11 @@ TEST(UI_button, to_from_json) EXPECT_EQ(button.fg_color(), button2.fg_color()); EXPECT_EQ(button.bg_color(), button2.bg_color()); } + +TEST(UI_button, signal) +{ + UI_button button("button_text", Color::Default, Color::Default); + QSignalSpy spy(&button, &UI_button::clicked); + button.click(); + EXPECT_EQ(spy.count(), 1); +} diff --git a/tests/UI_checkbox.cpp b/tests/UI_checkbox.cpp index 1106460..fd1a067 100644 --- a/tests/UI_checkbox.cpp +++ b/tests/UI_checkbox.cpp @@ -11,3 +11,11 @@ TEST(UI_checkbox, to_from_json) EXPECT_EQ(checkbox.bg_color(), checkbox2.bg_color()); EXPECT_EQ(checkbox.state(), checkbox2.state()); } + +TEST(UI_checkbox, signal) +{ + UI_checkbox checkbox("checkbox_text", Color::Default, Color::Default); + QSignalSpy spy(&checkbox, &UI_checkbox::changed); + checkbox.click(); + EXPECT_EQ(spy.count(), 1); +} diff --git a/tests/UI_combobox.cpp b/tests/UI_combobox.cpp index be4d848..757e7a0 100644 --- a/tests/UI_combobox.cpp +++ b/tests/UI_combobox.cpp @@ -11,3 +11,11 @@ TEST(UI_combobox, to_from_json) EXPECT_EQ(combobox.bg_color(), combobox2.bg_color()); EXPECT_EQ(combobox.selected(), combobox2.selected()); } + +TEST(UI_combobox, signal) +{ + UI_combobox combobox(Color::Default, Color::Default, {"option1", "option2"}); + QSignalSpy spy(&combobox, &UI_combobox::changed); + combobox.set_selected(1); + EXPECT_EQ(spy.count(), 1); +} diff --git a/tests/UI_dial.cpp b/tests/UI_dial.cpp index f91a104..6706034 100644 --- a/tests/UI_dial.cpp +++ b/tests/UI_dial.cpp @@ -16,8 +16,10 @@ TEST(UI_dial, to_from_json) TEST(UI_dial, set_value) { UI_dial dial("slider_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50); + QSignalSpy spy(&dial, &UI_dial::ui_changed); dial.set_value(75); EXPECT_EQ(dial.value(), 75); + EXPECT_EQ(spy.count(), 1); } TEST(UI_dial, unit) diff --git a/tests/UI_item.cpp b/tests/UI_item.cpp index 371e8f8..399b3e1 100644 --- a/tests/UI_item.cpp +++ b/tests/UI_item.cpp @@ -63,3 +63,16 @@ TEST(UI_item, to_json) EXPECT_EQ(j.at("fg_color"), Color(255, 255, 255).to_hex()); EXPECT_EQ(j.at("bg_color"), Color(0, 0, 0).to_hex()); } + +TEST(UI_item, signal) +{ + UI_item item("item", "item_text", Color(255, 255, 255), Color(0, 0, 0)); + QSignalSpy spy(&item, &UI_item::ui_changed); + item.set_text("text"); + item.append_text(" "); + item.set_bg_color(Color::Default); + item.set_fg_color(Color::Default); + item.set_enabled(0); + item.set_visible(0); + EXPECT_EQ(spy.count(), 6); +} diff --git a/tests/UI_slider.cpp b/tests/UI_slider.cpp index 9fb34ea..923a80d 100644 --- a/tests/UI_slider.cpp +++ b/tests/UI_slider.cpp @@ -15,5 +15,10 @@ TEST(UI_slider, to_from_json) TEST(UI_slider, value) { UI_slider slider("slider_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50); + QSignalSpy spy_ui(&slider, &UI_slider::ui_changed); + QSignalSpy spy_value(&slider, &UI_slider::changed); EXPECT_EQ(slider.value(), 50); + slider.set_value(10); + EXPECT_EQ(spy_ui.count(), 1); + EXPECT_EQ(spy_value.count(), 1); } diff --git a/tests/UI_textbox.cpp b/tests/UI_textbox.cpp index fc4c65a..a2b9cb3 100644 --- a/tests/UI_textbox.cpp +++ b/tests/UI_textbox.cpp @@ -15,7 +15,21 @@ TEST(UI_textbox, to_from_json) TEST(UI_textbox, set_text) { UI_textbox textbox("hint", Color(0, 0, 0), Color(255, 255, 255)); + QSignalSpy spy_ui(&textbox, &UI_textbox::ui_changed); + QSignalSpy spy_value(&textbox, &UI_textbox::changed); EXPECT_EQ(textbox.text(), ""); textbox.set_text("random text"); EXPECT_EQ(textbox.text(), "random text"); -} \ No newline at end of file + EXPECT_GE(spy_ui.count(), 0); + EXPECT_GE(spy_value.count(), 0); +} + +TEST(UI_textbox, set_hint) +{ + UI_textbox textbox("hint", Color(0, 0, 0), Color(255, 255, 255)); + QSignalSpy spy_ui(&textbox, &UI_textbox::ui_changed); + EXPECT_EQ(textbox.hint(), "hint"); + textbox.set_hint("random text"); + EXPECT_EQ(textbox.hint(), "random text"); + EXPECT_EQ(spy_ui.count(), 1); +} diff --git a/tests/UI_thermo.cpp b/tests/UI_thermo.cpp index b23703f..1a7511b 100644 --- a/tests/UI_thermo.cpp +++ b/tests/UI_thermo.cpp @@ -15,6 +15,8 @@ TEST(UI_thermo, to_from_json) TEST(UI_thermo, set_value) { UI_thermo thermo("thermo_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50); + QSignalSpy spy(&thermo, &UI_thermo::ui_changed); thermo.set_value(75); EXPECT_EQ(thermo.value(), 75); + EXPECT_EQ(spy.count(), 1); }