JComponent component3 = new JButton("3");
JWindow component4 = new JWindow();
component4.getContentPane().add(component1);
component1.add(component2);
component1.add(component3);
KeyEvent event1 = new KeyEvent(component1, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_A,
'a');
KeyEvent event2 = new KeyEvent(component2, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_B,
'b');
KeyEvent event3 = new KeyEvent(component3, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_C,
'c');
KeyEvent event4 = new KeyEvent(component2, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_D,
'd');
KeyEvent event5 = new KeyEvent(component1, KeyEvent.KEY_PRESSED, 0, 0,
KeyEvent.VK_ENTER, '\n');
KeyStroke keyStroke1 = KeyStroke.getKeyStrokeForEvent(event1);
KeyStroke keyStroke2 = KeyStroke.getKeyStrokeForEvent(event2);
KeyStroke keyStroke3 = KeyStroke.getKeyStrokeForEvent(event3);
KeyStroke keyStroke4 = KeyStroke.getKeyStrokeForEvent(event4);
KeyStroke keyStroke5 = KeyStroke.getKeyStrokeForEvent(event5);
component1.registerKeyboardAction(action1, keyStroke1, JComponent.WHEN_FOCUSED);
component1.registerKeyboardAction(action2, keyStroke2,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
component3.registerKeyboardAction(action3, keyStroke3,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
component1.registerKeyboardAction(action41, keyStroke4,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
component3.registerKeyboardAction(action42, keyStroke4,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
component3.registerKeyboardAction(action53, keyStroke5, JComponent.WHEN_FOCUSED);
component1.registerKeyboardAction(action51, keyStroke5,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
boolean result = SwingUtilities.processKeyBindings(event1);
assertTrue(result);
assertTrue("event1: actionPerformed called for component", action1.event != null);
assertFalse(event1.isConsumed());
action1.event = null;
result = SwingUtilities.processKeyBindings(event2);
assertTrue(result);
assertNull("event2: wrong actionPerformed called for parent", action1.event);
assertTrue("event2: right actionPerformed called for parent", action2.event != null);
assertFalse(event2.isConsumed());
action2.event = null;
result = SwingUtilities.processKeyBindings(event3);
assertTrue(result);
assertNull("event3: actionPerformed called for parent", action1.event);
assertNull("event3: actionPerformed called for brother", action2.event);
assertTrue("event3: actionPerformed called for component", action3.event != null);
assertFalse(event3.isConsumed());
action3.event = null;
result = SwingUtilities.processKeyBindings(event4);
assertTrue(result);
assertNull("event4: actionPerformed called for parent", action1.event);
assertNull("event4: actionPerformed called for brother", action2.event);
assertNull("event4: actionPerformed called for component", action3.event);
assertTrue("event4: actionPerformed called for brother", action41.event != null);
assertNull("event4: actionPerformed called for brother", action42.event);
assertFalse(event4.isConsumed());
result = SwingUtilities.processKeyBindings(event5);
assertTrue(result);
assertTrue("event5: actionPerformed called for parent", action51.event != null);
assertNull("event5: actionPerformed called for parent", action53.event);
assertFalse(event5.isConsumed());
JComponent panel1 = new JPanel();
JComponent panel2 = new JPanel();
JTextField editor = new JTextField();
KeyEvent event6 = new KeyEvent(editor, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_ENTER,
'\n');
panel1.registerKeyboardAction(action52, keyStroke5,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
panel2.registerKeyboardAction(action54, keyStroke5,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);