src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java

Mon, 27 Jul 2020 12:52:34 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Jul 2020 12:52:34 +0200
changeset 17
aad33a4db18d
parent 16
bddb2633c98b
child 19
627e1c99bcf1
permissions
-rw-r--r--

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

mercurial