Skip to content

Commit e0b4fe9

Browse files
committed
Remove type hierarchy of Win32AutoscaleTestBase
Auto-scale related, Windows-specific tests are currently organized in a class hierarchy based on Win32AutoscaleTestBase. This leads to unnecessarily initialized objects (e.g., shells or displays without any need for a test) and the strict necessity to use activated monitor-specific scaling in actual tests or to revert the setup logic affecting that value by hand. This change removes the Win32AutoscaleTestBase and moves the common test functionality into extensions (PlatformSpecificExecution, ResetMonitorSpecificScaling and WithMonitorSpecificScaling) as well as the utility class DPITestUtil.
1 parent f574463 commit e0b4fe9

15 files changed

+205
-105
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/GCWin32Tests.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@
2020

2121
import org.eclipse.swt.*;
2222
import org.eclipse.swt.internal.*;
23+
import org.eclipse.swt.widgets.*;
2324
import org.junit.jupiter.api.*;
25+
import org.junit.jupiter.api.extension.*;
2426

25-
class GCWin32Tests extends Win32AutoscaleTestBase {
27+
@ExtendWith(PlatformSpecificExecutionExtension.class)
28+
@ExtendWith(WithMonitorSpecificScalingExtension.class)
29+
class GCWin32Tests {
2630

2731
@Test
2832
public void gcZoomLevelMustChangeOnShellZoomChange() {
33+
Shell shell = new Shell(Display.getDefault());
34+
2935
CompletableFuture<Integer> gcNativeZoom = new CompletableFuture<>();
3036
CompletableFuture<Integer> scaledGcNativeZoom = new CompletableFuture<>();
3137
int zoom = DPIUtil.getDeviceZoom();
@@ -42,7 +48,7 @@ public void gcZoomLevelMustChangeOnShellZoomChange() {
4248
assertEquals("GCData must have a zoom level equal to the actual zoom level of the widget/shell", DPIUtil.getNativeDeviceZoom(), (int) gcNativeZoom.join());
4349

4450
int newSWTZoom = zoom * 2;
45-
changeDPIZoom(newSWTZoom);
51+
DPITestUtil.changeDPIZoom(shell, newSWTZoom);
4652
isScaled.set(true);
4753
shell.setVisible(false);
4854
shell.setVisible(true);
@@ -52,6 +58,8 @@ public void gcZoomLevelMustChangeOnShellZoomChange() {
5258

5359
@Test
5460
public void drawnElementsShouldScaleUpToTheRightZoomLevel() {
61+
Shell shell = new Shell(Display.getDefault());
62+
5563
int zoom = DPIUtil.getDeviceZoom();
5664
int scalingFactor = 2;
5765
GC gc = GC.win32_new(shell, new GCData());

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/PathWin32Tests.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@
1919

2020
import org.eclipse.swt.internal.*;
2121
import org.eclipse.swt.internal.gdip.*;
22+
import org.eclipse.swt.widgets.*;
2223
import org.junit.jupiter.api.*;
24+
import org.junit.jupiter.api.extension.*;
2325

24-
class PathWin32Tests extends Win32AutoscaleTestBase {
26+
@ExtendWith(PlatformSpecificExecutionExtension.class)
27+
@ExtendWith(WithMonitorSpecificScalingExtension.class)
28+
class PathWin32Tests {
2529

2630
int zoom = 100;
2731
int scaledZoom = 200;
2832

2933
@Test
3034
public void testPathMustBeScaledOnZoomLevelChange() {
35+
Display display = Display.getDefault();
3136
Path path = new Path(display);
3237
path.addArc(0, 0, 10, 10, 0, 90);
3338
PathData pathData = path.getPathData();
@@ -40,6 +45,7 @@ public void testPathMustBeScaledOnZoomLevelChange() {
4045

4146
@Test
4247
public void testHandlesExistForEachZoomLevelInHashMap() {
48+
Display display = Display.getDefault();
4349
DPIUtil.setDeviceZoom(zoom);
4450
Path path = new Path(display);
4551
path.addArc(0, 0, 10, 10, 0, 90);
@@ -50,6 +56,7 @@ public void testHandlesExistForEachZoomLevelInHashMap() {
5056

5157
@Test
5258
public void testBoundsAreScaledWRTZoomLevel() {
59+
Display display = Display.getDefault();
5360
DPIUtil.setDeviceZoom(zoom);
5461
int scalingFactor = scaledZoom/zoom;
5562
Path path = new Path(display);

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/RegionWin32Tests.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717

1818
import org.eclipse.swt.internal.*;
1919
import org.eclipse.swt.internal.win32.*;
20+
import org.eclipse.swt.widgets.*;
2021
import org.junit.jupiter.api.*;
22+
import org.junit.jupiter.api.extension.*;
2123

22-
class RegionWin32Tests extends Win32AutoscaleTestBase {
24+
@ExtendWith(PlatformSpecificExecutionExtension.class)
25+
@ExtendWith(WithMonitorSpecificScalingExtension.class)
26+
class RegionWin32Tests {
2327

2428
@Test
2529
public void testRegionMustBeScaledOnHandleOfScaledZoomLevel() {
30+
Display display = Display.getDefault();
31+
2632
int zoom = DPIUtil.getDeviceZoom();
2733
int scalingFactor = 2;
2834

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1818

1919
import org.eclipse.swt.internal.*;
20+
import org.eclipse.swt.widgets.*;
2021
import org.junit.jupiter.api.*;
22+
import org.junit.jupiter.api.extension.*;
2123

22-
class TextLayoutWin32Tests extends Win32AutoscaleTestBase {
24+
@ExtendWith(PlatformSpecificExecutionExtension.class)
25+
@ExtendWith(WithMonitorSpecificScalingExtension.class)
26+
class TextLayoutWin32Tests {
2327
final static String text = "This is a text for testing.";
2428

2529
@Test
2630
public void testGetBoundPublicAPIshouldReturnTheSameValueRegardlessOfZoomLevel() {
31+
Display display = Display.getDefault();
32+
2733
final TextLayout layout = new TextLayout(display);
2834
GCData unscaledData = new GCData();
2935
unscaledData.nativeZoom = DPIUtil.getNativeDeviceZoom();
@@ -44,14 +50,17 @@ public void testGetBoundPublicAPIshouldReturnTheSameValueRegardlessOfZoomLevel()
4450

4551
@Test
4652
public void testCalculateGetBoundsWithVerticalIndent() {
53+
Display display = Display.getDefault();
54+
Shell shell = new Shell(display);
55+
4756
TextLayout layout = new TextLayout(display);
4857
layout.setVerticalIndent(16);
4958
layout.setText(text);
5059
Rectangle unscaledBounds = layout.getBounds();
5160

5261
int scalingFactor = 2;
5362
int newZoom = DPIUtil.getNativeDeviceZoom() * scalingFactor;
54-
changeDPIZoom(newZoom);
63+
DPITestUtil.changeDPIZoom(shell, newZoom);
5564
TextLayout scaledLayout = new TextLayout(display);
5665
scaledLayout.setVerticalIndent(16);
5766
scaledLayout.setText(text);

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TransformWin32Tests.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818

1919
import org.eclipse.swt.internal.*;
2020
import org.eclipse.swt.internal.gdip.*;
21+
import org.eclipse.swt.widgets.*;
2122
import org.junit.jupiter.api.*;
23+
import org.junit.jupiter.api.extension.*;
2224

23-
class TransformWin32Tests extends Win32AutoscaleTestBase {
25+
@ExtendWith(PlatformSpecificExecutionExtension.class)
26+
@ExtendWith(WithMonitorSpecificScalingExtension.class)
27+
class TransformWin32Tests {
2428

2529
@Test
2630
public void testShouldHaveDifferentHandlesAtDifferentZoomLevels() {
31+
Display display = Display.getDefault();
2732
int zoom = DPIUtil.getDeviceZoom();
2833
Transform transform = new Transform(display);
2934
long scaledHandle = transform.getHandle(zoom * 2);
@@ -34,6 +39,7 @@ public void testShouldHaveDifferentHandlesAtDifferentZoomLevels() {
3439

3540
@Test
3641
public void testScaledTrasformMustHaveScaledValues() {
42+
Display display = Display.getDefault();
3743
int zoom = DPIUtil.getDeviceZoom();
3844
Transform transform = new Transform(display, 0, 0, 0, 0, 4, 2);
3945
float[] elements = new float[6];
+5-24
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,16 @@
1414
package org.eclipse.swt.internal;
1515

1616
import org.eclipse.swt.widgets.*;
17-
import org.junit.jupiter.api.*;
1817

19-
public abstract class Win32AutoscaleTestBase {
20-
protected Display display;
21-
protected Shell shell;
18+
public final class DPITestUtil {
2219

23-
@BeforeAll
24-
public static void assumeIsFittingPlatform() {
25-
PlatformSpecificExecution.assumeIsFittingPlatform();
20+
private DPITestUtil() {
2621
}
2722

28-
@BeforeEach
29-
public void setUpTest() {
30-
display = Display.getDefault();
31-
display.setRescalingAtRuntime(true);
32-
shell = new Shell(display);
33-
}
34-
35-
@AfterEach
36-
public void tearDownTest() {
37-
if (shell != null) {
38-
shell.dispose();
39-
}
40-
display.dispose();
41-
}
42-
43-
protected void changeDPIZoom (int nativeZoom) {
23+
public static void changeDPIZoom (Shell shell, int nativeZoom) {
4424
DPIUtil.setDeviceZoom(nativeZoom);
4525
float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(nativeZoom) / DPIUtil.getZoomForAutoscaleProperty(shell.nativeZoom);
4626
DPIZoomChangeRegistry.applyChange(shell, nativeZoom, scalingFactor);
4727
}
48-
}
28+
29+
}

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/DefaultSWTFontRegistryTests.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020
import org.eclipse.swt.graphics.*;
2121
import org.eclipse.swt.widgets.*;
2222
import org.junit.jupiter.api.*;
23+
import org.junit.jupiter.api.extension.*;
2324

25+
@ExtendWith(PlatformSpecificExecutionExtension.class)
2426
class DefaultSWTFontRegistryTests {
2527
private static String TEST_FONT = "Helvetica";
2628
private Display display;
2729
private SWTFontRegistry fontRegistry;
2830

29-
@BeforeAll
30-
public static void assumeIsFittingPlatform() {
31-
PlatformSpecificExecution.assumeIsFittingPlatform();
32-
}
33-
3431
@BeforeEach
3532
public void setUp() {
3633
this.display = Display.getDefault();
@@ -42,6 +39,7 @@ public void tearDown() {
4239
if (this.fontRegistry != null) {
4340
this.fontRegistry.dispose();
4441
}
42+
display.dispose();
4543
}
4644

4745
@Test

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/PlatformSpecificExecution.java renamed to bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/PlatformSpecificExecutionExtension.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414

1515
import java.net.*;
1616

17-
public final class PlatformSpecificExecution {
18-
private PlatformSpecificExecution() {
17+
import org.junit.jupiter.api.extension.*;
18+
19+
public final class PlatformSpecificExecutionExtension implements BeforeAllCallback {
20+
private PlatformSpecificExecutionExtension() {
1921
}
2022

21-
public static void assumeIsFittingPlatform() {
23+
@Override
24+
public void beforeAll(ExtensionContext context) throws Exception {
2225
assumeTrue("test is specific for Windows", isFittingOS());
2326
assumeTrue("architecture of platform does not match", isFittingArchitecture());
2427
}
@@ -28,7 +31,7 @@ private static boolean isFittingOS() {
2831
}
2932

3033
private static boolean isFittingArchitecture() {
31-
Class<?> thisClass = PlatformSpecificExecution.class;
34+
Class<?> thisClass = PlatformSpecificExecutionExtension.class;
3235
String thisClassResourcePath = thisClass.getName().replace('.', '/') + ".class";
3336
URL thisClassURL = thisClass.getClassLoader().getResource(thisClassResourcePath); //$NON-NLS-1$
3437
return thisClassURL.toString().contains(Library.arch());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik GmbH
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.swt.internal;
12+
13+
import org.eclipse.swt.widgets.*;
14+
import org.junit.jupiter.api.extension.*;
15+
16+
/**
17+
* Resets the monitor-specific scaling configuration after the test has been executed.
18+
* Disposes the default display before and after test execution.
19+
*/
20+
public sealed class ResetMonitorSpecificScalingExtension implements BeforeEachCallback, AfterEachCallback permits WithMonitorSpecificScalingExtension {
21+
private boolean wasMonitorSpecificScalingActive;
22+
23+
protected ResetMonitorSpecificScalingExtension() {
24+
}
25+
26+
@Override
27+
public void beforeEach(ExtensionContext context) throws Exception {
28+
wasMonitorSpecificScalingActive = DPIUtil.isMonitorSpecificScalingActive();
29+
Display.getDefault().dispose();
30+
}
31+
32+
@Override
33+
public void afterEach(ExtensionContext context) throws Exception {
34+
DPIUtil.setMonitorSpecificScaling(wasMonitorSpecificScalingActive);
35+
Display.getDefault().dispose();
36+
}
37+
38+
}

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/ScalingSWTFontRegistryTests.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@
1919
import org.eclipse.swt.graphics.*;
2020
import org.eclipse.swt.widgets.*;
2121
import org.junit.jupiter.api.*;
22+
import org.junit.jupiter.api.extension.*;
2223

24+
@ExtendWith(PlatformSpecificExecutionExtension.class)
2325
class ScalingSWTFontRegistryTests {
2426
private static String TEST_FONT = "Helvetica";
2527
private SWTFontRegistry fontRegistry;
2628

27-
@BeforeAll
28-
public static void assumeIsFittingPlatform() {
29-
PlatformSpecificExecution.assumeIsFittingPlatform();
30-
}
31-
3229
@BeforeEach
3330
public void setUp() {
3431
this.fontRegistry = new ScalingSWTFontRegistry(Display.getDefault());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik GmbH
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.swt.internal;
12+
13+
import org.junit.jupiter.api.extension.*;
14+
15+
/**
16+
* Runs a test with monitor specific scaling. Disposes the default display before and after execution.
17+
*/
18+
public final class WithMonitorSpecificScalingExtension extends ResetMonitorSpecificScalingExtension {
19+
20+
private WithMonitorSpecificScalingExtension() {
21+
super();
22+
}
23+
24+
@Override
25+
public void beforeEach(ExtensionContext context) throws Exception {
26+
super.beforeEach(context);
27+
DPIUtil.setMonitorSpecificScaling(true);
28+
}
29+
30+
}

0 commit comments

Comments
 (0)