Skip to content

[fix] comp text test for headless env#3484

Open
nandishjpatel wants to merge 1 commit into
delmic:masterfrom
nandishjpatel:fix-comp-text-test-for-headless-env
Open

[fix] comp text test for headless env#3484
nandishjpatel wants to merge 1 commit into
delmic:masterfrom
nandishjpatel:fix-comp-text-test-for-headless-env

Conversation

@nandishjpatel

Copy link
Copy Markdown
Contributor

directly invoke the ctrl's focus handler if SetFocus() didn't succeed in case of a headless display environment

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates GUI text control tests to behave reliably in headless display environments by ensuring the “on focus selects text” behavior still occurs even when SetFocus() does not actually grant OS-level focus.

Changes:

  • Added a _set_focus() helper that attempts SetFocus() and falls back to triggering focus-selection logic when focus cannot be obtained.
  • Replaced several direct SetFocus() + gui_loop() sequences with _set_focus() for consistency across the test cases.

Comment thread src/odemis/gui/test/comp_text_test.py Outdated
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 83c8a0de-d3de-4bb1-9b88-3d23f35d8287

📥 Commits

Reviewing files that changed from the base of the PR and between ab4da63 and c94151d.

📒 Files selected for processing (1)
  • src/odemis/gui/test/comp_text_test.py

📝 Walkthrough

Walkthrough

Adds NumberTextCtrlTestCase._set_focus(ctrl) which calls ctrl.SetFocus(), checks ctrl.HasFocus(), and—if focus wasn't acquired—creates and dispatches a synthetic wx.FocusEvent to invoke the control's focus handler. Five test sites that previously called SetFocus() directly were changed to call self._set_focus(...) so the tests exercise focus/selection logic in both GUI and headless environments.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: fixing component text tests for headless environments, which directly relates to the changeset's focus-handling improvement.
Description check ✅ Passed The description explains the core change: invoking focus handlers when SetFocus() fails in headless environments, which accurately relates to the changeset modifications.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/odemis/gui/test/comp_text_test.py (1)

122-123: ⚡ Quick win

Initialize the synthetic event with the target control.

For completeness and to follow wxPython best practices, call SetEventObject(ctrl) on the synthetic event before passing it to the handler. Although the current implementation works because on_focus only calls evt.Skip(), proper initialization ensures the event object is fully formed.

🔧 Proposed fix
         # Headless fallback: directly invoke the focus handler
         evt = wx.FocusEvent(wx.wxEVT_SET_FOCUS)
+        evt.SetEventObject(ctrl)
         ctrl.on_focus(evt)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/odemis/gui/test/comp_text_test.py` around lines 122 - 123, The synthetic
wx.FocusEvent created in the test is not initialized with its target control;
call evt.SetEventObject(ctrl) before invoking ctrl.on_focus(evt) so the event's
event object is properly set—update the test to set the event object on the evt
instance (evt) prior to calling the control's on_focus handler (ctrl.on_focus).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/odemis/gui/test/comp_text_test.py`:
- Around line 115-117: The _set_focus function lacks type hints and its
docstring must be converted to reStructuredText; update the signature to include
a concrete type for ctrl (e.g., ctrl: wx.Control or a suitable control
protocol/ABC) and a return type of None, and replace the triple-quoted docstring
with an rst-style description (one-line summary and an extended description)
that documents the parameter and return value; keep the body invoking
ctrl.SetFocus() and retain the headless note in the rst docstring.

---

Nitpick comments:
In `@src/odemis/gui/test/comp_text_test.py`:
- Around line 122-123: The synthetic wx.FocusEvent created in the test is not
initialized with its target control; call evt.SetEventObject(ctrl) before
invoking ctrl.on_focus(evt) so the event's event object is properly set—update
the test to set the event object on the evt instance (evt) prior to calling the
control's on_focus handler (ctrl.on_focus).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3ccef6d9-c3ba-4e0b-b0f5-9fc7c7fa560b

📥 Commits

Reviewing files that changed from the base of the PR and between f74a181 and ab4da63.

📒 Files selected for processing (1)
  • src/odemis/gui/test/comp_text_test.py

Comment on lines +115 to +117
def _set_focus(self, ctrl):
"""Set focus, falling back to directly invoking on_focus if run in a headless environment."""
ctrl.SetFocus()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Add type hints and convert docstring to reStructuredText format.

The coding guidelines require type hints for all function parameters and return types, and reStructuredText-style docstrings for all functions.

📝 Proposed fix
-    def _set_focus(self, ctrl):
-        """Set focus, falling back to directly invoking on_focus if run in a headless environment."""
+    def _set_focus(self, ctrl: wx.TextCtrl) -> None:
+        """Set focus on a text control, with headless environment fallback.
+        
+        Attempts to acquire GUI focus via SetFocus(). If the control does not
+        report focus after a brief wait (common in headless environments), directly
+        invokes the control's on_focus handler with a synthetic event.
+        
+        Args:
+            ctrl: The text control to focus.
+        """
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def _set_focus(self, ctrl):
"""Set focus, falling back to directly invoking on_focus if run in a headless environment."""
ctrl.SetFocus()
def _set_focus(self, ctrl: wx.TextCtrl) -> None:
"""Set focus on a text control, with headless environment fallback.
Attempts to acquire GUI focus via SetFocus(). If the control does not
report focus after a brief wait (common in headless environments), directly
invokes the control's on_focus handler with a synthetic event.
Args:
ctrl: The text control to focus.
"""
ctrl.SetFocus()
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/odemis/gui/test/comp_text_test.py` around lines 115 - 117, The _set_focus
function lacks type hints and its docstring must be converted to
reStructuredText; update the signature to include a concrete type for ctrl
(e.g., ctrl: wx.Control or a suitable control protocol/ABC) and a return type of
None, and replace the triple-quoted docstring with an rst-style description
(one-line summary and an extended description) that documents the parameter and
return value; keep the body invoking ctrl.SetFocus() and retain the headless
note in the rst docstring.

Source: Coding guidelines

… obtained in case of a headless display environment
@nandishjpatel nandishjpatel force-pushed the fix-comp-text-test-for-headless-env branch from ab4da63 to c94151d Compare June 11, 2026 06:49
@nandishjpatel nandishjpatel requested a review from tepals June 11, 2026 07:39
Comment on lines +120 to +125
if not ctrl.HasFocus():
# Headless fallback: directly invoke the focus handler
evt = wx.FocusEvent(wx.wxEVT_SET_FOCUS, ctrl.Id)
evt.SetEventObject(ctrl)
ctrl.GetEventHandler().ProcessEvent(evt)
test.gui_loop(0.1)

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.

Did you verify that this works? What do you mean by "headless"? As far as I understood, our VMs run with a simulated screen, with Gnome running. Why would it be "headless"? Do you know why such setup work differently from a normal computer?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I haven't tested this fix.

This fix is needed for Ubuntu 24, where xvfb a virtual display is needed to run the testcases. By headless I mean the testcases/programs run headless in the xvfb virtual display.

Ubuntu 24 testcases run command: https://bitbucket.org/delmic/odemis-testing/src/30268666bced16e45931ccaad349d5db38b6017c/ci/canonical_lxd/action.sh#lines-58

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.

@nandishjpatel I think that there are two options forward:

  • we find a way to test this fix without merging it. Maybe one way is to temporarily use this branch as target for testing on the Ubuntu 24 run. Another option would be to start the Ubuntu 24 image on your computer and run the test suite (or just this GUI part).
  • we merge this change, wait for the test cases to run, and if it doesn't help, we revert it.

I'd rather go with the first route. Can you try finding a way to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants