|
19 | 19 | import java.util.Arrays; |
20 | 20 | import java.util.concurrent.CompletableFuture; |
21 | 21 |
|
| 22 | +import org.eclipse.swt.SWT; |
22 | 23 | import org.eclipse.swt.dnd.DND; |
23 | 24 | import org.eclipse.swt.dnd.RTFTransfer; |
24 | 25 | import org.eclipse.swt.dnd.TextTransfer; |
25 | 26 | import org.eclipse.swt.dnd.Transfer; |
26 | 27 | import org.eclipse.swt.dnd.TransferData; |
| 28 | +import org.eclipse.swt.widgets.Shell; |
| 29 | +import org.eclipse.swt.widgets.Text; |
27 | 30 | import org.junit.jupiter.api.BeforeEach; |
28 | 31 | import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
29 | 32 | import org.junit.jupiter.api.Order; |
@@ -243,4 +246,137 @@ public void test_getAvailableTypes(int clipboardId) throws Exception { |
243 | 246 | availableTypes = clipboard.getAvailableTypes(clipboardId); |
244 | 247 | assertTrue(Arrays.stream(availableTypes).anyMatch(rtfTransfer::isSupportedType)); |
245 | 248 | } |
| 249 | + |
| 250 | + /** |
| 251 | + * tear down and start again without clearing off the clipboard |
| 252 | + */ |
| 253 | + private void tearDownAndStartAgain() { |
| 254 | + // we can't call tearDown because we don't want to clear the clipboard |
| 255 | + clipboard.dispose(); |
| 256 | + clipboard = null; |
| 257 | + shell.dispose(); |
| 258 | + shell = null; |
| 259 | + SwtTestUtil.processEvents(); |
| 260 | + |
| 261 | + // tearDown doesn't dispose on each test, but the error we are checking |
| 262 | + // for happens when Display is disposed (causing ClipboardProxy to be disposed) |
| 263 | + display.dispose(); |
| 264 | + display = null; |
| 265 | + |
| 266 | + // start back up |
| 267 | + setUp(); |
| 268 | + } |
| 269 | + |
| 270 | + private void checkStderrForGTK3Warning(String stderr) { |
| 271 | + if (SwtTestUtil.isGTK3()) { |
| 272 | + if ("***WARNING: Attempt to access SWT clipboard after disposing SWT Display.\n".equals(stderr)) { |
| 273 | + // test passed (VM didn't SIGSEGV) and we exercise the problematic code causing |
| 274 | + // https://github.com/eclipse-platform/eclipse.platform.swt/issues/2675 |
| 275 | + } else if (stderr.isEmpty()) { |
| 276 | + assumeTrue(false, """ |
| 277 | + test passed (VM didn't SIGSEGV) but we didn't exercise the problematic code causing |
| 278 | + https://github.com/eclipse-platform/eclipse.platform.swt/issues/2675 |
| 279 | + """); |
| 280 | + } else { |
| 281 | + // test passed (VM didn't SIGSEGV) but we don't know why there is other stderr |
| 282 | + // output, log it to the test results for further investigation. |
| 283 | + assertEquals("", stderr); |
| 284 | + } |
| 285 | + } else { |
| 286 | + assertEquals("", stderr); |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + private Text setContentsOnClipboardAndDisposeDisplay() throws InterruptedException { |
| 291 | + openAndFocusShell(false); |
| 292 | + |
| 293 | + String helloWorld = getUniqueTestString(); |
| 294 | + clipboard.setContents(new Object[] { helloWorld }, new Transfer[] { textTransfer }); |
| 295 | + assertEquals(helloWorld, clipboard.getContents(textTransfer)); |
| 296 | + |
| 297 | + tearDownAndStartAgain(); |
| 298 | + shell = new Shell(display); |
| 299 | + Text text = new Text(shell, SWT.SINGLE); |
| 300 | + String textBoxHello = getUniqueTestString(); |
| 301 | + text.setText(textBoxHello); |
| 302 | + text.setSelection(0, textBoxHello.length()); |
| 303 | + |
| 304 | + return text; |
| 305 | + } |
| 306 | + |
| 307 | + /** |
| 308 | + * Regression test for |
| 309 | + * https://github.com/eclipse-platform/eclipse.platform.swt/issues/2675 |
| 310 | + */ |
| 311 | + @Test |
| 312 | + public void test_AfterNewDisplay_TextCopy() throws Exception { |
| 313 | + Text text = setContentsOnClipboardAndDisposeDisplay(); |
| 314 | + SwtTestUtil.openShell(shell); |
| 315 | + |
| 316 | + /* |
| 317 | + * on GTK3 this Text.copy would trigger SIGSEGV on GTK calling disposed callback |
| 318 | + * ClipboardProxy.clearFunc |
| 319 | + */ |
| 320 | + String stderr = SwtTestUtil.runWithCapturedStderr(() -> text.copy()); |
| 321 | + checkStderrForGTK3Warning(stderr); |
| 322 | + |
| 323 | + // After the test, clear out the partially disposed clipboard by taking |
| 324 | + // ownership and then clearing it |
| 325 | + clipboard.setContents(new Object[] { getUniqueTestString() }, new Transfer[] { textTransfer }); |
| 326 | + clipboard.clearContents(); |
| 327 | + } |
| 328 | + |
| 329 | + /** |
| 330 | + * Regression test for |
| 331 | + * https://github.com/eclipse-platform/eclipse.platform.swt/issues/2675 |
| 332 | + */ |
| 333 | + @Test |
| 334 | + public void test_AfterNewDisplay_LocalSetContents() throws Exception { |
| 335 | + setContentsOnClipboardAndDisposeDisplay(); |
| 336 | + SwtTestUtil.openShell(shell); |
| 337 | + |
| 338 | + /* |
| 339 | + * on GTK3 this setContents would lead to undefined behavior because when the |
| 340 | + * ClipboardProxy called bind on the new ClipboardProxy.clearFunc there was a |
| 341 | + * pretty good chance it would bind to the same entry in the callback table. But |
| 342 | + * depending on exact order of operations that table entry could be empty, |
| 343 | + * leading to SIGSEGV. |
| 344 | + */ |
| 345 | + String stderr = SwtTestUtil.runWithCapturedStderr( |
| 346 | + () -> clipboard.setContents(new Object[] { getUniqueTestString() }, new Transfer[] { textTransfer })); |
| 347 | + checkStderrForGTK3Warning(stderr); |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * Regression test for |
| 352 | + * https://github.com/eclipse-platform/eclipse.platform.swt/issues/2675 |
| 353 | + */ |
| 354 | + @Test |
| 355 | + public void test_AfterNewDisplay_RemoteGetContents() throws Exception { |
| 356 | + setContentsOnClipboardAndDisposeDisplay(); |
| 357 | + openAndFocusRemote(); |
| 358 | + /* |
| 359 | + * on GTK3 this getting from the remote would trigger SIGSEGV on GTK calling |
| 360 | + * disposed callback ClipboardProxy.getFunc |
| 361 | + * https://github.com/eclipse-platform/eclipse.platform.swt/issues/2675 |
| 362 | + * |
| 363 | + * Caveat: What is available on the clipboard after Display dispose is very |
| 364 | + * platform and configuration dependent and no guarantees in the SWT API exist. |
| 365 | + * Therefore the return value of remote.getStringContents() is not checked here. |
| 366 | + */ |
| 367 | + String stderr = SwtTestUtil |
| 368 | + .runWithCapturedStderr(() -> SwtTestUtil.runOperationInThread(() -> remote.getStringContents())); |
| 369 | + checkStderrForGTK3Warning(stderr); |
| 370 | + |
| 371 | + /* |
| 372 | + * On GTK3 At this point the old ClipboardProxy has not been disposed because |
| 373 | + * this process still owns the clipboard from the original setContents done at |
| 374 | + * the beginning of the test. Therefore set some new contents so that the old |
| 375 | + * contents can be cleared. |
| 376 | + */ |
| 377 | + SwtTestUtil.openShell(shell); |
| 378 | + stderr = SwtTestUtil.runWithCapturedStderr( |
| 379 | + () -> clipboard.setContents(new Object[] { getUniqueTestString() }, new Transfer[] { textTransfer })); |
| 380 | + checkStderrForGTK3Warning(stderr); |
| 381 | + } |
246 | 382 | } |
0 commit comments