### Question:
KeyboardState { public EnumSet<Key> getKeysPressedSince(final KeyboardState previous) { final EnumSet<Key> result = EnumSet.copyOf(_keysDown); result.removeAll(previous._keysDown); return result; } KeyboardState(final EnumSet<Key> keysDown, final KeyEvent keyEvent); boolean isDown(final Key key); boolean isAllDown(final Key... keys); boolean isAtLeastOneDown(final Key... keys); Set<Key> getKeysDown(); KeyEvent getKeyEvent(); EnumSet<Key> getKeysReleasedSince(final KeyboardState previous); EnumSet<Key> getKeysPressedSince(final KeyboardState previous); EnumSet<Key> getKeysHeldSince(final KeyboardState previous); @Override String toString(); static final KeyboardState NOTHING; }### Answer:
@Test public void testKeysPressed1() throws Exception { ks1 = new KeyboardState(EnumSet.of(Key.A, Key.B), KeyEvent.NOTHING); ks2 = new KeyboardState(EnumSet.of(Key.A, Key.C, Key.D), KeyEvent.NOTHING); final EnumSet<Key> pressed = ks2.getKeysPressedSince(ks1); assertEquals("2 key", 2, pressed.size()); assertTrue("c pressed", pressed.contains(Key.C)); assertTrue("d pressed", pressed.contains(Key.D)); }
@Test public void testKeysPressed2() throws Exception { ks1 = new KeyboardState(EnumSet.noneOf(Key.class), KeyEvent.NOTHING); ks2 = new KeyboardState(EnumSet.of(Key.A, Key.C, Key.D), KeyEvent.NOTHING); final EnumSet<Key> pressed = ks2.getKeysPressedSince(ks1); assertEquals("2 key", 3, pressed.size()); assertTrue("a pressed", pressed.contains(Key.A)); assertTrue("c pressed", pressed.contains(Key.C)); assertTrue("d pressed", pressed.contains(Key.D)); }
@Test public void testKeysPressed3() throws Exception { ks1 = new KeyboardState(EnumSet.of(Key.A, Key.C, Key.D), KeyEvent.NOTHING); ks2 = new KeyboardState(EnumSet.of(Key.A), KeyEvent.NOTHING); final EnumSet<Key> pressed = ks2.getKeysPressedSince(ks1); assertEquals("0 key", 0, pressed.size()); }