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 Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Component;
024: import java.awt.Container;
025: import java.beans.PropertyVetoException;
026: import java.lang.reflect.InvocationTargetException;
027: import java.util.Comparator;
028:
029: /**
030: * Tests for JInternalFrame class that cannot be run in Event Dispatch Thread.
031: * These are test of focus subsystem mainly.
032: *
033: * These tests cannot be run on Event Dispatch Thread because we cannot
034: * request focus on some component synchronously (auxiliary
035: * function requestFocusForComponent() cannot be synchronous).
036: *
037: */
038: public class JInternalFrame_MultithreadedTest extends
039: BasicSwingTestCase {
040: private JInternalFrame frame;
041:
042: // is used in tests where frame.isShowing() must be true
043: private JFrame rootFrame;
044:
045: /*
046: * @see TestCase#setUp()
047: */
048: @Override
049: protected void setUp() throws Exception {
050: super .setUp();
051: frame = new JInternalFrame();
052: }
053:
054: /*
055: * @see TestCase#tearDown()
056: */
057: @Override
058: protected void tearDown() throws Exception {
059: if (rootFrame != null) {
060: rootFrame.dispose();
061: rootFrame = null;
062: }
063: super .tearDown();
064: }
065:
066: /**
067: * Constructor for JInternalFrameTest.
068: * @param name
069: */
070: public JInternalFrame_MultithreadedTest(final String name) {
071: super (name);
072: }
073:
074: /*
075: * Class under test for void restoreSubcomponentFocus()
076: */
077: public void testRestoreSubcomponentFocus()
078: throws InterruptedException, InvocationTargetException {
079: final Component comp1 = new JPanel();
080: final Component comp2 = new JPanel();
081: final Component comp3 = new JPanel();
082: frame.getContentPane().add(comp1, BorderLayout.NORTH);
083: frame.getContentPane().add(comp2, BorderLayout.SOUTH);
084: frame.getContentPane().add(comp3, BorderLayout.CENTER);
085: createAndShowRootFrame();
086: setSelectedFrame(frame, true);
087: SwingWaitTestCase.requestFocusInWindowForComponent(comp2);
088: setSelectedFrame(frame, false);
089: setSelectedFrame(frame, true);
090: assertTrue("focus is restored", frame.getFocusOwner() == comp2);
091: }
092:
093: /*
094: * Creates and shows rootFrame. This method is used when JInternalFrame
095: * need to be selected (isSelected() == true) for testing purposes.
096: */
097: protected void createAndShowRootFrame() {
098: frame.setSize(70, 100);
099: rootFrame = new JFrame();
100: JDesktopPane desktop = new JDesktopPane();
101: rootFrame.setContentPane(desktop);
102: rootFrame.getContentPane().add(frame);
103: rootFrame.setSize(100, 200);
104: frame.setVisible(true);
105: rootFrame.setVisible(true);
106: SwingWaitTestCase.isRealized(rootFrame);
107: }
108:
109: /*
110: * Thread safe function to make the internal frame selected
111: * or deselected
112: */
113: protected void setSelectedFrame(final JInternalFrame frame,
114: final boolean selected) {
115: try {
116: SwingUtilities.invokeAndWait(new Runnable() {
117: public void run() {
118: try {
119: frame.setSelected(selected);
120: } catch (PropertyVetoException e) {
121: }
122: }
123: });
124: } catch (Exception e) {
125: assertFalse("exception", true);
126: }
127: }
128:
129: /*
130: * Class under test for Component getMostRecentFocusOwner()
131: */
132: public void testGetMostRecentFocusOwner()
133: throws PropertyVetoException, InterruptedException,
134: InvocationTargetException {
135: final Component initial = new JPanel(); // initial focus component
136: final Component def = new JPanel(); // default focus component
137: final Component some = new JPanel(); // some another component
138: frame.getContentPane().add(initial, BorderLayout.NORTH);
139: frame.getContentPane().add(def, BorderLayout.SOUTH);
140: frame.getContentPane().add(some, BorderLayout.CENTER);
141: assertNull("null by default", frame.getMostRecentFocusOwner());
142: class MyFocusTraversalPolicy extends
143: SortingFocusTraversalPolicy {
144: Component initial;
145:
146: Component def;
147:
148: public MyFocusTraversalPolicy() {
149: setComparator(new Comparator<Object>() {
150: public int compare(final Object arg0,
151: final Object arg1) {
152: return System.identityHashCode(arg0)
153: - System.identityHashCode(arg1);
154: }
155: });
156: }
157:
158: @Override
159: public Component getInitialComponent(
160: final javax.swing.JInternalFrame frame) {
161: return initial;
162: }
163:
164: @Override
165: public Component getDefaultComponent(
166: final Container focusCycleRoot) {
167: return def;
168: }
169: }
170: MyFocusTraversalPolicy traversal = new MyFocusTraversalPolicy();
171: traversal.def = def;
172: frame.setFocusTraversalPolicy(traversal);
173: if (BasicSwingTestCase.isHarmony()) {
174: assertSame("== def (JRockit fails)", def, frame
175: .getMostRecentFocusOwner());
176: }
177: // no one component had ever the focus, initial is returned
178: traversal.initial = initial;
179: createAndShowRootFrame();
180: assertTrue("== initial",
181: frame.getMostRecentFocusOwner() == initial);
182: // request focus for 'some' component, this component must be returned by
183: // getMostRecentFocusOwner()
184: setSelectedFrame(frame, true);
185: SwingWaitTestCase.requestFocusInWindowForComponent(some);
186: setSelectedFrame(frame, false);
187: assertTrue("== some", frame.getMostRecentFocusOwner() == some);
188: // frame is selected, returns the same component as getFocusOwner()
189: setSelectedFrame(frame, true);
190: SwingWaitTestCase.requestFocusInWindowForComponent(def);
191: assertTrue("== getFocusOwner()", frame
192: .getMostRecentFocusOwner() == frame.getFocusOwner());
193: }
194:
195: /*
196: * Class under test for Component getFocusOwner()
197: */
198: public void testGetFocusOwner() throws InterruptedException,
199: InvocationTargetException {
200: final Component comp1 = new JPanel();
201: final Component comp2 = new JPanel();
202: final Component comp3 = new JPanel();
203: frame.getContentPane().add(comp1, BorderLayout.NORTH);
204: frame.getContentPane().add(comp2, BorderLayout.SOUTH);
205: frame.getContentPane().add(comp3, BorderLayout.CENTER);
206: assertNull("== null", frame.getFocusOwner());
207: createAndShowRootFrame();
208: // frame is selected, comp2 has focus
209: setSelectedFrame(frame, true);
210: SwingWaitTestCase.requestFocusInWindowForComponent(comp2);
211: assertSame("== comp2", comp2, frame.getFocusOwner());
212: // frame is not selected
213: setSelectedFrame(frame, false);
214: assertNull("== null", frame.getFocusOwner());
215: }
216: }
|