Skip to content

Commit

Permalink
Remove type hierarchy of Win32AutoscaleTestBase
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HeikoKlare committed Jan 15, 2025
1 parent f574463 commit e0b4fe9
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@

import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;

class GCWin32Tests extends Win32AutoscaleTestBase {
@ExtendWith(PlatformSpecificExecutionExtension.class)
@ExtendWith(WithMonitorSpecificScalingExtension.class)
class GCWin32Tests {

@Test
public void gcZoomLevelMustChangeOnShellZoomChange() {
Shell shell = new Shell(Display.getDefault());

CompletableFuture<Integer> gcNativeZoom = new CompletableFuture<>();
CompletableFuture<Integer> scaledGcNativeZoom = new CompletableFuture<>();
int zoom = DPIUtil.getDeviceZoom();
Expand All @@ -42,7 +48,7 @@ public void gcZoomLevelMustChangeOnShellZoomChange() {
assertEquals("GCData must have a zoom level equal to the actual zoom level of the widget/shell", DPIUtil.getNativeDeviceZoom(), (int) gcNativeZoom.join());

int newSWTZoom = zoom * 2;
changeDPIZoom(newSWTZoom);
DPITestUtil.changeDPIZoom(shell, newSWTZoom);
isScaled.set(true);
shell.setVisible(false);
shell.setVisible(true);
Expand All @@ -52,6 +58,8 @@ public void gcZoomLevelMustChangeOnShellZoomChange() {

@Test
public void drawnElementsShouldScaleUpToTheRightZoomLevel() {
Shell shell = new Shell(Display.getDefault());

int zoom = DPIUtil.getDeviceZoom();
int scalingFactor = 2;
GC gc = GC.win32_new(shell, new GCData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@

import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.gdip.*;
import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;

class PathWin32Tests extends Win32AutoscaleTestBase {
@ExtendWith(PlatformSpecificExecutionExtension.class)
@ExtendWith(WithMonitorSpecificScalingExtension.class)
class PathWin32Tests {

int zoom = 100;
int scaledZoom = 200;

@Test
public void testPathMustBeScaledOnZoomLevelChange() {
Display display = Display.getDefault();
Path path = new Path(display);
path.addArc(0, 0, 10, 10, 0, 90);
PathData pathData = path.getPathData();
Expand All @@ -40,6 +45,7 @@ public void testPathMustBeScaledOnZoomLevelChange() {

@Test
public void testHandlesExistForEachZoomLevelInHashMap() {
Display display = Display.getDefault();
DPIUtil.setDeviceZoom(zoom);
Path path = new Path(display);
path.addArc(0, 0, 10, 10, 0, 90);
Expand All @@ -50,6 +56,7 @@ public void testHandlesExistForEachZoomLevelInHashMap() {

@Test
public void testBoundsAreScaledWRTZoomLevel() {
Display display = Display.getDefault();
DPIUtil.setDeviceZoom(zoom);
int scalingFactor = scaledZoom/zoom;
Path path = new Path(display);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@

import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;

class RegionWin32Tests extends Win32AutoscaleTestBase {
@ExtendWith(PlatformSpecificExecutionExtension.class)
@ExtendWith(WithMonitorSpecificScalingExtension.class)
class RegionWin32Tests {

@Test
public void testRegionMustBeScaledOnHandleOfScaledZoomLevel() {
Display display = Display.getDefault();

int zoom = DPIUtil.getDeviceZoom();
int scalingFactor = 2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.eclipse.swt.internal.*;
import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;

class TextLayoutWin32Tests extends Win32AutoscaleTestBase {
@ExtendWith(PlatformSpecificExecutionExtension.class)
@ExtendWith(WithMonitorSpecificScalingExtension.class)
class TextLayoutWin32Tests {
final static String text = "This is a text for testing.";

@Test
public void testGetBoundPublicAPIshouldReturnTheSameValueRegardlessOfZoomLevel() {
Display display = Display.getDefault();

final TextLayout layout = new TextLayout(display);
GCData unscaledData = new GCData();
unscaledData.nativeZoom = DPIUtil.getNativeDeviceZoom();
Expand All @@ -44,14 +50,17 @@ public void testGetBoundPublicAPIshouldReturnTheSameValueRegardlessOfZoomLevel()

@Test
public void testCalculateGetBoundsWithVerticalIndent() {
Display display = Display.getDefault();
Shell shell = new Shell(display);

TextLayout layout = new TextLayout(display);
layout.setVerticalIndent(16);
layout.setText(text);
Rectangle unscaledBounds = layout.getBounds();

int scalingFactor = 2;
int newZoom = DPIUtil.getNativeDeviceZoom() * scalingFactor;
changeDPIZoom(newZoom);
DPITestUtil.changeDPIZoom(shell, newZoom);
TextLayout scaledLayout = new TextLayout(display);
scaledLayout.setVerticalIndent(16);
scaledLayout.setText(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@

import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.gdip.*;
import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;

class TransformWin32Tests extends Win32AutoscaleTestBase {
@ExtendWith(PlatformSpecificExecutionExtension.class)
@ExtendWith(WithMonitorSpecificScalingExtension.class)
class TransformWin32Tests {

@Test
public void testShouldHaveDifferentHandlesAtDifferentZoomLevels() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Transform transform = new Transform(display);
long scaledHandle = transform.getHandle(zoom * 2);
Expand All @@ -34,6 +39,7 @@ public void testShouldHaveDifferentHandlesAtDifferentZoomLevels() {

@Test
public void testScaledTrasformMustHaveScaledValues() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Transform transform = new Transform(display, 0, 0, 0, 0, 4, 2);
float[] elements = new float[6];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,16 @@
package org.eclipse.swt.internal;

import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;

public abstract class Win32AutoscaleTestBase {
protected Display display;
protected Shell shell;
public final class DPITestUtil {

@BeforeAll
public static void assumeIsFittingPlatform() {
PlatformSpecificExecution.assumeIsFittingPlatform();
private DPITestUtil() {
}

@BeforeEach
public void setUpTest() {
display = Display.getDefault();
display.setRescalingAtRuntime(true);
shell = new Shell(display);
}

@AfterEach
public void tearDownTest() {
if (shell != null) {
shell.dispose();
}
display.dispose();
}

protected void changeDPIZoom (int nativeZoom) {
public static void changeDPIZoom (Shell shell, int nativeZoom) {
DPIUtil.setDeviceZoom(nativeZoom);
float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(nativeZoom) / DPIUtil.getZoomForAutoscaleProperty(shell.nativeZoom);
DPIZoomChangeRegistry.applyChange(shell, nativeZoom, scalingFactor);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;

@ExtendWith(PlatformSpecificExecutionExtension.class)
class DefaultSWTFontRegistryTests {
private static String TEST_FONT = "Helvetica";
private Display display;
private SWTFontRegistry fontRegistry;

@BeforeAll
public static void assumeIsFittingPlatform() {
PlatformSpecificExecution.assumeIsFittingPlatform();
}

@BeforeEach
public void setUp() {
this.display = Display.getDefault();
Expand All @@ -42,6 +39,7 @@ public void tearDown() {
if (this.fontRegistry != null) {
this.fontRegistry.dispose();
}
display.dispose();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

import java.net.*;

public final class PlatformSpecificExecution {
private PlatformSpecificExecution() {
import org.junit.jupiter.api.extension.*;

public final class PlatformSpecificExecutionExtension implements BeforeAllCallback {
private PlatformSpecificExecutionExtension() {
}

public static void assumeIsFittingPlatform() {
@Override
public void beforeAll(ExtensionContext context) throws Exception {
assumeTrue("test is specific for Windows", isFittingOS());
assumeTrue("architecture of platform does not match", isFittingArchitecture());
}
Expand All @@ -28,7 +31,7 @@ private static boolean isFittingOS() {
}

private static boolean isFittingArchitecture() {
Class<?> thisClass = PlatformSpecificExecution.class;
Class<?> thisClass = PlatformSpecificExecutionExtension.class;
String thisClassResourcePath = thisClass.getName().replace('.', '/') + ".class";
URL thisClassURL = thisClass.getClassLoader().getResource(thisClassResourcePath); //$NON-NLS-1$
return thisClassURL.toString().contains(Library.arch());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2025 Vector Informatik GmbH
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.swt.internal;

import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.extension.*;

/**
* Resets the monitor-specific scaling configuration after the test has been executed.
* Disposes the default display before and after test execution.
*/
public sealed class ResetMonitorSpecificScalingExtension implements BeforeEachCallback, AfterEachCallback permits WithMonitorSpecificScalingExtension {
private boolean wasMonitorSpecificScalingActive;

protected ResetMonitorSpecificScalingExtension() {
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
wasMonitorSpecificScalingActive = DPIUtil.isMonitorSpecificScalingActive();
Display.getDefault().dispose();
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
DPIUtil.setMonitorSpecificScaling(wasMonitorSpecificScalingActive);
Display.getDefault().dispose();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;

@ExtendWith(PlatformSpecificExecutionExtension.class)
class ScalingSWTFontRegistryTests {
private static String TEST_FONT = "Helvetica";
private SWTFontRegistry fontRegistry;

@BeforeAll
public static void assumeIsFittingPlatform() {
PlatformSpecificExecution.assumeIsFittingPlatform();
}

@BeforeEach
public void setUp() {
this.fontRegistry = new ScalingSWTFontRegistry(Display.getDefault());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright (c) 2025 Vector Informatik GmbH
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.swt.internal;

import org.junit.jupiter.api.extension.*;

/**
* Runs a test with monitor specific scaling. Disposes the default display before and after execution.
*/
public final class WithMonitorSpecificScalingExtension extends ResetMonitorSpecificScalingExtension {

private WithMonitorSpecificScalingExtension() {
super();
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
super.beforeEach(context);
DPIUtil.setMonitorSpecificScaling(true);
}

}
Loading

0 comments on commit e0b4fe9

Please sign in to comment.