Skip to content

Commit 34becc0

Browse files
committed
New test for Clipboard
Testing the Clipboard can be difficult because there are a lot of system issues that can interfere, but of particular concern are: 1. System clipboard managers which may take ownership of the clipboard at unexpected times. 2. Limitations as to when processes can access clipboard, such as on Wayland where only active window can access clipboard. 3. Different behaviour when copying within a single process than between processes. These tests aim to resolve these issues. For the system clipboard manager there are a lot of extra sleep calls to allow clipboard manager to complete operations before continuing tests. In addition, we run all the tests multiple times by default to ensure stability. For the process limitations, we carefully control when the shell is created because we often cannot get focus back when shell ends up in the background. See the openAndFocusShell and openAndFocusRemote methods. For the different behaviours, we spin up a simple Swing app in a new process (the "remote" in openAndFocusRemote above). This app can be directed, over RMI, to access the clipboard. This allows our test to place data on the clipboard and ensure that the remote app can read the data successfully. For now this test only covers basic text (and a little of RTF). Adding Image and other transfers is part of the future work as such functionality is added in GTK4 while working on #2126 For the changes to SwtTestUtil that we required: 1. isGTK4 moved from Test_org_eclipse_swt_widgets_Shell.java to the Utils 2. processEvents was limited to 20 readAndDispatch calls per second due to the ordering of the targetTimestamp check. This change allows full speed readAndDispatching. 3. getPath was refactored to allow better control of source and destination of files extracted. See extracting of class files for remote Swing app in startRemoteClipboardCommands method Part of #2126 Split out of #2538
1 parent bf5544d commit 34becc0

File tree

7 files changed

+649
-20
lines changed

7 files changed

+649
-20
lines changed

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/AllNonBrowserTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
Test_org_eclipse_swt_accessibility_AccessibleControlEvent.class, //
4444
Test_org_eclipse_swt_accessibility_AccessibleEvent.class, //
4545
Test_org_eclipse_swt_accessibility_AccessibleTextEvent.class, //
46+
Test_org_eclipse_swt_dnd_Clipboard.class,
4647
Test_org_eclipse_swt_events_ArmEvent.class, //
4748
Test_org_eclipse_swt_events_ControlEvent.class, //
4849
Test_org_eclipse_swt_events_DisposeEvent.class, //

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public class SwtTestUtil {
105105

106106
public final static boolean isX11 = isGTK
107107
&& "x11".equals(System.getProperty("org.eclipse.swt.internal.gdk.backend"));
108-
108+
public final static boolean isGTK4 = isGTK
109+
&& System.getProperty("org.eclipse.swt.internal.gtk.version", "").startsWith("4");
109110

110111
/**
111112
* The palette used by images. See {@link #getAllPixels(Image)} and {@link #createImage}
@@ -400,13 +401,16 @@ public static void processEvents(int timeoutMs, BooleanSupplier breakCondition)
400401
long targetTimestamp = System.currentTimeMillis() + timeoutMs;
401402
Display display = Display.getCurrent();
402403
while (!breakCondition.getAsBoolean()) {
403-
if (!display.readAndDispatch()) {
404-
if (System.currentTimeMillis() < targetTimestamp) {
405-
Thread.sleep(50);
406-
} else {
404+
while (display.readAndDispatch()) {
405+
if (System.currentTimeMillis() >= targetTimestamp) {
407406
return;
408407
}
409408
}
409+
if (System.currentTimeMillis() < targetTimestamp) {
410+
Thread.sleep(50);
411+
} else {
412+
return;
413+
}
410414
}
411415
}
412416

@@ -583,18 +587,24 @@ public static boolean hasPixelNotMatching(Image image, Color nonMatchingColor, R
583587
}
584588

585589
public static Path getPath(String fileName, TemporaryFolder tempFolder) {
586-
Path filePath = tempFolder.getRoot().toPath().resolve("image-resources").resolve(Path.of(fileName));
587-
if (!Files.isRegularFile(filePath)) {
590+
Path path = tempFolder.getRoot().toPath();
591+
Path filePath = path.resolve("image-resources").resolve(Path.of(fileName));
592+
return getPath(fileName, filePath);
593+
}
594+
595+
public static Path getPath(String sourceFilename, Path destinationPath) {
596+
if (!Files.isRegularFile(destinationPath)) {
588597
// Extract resource on the classpath to a temporary file to ensure it's
589598
// available as plain file, even if this bundle is packed as jar
590-
try (InputStream inStream = SwtTestUtil.class.getResourceAsStream(fileName)) {
591-
assertNotNull(inStream, "InputStream == null for file " + fileName);
592-
Files.createDirectories(filePath.getParent());
593-
Files.copy(inStream, filePath);
599+
try (InputStream inStream = SwtTestUtil.class.getResourceAsStream(sourceFilename)) {
600+
assertNotNull(inStream, "InputStream == null for file " + sourceFilename);
601+
Files.createDirectories(destinationPath.getParent());
602+
Files.copy(inStream, destinationPath);
594603
} catch (IOException e) {
595604
throw new IllegalArgumentException(e);
596605
}
597606
}
598-
return filePath;
607+
return destinationPath;
599608
}
609+
600610
}

0 commit comments

Comments
 (0)