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