From adbd0ca949dc294e67bc31db709ba80c190f372e Mon Sep 17 00:00:00 2001 From: Jonah Graham Date: Thu, 25 Sep 2025 13:58:11 -0400 Subject: [PATCH] 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 --- ...est_org_eclipse_swt_custom_StyledText.java | 37 ++++++++----------- .../Test_org_eclipse_swt_widgets_Text.java | 36 +++++++----------- 2 files changed, 28 insertions(+), 45 deletions(-) diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java index 1947a219be3..9aaaefd5452 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java @@ -26,7 +26,6 @@ import static org.junit.Assert.fail; import static org.junit.Assume.assumeFalse; -import java.time.Instant; import java.util.ArrayList; import java.util.Random; import java.util.function.BooleanSupplier; @@ -5856,38 +5855,32 @@ public void test_arrowDownKeepsPositionAfterNewLine() { * Bug 565164 - SWT.BS event no longer working */ @Test -public void test_backspaceAndDelete() { +public void test_backspaceAndDelete() throws InterruptedException { shell.open(); text.setSize(10, 50); - final Instant timeOut = Instant.now().plusSeconds(10); + // The display.post needs to successfully obtain the focused window (at least on GTK3) + // so we can send events to it. This processEvents gives SWT/GTK time to draw/focus/etc + // the window so that org.eclipse.swt.widgets.Display.findFocusedWindow() + // returns non-zero + SwtTestUtil.processEvents(); Display display = Display.getDefault(); Event a = keyEvent('a', SWT.KeyDown, display.getFocusControl()); Event aUp = keyEvent('a', SWT.KeyUp, display.getFocusControl()); + Event backspace = keyEvent(SWT.BS, SWT.KeyDown, display.getFocusControl()); + Event backspaceUp = keyEvent(SWT.BS, SWT.KeyUp, display.getFocusControl()); - display.post(a); - display.post(aUp); + assertTrue(display.post(a)); + assertTrue(display.post(aUp)); - while (Instant.now().isBefore(timeOut)) { - if (text.getText().length() == 1) break; + SwtTestUtil.processEvents(10000, () -> text.getText().length() == 1); + assertEquals(1, text.getText().length()); - if (!shell.isDisposed()) { - display.readAndDispatch(); - } - } - - // Simulate the backspace, ensuring that the caret is in the correct position - text.invokeAction(ST.DELETE_PREVIOUS); - - while (Instant.now().isBefore(timeOut)) { - if (text.getText().length() == 0) break; - - if (!shell.isDisposed()) { - display.readAndDispatch(); - } - } + assertTrue(display.post(backspace)); + assertTrue(display.post(backspaceUp)); + SwtTestUtil.processEvents(10000, () -> text.getText().length() == 0); assertEquals(0, text.getText().length()); } diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Text.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Text.java index e17b33e3ebf..10a25b75818 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Text.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Text.java @@ -18,8 +18,6 @@ import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import java.time.Instant; - import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SegmentListener; @@ -1552,10 +1550,14 @@ private void doSegmentsTest (boolean isListening) { * Bug 565164 - SWT.BS event no longer working */ @Test -public void test_backspaceAndDelete() { +public void test_backspaceAndDelete() throws InterruptedException { shell.open(); text.setSize(10, 50); - final Instant timeOut = Instant.now().plusSeconds(10); + // The display.post needs to successfully obtain the focused window (at least on GTK3) + // so we can send events to it. This processEvents gives SWT/GTK time to draw/focus/etc + // the window so that org.eclipse.swt.widgets.Display.findFocusedWindow() + // returns non-zero + SwtTestUtil.processEvents(); Display display = Display.getDefault(); @@ -1564,28 +1566,16 @@ public void test_backspaceAndDelete() { Event backspace = keyEvent(SWT.BS, SWT.KeyDown, display.getFocusControl()); Event backspaceUp = keyEvent(SWT.BS, SWT.KeyUp, display.getFocusControl()); - display.post(a); - display.post(aUp); - - while (Instant.now().isBefore(timeOut)) { - if (text.getText().length() == 1) break; - - if (!shell.isDisposed()) { - display.readAndDispatch(); - } - } - - display.post(backspace); - display.post(backspaceUp); + assertTrue(display.post(a)); + assertTrue(display.post(aUp)); - while (Instant.now().isBefore(timeOut)) { - if (text.getText().length() == 0) break; + SwtTestUtil.processEvents(10000, () -> text.getText().length() == 1); + assertEquals(1, text.getText().length()); - if (!shell.isDisposed()) { - display.readAndDispatch(); - } - } + assertTrue(display.post(backspace)); + assertTrue(display.post(backspaceUp)); + SwtTestUtil.processEvents(10000, () -> text.getText().length() == 0); assertEquals(0, text.getText().length()); }