Skip to content

Commit 696cc30

Browse files
committed
Improve stability of Text cut/copy/paste tests on Windows
The tests for cut/copy/paste functionality of Text widgets in Test_org_eclipse_swt_widgets_Text sporadically fail on Windows. Locally, they fail on almost every execution. This seems to either be caused by some timing issue at the OS or by the need to spin the event queue for processing the cut/copy/paste operations at the OS. In some tests, according event processing for spinning the event loop and waiting for events have been implemented already for GTK. This change adapts all calls to text.cut()/copy()/paste() such that the event queue is spinned. Fixes #106 Fixes #876
1 parent 3e2dbb7 commit 696cc30

1 file changed

Lines changed: 57 additions & 54 deletions

File tree

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

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public void test_computeSizeIIZ() {
268268

269269
@Tag("clipboard")
270270
@Test
271-
public void test_copy() {
271+
public void test_copy() throws InterruptedException {
272272
if (SwtTestUtil.isCocoa) {
273273
// TODO Fix Cocoa failure.
274274
if (SwtTestUtil.verbose) {
@@ -277,52 +277,45 @@ public void test_copy() {
277277
}
278278
return;
279279
}
280-
text.copy();
280+
copyToClipboard(text);
281281

282282
text.selectAll();
283-
text.copy();
283+
copyToClipboard(text);
284284
assertEquals("", text.getSelectionText());
285285

286286
text.setText("00000");
287287
text.selectAll();
288-
text.copy();
288+
copyToClipboard(text);
289289
text.setSelection(2);
290290
assertEquals("", text.getSelectionText());
291291

292+
System.out.println(text.getText());
292293
text.setText("");
293-
text.paste();
294-
// Spin the event loop to let GTK process the clipboard + entry update
295-
Display display = text.getDisplay();
296-
while (display.readAndDispatch()) {
297-
// loop until no more events
298-
}
294+
pasteFromClipboard(text);
299295
assertEquals("00000", text.getText());
300296

301297
// tests a SINGLE line text editor
302298
makeCleanEnvironment(true);
303299

304-
text.copy();
300+
copyToClipboard(text);
305301

306302
text.selectAll();
307-
text.copy();
303+
copyToClipboard(text);
308304
assertEquals("", text.getSelectionText());
309305

310306
text.setText("00000");
311307
text.selectAll();
312-
text.copy();
308+
copyToClipboard(text);
313309
text.setSelection(2);
314310
assertEquals("", text.getSelectionText());
315311

316312
text.setText("");
317-
text.paste();
318-
while (display.readAndDispatch()) {
319-
// loop until no more events
320-
}
313+
pasteFromClipboard(text);
321314
assertEquals("00000", text.getText());
322315
}
323316

324317
@Test
325-
public void test_cut() {
318+
public void test_cut() throws InterruptedException {
326319
if (SwtTestUtil.isCocoa) {
327320
// TODO Fix Cocoa failure.
328321
if (SwtTestUtil.verbose) {
@@ -331,28 +324,28 @@ public void test_cut() {
331324
}
332325
return;
333326
}
334-
text.cut();
327+
cutToClipboard(text);
335328
text.setText("01234567890");
336329
text.setSelection(2, 5);
337-
text.cut();
330+
cutToClipboard(text);
338331
assertEquals("01567890", text.getText());
339332

340333
text.selectAll();
341-
text.cut();
334+
cutToClipboard(text);
342335
assertEquals("", text.getText());
343336

344337
// tests a SINGLE line text editor
345338
makeCleanEnvironment(true);
346339

347-
text.cut();
340+
cutToClipboard(text);
348341

349342
text.setText("01234567890");
350343
text.setSelection(2, 5);
351-
text.cut();
344+
cutToClipboard(text);
352345
assertEquals("01567890", text.getText());
353346

354347
text.selectAll();
355-
text.cut();
348+
cutToClipboard(text);
356349
assertEquals("", text.getText());
357350
}
358351

@@ -946,26 +939,21 @@ public void test_paste() throws InterruptedException {
946939
text.setText("01234567890");
947940
text.setSelection(2, 4);
948941
assertEquals("01234567890", text.getText());
949-
text.copy();
950-
SwtTestUtil.processEvents(100, null);
942+
copyToClipboard(text);
951943
text.setSelection(0);
952-
text.paste();
953-
SwtTestUtil.processEvents(1000, () -> "2301234567890".equals(text.getText()));
944+
pasteFromClipboard(text);
954945
assertEquals("2301234567890", text.getText());
955-
text.copy();
956-
SwtTestUtil.processEvents(100, null);
946+
copyToClipboard(text);
957947
text.setSelection(3);
958-
text.paste();
959-
SwtTestUtil.processEvents(1000, () -> "230231234567890".equals(text.getText()));
948+
pasteFromClipboard(text);
960949
assertEquals("230231234567890", text.getText());
961950

962951
text.setText("0" + delimiterString + "1");
963952
text.selectAll();
964-
text.copy();
953+
copyToClipboard(text);
965954
text.setSelection(0);
966-
text.paste();
955+
pasteFromClipboard(text);
967956
String expected = "0" + delimiterString + "1" + "0" + delimiterString + "1";
968-
SwtTestUtil.processEvents(1000, () -> expected.equals(text.getText()));
969957
assertEquals(expected, text.getText());
970958

971959
// tests a SINGLE line text editor
@@ -974,25 +962,23 @@ public void test_paste() throws InterruptedException {
974962
text.setText("01234567890");
975963
text.setSelection(2, 4);
976964
assertEquals("01234567890", text.getText());
977-
text.copy();
965+
copyToClipboard(text);
978966
text.setSelection(0);
979-
text.paste();
980-
SwtTestUtil.processEvents(1000, () -> "2301234567890".equals(text.getText()));
967+
pasteFromClipboard(text);
981968
assertEquals("2301234567890", text.getText());
982-
text.copy();
969+
copyToClipboard(text);
983970
text.setSelection(3);
984-
text.paste();
985-
SwtTestUtil.processEvents(1000, () -> "230231234567890".equals(text.getText()));
971+
pasteFromClipboard(text);
986972
assertEquals("230231234567890", text.getText());
987973

988974
// tests a SINGLE line text editor
989975
makeCleanEnvironment(true);
990976

991977
text.setText("0" + delimiterString + "1");
992978
text.selectAll();
993-
text.copy();
979+
copyToClipboard(text);
994980
text.setSelection(0);
995-
text.paste();
981+
pasteFromClipboard(text);
996982

997983
if (SwtTestUtil.fCheckSWTPolicy) {
998984
String expected2 = "0" + delimiterString + "1" + "0" + delimiterString + "1";
@@ -1002,7 +988,7 @@ public void test_paste() throws InterruptedException {
1002988
}
1003989

1004990
@Test
1005-
public void test_selectAll() {
991+
public void test_selectAll() throws InterruptedException {
1006992
if (SwtTestUtil.isCocoa) {
1007993
// TODO Fix Cocoa failure.
1008994
if (SwtTestUtil.verbose) {
@@ -1015,14 +1001,14 @@ public void test_selectAll() {
10151001
assertEquals("01234567890", text.getText());
10161002
text.selectAll();
10171003
assertEquals("01234567890", text.getSelectionText());
1018-
text.cut();
1004+
cutToClipboard(text);
10191005
assertEquals("", text.getText());
10201006

10211007
text.setText("01234" + delimiterString+"567890");
10221008
assertEquals("01234" + delimiterString+"567890", text.getText());
10231009
text.selectAll();
10241010
assertEquals("01234" + delimiterString+"567890", text.getSelectionText());
1025-
text.cut();
1011+
cutToClipboard(text);
10261012
assertEquals("", text.getText());
10271013

10281014
// tests a SINGLE line text editor
@@ -1032,7 +1018,7 @@ public void test_selectAll() {
10321018
assertEquals("01234567890", text.getText());
10331019
text.selectAll();
10341020
assertEquals("01234567890", text.getSelectionText());
1035-
text.cut();
1021+
cutToClipboard(text);
10361022
assertEquals("", text.getText());
10371023

10381024
// tests a SINGLE line text editor
@@ -1041,7 +1027,7 @@ public void test_selectAll() {
10411027
assertEquals("01234" + delimiterString+"567890", text.getText());
10421028
text.selectAll();
10431029
assertEquals("01234" + delimiterString+"567890", text.getSelectionText());
1044-
text.cut();
1030+
cutToClipboard(text);
10451031
assertEquals("", text.getText());
10461032
}
10471033
}
@@ -1449,7 +1435,7 @@ public void test_consistency_DragDetect () {
14491435
@Tag("gtk4-todo")
14501436
@Tag("clipboard")
14511437
@Test
1452-
public void test_consistency_Segments () {
1438+
public void test_consistency_Segments () throws InterruptedException {
14531439
if (SwtTestUtil.isCocoa) {
14541440
// TODO Fix Cocoa failure.
14551441
if (SwtTestUtil.verbose) {
@@ -1487,7 +1473,7 @@ public void test_consistency_Segments () {
14871473
}
14881474
}
14891475

1490-
private void doSegmentsTest (boolean isListening) {
1476+
private void doSegmentsTest (boolean isListening) throws InterruptedException {
14911477
String string = "1234";
14921478

14931479
// Test setText
@@ -1522,14 +1508,14 @@ private void doSegmentsTest (boolean isListening) {
15221508
text.setSelection(pt);
15231509
assertEquals(pt, text.getSelection());
15241510
assertFalse(listenerCalled);
1525-
text.copy();
1511+
copyToClipboard(text);
15261512
assertEquals(isListening, listenerCalled);
15271513
listenerCalled = false;
15281514

15291515
substr = string.substring(pt.x, pt.y);
15301516
pt.x = pt.y = 1;
15311517
text.setSelection(pt);
1532-
text.paste();
1518+
pasteFromClipboard(text);
15331519
assertEquals(isListening, listenerCalled);
15341520
listenerCalled = false;
15351521

@@ -1542,7 +1528,7 @@ private void doSegmentsTest (boolean isListening) {
15421528
text.setSelection(pt);
15431529
assertEquals(substr, text.getSelectionText());
15441530
assertEquals(substr, text.getText(pt.x, pt.y - 1));
1545-
text.cut();
1531+
cutToClipboard(text);
15461532
assertEquals(isListening, listenerCalled);
15471533
listenerCalled = false;
15481534
assertEquals(string, text.getText());
@@ -1552,7 +1538,7 @@ private void doSegmentsTest (boolean isListening) {
15521538
pt.x = 6;
15531539
pt.y = 8;
15541540
text.setSelection(pt.x, pt.y);
1555-
text.cut();
1541+
cutToClipboard(text);
15561542
pt.y = pt.x;
15571543
assertEquals(pt, text.getSelection());
15581544
listenerCalled = false;
@@ -1636,4 +1622,21 @@ public void test_issue472() {
16361622
}
16371623
}
16381624
}
1625+
1626+
private void cutToClipboard(Text text) throws InterruptedException {
1627+
text.cut();
1628+
SwtTestUtil.processEvents(100, null);
1629+
}
1630+
1631+
private void copyToClipboard(Text text) throws InterruptedException {
1632+
text.copy();
1633+
SwtTestUtil.processEvents(100, null);
1634+
}
1635+
1636+
private void pasteFromClipboard(Text text) throws InterruptedException {
1637+
String oldText = text.getText();
1638+
text.paste();
1639+
SwtTestUtil.processEvents(1000, () -> !oldText.equals(text.getText()));
1640+
}
1641+
16391642
}

0 commit comments

Comments
 (0)