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

Mon, 27 Jul 2020 11:57:42 +0200

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

renames help menu

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 getValue() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 void setValue() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 for (int i = 0 ; i <= 9 ; i++) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 tf.setValue(i);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 assertEquals(i, tf.getValue());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 assertEquals(i == 0 ? "" : String.valueOf(i), tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 void setInvalidValue() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 // when / then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 assertThrows(IllegalArgumentException.class, () -> tf.setValue(10));
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 assertThrows(IllegalArgumentException.class, () -> tf.setValue(-1));
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 void testModifiedByCall() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 // then initially
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 tf.setModified(true);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 assertTrue(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 tf.setModified(false);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 void testModifiedByKeystroke() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 assertTrue(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 void testTextChangeByKeystore() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 assertEquals("4", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 tf.setText("6");
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 tf.selectAll();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 assertEquals("4", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 void testNumbersOnly() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 tf.setText("6");
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 tf.selectAll();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 0, KeyEvent.VK_A, 'a');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 assertEquals("6", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 void testSingleDigit() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 final var firstEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 0, KeyEvent.VK_A, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 0, KeyEvent.VK_A, '6');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 dispatch(tf, firstEvent);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 dispatch(tf, secondEvent);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 assertEquals("4", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 void testBackspace() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 0, KeyEvent.VK_A, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 0, KeyEvent.VK_A, '6');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 dispatch(tf, typeFour);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 dispatch(tf, typeBackspace);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 dispatch(tf, typeSix);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 assertEquals("6", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 }

mercurial