Skip to content

Commit cc5eb77

Browse files
committed
Make test valid for backspace/delete
This test taking a very long time (10 seconds) on Wayland backend. The test as written was invalid because the loop had a timeout without the exit condition being true. This commit updates the test to: - resolve the false positive by checking condition after timeout loop - using common code, SwtTestUtil.processEvents - asserts on the display.post so the underlying error is flagged as the error - adds SwtTestUtil.processEvents at the beginning of the function to ensure window is focused so that the current windown can be posted to - for styled text changes from invokeAction to posting key events, this is to ensure the original intention of the test worked to ensure we don't have a regression in the future on sending BS event - see https://bugs.eclipse.org/565164. `test_invokeActionI` also has a test for `invokeAction(ST.DELETE_PREVIOUS)` so nothing is lost here
1 parent 333b73e commit cc5eb77

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
lines changed

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

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import static org.junit.Assert.fail;
2727
import static org.junit.Assume.assumeFalse;
2828

29-
import java.time.Instant;
3029
import java.util.ArrayList;
3130
import java.util.Random;
3231
import java.util.function.BooleanSupplier;
@@ -5856,38 +5855,32 @@ public void test_arrowDownKeepsPositionAfterNewLine() {
58565855
* Bug 565164 - SWT.BS event no longer working
58575856
*/
58585857
@Test
5859-
public void test_backspaceAndDelete() {
5858+
public void test_backspaceAndDelete() throws InterruptedException {
58605859
shell.open();
58615860
text.setSize(10, 50);
5862-
final Instant timeOut = Instant.now().plusSeconds(10);
5861+
// The display.post needs to successfully obtain the focused window (at least on GTK3)
5862+
// so we can send events to it. This processEvents gives SWT/GTK time to draw/focus/etc
5863+
// the window so that org.eclipse.swt.widgets.Display.findFocusedWindow()
5864+
// returns non-zero
5865+
SwtTestUtil.processEvents();
58635866

58645867
Display display = Display.getDefault();
58655868

58665869
Event a = keyEvent('a', SWT.KeyDown, display.getFocusControl());
58675870
Event aUp = keyEvent('a', SWT.KeyUp, display.getFocusControl());
5871+
Event backspace = keyEvent(SWT.BS, SWT.KeyDown, display.getFocusControl());
5872+
Event backspaceUp = keyEvent(SWT.BS, SWT.KeyUp, display.getFocusControl());
58685873

5869-
display.post(a);
5870-
display.post(aUp);
5874+
assertTrue(display.post(a));
5875+
assertTrue(display.post(aUp));
58715876

5872-
while (Instant.now().isBefore(timeOut)) {
5873-
if (text.getText().length() == 1) break;
5877+
SwtTestUtil.processEvents(10000, () -> text.getText().length() == 1);
5878+
assertEquals(1, text.getText().length());
58745879

5875-
if (!shell.isDisposed()) {
5876-
display.readAndDispatch();
5877-
}
5878-
}
5879-
5880-
// Simulate the backspace, ensuring that the caret is in the correct position
5881-
text.invokeAction(ST.DELETE_PREVIOUS);
5882-
5883-
while (Instant.now().isBefore(timeOut)) {
5884-
if (text.getText().length() == 0) break;
5885-
5886-
if (!shell.isDisposed()) {
5887-
display.readAndDispatch();
5888-
}
5889-
}
5880+
assertTrue(display.post(backspace));
5881+
assertTrue(display.post(backspaceUp));
58905882

5883+
SwtTestUtil.processEvents(10000, () -> text.getText().length() == 0);
58915884
assertEquals(0, text.getText().length());
58925885
}
58935886

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

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import static org.junit.Assert.assertThrows;
1919
import static org.junit.Assert.assertTrue;
2020

21-
import java.time.Instant;
22-
2321
import org.eclipse.swt.SWT;
2422
import org.eclipse.swt.events.ModifyListener;
2523
import org.eclipse.swt.events.SegmentListener;
@@ -1552,10 +1550,14 @@ private void doSegmentsTest (boolean isListening) {
15521550
* Bug 565164 - SWT.BS event no longer working
15531551
*/
15541552
@Test
1555-
public void test_backspaceAndDelete() {
1553+
public void test_backspaceAndDelete() throws InterruptedException {
15561554
shell.open();
15571555
text.setSize(10, 50);
1558-
final Instant timeOut = Instant.now().plusSeconds(10);
1556+
// The display.post needs to successfully obtain the focused window (at least on GTK3)
1557+
// so we can send events to it. This processEvents gives SWT/GTK time to draw/focus/etc
1558+
// the window so that org.eclipse.swt.widgets.Display.findFocusedWindow()
1559+
// returns non-zero
1560+
SwtTestUtil.processEvents();
15591561

15601562
Display display = Display.getDefault();
15611563

@@ -1564,28 +1566,16 @@ public void test_backspaceAndDelete() {
15641566
Event backspace = keyEvent(SWT.BS, SWT.KeyDown, display.getFocusControl());
15651567
Event backspaceUp = keyEvent(SWT.BS, SWT.KeyUp, display.getFocusControl());
15661568

1567-
display.post(a);
1568-
display.post(aUp);
1569-
1570-
while (Instant.now().isBefore(timeOut)) {
1571-
if (text.getText().length() == 1) break;
1572-
1573-
if (!shell.isDisposed()) {
1574-
display.readAndDispatch();
1575-
}
1576-
}
1577-
1578-
display.post(backspace);
1579-
display.post(backspaceUp);
1569+
assertTrue(display.post(a));
1570+
assertTrue(display.post(aUp));
15801571

1581-
while (Instant.now().isBefore(timeOut)) {
1582-
if (text.getText().length() == 0) break;
1572+
SwtTestUtil.processEvents(10000, () -> text.getText().length() == 1);
1573+
assertEquals(1, text.getText().length());
15831574

1584-
if (!shell.isDisposed()) {
1585-
display.readAndDispatch();
1586-
}
1587-
}
1575+
assertTrue(display.post(backspace));
1576+
assertTrue(display.post(backspaceUp));
15881577

1578+
SwtTestUtil.processEvents(10000, () -> text.getText().length() == 0);
15891579
assertEquals(0, text.getText().length());
15901580
}
15911581

0 commit comments

Comments
 (0)