# HG changeset patch # User Mike Becker # Date 1595930036 -7200 # Node ID 627e1c99bcf168eecdf9b37d4bf04a3c3aada41f # Parent 530f60fa3dc977c18de74f7b45b97e4f0a005e47 adds tests for focus events diff -r 530f60fa3dc9 -r 627e1c99bcf1 src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java --- a/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java Mon Jul 27 13:12:40 2020 +0200 +++ b/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java Tue Jul 28 11:53:56 2020 +0200 @@ -6,8 +6,10 @@ import org.junit.jupiter.api.Test; import javax.swing.*; +import java.awt.*; import java.awt.event.KeyEvent; import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.*; @@ -42,6 +44,7 @@ @AfterEach void disposeTestFrame() { + testFrame.setVisible(false); testFrame.dispose(); } @@ -206,4 +209,39 @@ assertEquals("6", tf.getText()); }); } + + @Test + void testFocusGained() throws InvocationTargetException, InterruptedException { + final var tf = createTestSubject(); + SwingUtilities.invokeAndWait(() -> { + // given + tf.setText("5"); + assertNull(tf.getSelectedText()); + // when + tf.requestFocusInWindow(); + }); + // give WM time to deliver the event + TimeUnit.MILLISECONDS.sleep(100); + // then + SwingUtilities.invokeAndWait(() -> assertEquals("5", tf.getSelectedText())); + } + + @Test + void testFocusLost() throws InvocationTargetException, InterruptedException { + final var tf = createTestSubject(); + final var focusStealer = new TextField(); + testFrame.add(focusStealer); + SwingUtilities.invokeAndWait(() -> { + // given + tf.setText("5"); + tf.selectAll(); + assertEquals("5", tf.getSelectedText()); + // when + focusStealer.requestFocusInWindow(); + }); + // give WM time to deliver the event + TimeUnit.MILLISECONDS.sleep(100); + // then + SwingUtilities.invokeAndWait(() -> assertNull(tf.getSelectedText())); + } } \ No newline at end of file