Tue, 28 Jul 2020 11:53:56 +0200
adds tests for focus events
src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java Mon Jul 27 13:12:40 2020 +0200 1.2 +++ b/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java Tue Jul 28 11:53:56 2020 +0200 1.3 @@ -6,8 +6,10 @@ 1.4 import org.junit.jupiter.api.Test; 1.5 1.6 import javax.swing.*; 1.7 +import java.awt.*; 1.8 import java.awt.event.KeyEvent; 1.9 import java.lang.reflect.InvocationTargetException; 1.10 +import java.util.concurrent.TimeUnit; 1.11 1.12 import static org.junit.jupiter.api.Assertions.*; 1.13 1.14 @@ -42,6 +44,7 @@ 1.15 1.16 @AfterEach 1.17 void disposeTestFrame() { 1.18 + testFrame.setVisible(false); 1.19 testFrame.dispose(); 1.20 } 1.21 1.22 @@ -206,4 +209,39 @@ 1.23 assertEquals("6", tf.getText()); 1.24 }); 1.25 } 1.26 + 1.27 + @Test 1.28 + void testFocusGained() throws InvocationTargetException, InterruptedException { 1.29 + final var tf = createTestSubject(); 1.30 + SwingUtilities.invokeAndWait(() -> { 1.31 + // given 1.32 + tf.setText("5"); 1.33 + assertNull(tf.getSelectedText()); 1.34 + // when 1.35 + tf.requestFocusInWindow(); 1.36 + }); 1.37 + // give WM time to deliver the event 1.38 + TimeUnit.MILLISECONDS.sleep(100); 1.39 + // then 1.40 + SwingUtilities.invokeAndWait(() -> assertEquals("5", tf.getSelectedText())); 1.41 + } 1.42 + 1.43 + @Test 1.44 + void testFocusLost() throws InvocationTargetException, InterruptedException { 1.45 + final var tf = createTestSubject(); 1.46 + final var focusStealer = new TextField(); 1.47 + testFrame.add(focusStealer); 1.48 + SwingUtilities.invokeAndWait(() -> { 1.49 + // given 1.50 + tf.setText("5"); 1.51 + tf.selectAll(); 1.52 + assertEquals("5", tf.getSelectedText()); 1.53 + // when 1.54 + focusStealer.requestFocusInWindow(); 1.55 + }); 1.56 + // give WM time to deliver the event 1.57 + TimeUnit.MILLISECONDS.sleep(100); 1.58 + // then 1.59 + SwingUtilities.invokeAndWait(() -> assertNull(tf.getSelectedText())); 1.60 + } 1.61 } 1.62 \ No newline at end of file