Simplify and clean up Win32 Cursor implementation#3220
Simplify and clean up Win32 Cursor implementation#3220HeikoKlare wants to merge 2 commits intoeclipse-platform:masterfrom
Conversation
|
The new tests look like they are not so helpful, I would not not test win32_getHandle in isolation. |
Why do you think so? Those are actual unit tests (which SWT is mostly lacking). Some of them may not be that useful, but having core functionality of per-zoom handle provision, non-redundancy etc. regression tested sounds reasonable to me to make SWT develop less "let's hope we don't break anything, but we have sufficient time to find implicitly find regressions until the release" 🙂 |
|
The only useful tests seem to be the last two once to me (starting with void testImageDataCursorProducesDifferentHandlesForDifferentZoomLevels). But if the "a cursor creates a handler" tests are useful to you, that is fine for me. Just keep an open eye for AI slope, no need to pollute our repos with unnecessary tests. |
Several small improvements to the Win32 Cursor implementation: - Replace double map-lookup and setHandleForZoomLevel() helper with a single HashMap.computeIfAbsent() call in win32_getHandle(). The helper method contained redundant null/containsKey guards that were already enforced by the call-site; both are now unnecessary. - Change win32_getHandle() return type from boxed Long to primitive long, consistent with Font.win32_getHandle(), Image.win32_getHandle() and Region.win32_getHandle(). All existing call sites already consumed the result as a long, so no callers required changes. - Fix latent equality bug in equals(): the previous code compared two Long wrapper objects with ==, which only gives correct results for cached values in [-128, 127]. Cursor handles are OS memory addresses and fall well outside that range. With the primitive return type the comparison is now a straightforward == on long values, which is both correct and avoids boxing. - Use Java 16+ instanceof pattern matching in equals() to eliminate the explicit cast after the type check. - Remove unnecessary clone() in ImageDataWithMaskCursorHandleProvider .validateMask(): the cloned ImageData was only ever read for its width/height fields, so the defensive copy was pure overhead. - Remove redundant 'static' modifier on the nested CursorHandleProvider interface declaration; nested interfaces are implicitly static. - Remove redundant 'final' modifier on the private static method getOSCursorIdFromStyle(); private static methods cannot be overridden. - Remove redundant 'final' modifier on the lambda-captured local variable in destroyHandlesExcept(); the variable is effectively final without the explicit keyword. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CursorWin32Tests covers behaviour that is either Windows-only or directly exercises the internal implementation paths changed in the preceding cleanup commit: - testDisposedCursorReturnsZeroHandle: verifies the isDisposed() guard at the top of win32_getHandle() returns 0L after disposal. - testHandleIsCachedForSameZoomLevel: verifies the computeIfAbsent() caching - two calls with the same zoom must return the identical cached OS handle without re-creating it. - testImageDataCursorProducesDifferentHandlesForDifferentZoomLevels: verifies that ImageDataCursorHandleProvider scales the source image and produces a distinct OS cursor handle for each zoom level. - testDestroyHandlesExceptPreservesRetainedHandle: verifies that destroyHandlesExcept() leaves the retained zoom entry intact and does not mark the cursor as disposed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fc2497e to
68ef2f3
Compare
Sure, we must not accept whatever an agent proposed, so I have an eye on slop. As said, I agree that some tests are rather useless, which is why I removed them. I just kept those tests that ensure important contracts of the Win32 cursor implementation. |
Summary
This pull request simplifies and cleans up the Win32-specific
Cursorclass (bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Cursor.java) and adds a corresponding set of Win32-specific unit tests.Code Simplifications & Improvements
setHandleForZoomLevel()withHashMap.computeIfAbsent()inwin32_getHandle()win32_getHandle()return type from boxedLongto primitivelongFont.win32_getHandle(),Image.win32_getHandle(), andRegion.win32_getHandle(); eliminates unnecessary boxing overhead; all call sites already usedlongequals():Longwrapper objects compared with==[-128, 127], so==onLongcould silently returnfalsefor equal handles. With the primitive return type,==onlongis now correctequals()clone()inImageDataWithMaskCursorHandleProvider.validateMask()ImageDatawas only ever read for itswidth/heightfields — the defensive copy was pure overheadstaticmodifier on the nestedCursorHandleProviderinterfacestaticin Javafinalmodifier onprivate staticmethodgetOSCursorIdFromStyle()private staticmethods cannot be overridden;finalis meaningless herefinalmodifier on lambda-captured local variable indestroyHandlesExcept()New Win32-Specific Tests
A new test class
CursorWin32Testswas added alongside the existing Win32 test classes, following the same patterns used inRegionWin32TestsandImagesWin32Tests.testDisposedCursorReturnsZeroHandleisDisposed()guard at the top ofwin32_getHandle()returns0Lafter disposaltestHandleIsCachedForSameZoomLevelcomputeIfAbsent()caching — repeated calls with the same zoom return the identical cached OS handletestImageDataCursorProducesDifferentHandlesForDifferentZoomLevelsImageDataCursorHandleProviderscales the source image and produces a distinct OS cursor handle per zoom leveltestDestroyHandlesExceptPreservesRetainedHandledestroyHandlesExcept()leaves the retained zoom entry intact without marking the cursor as disposed