Commit 8183310
Add unit tests for Component methods (#4099)
* Add comprehensive unit tests for Component methods
This commit adds extensive unit test coverage for previously untested
Component methods. Tests are designed to trigger methods through their
natural execution paths and public APIs rather than attempting to invoke
private methods directly.
Coverage includes:
- Shadow rendering (paintShadows with elevation, shadow calculations)
- Drag and drop operations (dragFinished, drop events, listeners)
- Pull-to-refresh functionality (triggered through paint cycle)
- Scroll and motion (scrollable components, tensile drag, listeners)
- Style processing (inline styles trigger internal string methods)
- Focus and events (focus loss, action events, state changes)
- Input handling (keyboard and pointer events, pinch gestures)
- Property binding (BindTarget implementation, property access)
- Utility methods (toString, paramString, hierarchy checks, editing)
- Component rendering and lifecycle methods
Tests follow existing patterns:
- Extend UITestBase for proper test environment
- Override protected methods (dragFinished, paramString, paintBorderBackground)
- Trigger private methods through public API calls and lifecycle
- No reflection or direct access to package-private methods
- No Mockito - uses TestCodenameOneImplementation where needed
- Java 8 syntax only
Total: 70+ test methods covering the previously untested methods.
* Fix compilation error in ComponentCoverageTest
Changed testFireClickedEvent to testPointerReleasedEvent to use
addPointerReleasedListener instead of addActionListener, as the base
Component class does not have addActionListener/fireActionEvent methods.
Those are specific to Button and other actionable components.
* Fix multiple test failures in ComponentCoverageTest
- Fix testHintLabel: Base Component's setHintLabelImpl/getHintLabelImpl
are stubs that don't store values. Updated test to verify stub behavior.
- Fix testTactileTouch: tactileTouch defaults to isFocusable(). Set both
to false explicitly and add positive test case.
- Fix testPointerReleasedEvent: Add component to form before testing
pointer events to ensure proper initialization and parent hierarchy.
- Add UIID to TestableComponent and ScrollableComponent constructors
to match pattern from existing tests and ensure proper theming.
* Fix all remaining test failures in ComponentCoverageTest
Removed form.show() calls that were causing "Initialize must be invoked
before setCurrent!" errors. These calls were not essential for the tests:
- testPaintShadowsWithElevation: Removed form.show() - paintShadows can be
tested without showing the form
- testComponentWithElevationRendering: Paint component directly instead of
showing form
- testDropTargetBehavior: Properties can be tested without showing form
- testAddPullToRefreshTask: Client property check doesn't need form shown
- testPullToRefreshTriggeredByPaint: Paint directly without showing form
- testPointerReleasedEvent: Listeners work without form.show()
- testGrowShrinkAnimation: Method can be called without showing form
- testPaintBorderBackground: Paint method test doesn't need form shown
- testComponentRendering: Paint directly without showing form
- testFindSurface: findSurface works with hierarchy, doesn't need form shown
- testIsVisibleOnForm: Test method call without showing form
Fixed property-related test failures:
- testPropertyNameAccess: Base Component returns null for property methods,
updated test to expect null instead of non-null
- testBindableProperties: Base Component returns null for bindable property
methods, updated test to expect null
All tests now properly test Component behavior without requiring the Display
to be fully initialized with form.show() calls.
* Fix 3 more test failures in ComponentCoverageTest
- testAddPullToRefreshTask: addPullToRefresh just stores the task, doesn't
set client properties. Changed test to use assertDoesNotThrow instead.
- testPropertyValueAccess: Base Component returns null for getPropertyValue
and setPropertyValue is a stub. Updated test to expect null.
- testPointerReleasedEvent: Added component to Form container to ensure
proper parent hierarchy for event firing (LeadUtil.leadParentImpl needs
parent reference).
Down from 19 failures to 4 remaining.
* Fix EDT-related test failures in ComponentCoverageTest
Fixed tests that were trying to fire events without proper EDT setup:
- testPointerReleasedEvent: Changed to test listener add/remove instead of
actually firing events. Event firing requires proper EDT (Event Dispatch
Thread) setup via Display.callSerially which throws "This method should
not be invoked by external code!" without proper initialization.
- testFocusLostEvent: Removed form.show() call and changed to test listener
add/remove. Focus events also require EDT and Display initialization.
- testLabelForComponent: Removed label.startTicker() call which requires
EDT. Changed deinitialize test to use assertDoesNotThrow.
These tests now properly verify the listener management APIs work without
requiring full Display/EDT initialization, which is difficult to set up
in unit test context.
* Fix testScrollListeners EDT issue
setScrollY() fires scroll events via Display.callSerially which requires
EDT setup. Changed test to verify listener add/remove works instead of
actually triggering scroll events.
This is the same pattern as the other event-related tests - we test the
listener management API works without requiring full EDT initialization.
* Fix testTensileDragEnabled default value issue
The default value of tensileDragEnabled depends on LAF settings
(laf.isDefaultTensileDrag()), not hardcoded to false. Updated test to:
- Not assume initial value is false
- Test both true and false setter/getter behavior
- Continue testing tensile length functionality
* Fix getTensileLength default value in test
The default value of tensileLength is -1, not 0 (as defined in Component.java).
Updated test to expect -1 as the initial value.
* Fix testComponentWithElevationRendering style setting
Changed to:
1. Add component to form before setting elevation (ensures proper initialization)
2. Use getUnselectedStyle() instead of getAllStyles() - getAllStyles() returns
a merged style that may not persist changes. Setting elevation on the
base unselected style works correctly.
* Simplify testComponentWithElevationRendering
Simplified test to focus on elevation setting functionality:
- Test setting and getting elevation on style
- Test that paintShadows() can be called (used during rendering)
Removed the painted flag check since paintComponent() doesn't directly
call our overridden paint() method, making that assertion unreliable.
* Document uncovered methods and add additional tests
Added comprehensive documentation at the top of ComponentCoverageTest
explaining why 39 methods cannot be covered in unit tests:
1. EDT/Event methods - Require Display initialization and EDT setup
2. Protected/package-private methods - Internal implementation details
3. Private methods - Not accessible from tests
4. Platform-specific methods - Require native setup
5. Complex hierarchy methods - Require specific component states
6. Property binding methods - Return null/no-op on base Component
Added tests for public methods that can be covered:
- testLongPointerPress() - Tests longPointerPress(int, int) method
- testBoundPropertyValues() - Tests getBoundPropertyValue() and
setBoundPropertyValue() methods
These methods are tested to the extent possible in unit test context,
verifying they execute without errors even though their full behavior
requires EDT or more complex setup.
* Remove testLongPointerPress - requires EDT
longPointerPress() fires events via Display.callSerially, requiring EDT
setup just like other event methods.
Changes:
- Removed testLongPointerPress() test
- Added longPointerPress(int, int) to EDT methods documentation
- Updated method count from 39 to 40 uncovered methods
- Clarified that property binding methods ARE tested via testBoundPropertyValues()
- Added summary breakdown of uncovered method categories
Long press listener management is already tested in testLongPressListener().
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 793b58f commit 8183310
File tree
1 file changed
+1029
-0
lines changed- maven/core-unittests/src/test/java/com/codename1/ui
1 file changed
+1029
-0
lines changed
0 commit comments