|
13 | 13 | *******************************************************************************/ |
14 | 14 | package org.eclipse.swt.tests.junit; |
15 | 15 |
|
| 16 | +import static java.lang.System.currentTimeMillis; |
16 | 17 | import static org.eclipse.swt.tests.junit.SwtTestUtil.JENKINS_DETECT_ENV_VAR; |
17 | 18 | import static org.eclipse.swt.tests.junit.SwtTestUtil.JENKINS_DETECT_REGEX; |
18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; |
19 | 20 | import static org.junit.jupiter.api.Assertions.assertFalse; |
20 | 21 | import static org.junit.jupiter.api.Assertions.assertThrows; |
21 | 22 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | +import static org.junit.jupiter.api.Assertions.fail; |
22 | 24 |
|
23 | 25 | import org.eclipse.swt.SWT; |
24 | 26 | import org.eclipse.swt.events.ModifyListener; |
|
30 | 32 | import org.eclipse.swt.graphics.Font; |
31 | 33 | import org.eclipse.swt.graphics.FontData; |
32 | 34 | import org.eclipse.swt.graphics.Point; |
| 35 | +import org.eclipse.swt.layout.FillLayout; |
33 | 36 | import org.eclipse.swt.widgets.Display; |
34 | 37 | import org.eclipse.swt.widgets.Event; |
35 | 38 | import org.eclipse.swt.widgets.Group; |
@@ -1376,6 +1379,48 @@ public void test_showSelection() { |
1376 | 1379 | text.showSelection(); |
1377 | 1380 | } |
1378 | 1381 |
|
| 1382 | +// Originally reported as https://github.com/eclipse-platform/eclipse.platform.ui/issues/3920 |
| 1383 | +@Test |
| 1384 | +public void test_finiteRedraw() { |
| 1385 | + if ( text != null ) text.dispose(); |
| 1386 | + // Style constants are causing |
| 1387 | + // org.eclipse.swt.widgets.Text.drawInteriorWithFrame_inView_searchfield(long, long, NSRect, long) |
| 1388 | + // to call |
| 1389 | + // org.eclipse.swt.internal.cocoa.NSControl.stringValue() |
| 1390 | + // which schedules redraw |
| 1391 | + text = new Text(shell, SWT.SEARCH | SWT.ICON_CANCEL); |
| 1392 | + // Background prevents early exit from drawInteriorWithFrame_inView_searchfield(long, long, NSRect, long) |
| 1393 | + text.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED)); |
| 1394 | + setWidget(text); |
| 1395 | + shell.setLayout(new FillLayout()); |
| 1396 | + text.requestLayout(); |
| 1397 | + shell.open(); |
| 1398 | + Display display = shell.getDisplay(); |
| 1399 | + text.forceFocus(); |
| 1400 | + waitUntilIdle(); |
| 1401 | + long start = currentTimeMillis(); |
| 1402 | + long stop = start + 1000; |
| 1403 | + int eventStreakCount = 0; |
| 1404 | + while (currentTimeMillis() < stop) { |
| 1405 | + // If redraws are constantly scheduled, readAndDispatch() rarely return false. |
| 1406 | + // Side effects - high CPU usage, asyncExec() stops working in modal contexts |
| 1407 | + if (display.readAndDispatch()) { |
| 1408 | + eventStreakCount++; |
| 1409 | + // Skip other events of the streak |
| 1410 | + // No need to count events individually, as many events immediately following each other are common and normal |
| 1411 | + // Currently, streaks are short, but we do no want any noise to cause test failures as UI evolves |
| 1412 | + waitUntilIdle(); |
| 1413 | + } else { |
| 1414 | + Thread.yield(); |
| 1415 | + } |
| 1416 | + } |
| 1417 | + String message = "Detected " + eventStreakCount + " UI event streaks per second. Expected rate: 5 event streaks per second or less."; |
| 1418 | + assertTrue(eventStreakCount < 50, message); |
| 1419 | + if (SwtTestUtil.verbose) { |
| 1420 | + System.out.println(message); |
| 1421 | + } |
| 1422 | +} |
| 1423 | + |
1379 | 1424 | /* custom */ |
1380 | 1425 | Text text; |
1381 | 1426 | String delimiterString; |
@@ -1639,4 +1684,13 @@ private void pasteFromClipboard(Text text) throws InterruptedException { |
1639 | 1684 | SwtTestUtil.processEvents(1000, () -> !oldText.equals(text.getText())); |
1640 | 1685 | } |
1641 | 1686 |
|
| 1687 | +private void waitUntilIdle() { |
| 1688 | + long hangTimeout = currentTimeMillis() + 1000; |
| 1689 | + while (shell.getDisplay().readAndDispatch()) { |
| 1690 | + if (currentTimeMillis() > hangTimeout) { |
| 1691 | + fail("UI scheduler should settle"); |
| 1692 | + } |
| 1693 | + } |
| 1694 | +} |
| 1695 | + |
1642 | 1696 | } |
0 commit comments