01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Alexander T. Simbirtsev
19: * @version $Revision$
20: */package javax.swing;
21:
22: import java.awt.Container;
23: import java.awt.Panel;
24: import java.awt.Rectangle;
25: import java.awt.event.ActionEvent;
26: import java.awt.event.ActionListener;
27: import java.awt.event.KeyEvent;
28:
29: public class JTextField_NotifyActionRTest extends BasicSwingTestCase {
30: private JFrame frame;
31:
32: private JPanel c;
33:
34: private JTextField textField;
35:
36: @Override
37: protected void setUp() throws Exception {
38: super .setUp();
39: frame = new JFrame();
40: }
41:
42: @Override
43: protected void tearDown() throws Exception {
44: if (frame != null) {
45: frame.dispose();
46: frame = null;
47: }
48: super .tearDown();
49: }
50:
51: public void testActionPerformed() throws Exception {
52: c = new JPanel();
53: Container parent = new Panel();
54: textField = new JTextField(" ");
55: frame.getContentPane().add(c);
56: c.add(parent);
57: parent.add(textField);
58: frame.pack();
59: frame.setVisible(true);
60: final Rectangle flag = new Rectangle();
61: final ActionListener parentListener = new ActionListener() {
62: public void actionPerformed(final ActionEvent event) {
63: flag.x = 100;
64: }
65: };
66: final ActionListener textFieldListener = new ActionListener() {
67: public void actionPerformed(final ActionEvent event) {
68: flag.y = 100;
69: }
70: };
71: c.registerKeyboardAction(parentListener, KeyStroke
72: .getKeyStroke(KeyEvent.VK_ENTER, 0),
73: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
74: SwingWaitTestCase.requestFocusInWindowForComponent(textField);
75: textField.dispatchEvent(new KeyEvent(textField,
76: KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_ENTER,
77: (char) 13));
78: assertEquals("parent's action has been fired", 100, flag.x);
79: assertEquals("textFields action hasn't been fired", 0, flag.y);
80: flag.x = 0;
81: textField.addActionListener(textFieldListener);
82: textField.dispatchEvent(new KeyEvent(textField,
83: KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_ENTER,
84: (char) 13));
85: assertEquals("parent's action hasn't been fired", 0, flag.x);
86: assertEquals("textFields action has been fired", 100, flag.y);
87: }
88: }
|