src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java

changeset 19
627e1c99bcf1
parent 17
aad33a4db18d
child 22
06170a0be62a
--- 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

mercurial