0

I want to avoid sleeping snail in my swing unit tests, but I don't see a way to do this with events. Currently I have somethink like

WindowWithKeyListener window = new WindowWithKeyListener();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
assertTrue(window.key1pressed());

robot.keyPress(KeyEvent.VK_2);
robot.keyRelease(KeyEvent.VK_2);
assertTrue(window.key2pressed());

This doesn't work without Thread.sleep() before the assertions, as the events are to slow. But how could I avoid Thread.sleep() here?

Swing Timers cannot be executed in a specific order. What is the best approach for testing here?

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
jan
  • 2,741
  • 4
  • 35
  • 56
  • We already use Jemmy, if there exists a good solution, that would be a plus. – jan Mar 06 '14 at 09:40
  • 1
    Why not register a listener that does the assertations or maybe use a future and wait for the listeners to finish? – Thomas Mar 06 '14 at 09:51
  • I don't know if I only misunderstand, but of course I don't want to simply verify the event triggering, `key1pressed` stands for more complex logic that is performed because the event was triggered... A single executor and Futures should work, right. Looks like overkill to me, is there a easier solution? But how do you know know that the assertion Future can be started? – jan Mar 06 '14 at 09:57
  • Well, since you always need to wait until the events are finished you'd have to wait for that situation or synchronize the calls somehow. – Thomas Mar 06 '14 at 10:01
  • The article cited suggests using a `CountDownLatch` to let separate threads rendezvous; you might get some ideas from the examples shown [here](http://stackoverflow.com/a/11372932/230513) and [here](http://stackoverflow.com/a/3588523/230513) – trashgod Mar 06 '14 at 10:15

1 Answers1

0

Jemmy seems to handle this already

org.netbeans.jemmy.operators.ComponentOperator.typeKey(int)

seems to do some waiting.

jan
  • 2,741
  • 4
  • 35
  • 56