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