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 |
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 } |