Community
Participate
Working Groups
I20110208-0800 on RHEL 5 - Use the snippet below - Run as Java Application => 'a' is printed 5 times => good - Debug as Java Application => 'a' is printed only 2 times => *bad* Now comment both runEventQueue() calls in the for loop and uncomment the runEventQueue() call after the for loop. - Run as Java Application => 'a' is printed 5 times => good - Debug as Java Application => 'a' is printed 5 times => good On WinXP 'a' is printed 5 times in all 4 cases. Maybe this is similar to or related to Bug 323044 ? I ran into this while working on Bug 330353 and Bug 336514. Moving the runEventQueue() calls outside the loop and calling it only once seems to solve the problem in those bugs, but I am not sure why calling runEventQueue() multiple times should be a problem... Is there something wrong with the snippet ? Or is this a SWT/Linux bug ? ------------------------------------------------------------------------ package org.eclipse.testsnippets; import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class TestSnippet2 { public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); final Text text = new Text(shell, SWT.BORDER); text.setSize(text.computeSize(150, SWT.DEFAULT)); shell.pack(); shell.open(); Event charEvent = new Event(); System.out.println("Start"); for (int i = 1; i <= 5; i++) { charEvent.type = SWT.KeyDown; charEvent.character = 'a'; display.post(charEvent); runEventQueue(display); charEvent.type = SWT.KeyUp; charEvent.character = 'a'; display.post(charEvent); runEventQueue(display); } //runEventQueue(display); System.out.println("End"); while (!shell.isDisposed()) { } display.dispose(); } private static void runEventQueue(final Display display) { while (display.readAndDispatch()) ; } } -------------------------------------------------------------------------
When you run as "Debug", Does Eclipse change to the Debug Perspective, causing Eclipse to "steal" the focus from the running application ?
(In reply to comment #1) > When you run as "Debug", Does Eclipse change to the Debug Perspective, causing > Eclipse to "steal" the focus from the running application ? No.
A bit more detail... (if it helps) Bug 330353 is about JavaMoveLineTest. In this test we open a Java editor and move a line down by 100 lines by posting multiple 'ALT+Down Arrow' events. But sometimes instead of moving down by only 100 lines, the line moves to the end of the file which has 10000 lines, and then the test sort of gets stuck as if it wants to move the line even further down (which is not possible). Bug 330353 comment 9 > Calling EditorTestHelper.runEventQueue() multiple times seems to be executing > the same event multiple times on Linux (a bug ?). If I just call it just once, > the test seems to be working, at least it does not fail on my Linux machine > anymore. I know this conflicts with the situation described here, as 3 key strokes are lost here. But this is the only way I could describe the situation in Bug 330353 :/ In Bug 336514 we open the Find Replace dialog (Ctrl+F), and then replace all occurrences of 'e' with 'x' in an open editor. We do this by posting key events e.g Alt+a for Replace All button. Even here the test sort of gets stuck as it tries to press Alt+a indefinitely. Debug vs Run The only way I could consistently reproduce the failures in Bug 330353 and Bug 336514 was if I start the tests in debug mode (of course with no breakpoints set and no stealing of focus involved). The tests failed sometimes even when I used 'Run' but not as often as while using 'Debug'. But I have absolutely no clue why there should be a difference...
The issue can be fixed posting key events in new thread. My guess is that the original snippet sometimes sends the event before text widget gains focus, so the characters are not printed. ------------------------------------------------------------------- public class Bug337119_PostKeyEvent { public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); final Text text = new Text(shell, SWT.BORDER); text.setSize(text.computeSize(150, SWT.DEFAULT)); shell.pack(); shell.open(); new Thread(){ @Override public void run(){ Event charEvent = new Event(); System.out.println("Start"); try { Thread.sleep(100); } catch (Exception e) { } for (int i = 1; i <= 5; i++) { charEvent.type = SWT.KeyDown; charEvent.character = 'a'; display.post(charEvent); charEvent.type = SWT.KeyUp; charEvent.character = 'a'; display.post(charEvent); } System.out.println("End"); } }.start(); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose(); } }
The issue can be fixed by fixing the snippet.