001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 02.03.2005
021:
022: */package javax.swing.text;
023:
024: import java.awt.event.ActionEvent;
025: import javax.swing.JButton;
026: import javax.swing.JComponent;
027: import javax.swing.JDialog;
028: import javax.swing.JTextArea;
029: import javax.swing.JTextField;
030: import javax.swing.SwingWaitTestCase;
031: import junit.framework.TestCase;
032:
033: public class TextAction_MultithreadedTest extends TestCase {
034: JDialog window1;
035:
036: JDialog window2;
037:
038: JDialog window3;
039:
040: @Override
041: protected void setUp() throws Exception {
042: super .setUp();
043: }
044:
045: @Override
046: protected void tearDown() throws Exception {
047: if (window1 != null) {
048: window1.dispose();
049: window1 = null;
050: }
051: if (window2 != null) {
052: window2.dispose();
053: window2 = null;
054: }
055: if (window3 != null) {
056: window3.dispose();
057: window3 = null;
058: }
059: super .tearDown();
060: }
061:
062: public void testGetFocusedComponent() throws Exception {
063: TextAction action = new TextAction("") {
064: private static final long serialVersionUID = 1L;
065:
066: public void actionPerformed(final ActionEvent e) {
067: }
068: };
069: window1 = new JDialog();
070: window2 = new JDialog();
071: window3 = new JDialog();
072: JButton component1 = new JButton();
073: JComponent component2 = new JTextField();
074: JComponent component3 = new JTextArea();
075: ((JTextComponent) component2).setText("3");
076: window1.getContentPane().add(component1);
077: window2.getContentPane().add(component2);
078: window3.getContentPane().add(component3);
079: window1.pack();
080: window2.pack();
081: window3.pack();
082: window2.setVisible(true);
083: SwingWaitTestCase.requestFocusInWindowForComponent(component2);
084: Object res = action.getFocusedComponent();
085: assertEquals("focused component", component2, res);
086: window3.setVisible(true);
087: SwingWaitTestCase.requestFocusInWindowForComponent(component3);
088: res = action.getFocusedComponent();
089: assertEquals("focused component", component3, res);
090: window1.setVisible(true);
091: SwingWaitTestCase.requestFocusInWindowForComponent(component1);
092: res = action.getFocusedComponent();
093: assertEquals("focused component", component3, res);
094: }
095:
096: public void testGetTextComponent() throws Exception {
097: TextAction action = new TextAction("") {
098: private static final long serialVersionUID = 1L;
099:
100: public void actionPerformed(final ActionEvent e) {
101: }
102: };
103: window1 = new JDialog();
104: window2 = new JDialog();
105: JButton componentNoText1 = new JButton();
106: JButton componentNoText2 = new JButton();
107: JTextComponent componentText1 = new JTextField();
108: JTextComponent componentText2 = new JTextField();
109: componentText1.setText("1");
110: componentText1.setText("2");
111: ActionEvent eventNoTextComponentInside = new ActionEvent(
112: componentNoText2, 0, "event1");
113: ActionEvent eventTextComponentInside = new ActionEvent(
114: componentText2, 0, "event2");
115: window1.getContentPane().add(componentNoText1);
116: window2.getContentPane().add(componentText1);
117: window1.pack();
118: window2.pack();
119: Object previouslyFocused = action.getFocusedComponent();
120: window1.setVisible(true);
121: SwingWaitTestCase
122: .requestFocusInWindowForComponent(componentNoText1);
123: Object res = action
124: .getTextComponent(eventNoTextComponentInside);
125: assertEquals("focused component", previouslyFocused, res);
126: window1.setVisible(true);
127: SwingWaitTestCase
128: .requestFocusInWindowForComponent(componentNoText1);
129: res = action.getTextComponent(eventTextComponentInside);
130: assertEquals("focused component", componentText2, res);
131: window2.setVisible(true);
132: SwingWaitTestCase
133: .requestFocusInWindowForComponent(componentText1);
134: res = action.getTextComponent(eventNoTextComponentInside);
135: assertEquals("focused component", componentText1, res);
136: SwingWaitTestCase
137: .requestFocusInWindowForComponent(componentText1);
138: res = action.getTextComponent(eventTextComponentInside);
139: assertEquals("focused component", componentText2, res);
140: }
141: }
|