Mon, 27 Jul 2020 12:08:23 +0200
fix: window cannot get smaller than the field
package de.uapcore.sudoku; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import javax.swing.*; import java.awt.event.KeyEvent; import java.lang.reflect.InvocationTargetException; import static org.junit.jupiter.api.Assertions.*; class SudokuTextFieldTest { private void dispatch(SudokuTextField tf, KeyEvent pressed) { final var released = new KeyEvent(tf, KeyEvent.KEY_RELEASED, pressed.getWhen()+1, pressed.getModifiersEx(), pressed.getKeyCode(), pressed.getKeyChar()); final KeyEvent typed; if (pressed.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { typed = new KeyEvent(tf, KeyEvent.KEY_TYPED, pressed.getWhen(), pressed.getModifiersEx(), KeyEvent.VK_UNDEFINED, pressed.getKeyChar()); } else { typed = null; } tf.requestFocusInWindow(); tf.dispatchEvent(pressed); if (typed != null) { tf.dispatchEvent(typed); } tf.dispatchEvent(released); } private JFrame testFrame; @BeforeEach void createTestFrame() { // we have to use a visible frame for the events to be dispatched testFrame = new JFrame(); testFrame.setVisible(true); } @AfterEach void disposeTestFrame() { testFrame.dispose(); } private SudokuTextField createTestSubject() { final var tf = new SudokuTextField(); testFrame.add(tf); return tf; } @Test void getValue() { } @Test void setValue() { // given final var tf = createTestSubject(); for (int i = 0 ; i <= 9 ; i++) { // when tf.setValue(i); // then assertEquals(i, tf.getValue()); assertEquals(i == 0 ? "" : String.valueOf(i), tf.getText()); assertFalse(tf.isModified()); } } @Test void setInvalidValue() { // given final var tf = createTestSubject(); // when / then assertThrows(IllegalArgumentException.class, () -> tf.setValue(10)); assertThrows(IllegalArgumentException.class, () -> tf.setValue(-1)); assertFalse(tf.isModified()); } @Test void testModifiedByCall() { // given final var tf = createTestSubject(); // then initially assertFalse(tf.isModified()); // when tf.setModified(true); // then assertTrue(tf.isModified()); // when tf.setModified(false); // then assertFalse(tf.isModified()); } @Test void testModifiedByKeystroke() throws InvocationTargetException, InterruptedException { // given final var tf = createTestSubject(); assertFalse(tf.isModified()); final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_4, '4'); SwingUtilities.invokeAndWait(() -> { // when dispatch(tf, event); // then assertTrue(tf.isModified()); }); } @Test void testTextChangeByKeystore() throws InvocationTargetException, InterruptedException { // given final var tf = createTestSubject(); assertEquals("", tf.getText()); final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_4, '4'); SwingUtilities.invokeAndWait(() -> { // when dispatch(tf, event); // then assertEquals("4", tf.getText()); }); } @Test void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException { // given final var tf = createTestSubject(); tf.setText("6"); tf.selectAll(); final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_4, '4'); SwingUtilities.invokeAndWait(() -> { // when dispatch(tf, event); // then assertEquals("4", tf.getText()); }); } @Test void testNumbersOnly() throws InvocationTargetException, InterruptedException { // given final var tf = createTestSubject(); tf.setText("6"); tf.selectAll(); final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_A, 'a'); SwingUtilities.invokeAndWait(() -> { // when dispatch(tf, event); // then assertEquals("6", tf.getText()); }); } @Test void testSingleDigit() throws InvocationTargetException, InterruptedException { // given final var tf = createTestSubject(); assertEquals("", tf.getText()); final var firstEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_A, '4'); final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_A, '6'); SwingUtilities.invokeAndWait(() -> { // when dispatch(tf, firstEvent); dispatch(tf, secondEvent); // then assertEquals("4", tf.getText()); }); } @Test void testBackspace() throws InvocationTargetException, InterruptedException { // given final var tf = createTestSubject(); assertEquals("", tf.getText()); final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_A, '4'); final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED); final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_A, '6'); SwingUtilities.invokeAndWait(() -> { // when dispatch(tf, typeFour); dispatch(tf, typeBackspace); dispatch(tf, typeSix); // then assertEquals("6", tf.getText()); }); } }