1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java Mon Jul 27 11:56:41 2020 +0200 1.3 @@ -0,0 +1,199 @@ 1.4 +package de.uapcore.sudoku; 1.5 + 1.6 + 1.7 +import org.junit.jupiter.api.AfterEach; 1.8 +import org.junit.jupiter.api.BeforeEach; 1.9 +import org.junit.jupiter.api.Test; 1.10 + 1.11 +import javax.swing.*; 1.12 +import java.awt.event.KeyEvent; 1.13 +import java.lang.reflect.InvocationTargetException; 1.14 + 1.15 +import static org.junit.jupiter.api.Assertions.*; 1.16 + 1.17 +class SudokuTextFieldTest { 1.18 + 1.19 + private void dispatch(SudokuTextField tf, KeyEvent pressed) { 1.20 + final var released = new KeyEvent(tf, KeyEvent.KEY_RELEASED, pressed.getWhen()+1, 1.21 + pressed.getModifiersEx(), pressed.getKeyCode(), pressed.getKeyChar()); 1.22 + final KeyEvent typed; 1.23 + if (pressed.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { 1.24 + typed = new KeyEvent(tf, KeyEvent.KEY_TYPED, pressed.getWhen(), 1.25 + pressed.getModifiersEx(), KeyEvent.VK_UNDEFINED, pressed.getKeyChar()); 1.26 + } else { 1.27 + typed = null; 1.28 + } 1.29 + tf.requestFocusInWindow(); 1.30 + tf.dispatchEvent(pressed); 1.31 + if (typed != null) { 1.32 + tf.dispatchEvent(typed); 1.33 + } 1.34 + tf.dispatchEvent(released); 1.35 + } 1.36 + 1.37 + private JFrame testFrame; 1.38 + 1.39 + @BeforeEach 1.40 + void createTestFrame() { 1.41 + // we have to use a visible frame for the events to be dispatched 1.42 + testFrame = new JFrame(); 1.43 + testFrame.setVisible(true); 1.44 + } 1.45 + 1.46 + @AfterEach 1.47 + void disposeTestFrame() { 1.48 + testFrame.dispose(); 1.49 + } 1.50 + 1.51 + private SudokuTextField createTestSubject() { 1.52 + final var tf = new SudokuTextField(); 1.53 + testFrame.add(tf); 1.54 + return tf; 1.55 + } 1.56 + 1.57 + @Test 1.58 + void getValue() { 1.59 + 1.60 + } 1.61 + 1.62 + @Test 1.63 + void setValue() { 1.64 + // given 1.65 + final var tf = createTestSubject(); 1.66 + for (int i = 0 ; i <= 9 ; i++) { 1.67 + // when 1.68 + tf.setValue(i); 1.69 + // then 1.70 + assertEquals(i, tf.getValue()); 1.71 + assertEquals(i == 0 ? "" : String.valueOf(i), tf.getText()); 1.72 + assertFalse(tf.isModified()); 1.73 + } 1.74 + } 1.75 + 1.76 + @Test 1.77 + void setInvalidValue() { 1.78 + // given 1.79 + final var tf = createTestSubject(); 1.80 + // when / then 1.81 + assertThrows(IllegalArgumentException.class, () -> tf.setValue(10)); 1.82 + assertThrows(IllegalArgumentException.class, () -> tf.setValue(-1)); 1.83 + assertFalse(tf.isModified()); 1.84 + } 1.85 + 1.86 + @Test 1.87 + void testModifiedByCall() { 1.88 + // given 1.89 + final var tf = createTestSubject(); 1.90 + // then initially 1.91 + assertFalse(tf.isModified()); 1.92 + // when 1.93 + tf.setModified(true); 1.94 + // then 1.95 + assertTrue(tf.isModified()); 1.96 + // when 1.97 + tf.setModified(false); 1.98 + // then 1.99 + assertFalse(tf.isModified()); 1.100 + } 1.101 + 1.102 + @Test 1.103 + void testModifiedByKeystroke() throws InvocationTargetException, InterruptedException { 1.104 + // given 1.105 + final var tf = createTestSubject(); 1.106 + assertFalse(tf.isModified()); 1.107 + final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 1.108 + 0, KeyEvent.VK_4, '4'); 1.109 + SwingUtilities.invokeAndWait(() -> { 1.110 + // when 1.111 + dispatch(tf, event); 1.112 + // then 1.113 + assertTrue(tf.isModified()); 1.114 + }); 1.115 + } 1.116 + 1.117 + @Test 1.118 + void testTextChangeByKeystore() throws InvocationTargetException, InterruptedException { 1.119 + // given 1.120 + final var tf = createTestSubject(); 1.121 + assertEquals("", tf.getText()); 1.122 + final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 1.123 + 0, KeyEvent.VK_4, '4'); 1.124 + SwingUtilities.invokeAndWait(() -> { 1.125 + // when 1.126 + dispatch(tf, event); 1.127 + // then 1.128 + assertEquals("4", tf.getText()); 1.129 + }); 1.130 + } 1.131 + 1.132 + @Test 1.133 + void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException { 1.134 + // given 1.135 + final var tf = createTestSubject(); 1.136 + tf.setText("6"); 1.137 + tf.selectAll(); 1.138 + final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 1.139 + 0, KeyEvent.VK_4, '4'); 1.140 + SwingUtilities.invokeAndWait(() -> { 1.141 + // when 1.142 + dispatch(tf, event); 1.143 + // then 1.144 + assertEquals("4", tf.getText()); 1.145 + }); 1.146 + } 1.147 + 1.148 + @Test 1.149 + void testNumbersOnly() throws InvocationTargetException, InterruptedException { 1.150 + // given 1.151 + final var tf = createTestSubject(); 1.152 + tf.setText("6"); 1.153 + tf.selectAll(); 1.154 + final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 1.155 + 0, KeyEvent.VK_A, 'a'); 1.156 + SwingUtilities.invokeAndWait(() -> { 1.157 + // when 1.158 + dispatch(tf, event); 1.159 + // then 1.160 + assertEquals("6", tf.getText()); 1.161 + }); 1.162 + } 1.163 + 1.164 + @Test 1.165 + void testSingleDigit() throws InvocationTargetException, InterruptedException { 1.166 + // given 1.167 + final var tf = createTestSubject(); 1.168 + assertEquals("", tf.getText()); 1.169 + final var firstEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 1.170 + 0, KeyEvent.VK_A, '4'); 1.171 + final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 1.172 + 0, KeyEvent.VK_A, '6'); 1.173 + SwingUtilities.invokeAndWait(() -> { 1.174 + // when 1.175 + dispatch(tf, firstEvent); 1.176 + dispatch(tf, secondEvent); 1.177 + // then 1.178 + assertEquals("4", tf.getText()); 1.179 + }); 1.180 + } 1.181 + 1.182 + @Test 1.183 + void testBackspace() throws InvocationTargetException, InterruptedException { 1.184 + // given 1.185 + final var tf = createTestSubject(); 1.186 + assertEquals("", tf.getText()); 1.187 + final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 1.188 + 0, KeyEvent.VK_A, '4'); 1.189 + final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 1.190 + 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED); 1.191 + final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 1.192 + 0, KeyEvent.VK_A, '6'); 1.193 + SwingUtilities.invokeAndWait(() -> { 1.194 + // when 1.195 + dispatch(tf, typeFour); 1.196 + dispatch(tf, typeBackspace); 1.197 + dispatch(tf, typeSix); 1.198 + // then 1.199 + assertEquals("6", tf.getText()); 1.200 + }); 1.201 + } 1.202 +} 1.203 \ No newline at end of file