|
13 | 13 | *******************************************************************************/
|
14 | 14 | package org.eclipse.swt.tests.junit;
|
15 | 15 |
|
| 16 | +import static java.lang.System.currentTimeMillis; |
16 | 17 | import static org.junit.Assert.assertArrayEquals;
|
17 | 18 | import static org.junit.Assert.assertEquals;
|
18 | 19 | import static org.junit.Assert.assertFalse;
|
|
27 | 28 | import org.eclipse.swt.SWT;
|
28 | 29 | import org.eclipse.swt.events.TreeListener;
|
29 | 30 | import org.eclipse.swt.graphics.Color;
|
| 31 | +import org.eclipse.swt.graphics.Image; |
30 | 32 | import org.eclipse.swt.layout.FillLayout;
|
31 | 33 | import org.eclipse.swt.widgets.Display;
|
32 | 34 | import org.eclipse.swt.widgets.Event;
|
@@ -967,6 +969,57 @@ public void test_Virtual() {
|
967 | 969 | dataCounter[0] > visibleCount / 2 && dataCounter[0] <= visibleCount * 3);
|
968 | 970 | }
|
969 | 971 |
|
| 972 | +/** Ensure setText() and setImage() can be set from SetData handler. |
| 973 | +@see https://github.com/eclipse-platform/eclipse.platform.swt/issues/678 |
| 974 | +**/ |
| 975 | +@Test |
| 976 | +public void test_setData() { |
| 977 | + tree.dispose(); |
| 978 | + disposedIntentionally = true; |
| 979 | + Image image = new Image(Display.getCurrent(), 20, 20); |
| 980 | + try { |
| 981 | + shell.setSize(200, 200); |
| 982 | + shell.setLayout(new FillLayout()); |
| 983 | + shell.open(); |
| 984 | + waitUntilIdle(); |
| 985 | + for (int i = 0; i < 200; i++) { |
| 986 | + Tree tree = new Tree(shell, SWT.VIRTUAL); |
| 987 | + tree.addListener(SWT.SetData, e -> { |
| 988 | + TreeItem item = (TreeItem) e.item; |
| 989 | + item.setText(0, "A"); |
| 990 | + item.setImage(image); // <-- this is the critical line! |
| 991 | + }); |
| 992 | + tree.setItemCount(1); |
| 993 | + |
| 994 | + waitUntilIdle(); // may crash while processing asynchronous events |
| 995 | + |
| 996 | + assertEquals("A", tree.getItem(0).getText(0)); |
| 997 | + assertEquals(image, tree.getItem(0).getImage()); |
| 998 | + tree.dispose(); |
| 999 | + } |
| 1000 | + } finally { |
| 1001 | + image.dispose(); |
| 1002 | + } |
| 1003 | +} |
| 1004 | + |
| 1005 | +private void waitUntilIdle() { |
| 1006 | + long lastActive = currentTimeMillis(); |
| 1007 | + while (true) { |
| 1008 | + if (Thread.interrupted()) { |
| 1009 | + throw new AssertionError(); |
| 1010 | + } |
| 1011 | + if (Display.getCurrent().readAndDispatch()) { |
| 1012 | + lastActive = currentTimeMillis(); |
| 1013 | + } else { |
| 1014 | + if (lastActive + 10 < currentTimeMillis()) { |
| 1015 | + return; |
| 1016 | + } |
| 1017 | + Thread.yield(); |
| 1018 | + } |
| 1019 | + } |
| 1020 | +} |
| 1021 | + |
| 1022 | + |
970 | 1023 | @Test
|
971 | 1024 | public void test_emptinessChanged() {
|
972 | 1025 | int NOT_EMPTY = 0;
|
@@ -1182,4 +1235,6 @@ public void test_setItemCount_itemCount2() {
|
1182 | 1235 | });
|
1183 | 1236 | }
|
1184 | 1237 |
|
| 1238 | + |
| 1239 | + |
1185 | 1240 | }
|
0 commit comments