JComponent component3 = new JButton("3");
JWindow component4 = new JWindow();
component4.getContentPane().add(component1);
component1.add(component2);
component1.add(component3);
KeyEvent event11 = new KeyEvent(component1, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_A,
'a');
KeyEvent event22 = new KeyEvent(component2, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_B,
'b');
KeyEvent event33 = new KeyEvent(component3, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_C,
'c');
KeyEvent event42 = new KeyEvent(component2, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_D,
'd');
KeyEvent event51 = new KeyEvent(component1, KeyEvent.KEY_PRESSED, 0, 0,
KeyEvent.VK_ENTER, '\n');
KeyStroke keyStroke1 = KeyStroke.getKeyStrokeForEvent(event11);
KeyStroke keyStroke2 = KeyStroke.getKeyStrokeForEvent(event22);
KeyStroke keyStroke3 = KeyStroke.getKeyStrokeForEvent(event33);
KeyStroke keyStroke4 = KeyStroke.getKeyStrokeForEvent(event42);
KeyStroke keyStroke5 = KeyStroke.getKeyStrokeForEvent(event51);
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(action43, keyStroke4,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
component3.registerKeyboardAction(action43, keyStroke4,
JComponent.WHEN_IN_FOCUSED_WINDOW);
component3.registerKeyboardAction(action53, keyStroke5, JComponent.WHEN_FOCUSED);
component1.registerKeyboardAction(action51, keyStroke5,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
// component3.processKeyEvent(event1);
// assertFalse(event1.isConsumed());
//
// event1 = new KeyEvent(component1, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_A, 'a');
// new JPanel().processKeyEvent(event1);
// assertFalse(event1.isConsumed());
component1.processKeyEvent(event11);
assertTrue("event1: actionPerformed called for component", action1.eventHeard != null);
assertTrue(event11.isConsumed());
action1.eventHeard = null;
component3.processKeyEvent(event22);
assertNull("event2: wrong actionPerformed called for parent", action1.eventHeard);
assertTrue("event2: right actionPerformed called for parent",
action2.eventHeard != null);
assertTrue(event22.isConsumed());
action2.eventHeard = null;
component3.processKeyEvent(event33);
assertNull("event3: actionPerformed called for parent", action1.eventHeard);
assertNull("event3: actionPerformed called for brother", action2.eventHeard);
assertTrue("event3: actionPerformed called for component", action3.eventHeard != null);
assertTrue(event33.isConsumed());
action3.eventHeard = null;
component3.processKeyEvent(event42);
assertNull("event4: actionPerformed called for parent", action1.eventHeard);
assertNull("event4: actionPerformed called for brother", action2.eventHeard);
assertNull("event4: actionPerformed called for component", action3.eventHeard);
assertNull("event4: actionPerformed called for brother", action41.eventHeard);
assertTrue("event4: actionPerformed called for brother", action43.eventHeard != null);
assertTrue(event42.isConsumed());
component3.processKeyEvent(event51);
assertNull("event5: actionPerformed called for parent", action51.eventHeard);
assertTrue("event5: actionPerformed called for parent", action53.eventHeard != null);
assertTrue(event51.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);
panel2.add(panel1);
panel1.add(editor);
action52.eventHeard = null;
action54.eventHeard = null;
panel2.processKeyEvent(event6);
assertNull("event6: actionPerformed called for parent", action52.eventHeard);
assertTrue("event6: actionPerformed called for parent", action54.eventHeard != null);
assertTrue(event6.isConsumed());
action52.eventHeard = null;
action54.eventHeard = null;
event6 = new KeyEvent(editor, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_ENTER, '\n');
panel1.processKeyEvent(event6);
assertTrue("event6: actionPerformed called for parent", action52.eventHeard != null);
assertNull("event6: actionPerformed called for parent", action54.eventHeard);
assertTrue(event6.isConsumed());
}