|
14 | 14 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
15 | 15 | import static org.junit.jupiter.api.Assertions.assertNull; |
16 | 16 | import static org.junit.jupiter.api.Assertions.assertTrue; |
17 | | -import static org.junit.jupiter.api.Assumptions.assumeFalse; |
18 | 17 | import static org.junit.jupiter.api.Assumptions.assumeTrue; |
19 | 18 |
|
20 | 19 | import java.util.Arrays; |
21 | | -import java.util.List; |
22 | 20 | import java.util.concurrent.CompletableFuture; |
23 | | -import java.util.concurrent.atomic.AtomicBoolean; |
24 | 21 |
|
25 | | -import org.eclipse.swt.SWT; |
26 | | -import org.eclipse.swt.dnd.Clipboard; |
27 | 22 | import org.eclipse.swt.dnd.DND; |
28 | 23 | import org.eclipse.swt.dnd.RTFTransfer; |
29 | 24 | import org.eclipse.swt.dnd.TextTransfer; |
30 | 25 | import org.eclipse.swt.dnd.Transfer; |
31 | 26 | import org.eclipse.swt.dnd.TransferData; |
32 | | -import org.eclipse.swt.layout.RowLayout; |
33 | | -import org.eclipse.swt.widgets.Button; |
34 | | -import org.eclipse.swt.widgets.Display; |
35 | | -import org.eclipse.swt.widgets.Label; |
36 | | -import org.eclipse.swt.widgets.Shell; |
37 | | -import org.junit.jupiter.api.AfterEach; |
38 | 27 | import org.junit.jupiter.api.BeforeEach; |
39 | 28 | import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
40 | 29 | import org.junit.jupiter.api.Order; |
41 | | -import org.junit.jupiter.api.RepeatedTest; |
42 | 30 | import org.junit.jupiter.api.Tag; |
43 | 31 | import org.junit.jupiter.api.Test; |
44 | 32 | import org.junit.jupiter.api.TestMethodOrder; |
|
54 | 42 | */ |
55 | 43 | @Tag("clipboard") |
56 | 44 | @TestMethodOrder(OrderAnnotation.class) // run tests needing button presses first |
57 | | -public class Test_org_eclipse_swt_dnd_Clipboard { |
| 45 | +public class Test_org_eclipse_swt_dnd_Clipboard extends ClipboardBase { |
58 | 46 |
|
59 | | - /** |
60 | | - * See {@link #openAndFocusShell(boolean)} - some tests require user to actually |
61 | | - * interact with the shell. |
62 | | - * |
63 | | - * Default to skipping tests requiring "real" activation on GHA and Jenkins. |
64 | | - * |
65 | | - * <code>true</code>: skip tests <code>false</code>: don't skip tests |
66 | | - * <code>null</code>: unknown whether to skip tests yet |
67 | | - */ |
68 | | - private static Boolean skipTestsRequiringButtonPress = (Boolean.parseBoolean(System.getenv("GITHUB_ACTIONS")) |
69 | | - || System.getenv("JOB_NAME") != null) ? true : null; |
70 | | - private static int uniqueId = 1; |
71 | | - private Display display; |
72 | | - private Shell shell; |
73 | | - private Clipboard clipboard; |
74 | 47 | private TextTransfer textTransfer; |
75 | 48 | private RTFTransfer rtfTransfer; |
76 | | - private RemoteClipboard remote; |
77 | 49 |
|
| 50 | + @Override |
78 | 51 | @BeforeEach |
79 | 52 | public void setUp() { |
80 | | - display = Display.getCurrent(); |
81 | | - if (display == null) { |
82 | | - display = Display.getDefault(); |
83 | | - } |
84 | | - clipboard = new Clipboard(display); |
| 53 | + super.setUp(); |
85 | 54 | textTransfer = TextTransfer.getInstance(); |
86 | 55 | rtfTransfer = RTFTransfer.getInstance(); |
87 | 56 | } |
88 | 57 |
|
89 | | - /** |
90 | | - * Note: Wayland backend does not allow access to system clipboard from |
91 | | - * non-focussed windows. So we have to create/open and focus a window here so |
92 | | - * that clipboard operations work. |
93 | | - * |
94 | | - * Additionally, if we want to provide data to the clipboard, we require user |
95 | | - * interaction on the created shell. Therefore if forSetContents is true the |
96 | | - * tester needs to press a button for test to work. |
97 | | - * |
98 | | - * If there is no user interaction (button not pressed) then the test is |
99 | | - * skipped, rather than failed, and subsequent tests requiring user interaction |
100 | | - * as skipped too. See {@link #skipTestsRequiringButtonPress} |
101 | | - */ |
102 | | - private void openAndFocusShell(boolean forSetContents) throws InterruptedException { |
103 | | - assertNull(shell); |
104 | | - shell = new Shell(display); |
105 | | - |
106 | | - boolean requireUserPress = forSetContents && SwtTestUtil.isWayland(); |
107 | | - if (requireUserPress) { |
108 | | - assumeFalse(skipTestsRequiringButtonPress != null && skipTestsRequiringButtonPress, |
109 | | - "Skipping tests that require user input"); |
110 | | - |
111 | | - AtomicBoolean pressed = new AtomicBoolean(false); |
112 | | - shell.setLayout(new RowLayout(SWT.VERTICAL)); |
113 | | - Button button = new Button(shell, SWT.PUSH); |
114 | | - button.setText("Press me!"); |
115 | | - button.addListener(SWT.Selection, (e) -> pressed.set(true)); |
116 | | - button.setSize(200, 50); |
117 | | - Label label = new Label(shell, SWT.NONE); |
118 | | - label.setText(""" |
119 | | - Press the button to tell Wayland that you really want this window to have access to clipboard. |
120 | | - This is needed on Wayland because only really focussed programs are allowed to write to the |
121 | | - global keyboard. |
122 | | -
|
123 | | - If you don't press this button soon, the test will be skipped and you won't be asked again. |
124 | | - """); |
125 | | - Label timeleft = new Label(shell, SWT.NONE); |
126 | | - timeleft.setText("Time left to press button: XXXXXXXXXXXXXXXXXXXX seconds"); |
127 | | - |
128 | | - SwtTestUtil.openShell(shell); |
129 | | - |
130 | | - // If we know there is a tester pressing the buttons, allow them |
131 | | - // a little grace on the timeout. If we don't know if there is a |
132 | | - // tester around, skip tests fairly quickly and don't |
133 | | - // ask again. |
134 | | - int timeout = skipTestsRequiringButtonPress == null ? 1500 : 10000; |
135 | | - long startTime = System.nanoTime(); |
136 | | - SwtTestUtil.processEvents(timeout, () -> { |
137 | | - long nowTime = System.nanoTime(); |
138 | | - long timeLeft = nowTime - startTime; |
139 | | - long timeLeftMs = timeout - (timeLeft / 1_000_000); |
140 | | - double timeLeftS = timeLeftMs / 1_000.0d; |
141 | | - timeleft.setText("Time left to press button: " + timeLeftS + " seconds"); |
142 | | - return pressed.get(); |
143 | | - }); |
144 | | - boolean userPressedButton = pressed.get(); |
145 | | - if (userPressedButton) { |
146 | | - skipTestsRequiringButtonPress = false; |
147 | | - } else { |
148 | | - skipTestsRequiringButtonPress = true; |
149 | | - assumeTrue(false, "Skipping tests that require user input"); |
150 | | - } |
151 | | - } else { |
152 | | - SwtTestUtil.openShell(shell); |
153 | | - } |
154 | | - |
155 | | - } |
156 | | - |
157 | | - /** |
158 | | - * Note: Wayland backend does not allow access to system clipboard from |
159 | | - * non-focussed windows. So we have to open and focus remote here so that |
160 | | - * clipboard operations work. |
161 | | - */ |
162 | | - private void openAndFocusRemote() throws Exception { |
163 | | - assertNull(remote); |
164 | | - remote = new RemoteClipboard(); |
165 | | - remote.start(); |
166 | | - |
167 | | - /* |
168 | | - * If/when OpenJDK Project Wakefield gets merged then we may need to wait for |
169 | | - * button pressed on the swing app just like the SWT app. This may also be |
170 | | - * needed if Wayland implementations get more restrictive on X apps too. |
171 | | - */ |
172 | | - // remote.waitForButtonPress(); |
173 | | - } |
174 | | - |
175 | | - @AfterEach |
176 | | - public void tearDown() throws Exception { |
177 | | - try { |
178 | | - if (remote != null) { |
179 | | - remote.stop(); |
180 | | - } |
181 | | - } finally { |
182 | | - if (clipboard != null) { |
183 | | - clipboard.dispose(); |
184 | | - } |
185 | | - if (shell != null) { |
186 | | - shell.dispose(); |
187 | | - } |
188 | | - SwtTestUtil.processEvents(); |
189 | | - } |
190 | | - } |
191 | | - |
192 | | - /** |
193 | | - * Make sure to always copy/paste unique strings - this ensures that tests run |
194 | | - * under {@link RepeatedTest}s don't false pass because of clipboard value on |
195 | | - * previous iteration. |
196 | | - */ |
197 | | - private String getUniqueTestString() { |
198 | | - return "Hello World " + uniqueId++; |
199 | | - } |
200 | | - |
201 | | - /** |
202 | | - * Return the set of clipboards that are supported on this platform, this method |
203 | | - * is referenced in the {@link MethodSource} annotations on the |
204 | | - * {@link ParameterizedTest}s within this class. |
205 | | - */ |
206 | | - public static List<Integer> supportedClipboardIds() { |
207 | | - if (SwtTestUtil.isGTK) { |
208 | | - return List.of(DND.CLIPBOARD, DND.SELECTION_CLIPBOARD); |
209 | | - } |
210 | | - return List.of(DND.CLIPBOARD); |
211 | | - } |
212 | | - |
213 | 58 | /** |
214 | 59 | * Test that the remote application clipboard works |
215 | 60 | */ |
|
0 commit comments