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