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

13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 package de.uapcore.sudoku;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 import org.junit.jupiter.api.AfterEach;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 import org.junit.jupiter.api.BeforeEach;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 import org.junit.jupiter.api.Test;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 import javax.swing.*;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 import java.awt.event.KeyEvent;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 import java.lang.reflect.InvocationTargetException;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 import static org.junit.jupiter.api.Assertions.*;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 class SudokuTextFieldTest {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 private void dispatch(SudokuTextField tf, KeyEvent pressed) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 final var released = new KeyEvent(tf, KeyEvent.KEY_RELEASED, pressed.getWhen()+1,
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 pressed.getModifiersEx(), pressed.getKeyCode(), pressed.getKeyChar());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 final KeyEvent typed;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 if (pressed.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 typed = new KeyEvent(tf, KeyEvent.KEY_TYPED, pressed.getWhen(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 pressed.getModifiersEx(), KeyEvent.VK_UNDEFINED, pressed.getKeyChar());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 } else {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 typed = null;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 tf.requestFocusInWindow();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 tf.dispatchEvent(pressed);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 if (typed != null) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 tf.dispatchEvent(typed);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 tf.dispatchEvent(released);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 private JFrame testFrame;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 @BeforeEach
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 void createTestFrame() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 // we have to use a visible frame for the events to be dispatched
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 testFrame = new JFrame();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 testFrame.setVisible(true);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 @AfterEach
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 void disposeTestFrame() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 testFrame.dispose();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 private SudokuTextField createTestSubject() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 final var tf = new SudokuTextField();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 testFrame.add(tf);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 return tf;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 void setValue() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 for (int i = 0 ; i <= 9 ; i++) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 tf.setValue(i);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 assertEquals(i, tf.getValue());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 assertEquals(i == 0 ? "" : String.valueOf(i), tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 void setInvalidValue() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 // when / then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 assertThrows(IllegalArgumentException.class, () -> tf.setValue(10));
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 assertThrows(IllegalArgumentException.class, () -> tf.setValue(-1));
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 void testModifiedByCall() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 // then initially
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 tf.setModified(true);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 assertTrue(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 tf.setModified(false);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 void testModifiedByKeystroke() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 assertTrue(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 void testTextChangeByKeystore() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 assertEquals("4", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 @Test
16
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
125 void testNumpad() throws InvocationTargetException, InterruptedException {
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
126 // given
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
127 final var tf = createTestSubject();
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
128 assertEquals("", tf.getText());
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
129 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
130 0, KeyEvent.VK_NUMPAD4, '4');
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
131 SwingUtilities.invokeAndWait(() -> {
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
132 // when
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
133 dispatch(tf, event);
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
134 // then
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
135 assertEquals("4", tf.getText());
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
136 });
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
137 }
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
138
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
139 @Test
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 tf.setText("6");
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 tf.selectAll();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 assertEquals("4", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 void testNumbersOnly() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 tf.setText("6");
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 tf.selectAll();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 0, KeyEvent.VK_A, 'a');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 assertEquals("6", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 void testSingleDigit() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 final var firstEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 0, KeyEvent.VK_A, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 0, KeyEvent.VK_A, '6');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 dispatch(tf, firstEvent);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 dispatch(tf, secondEvent);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 assertEquals("4", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 void testBackspace() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 0, KeyEvent.VK_A, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 0, KeyEvent.VK_A, '6');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 dispatch(tf, typeFour);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 dispatch(tf, typeBackspace);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 dispatch(tf, typeSix);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 assertEquals("6", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 }

mercurial