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: import junit.framework.TestCase;
29:
30: public class JFormattedTextField_CommitActionRTest extends TestCase {
31: private JFrame frame;
32:
33: private JPanel c;
34:
35: private JTextField textField;
36:
37: @Override
38: protected void setUp() throws Exception {
39: super .setUp();
40: frame = new JFrame();
41: }
42:
43: @Override
44: protected void tearDown() throws Exception {
45: if (frame != null) {
46: frame.dispose();
47: frame = null;
48: }
49: super .tearDown();
50: }
51:
52: public void testActionPerformed() throws Exception {
53: c = new JPanel();
54: Container parent = new Panel();
55: textField = new JFormattedTextField(" ");
56: frame.getContentPane().add(c);
57: c.add(parent);
58: parent.add(textField);
59: frame.pack();
60: frame.setVisible(true);
61: final Rectangle flag = new Rectangle();
62: final ActionListener parentListener = new ActionListener() {
63: public void actionPerformed(final ActionEvent event) {
64: flag.x = 100;
65: }
66: };
67: final ActionListener textFieldListener = new ActionListener() {
68: public void actionPerformed(final ActionEvent event) {
69: flag.y = 100;
70: }
71: };
72: c.registerKeyboardAction(parentListener, KeyStroke
73: .getKeyStroke(KeyEvent.VK_ENTER, 0),
74: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
75: SwingWaitTestCase.requestFocusInWindowForComponent(textField);
76: textField.dispatchEvent(new KeyEvent(textField,
77: KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_ENTER,
78: (char) 13));
79: assertEquals("parent's action hasn't been fired", 0, flag.x);
80: assertEquals("textFields action hasn't been fired", 0, flag.y);
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: }
|