|
5 | 5 | import org.junit.Test;
|
6 | 6 | import processing.event.KeyEvent;
|
7 | 7 |
|
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | +import static org.junit.Assert.assertFalse; |
| 10 | +import static org.junit.Assert.assertTrue; |
| 11 | + |
8 | 12 | public class PAppletKeyEventTest {
|
9 | 13 |
|
10 | 14 | private static final int SHIFT_MASK = 1;
|
@@ -137,4 +141,37 @@ public void testKeyFocusLost() {
|
137 | 141 | Assert.assertFalse("keyPressed should be false after focus lost", applet.keyPressed);
|
138 | 142 | Assert.assertEquals("pressedKeys should be empty after focus lost", true, applet.pressedKeys.isEmpty());
|
139 | 143 | }
|
| 144 | + |
| 145 | + @Test |
| 146 | + public void testShiftAndPageUpKeyCodesAreDifferent() { |
| 147 | + final int VK_SHIFT = java.awt.event.KeyEvent.VK_SHIFT; |
| 148 | + final int VK_PAGE_UP = java.awt.event.KeyEvent.VK_PAGE_UP; |
| 149 | + |
| 150 | + long shiftHash = ((long)VK_SHIFT << Character.SIZE); |
| 151 | + long pageUpHash = ((long)VK_PAGE_UP << Character.SIZE); |
| 152 | + |
| 153 | + KeyEvent shiftPressEvent = new KeyEvent(null, 0L, KeyEvent.PRESS, SHIFT_MASK, '\0', VK_SHIFT, false); |
| 154 | + applet.handleKeyEvent(shiftPressEvent); |
| 155 | + |
| 156 | + assertTrue("keyPressed must be true", applet.keyPressed); |
| 157 | + assertTrue("SHIFT should be in pressedKeys", applet.pressedKeys.contains(shiftHash)); |
| 158 | + |
| 159 | + KeyEvent pageUpPressEvent = new KeyEvent(null, 0L, KeyEvent.PRESS, 0, '\0', VK_PAGE_UP, false); |
| 160 | + applet.handleKeyEvent(pageUpPressEvent); |
| 161 | + |
| 162 | + assertEquals("pressedKeys must contain exactly two keys", 2, applet.pressedKeys.size()); |
| 163 | + assertTrue("PAGE_UP should be in pressedKeys", applet.pressedKeys.contains(pageUpHash)); |
| 164 | + |
| 165 | + KeyEvent shiftRelease = new KeyEvent(null, 0L, KeyEvent.RELEASE, 0, '\0', VK_SHIFT, false); |
| 166 | + applet.handleKeyEvent(shiftRelease); |
| 167 | + assertFalse("SHIFT should have been removed", applet.pressedKeys.contains(shiftHash)); |
| 168 | + assertTrue ("PAGE_UP should still be down", applet.pressedKeys.contains(pageUpHash)); |
| 169 | + assertTrue ("keyPressed must still be true", applet.keyPressed); |
| 170 | + assertEquals("pressedKeys must now have one key", 1, applet.pressedKeys.size()); |
| 171 | + |
| 172 | + KeyEvent pageUpRelease = new KeyEvent(null, 0L, KeyEvent.RELEASE, 0, '\0', VK_PAGE_UP, false); |
| 173 | + applet.handleKeyEvent(pageUpRelease); |
| 174 | + assertTrue ("pressedKeys must now be empty", applet.pressedKeys.isEmpty()); |
| 175 | + assertFalse("keyPressed must be false", applet.keyPressed); |
| 176 | + } |
140 | 177 | }
|
0 commit comments