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

changeset 19
627e1c99bcf1
parent 17
aad33a4db18d
child 22
06170a0be62a
equal deleted inserted replaced
18:530f60fa3dc9 19:627e1c99bcf1
4 import org.junit.jupiter.api.AfterEach; 4 import org.junit.jupiter.api.AfterEach;
5 import org.junit.jupiter.api.BeforeEach; 5 import org.junit.jupiter.api.BeforeEach;
6 import org.junit.jupiter.api.Test; 6 import org.junit.jupiter.api.Test;
7 7
8 import javax.swing.*; 8 import javax.swing.*;
9 import java.awt.*;
9 import java.awt.event.KeyEvent; 10 import java.awt.event.KeyEvent;
10 import java.lang.reflect.InvocationTargetException; 11 import java.lang.reflect.InvocationTargetException;
12 import java.util.concurrent.TimeUnit;
11 13
12 import static org.junit.jupiter.api.Assertions.*; 14 import static org.junit.jupiter.api.Assertions.*;
13 15
14 class SudokuTextFieldTest { 16 class SudokuTextFieldTest {
15 17
40 testFrame.setVisible(true); 42 testFrame.setVisible(true);
41 } 43 }
42 44
43 @AfterEach 45 @AfterEach
44 void disposeTestFrame() { 46 void disposeTestFrame() {
47 testFrame.setVisible(false);
45 testFrame.dispose(); 48 testFrame.dispose();
46 } 49 }
47 50
48 private SudokuTextField createTestSubject() { 51 private SudokuTextField createTestSubject() {
49 final var tf = new SudokuTextField(); 52 final var tf = new SudokuTextField();
204 dispatch(tf, typeSix); 207 dispatch(tf, typeSix);
205 // then 208 // then
206 assertEquals("6", tf.getText()); 209 assertEquals("6", tf.getText());
207 }); 210 });
208 } 211 }
212
213 @Test
214 void testFocusGained() throws InvocationTargetException, InterruptedException {
215 final var tf = createTestSubject();
216 SwingUtilities.invokeAndWait(() -> {
217 // given
218 tf.setText("5");
219 assertNull(tf.getSelectedText());
220 // when
221 tf.requestFocusInWindow();
222 });
223 // give WM time to deliver the event
224 TimeUnit.MILLISECONDS.sleep(100);
225 // then
226 SwingUtilities.invokeAndWait(() -> assertEquals("5", tf.getSelectedText()));
227 }
228
229 @Test
230 void testFocusLost() throws InvocationTargetException, InterruptedException {
231 final var tf = createTestSubject();
232 final var focusStealer = new TextField();
233 testFrame.add(focusStealer);
234 SwingUtilities.invokeAndWait(() -> {
235 // given
236 tf.setText("5");
237 tf.selectAll();
238 assertEquals("5", tf.getSelectedText());
239 // when
240 focusStealer.requestFocusInWindow();
241 });
242 // give WM time to deliver the event
243 TimeUnit.MILLISECONDS.sleep(100);
244 // then
245 SwingUtilities.invokeAndWait(() -> assertNull(tf.getSelectedText()));
246 }
209 } 247 }

mercurial