adds tests for focus events

Tue, 28 Jul 2020 11:53:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Jul 2020 11:53:56 +0200
changeset 19
627e1c99bcf1
parent 18
530f60fa3dc9
child 20
a95423d37588

adds tests for focus events

src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java file | annotate | diff | comparison | revisions
--- 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