Skip to content

Add unit tests for BaseButton and ButtonGroup - #120994

Open
PedroHOCostaa wants to merge 5 commits into
godotengine:masterfrom
PedroHOCostaa:button-unit-test
Open

PedroHOCostaa wants to merge 5 commits into
godotengine:masterfrom
PedroHOCostaa:button-unit-test

Conversation

@PedroHOCostaa

@PedroHOCostaa PedroHOCostaa commented Jul 6, 2026 •

Copy link
Copy Markdown

Description

This Pull Request attends to issue #43440 implementing unit tests for BaseButton and ButtonGroup classes. These tests were developed using specification testing and then structural testing, using branch coverage and code analysis.

Changes Covered by New Tests

BaseButton Core Interaction & State Machine

  • Press/Release Mechanics: Validated is_pressed() conditions and verified correct state behavior across both action modes (ACTION_MODE_BUTTON_PRESS vs ACTION_MODE_BUTTON_RELEASE).
  • Toggle System: Tested toggle states under both press and release action modes, ensuring the pressed and toggled signals emit at precise, expected times.
  • Silent State Changes: Verified that set_pressed_no_signal() updates the button state cleanly without triggering any signal listeners (and ensures it is ignored when toggle_mode is off).
  • Disabled State Guardrails: Ensured disabled buttons still receive mouse hover events but strictly ignore input clicks and suppress signal emissions.
  • Draw Mode State Machine: Covered all structural transitions for get_draw_mode() (DRAW_NORMAL, DRAW_HOVER, DRAW_PRESSED, DRAW_HOVER_PRESSED, and DRAW_DISABLED).

Input Gestures & System Notifications

  • Touch & Drag Input: Emulated touch workflows (InputEventScreenTouch, InputEventScreenDrag) along with mouse motion events to validate complex pointer interactions under varying keep_pressed_outside conditions.
  • Notification Lifecycle: Tested click cancellation mechanisms via standard GUI notifications such as NOTIFICATION_DRAG_BEGIN, NOTIFICATION_SCROLL_BEGIN, and focus loss (NOTIFICATION_FOCUS_EXIT).
  • Masks & Shortcuts: Validated custom mouse button masks (MouseButtonMask) and tested automatic trigger responses mapped via Shortcut associations.

ButtonGroup Mechanics & Lifecycle

  • Mutual Exclusion: Validated radio-button style exclusion behaviors and the allow_unpress property configuration.
  • Exposed Bindings: Added validations for exposed list methods (get_buttons internally and _get_buttons exposed to GDScript).
  • Safe Cleanup: Guaranteed that buttons are automatically and safely removed from their assigned ButtonGroup when destroyed in memory.

Core Files Involved

  • tests/scene/test_button.cpp
  • tests/scene/test_button.h

Results of the coverage report in base_button.cpp

Branch Line Coverage percentage Line Coverage Branches percentage Branches
Master branch 42.1 % 182 / 432 17.8 % 56 / 314
This branch 79.5 % 348 / 438 57.5 % 214 / 372

@PedroHOCostaa
PedroHOCostaa requested review from a team as code owners July 6, 2026 02:48
@PedroHOCostaa PedroHOCostaa changed the title Button unit test Add unit tests for BaseButton and ButtonGroup Jul 6, 2026
@AThousandShips

Copy link
Copy Markdown
Member

Did you use AI to write any of the code or the PR description? Please see our contribution guidelines and this post

#include "tests/display_server_mock.h"

namespace TestButton {

// Tests related to `BaseButton` using `Button` as a concrete implementation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this comment?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reasons: to separate the following tests from the ButtonGroup tests, and because none of them are actually testing the mechanics of the Button class. All of them refer to the BaseButton class, but since BaseButton is an abstract class, I think it feels 'wrong' to instantiate it directly. Perhaps I could change the code so that the tests are separated by namespace.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's redundant and obvious from the code

@PedroHOCostaa PedroHOCostaa Jul 6, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will remove it along with other useless comments

Comment thread .pre-commit-config.yaml Outdated
@PedroHOCostaa

Copy link
Copy Markdown
Author

Did you use AI to write any of the code or the PR description? Please see our contribution guidelines and this post

I did use it for assistance in some parts, but not an agent like copilot, and I did in fact use it to create that description, it's for a It was for a university project. And the dead line was yesterday, I wanted to have this pull request created before submitting it. I will make it smaller and more precise (y).

@AThousandShips

Copy link
Copy Markdown
Member

and I did in fact use it to create that description, it's for a It was for a university project.

Thank you yes please do rewrite it yourself to make sure it is valid, and disclose how and where you used AI tools and if it generated any code

@PedroHOCostaa PedroHOCostaa reopened this Jul 6, 2026
@Repiteo Repiteo changed the title Add unit tests for BaseButton and ButtonGroup Add unit tests for BaseButton and ButtonGroup Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants