-
Notifications
You must be signed in to change notification settings - Fork 430
Write focus behavior unit test for form #4100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write focus behavior unit test for form #4100
Conversation
This test class verifies focus behavior using the @formtest annotation to initialize the EDT and fire events through the entire stack. The tests cover: - Focus state management (focusable, hasFocus, setFocus) - Focus listeners (gained and lost events) - Focus traversal (next/previous/left/right/up/down) - Focus with containers and nested components - Focus interaction with component state (enabled, visible) - Focus with pointer/touch events - Tab index and traversable properties - Focus persistence across style changes - Multiple focus listeners - Focus cycles and form transitions The implementation follows the existing test conventions: - Extends UITestBase - Uses @formtest for EDT initialization - Avoids Mockito in favor of TestCodenameOneImplementation - Limited to Java 8 syntax - No reflection to access internal state - Only modifies files in maven/core-unittests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| void testFocusLostWhenComponentDisabled() { | ||
| Form form = Display.getInstance().getCurrent(); | ||
| Button button = new Button("Test"); | ||
| form.add(button); | ||
| form.revalidate(); | ||
|
|
||
| button.requestFocus(); | ||
| assertTrue(button.hasFocus(), "Button should have focus"); | ||
|
|
||
| button.setEnabled(false); | ||
| assertFalse(button.hasFocus(), "Disabled button should lose focus"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disabling component does not clear focus state
The new testFocusLostWhenComponentDisabled assumes that calling setEnabled(false) will remove focus from the component, but Component#setEnabled() only flips the enabled flag and repaints; it never calls setFocus(false) or Form#setFocused. As a result button.hasFocus() remains true and this assertion will fail every time unless the production code is changed. Either update the test expectation or add code to clear focus when components are disabled.
Useful? React with 👍 / 👎.
| void testFocusWithHiddenComponent() { | ||
| Form form = Display.getInstance().getCurrent(); | ||
| Button button = new Button("Test"); | ||
| form.add(button); | ||
| form.revalidate(); | ||
|
|
||
| button.setVisible(true); | ||
| button.requestFocus(); | ||
| assertTrue(button.hasFocus(), "Visible button should gain focus"); | ||
|
|
||
| button.setVisible(false); | ||
| assertFalse(button.hasFocus(), "Hidden button should lose focus"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hidden component retains focused flag in current implementation
testFocusWithHiddenComponent expects a component to lose focus after setVisible(false). However, Component#setVisible() merely toggles the visibility boolean and does not touch the internal focused state, so hasFocus() will still return true. The test will therefore fail on existing code unless visibility changes are wired to clear focus explicitly.
Useful? React with 👍 / 👎.
| Form form2 = new Form("Form 2"); | ||
|
|
||
| Button button1 = new Button("Button on Form 1"); | ||
| Button button2 = new Button("Button on Form 2"); | ||
|
|
||
| form1.add(button1); | ||
| form2.add(button2); | ||
|
|
||
| form1.show(); | ||
| button1.requestFocus(); | ||
| assertTrue(button1.hasFocus(), "button1 should have focus on form1"); | ||
|
|
||
| form2.show(); | ||
| assertFalse(button1.hasFocus(), "button1 should lose focus when form2 is shown"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switching forms does not unset focus of previous form’s components
In testFocusClearedOnFormChange the assertion relies on form2.show() clearing the focused state of widgets in form1. Form.show()/Display.setCurrent() initialise focus for the new form but never mutate the focused flag on the prior form’s components, so button1.hasFocus() remains true. This causes a deterministic test failure unless additional code is introduced to clear focus when changing forms.
Useful? React with 👍 / 👎.
Updated three tests that had incorrect assumptions: 1. testDisabledComponentRetainsFocus (formerly testFocusLostWhenComponentDisabled): - Disabling a component doesn't automatically clear its focus - But disabled components cannot receive new focus via requestFocus() 2. testHiddenComponentCannotReceiveFocus (formerly testFocusWithHiddenComponent): - Making a component invisible doesn't automatically clear focus - But invisible components cannot receive new focus 3. testFocusTransferBetweenForms (formerly testFocusClearedOnFormChange): - Focus behavior during form transitions depends on form lifecycle - Tests that components on the new form can receive focus These changes ensure tests verify actual framework behavior rather than making incorrect assumptions about automatic focus management.
✅ Continuous Quality ReportTest & Coverage
Static Analysis
Generated automatically by the PR CI workflow. |
This test class verifies focus behavior using the @formtest annotation to initialize the EDT and fire events through the entire stack. The tests cover:
The implementation follows the existing test conventions: