# HG changeset patch # User Mike Becker # Date 1595843801 -7200 # Node ID 5e69b1bb707fca8ebc61a042022acc986ff9e82e # Parent 1c62c60091618a5c0dbaa489c7ad0f2077859175 adds SudokuTextFieldTest diff -r 1c62c6009161 -r 5e69b1bb707f pom.xml --- a/pom.xml Mon Jul 27 10:56:17 2020 +0200 +++ b/pom.xml Mon Jul 27 11:56:41 2020 +0200 @@ -5,17 +5,30 @@ sudoku 1.0 + + UTF-8 + 11 + 11 + + + + + org.junit.jupiter + junit-jupiter + 5.6.2 + test + + + org.junit.jupiter + junit-jupiter-params + 5.6.2 + test + + + - maven-compiler-plugin - 3.8.1 - - 1.8 - 1.8 - - - org.apache.maven.plugins maven-jar-plugin 3.0.2 diff -r 1c62c6009161 -r 5e69b1bb707f src/main/java/de/uapcore/sudoku/SudokuTextField.java --- a/src/main/java/de/uapcore/sudoku/SudokuTextField.java Mon Jul 27 10:56:17 2020 +0200 +++ b/src/main/java/de/uapcore/sudoku/SudokuTextField.java Mon Jul 27 11:56:41 2020 +0200 @@ -127,7 +127,7 @@ public void setValue(int v) { if (v == 0) { setText(""); - } else if (v < 10) { + } else if (v > 0 && v < 10) { setText(String.valueOf(v)); } else { throw new IllegalArgumentException( diff -r 1c62c6009161 -r 5e69b1bb707f src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java Mon Jul 27 11:56:41 2020 +0200 @@ -0,0 +1,199 @@ +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()); + }); + } +} \ No newline at end of file