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

Tue, 28 Jul 2020 14:05:04 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Jul 2020 14:05:04 +0200
changeset 21
11cb2d9413d4
parent 19
627e1c99bcf1
child 22
06170a0be62a
permissions
-rw-r--r--

DocumentHandlerTest tests if modified state is reset

13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 package de.uapcore.sudoku;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 import org.junit.jupiter.api.AfterEach;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 import org.junit.jupiter.api.BeforeEach;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 import org.junit.jupiter.api.Test;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 import javax.swing.*;
19
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
9 import java.awt.*;
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 import java.awt.event.KeyEvent;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 import java.lang.reflect.InvocationTargetException;
19
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
12 import java.util.concurrent.TimeUnit;
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 import static org.junit.jupiter.api.Assertions.*;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 class SudokuTextFieldTest {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 private void dispatch(SudokuTextField tf, KeyEvent pressed) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 final var released = new KeyEvent(tf, KeyEvent.KEY_RELEASED, pressed.getWhen()+1,
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 pressed.getModifiersEx(), pressed.getKeyCode(), pressed.getKeyChar());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 final KeyEvent typed;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 if (pressed.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 typed = new KeyEvent(tf, KeyEvent.KEY_TYPED, pressed.getWhen(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 pressed.getModifiersEx(), KeyEvent.VK_UNDEFINED, pressed.getKeyChar());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 } else {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 typed = null;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 tf.requestFocusInWindow();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 tf.dispatchEvent(pressed);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 if (typed != null) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 tf.dispatchEvent(typed);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 tf.dispatchEvent(released);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 private JFrame testFrame;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 @BeforeEach
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 void createTestFrame() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 // we have to use a visible frame for the events to be dispatched
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 testFrame = new JFrame();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 testFrame.setVisible(true);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 @AfterEach
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 void disposeTestFrame() {
19
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
47 testFrame.setVisible(false);
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 testFrame.dispose();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 private SudokuTextField createTestSubject() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 final var tf = new SudokuTextField();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 testFrame.add(tf);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 return tf;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 void setValue() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 for (int i = 0 ; i <= 9 ; i++) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 tf.setValue(i);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 assertEquals(i, tf.getValue());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 assertEquals(i == 0 ? "" : String.valueOf(i), tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 void setInvalidValue() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 // when / then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 assertThrows(IllegalArgumentException.class, () -> tf.setValue(10));
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 assertThrows(IllegalArgumentException.class, () -> tf.setValue(-1));
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 void testModifiedByCall() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 // then initially
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 tf.setModified(true);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 assertTrue(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 tf.setModified(false);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 void testModifiedByKeystroke() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 assertTrue(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 void testTextChangeByKeystore() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 assertEquals("4", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 @Test
16
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
128 void testNumpad() throws InvocationTargetException, InterruptedException {
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
129 // given
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
130 final var tf = createTestSubject();
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
131 assertEquals("", tf.getText());
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
132 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
133 0, KeyEvent.VK_NUMPAD4, '4');
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
134 SwingUtilities.invokeAndWait(() -> {
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
135 // when
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
136 dispatch(tf, event);
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
137 // then
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
138 assertEquals("4", tf.getText());
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
139 });
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
140 }
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
141
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
142 @Test
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 tf.setText("6");
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 tf.selectAll();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 assertEquals("4", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 void testNumbersOnly() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 tf.setText("6");
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 tf.selectAll();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 0, KeyEvent.VK_A, 'a');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 assertEquals("6", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 void testSingleDigit() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 final var firstEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 0, KeyEvent.VK_A, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 0, KeyEvent.VK_A, '6');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 dispatch(tf, firstEvent);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 dispatch(tf, secondEvent);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 assertEquals("4", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 void testBackspace() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 0, KeyEvent.VK_A, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 0, KeyEvent.VK_A, '6');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 dispatch(tf, typeFour);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 dispatch(tf, typeBackspace);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 dispatch(tf, typeSix);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 assertEquals("6", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 }
19
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
212
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
213 @Test
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
214 void testFocusGained() throws InvocationTargetException, InterruptedException {
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
215 final var tf = createTestSubject();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
216 SwingUtilities.invokeAndWait(() -> {
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
217 // given
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
218 tf.setText("5");
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
219 assertNull(tf.getSelectedText());
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
220 // when
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
221 tf.requestFocusInWindow();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
222 });
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
223 // give WM time to deliver the event
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
224 TimeUnit.MILLISECONDS.sleep(100);
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
225 // then
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
226 SwingUtilities.invokeAndWait(() -> assertEquals("5", tf.getSelectedText()));
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
227 }
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
228
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
229 @Test
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
230 void testFocusLost() throws InvocationTargetException, InterruptedException {
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
231 final var tf = createTestSubject();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
232 final var focusStealer = new TextField();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
233 testFrame.add(focusStealer);
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
234 SwingUtilities.invokeAndWait(() -> {
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
235 // given
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
236 tf.setText("5");
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
237 tf.selectAll();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
238 assertEquals("5", tf.getSelectedText());
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
239 // when
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
240 focusStealer.requestFocusInWindow();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
241 });
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
242 // give WM time to deliver the event
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
243 TimeUnit.MILLISECONDS.sleep(100);
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
244 // then
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
245 SwingUtilities.invokeAndWait(() -> assertNull(tf.getSelectedText()));
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
246 }
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 }

mercurial