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

Mon, 27 Jul 2020 11:56:41 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Jul 2020 11:56:41 +0200
changeset 13
5e69b1bb707f
child 16
bddb2633c98b
permissions
-rw-r--r--

adds SudokuTextFieldTest

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@13 130 void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException {
universe@13 131 // given
universe@13 132 final var tf = createTestSubject();
universe@13 133 tf.setText("6");
universe@13 134 tf.selectAll();
universe@13 135 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
universe@13 136 0, KeyEvent.VK_4, '4');
universe@13 137 SwingUtilities.invokeAndWait(() -> {
universe@13 138 // when
universe@13 139 dispatch(tf, event);
universe@13 140 // then
universe@13 141 assertEquals("4", tf.getText());
universe@13 142 });
universe@13 143 }
universe@13 144
universe@13 145 @Test
universe@13 146 void testNumbersOnly() throws InvocationTargetException, InterruptedException {
universe@13 147 // given
universe@13 148 final var tf = createTestSubject();
universe@13 149 tf.setText("6");
universe@13 150 tf.selectAll();
universe@13 151 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
universe@13 152 0, KeyEvent.VK_A, 'a');
universe@13 153 SwingUtilities.invokeAndWait(() -> {
universe@13 154 // when
universe@13 155 dispatch(tf, event);
universe@13 156 // then
universe@13 157 assertEquals("6", tf.getText());
universe@13 158 });
universe@13 159 }
universe@13 160
universe@13 161 @Test
universe@13 162 void testSingleDigit() throws InvocationTargetException, InterruptedException {
universe@13 163 // given
universe@13 164 final var tf = createTestSubject();
universe@13 165 assertEquals("", tf.getText());
universe@13 166 final var firstEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
universe@13 167 0, KeyEvent.VK_A, '4');
universe@13 168 final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
universe@13 169 0, KeyEvent.VK_A, '6');
universe@13 170 SwingUtilities.invokeAndWait(() -> {
universe@13 171 // when
universe@13 172 dispatch(tf, firstEvent);
universe@13 173 dispatch(tf, secondEvent);
universe@13 174 // then
universe@13 175 assertEquals("4", tf.getText());
universe@13 176 });
universe@13 177 }
universe@13 178
universe@13 179 @Test
universe@13 180 void testBackspace() throws InvocationTargetException, InterruptedException {
universe@13 181 // given
universe@13 182 final var tf = createTestSubject();
universe@13 183 assertEquals("", tf.getText());
universe@13 184 final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
universe@13 185 0, KeyEvent.VK_A, '4');
universe@13 186 final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
universe@13 187 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED);
universe@13 188 final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
universe@13 189 0, KeyEvent.VK_A, '6');
universe@13 190 SwingUtilities.invokeAndWait(() -> {
universe@13 191 // when
universe@13 192 dispatch(tf, typeFour);
universe@13 193 dispatch(tf, typeBackspace);
universe@13 194 dispatch(tf, typeSix);
universe@13 195 // then
universe@13 196 assertEquals("6", tf.getText());
universe@13 197 });
universe@13 198 }
universe@13 199 }

mercurial