Skip to content

Commit

Permalink
Reduce Win32 FontRegistries visibility and move tests inside fragment
Browse files Browse the repository at this point in the history
The classes DefaultSWTFontRegistry and ScalingSWTFontRegistry are
currently public only to make them testable from a test bundle. There is
currently no pre-configured way to implement unit tests for classes that
are not directly accessible via public API.

With this change, the SWT Win32 fragments are extended by a
configuration for providing fragment-internal unit tests executed with
plain Maven surefire, which can access the classes under test even with
reduced visibility. This configuration is applied to the tests for the
DefaultSWTFontRegistry and ScalingSWTFontRegistry, such that their
visibilities can properly be reduced to package visibility.
  • Loading branch information
HeikoKlare committed Apr 30, 2024
1 parent 04f724a commit faebc93
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bin/
bin_test/
*.log
target/
/.project
Expand Down
6 changes: 6 additions & 0 deletions binaries/.classpath_win32
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@
<classpathentry kind="src" path="Eclipse SWT Browser/win32"/>
<classpathentry kind="src" path="Eclipse SWT OpenGL/win32"/>
<classpathentry kind="src" path="Eclipse SWT OpenGL/common"/>
<classpathentry kind="src" output="bin_test" path="Eclipse SWT Tests/win32">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
5 changes: 5 additions & 0 deletions binaries/org.eclipse.swt.win32.win32.aarch64/.project
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
<type>2</type>
<locationURI>SWT_HOST_PLUGIN/Eclipse%20SWT%20WebKit</locationURI>
</link>
<link>
<name>Eclipse SWT Tests</name>
<type>2</type>
<locationURI>SWT_HOST_PLUGIN/Eclipse%20SWT%20Tests</locationURI>
</link>
</linkedResources>
<variableList>
<variable>
Expand Down
5 changes: 5 additions & 0 deletions binaries/org.eclipse.swt.win32.win32.x86_64/.project
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
<type>2</type>
<locationURI>SWT_HOST_PLUGIN/Eclipse%20SWT%20WebKit</locationURI>
</link>
<link>
<name>Eclipse SWT Tests</name>
<type>2</type>
<locationURI>SWT_HOST_PLUGIN/Eclipse%20SWT%20Tests</locationURI>
</link>
</linkedResources>
<variableList>
<variable>
Expand Down
6 changes: 6 additions & 0 deletions binaries/org.eclipse.swt.win32.win32.x86_64/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ source.. = \
../../bundles/org.eclipse.swt/Eclipse SWT Browser/win32,\
../../bundles/org.eclipse.swt/Eclipse SWT OpenGL/win32,\
../../bundles/org.eclipse.swt/Eclipse SWT OpenGL/common
source.test = \
../../bundles/org.eclipse.swt/Eclipse SWT Tests/win32
output.. = bin/
output.test = bin_test/

pom.model.property.os=win32
pom.model.property.ws=win32
pom.model.property.arch=x86_64

additional.bundles = junit-jupiter-api,\
org.junit
12 changes: 12 additions & 0 deletions binaries/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
<timestampProvider>fragment-host</timestampProvider>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>execute-tests</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.testutil.*;
import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;

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

@Before
@BeforeEach
public void setUp() {
this.display = Display.getDefault();
this.fontRegistry = new DefaultSWTFontRegistry(display);
}

@After
@AfterEach
public void tearDown() {
if (this.fontRegistry != null) {
this.fontRegistry.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,26 @@
*******************************************************************************/
package org.eclipse.swt.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.testutil.*;
import org.eclipse.swt.widgets.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;

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

@Before
@BeforeEach
public void setUp() {
this.fontRegistry = new ScalingSWTFontRegistry(Display.getDefault());
}

@After
@AfterEach
public void tearDown() {
if (this.fontRegistry != null) {
this.fontRegistry.dispose();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.eclipse.swt.testutil;

import static org.junit.Assume.assumeTrue;

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

/**
* JUnit 5 extension to execute test class only when the SWT platform is Win32.
*/
public class OnlyOnWindowExecutionExtension implements BeforeAllCallback {

@Override
public void beforeAll(ExtensionContext context) throws Exception {
assumeTrue("tests are specific for Win32", Platform.PLATFORM.equals("win32"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
* @noreference This class is not intended to be referenced by clients.
* @noinstantiate This class is not intended to be instantiated by clients.
*/
public final class DefaultSWTFontRegistry implements SWTFontRegistry {
final class DefaultSWTFontRegistry implements SWTFontRegistry {
private static FontData KEY_SYSTEM_FONTS = new FontData();
private Map<FontData, Font> fontsMap = new HashMap<>();
private Device device;

public DefaultSWTFontRegistry(Device device) {
DefaultSWTFontRegistry(Device device) {
this.device = device;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @noreference This class is not intended to be referenced by clients.
* @noinstantiate This class is not intended to be instantiated by clients.
*/
public final class ScalingSWTFontRegistry implements SWTFontRegistry {
final class ScalingSWTFontRegistry implements SWTFontRegistry {
private class ScaledFontContainer {
// the first (unknown) font to be requested as scaled variant
// usually it is scaled to the primary monitor zoom, but that is not guaranteed
Expand Down Expand Up @@ -72,7 +72,7 @@ private void addScaledFont(int targetZoom, Font scaledFont) {
private Map<FontData, ScaledFontContainer> fontKeyMap = new HashMap<>();
private Device device;

public ScalingSWTFontRegistry(Device device) {
ScalingSWTFontRegistry(Device device) {
this.device = device;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,15 @@
package org.eclipse.swt.tests.win32;

import org.eclipse.swt.graphics.ImageWin32Tests;
import org.eclipse.swt.internal.DefaultSWTFontRegistryTests;
import org.eclipse.swt.internal.ScalingSWTFontRegistryTests;
import org.eclipse.swt.tests.win32.widgets.TestTreeColumn;
import org.eclipse.swt.tests.win32.widgets.Test_org_eclipse_swt_widgets_Display;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;


@RunWith(Suite.class)
@Suite.SuiteClasses({
DefaultSWTFontRegistryTests.class,
ImageWin32Tests.class,
ScalingSWTFontRegistryTests.class,
Test_org_eclipse_swt_dnd_DND.class,
Test_org_eclipse_swt_events_KeyEvent.class,
Test_org_eclipse_swt_widgets_Display.class,
Expand Down

0 comments on commit faebc93

Please sign in to comment.