Mon, 27 Jul 2020 11:57:42 +0200
renames help menu
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 getValue() { | |
56 | ||
57 | } | |
58 | ||
59 | @Test | |
60 | void setValue() { | |
61 | // given | |
62 | final var tf = createTestSubject(); | |
63 | for (int i = 0 ; i <= 9 ; i++) { | |
64 | // when | |
65 | tf.setValue(i); | |
66 | // then | |
67 | assertEquals(i, tf.getValue()); | |
68 | assertEquals(i == 0 ? "" : String.valueOf(i), tf.getText()); | |
69 | assertFalse(tf.isModified()); | |
70 | } | |
71 | } | |
72 | ||
73 | @Test | |
74 | void setInvalidValue() { | |
75 | // given | |
76 | final var tf = createTestSubject(); | |
77 | // when / then | |
78 | assertThrows(IllegalArgumentException.class, () -> tf.setValue(10)); | |
79 | assertThrows(IllegalArgumentException.class, () -> tf.setValue(-1)); | |
80 | assertFalse(tf.isModified()); | |
81 | } | |
82 | ||
83 | @Test | |
84 | void testModifiedByCall() { | |
85 | // given | |
86 | final var tf = createTestSubject(); | |
87 | // then initially | |
88 | assertFalse(tf.isModified()); | |
89 | // when | |
90 | tf.setModified(true); | |
91 | // then | |
92 | assertTrue(tf.isModified()); | |
93 | // when | |
94 | tf.setModified(false); | |
95 | // then | |
96 | assertFalse(tf.isModified()); | |
97 | } | |
98 | ||
99 | @Test | |
100 | void testModifiedByKeystroke() throws InvocationTargetException, InterruptedException { | |
101 | // given | |
102 | final var tf = createTestSubject(); | |
103 | assertFalse(tf.isModified()); | |
104 | final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), | |
105 | 0, KeyEvent.VK_4, '4'); | |
106 | SwingUtilities.invokeAndWait(() -> { | |
107 | // when | |
108 | dispatch(tf, event); | |
109 | // then | |
110 | assertTrue(tf.isModified()); | |
111 | }); | |
112 | } | |
113 | ||
114 | @Test | |
115 | void testTextChangeByKeystore() throws InvocationTargetException, InterruptedException { | |
116 | // given | |
117 | final var tf = createTestSubject(); | |
118 | assertEquals("", tf.getText()); | |
119 | final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), | |
120 | 0, KeyEvent.VK_4, '4'); | |
121 | SwingUtilities.invokeAndWait(() -> { | |
122 | // when | |
123 | dispatch(tf, event); | |
124 | // then | |
125 | assertEquals("4", tf.getText()); | |
126 | }); | |
127 | } | |
128 | ||
129 | @Test | |
130 | void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException { | |
131 | // given | |
132 | final var tf = createTestSubject(); | |
133 | tf.setText("6"); | |
134 | tf.selectAll(); | |
135 | final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), | |
136 | 0, KeyEvent.VK_4, '4'); | |
137 | SwingUtilities.invokeAndWait(() -> { | |
138 | // when | |
139 | dispatch(tf, event); | |
140 | // then | |
141 | assertEquals("4", tf.getText()); | |
142 | }); | |
143 | } | |
144 | ||
145 | @Test | |
146 | void testNumbersOnly() throws InvocationTargetException, InterruptedException { | |
147 | // given | |
148 | final var tf = createTestSubject(); | |
149 | tf.setText("6"); | |
150 | tf.selectAll(); | |
151 | final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), | |
152 | 0, KeyEvent.VK_A, 'a'); | |
153 | SwingUtilities.invokeAndWait(() -> { | |
154 | // when | |
155 | dispatch(tf, event); | |
156 | // then | |
157 | assertEquals("6", tf.getText()); | |
158 | }); | |
159 | } | |
160 | ||
161 | @Test | |
162 | void testSingleDigit() throws InvocationTargetException, InterruptedException { | |
163 | // given | |
164 | final var tf = createTestSubject(); | |
165 | assertEquals("", tf.getText()); | |
166 | final var firstEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), | |
167 | 0, KeyEvent.VK_A, '4'); | |
168 | final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), | |
169 | 0, KeyEvent.VK_A, '6'); | |
170 | SwingUtilities.invokeAndWait(() -> { | |
171 | // when | |
172 | dispatch(tf, firstEvent); | |
173 | dispatch(tf, secondEvent); | |
174 | // then | |
175 | assertEquals("4", tf.getText()); | |
176 | }); | |
177 | } | |
178 | ||
179 | @Test | |
180 | void testBackspace() throws InvocationTargetException, InterruptedException { | |
181 | // given | |
182 | final var tf = createTestSubject(); | |
183 | assertEquals("", tf.getText()); | |
184 | final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), | |
185 | 0, KeyEvent.VK_A, '4'); | |
186 | final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), | |
187 | 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED); | |
188 | final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), | |
189 | 0, KeyEvent.VK_A, '6'); | |
190 | SwingUtilities.invokeAndWait(() -> { | |
191 | // when | |
192 | dispatch(tf, typeFour); | |
193 | dispatch(tf, typeBackspace); | |
194 | dispatch(tf, typeSix); | |
195 | // then | |
196 | assertEquals("6", tf.getText()); | |
197 | }); | |
198 | } | |
199 | } |