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

Tue, 28 Jul 2020 14:27:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Jul 2020 14:27:14 +0200
changeset 22
06170a0be62a
parent 19
627e1c99bcf1
permissions
-rw-r--r--

bugfix: modified state is reset even when saving fails + more tests

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 import org.junit.jupiter.api.AfterEach;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 import org.junit.jupiter.api.BeforeEach;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 import org.junit.jupiter.api.Test;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 import javax.swing.*;
19
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
8 import java.awt.*;
13
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;
19
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
11 import java.util.concurrent.TimeUnit;
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 import static org.junit.jupiter.api.Assertions.*;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 class SudokuTextFieldTest {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 private void dispatch(SudokuTextField tf, KeyEvent pressed) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 final var released = new KeyEvent(tf, KeyEvent.KEY_RELEASED, pressed.getWhen()+1,
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 pressed.getModifiersEx(), pressed.getKeyCode(), pressed.getKeyChar());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 final KeyEvent typed;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 if (pressed.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 typed = new KeyEvent(tf, KeyEvent.KEY_TYPED, pressed.getWhen(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 pressed.getModifiersEx(), KeyEvent.VK_UNDEFINED, pressed.getKeyChar());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 } else {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 typed = null;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 tf.requestFocusInWindow();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 tf.dispatchEvent(pressed);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 if (typed != null) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 tf.dispatchEvent(typed);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 tf.dispatchEvent(released);
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
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 private JFrame testFrame;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 @BeforeEach
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 void createTestFrame() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 // 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
40 testFrame = new JFrame();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 testFrame.setVisible(true);
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
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 @AfterEach
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 void disposeTestFrame() {
19
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
46 testFrame.setVisible(false);
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 testFrame.dispose();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 private SudokuTextField createTestSubject() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 final var tf = new SudokuTextField();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 testFrame.add(tf);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 return tf;
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 void setValue() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 for (int i = 0 ; i <= 9 ; i++) {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 tf.setValue(i);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 assertEquals(i, tf.getValue());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 assertEquals(i == 0 ? "" : String.valueOf(i), tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 assertFalse(tf.isModified());
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 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 void setInvalidValue() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 // when / then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 assertThrows(IllegalArgumentException.class, () -> tf.setValue(10));
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 assertThrows(IllegalArgumentException.class, () -> tf.setValue(-1));
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 void testModifiedByCall() {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 // then initially
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 tf.setModified(true);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 assertTrue(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 tf.setModified(false);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 void testModifiedByKeystroke() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 assertFalse(tf.isModified());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 assertTrue(tf.isModified());
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 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 void testTextChangeByKeystore() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 assertEquals("4", tf.getText());
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 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 @Test
16
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
127 void testNumpad() throws InvocationTargetException, InterruptedException {
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
128 // given
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
129 final var tf = createTestSubject();
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
130 assertEquals("", tf.getText());
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
131 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
132 0, KeyEvent.VK_NUMPAD4, '4');
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
133 SwingUtilities.invokeAndWait(() -> {
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
134 // when
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
135 dispatch(tf, event);
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
136 // then
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
137 assertEquals("4", tf.getText());
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 }
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
140
bddb2633c98b adds numpad test
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
141 @Test
13
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 tf.setText("6");
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 tf.selectAll();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 0, KeyEvent.VK_4, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 assertEquals("4", tf.getText());
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 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 void testNumbersOnly() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 tf.setText("6");
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 tf.selectAll();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 0, KeyEvent.VK_A, 'a');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 dispatch(tf, event);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 assertEquals("6", tf.getText());
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 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 void testSingleDigit() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 final var firstEvent = 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, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 0, KeyEvent.VK_A, '6');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 dispatch(tf, firstEvent);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 dispatch(tf, secondEvent);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 assertEquals("4", tf.getText());
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 }
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 @Test
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 void testBackspace() throws InvocationTargetException, InterruptedException {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 // given
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 final var tf = createTestSubject();
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 assertEquals("", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 0, KeyEvent.VK_A, '4');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 0, KeyEvent.VK_A, '6');
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 SwingUtilities.invokeAndWait(() -> {
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 // when
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 dispatch(tf, typeFour);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 dispatch(tf, typeBackspace);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 dispatch(tf, typeSix);
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 // then
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 assertEquals("6", tf.getText());
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 });
5e69b1bb707f adds SudokuTextFieldTest
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 }
19
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
211
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
212 @Test
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
213 void testFocusGained() throws InvocationTargetException, InterruptedException {
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
214 final var tf = createTestSubject();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
215 SwingUtilities.invokeAndWait(() -> {
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
216 // given
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
217 tf.setText("5");
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
218 assertNull(tf.getSelectedText());
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
219 // when
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
220 tf.requestFocusInWindow();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
221 });
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
222 // give WM time to deliver the event
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
223 TimeUnit.MILLISECONDS.sleep(100);
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
224 // then
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
225 SwingUtilities.invokeAndWait(() -> assertEquals("5", tf.getSelectedText()));
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
226 }
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
227
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
228 @Test
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
229 void testFocusLost() throws InvocationTargetException, InterruptedException {
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
230 final var tf = createTestSubject();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
231 final var focusStealer = new TextField();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
232 testFrame.add(focusStealer);
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
233 SwingUtilities.invokeAndWait(() -> {
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
234 // given
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
235 tf.setText("5");
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
236 tf.selectAll();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
237 assertEquals("5", tf.getSelectedText());
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
238 // when
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
239 focusStealer.requestFocusInWindow();
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
240 });
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
241 // give WM time to deliver the event
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
242 TimeUnit.MILLISECONDS.sleep(100);
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
243 // then
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
244 SwingUtilities.invokeAndWait(() -> assertNull(tf.getSelectedText()));
627e1c99bcf1 adds tests for focus events
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
245 }
22
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
246
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
247 @Test
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
248 void testFont() {
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
249 final var tf = new SudokuTextField();
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
250 assertEquals(18, tf.getFont().getSize());
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
251 }
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
252
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
253 @Test
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
254 void testAlignment() {
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
255 final var tf = new SudokuTextField();
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
256 assertEquals(JTextField.CENTER, tf.getHorizontalAlignment());
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
257 }
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
258
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
259 @Test
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
260 void testBackground() {
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
261 final var tf = new SudokuTextField();
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
262 assertEquals(Color.WHITE, tf.getBackground());
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
263 }
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
264
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
265 @Test
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
266 void testSize() {
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
267 final var tf = new SudokuTextField();
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
268 final var expected = new Dimension(40, 40);
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
269 assertEquals(expected, tf.getMinimumSize());
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
270 assertEquals(expected, tf.getPreferredSize());
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
271 assertEquals(expected, tf.getMaximumSize());
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
272 }
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
273
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
274 @Test
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
275 void testBorder() {
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
276 final var tf = new SudokuTextField();
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
277 assertNull(tf.getBorder());
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
278 }
06170a0be62a bugfix: modified state is reset even when saving fails + more tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
279 }

mercurial