From 2dc0189d401a8e8297c69c81dbdb46a60aecb082 Mon Sep 17 00:00:00 2001 From: Vasili Gulevich Date: Fri, 22 Nov 2024 17:24:49 +0400 Subject: [PATCH] [GTK3] Test Tree crash in SetData event The failure only happens in Ubuntu 24 (GTK 3.24.41) --- .../Test_org_eclipse_swt_widgets_Tree.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Tree.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Tree.java index cefb3846c7..19cdf1fa04 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Tree.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Tree.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.swt.tests.junit; +import static java.lang.System.currentTimeMillis; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -27,6 +28,7 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.events.TreeListener; import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; @@ -967,6 +969,57 @@ public void test_Virtual() { dataCounter[0] > visibleCount / 2 && dataCounter[0] <= visibleCount * 3); } +/** Ensure setText() and setImage() can be set from SetData handler. +@see https://github.com/eclipse-platform/eclipse.platform.swt/issues/678 +**/ +@Test +public void test_setData() { + tree.dispose(); + disposedIntentionally = true; + Image image = new Image(Display.getCurrent(), 20, 20); + try { + shell.setSize(200, 200); + shell.setLayout(new FillLayout()); + shell.open(); + waitUntilIdle(); + for (int i = 0; i < 200; i++) { + Tree tree = new Tree(shell, SWT.VIRTUAL); + tree.addListener(SWT.SetData, e -> { + TreeItem item = (TreeItem) e.item; + item.setText(0, "A"); + item.setImage(image); // <-- this is the critical line! + }); + tree.setItemCount(1); + + waitUntilIdle(); // may crash while processing asynchronous events + + assertEquals("A", tree.getItem(0).getText(0)); + assertEquals(image, tree.getItem(0).getImage()); + tree.dispose(); + } + } finally { + image.dispose(); + } +} + +private void waitUntilIdle() { + long lastActive = currentTimeMillis(); + while (true) { + if (Thread.interrupted()) { + throw new AssertionError(); + } + if (Display.getCurrent().readAndDispatch()) { + lastActive = currentTimeMillis(); + } else { + if (lastActive + 10 < currentTimeMillis()) { + return; + } + Thread.yield(); + } + } +} + + @Test public void test_emptinessChanged() { int NOT_EMPTY = 0; @@ -1182,4 +1235,6 @@ public void test_setItemCount_itemCount2() { }); } + + }