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