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 Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Component;
023: import java.awt.ComponentOrientation;
024: import java.awt.GridLayout;
025: import javax.swing.plaf.ComboBoxUI;
026:
027: public class LayoutFocusTraversalPolicyTest extends BasicSwingTestCase {
028: private LayoutFocusTraversalPolicy policy;
029:
030: private JButton button1;
031:
032: private JButton button2;
033:
034: private JButton button3;
035:
036: private JButton button4;
037:
038: private JFrame frame;
039:
040: public LayoutFocusTraversalPolicyTest(final String name) {
041: super (name);
042: }
043:
044: @Override
045: protected void setUp() throws Exception {
046: policy = new LayoutFocusTraversalPolicy();
047: frame = new JFrame();
048: button1 = createButton("1");
049: button2 = createButton("2");
050: button3 = createButton("3");
051: button4 = createButton("4");
052: }
053:
054: @Override
055: protected void tearDown() throws Exception {
056: policy = null;
057: button1 = null;
058: button2 = null;
059: button3 = null;
060: button4 = null;
061: frame.dispose();
062: frame = null;
063: }
064:
065: public void testAccept() throws Exception {
066: frame.setVisible(true);
067: SwingWaitTestCase.isRealized(frame);
068: Component invisible = new JPanel();
069: frame.getContentPane().add(invisible);
070: invisible.setVisible(false);
071: assertFalse(policy.accept(invisible));
072: Component undisplayable = new JPanel();
073: assertFalse(policy.accept(undisplayable));
074: Component disabled = new JPanel();
075: frame.getContentPane().add(disabled);
076: disabled.setEnabled(false);
077: assertFalse(policy.accept(disabled));
078: Component unfocusable = new JPanel();
079: frame.getContentPane().add(unfocusable);
080: unfocusable.setFocusable(false);
081: assertFalse(policy.accept(unfocusable));
082: JComboBox comboBox = new JComboBox();
083: comboBox.setUI(new TestComboBoxUI(false));
084: frame.getContentPane().add(comboBox);
085: assertFalse(policy.accept(comboBox));
086: comboBox = new JComboBox();
087: comboBox.setUI(new TestComboBoxUI(true));
088: frame.getContentPane().add(comboBox);
089: assertTrue(policy.accept(comboBox));
090: JComponent panel = new JPanel();
091: frame.getContentPane().add(panel);
092: panel.getInputMap(JComponent.WHEN_FOCUSED).put(
093: KeyStroke.getKeyStroke('a'), "anything");
094: assertTrue(policy.accept(panel));
095: panel.getInputMap(JComponent.WHEN_FOCUSED).clear();
096: assertFalse(policy.accept(panel));
097: JMenuBar menuBar = new JMenuBar();
098: frame.setJMenuBar(menuBar);
099: assertFalse(policy.accept(menuBar));
100: JMenu menu = new JMenu();
101: menuBar.add(menu);
102: assertFalse(policy.accept(menu));
103: //TODO: check for DefaultFocusTraversalPolicy.accept() should be provided here
104: }
105:
106: public void testGetComponentBefore() throws Exception {
107: JPanel cycleRoot = createTestPanel(ComponentOrientation.LEFT_TO_RIGHT);
108: assertEquals(button1, policy.getComponentBefore(cycleRoot,
109: button2));
110: assertEquals(button2, policy.getComponentBefore(cycleRoot,
111: button3));
112: assertEquals(button3, policy.getComponentBefore(cycleRoot,
113: button4));
114: assertEquals(button4, policy.getComponentBefore(cycleRoot,
115: button1));
116: cycleRoot = createTestPanel(ComponentOrientation.RIGHT_TO_LEFT);
117: assertEquals(button1, policy.getComponentBefore(cycleRoot,
118: button2));
119: assertEquals(button2, policy.getComponentBefore(cycleRoot,
120: button3));
121: assertEquals(button3, policy.getComponentBefore(cycleRoot,
122: button4));
123: assertEquals(button4, policy.getComponentBefore(cycleRoot,
124: button1));
125: }
126:
127: public void testGetComponentBefore_Null() throws Exception {
128: JPanel cycleRoot = createTestPanel(ComponentOrientation.LEFT_TO_RIGHT);
129:
130: try {
131: policy.getComponentBefore(cycleRoot, null);
132: fail("IllegalArgumentException should have been thrown");
133: } catch (IllegalArgumentException e) {
134: // Expected
135: }
136:
137: try {
138: policy.getComponentBefore(null, button1);
139: fail("IllegalArgumentException should have been thrown");
140: } catch (IllegalArgumentException e) {
141: // Expected
142: }
143:
144: try {
145: policy.getComponentBefore(null, null);
146: fail("IllegalArgumentException should have been thrown");
147: } catch (IllegalArgumentException e) {
148: // Expected
149: }
150: }
151:
152: public void testGetComponentAfter() throws Exception {
153: JPanel cycleRoot = createTestPanel(ComponentOrientation.LEFT_TO_RIGHT);
154: assertEquals(button1, policy.getComponentAfter(cycleRoot,
155: button4));
156: assertEquals(button2, policy.getComponentAfter(cycleRoot,
157: button1));
158: assertEquals(button3, policy.getComponentAfter(cycleRoot,
159: button2));
160: assertEquals(button4, policy.getComponentAfter(cycleRoot,
161: button3));
162: cycleRoot = createTestPanel(ComponentOrientation.RIGHT_TO_LEFT);
163: assertEquals(button1, policy.getComponentAfter(cycleRoot,
164: button4));
165: assertEquals(button2, policy.getComponentAfter(cycleRoot,
166: button1));
167: assertEquals(button3, policy.getComponentAfter(cycleRoot,
168: button2));
169: assertEquals(button4, policy.getComponentAfter(cycleRoot,
170: button3));
171: }
172:
173: public void testGetComponentAfter_Null() throws Exception {
174: JPanel cycleRoot = createTestPanel(ComponentOrientation.LEFT_TO_RIGHT);
175:
176: try {
177: policy.getComponentAfter(cycleRoot, null);
178: fail("IllegalArgumentException should have been thrown");
179: } catch (IllegalArgumentException e) {
180: // Expected
181: }
182:
183: try {
184: policy.getComponentAfter(null, button1);
185: fail("IllegalArgumentException should have been thrown");
186: } catch (IllegalArgumentException e) {
187: // Expected
188: }
189:
190: try {
191: policy.getComponentAfter(null, null);
192: fail("IllegalArgumentException should have been thrown");
193: } catch (IllegalArgumentException e) {
194: // Expected
195: }
196: }
197:
198: public void testGetLastComponent() throws Exception {
199: JPanel cycleRoot = createTestPanel(ComponentOrientation.LEFT_TO_RIGHT);
200: assertEquals(button1, policy.getLastComponent(cycleRoot));
201: cycleRoot = createTestPanel(ComponentOrientation.RIGHT_TO_LEFT);
202: assertEquals(button1, policy.getLastComponent(cycleRoot));
203: }
204:
205: public void testGetLastComponent_Null() throws Exception {
206: JPanel cycleRoot = createTestPanel(ComponentOrientation.LEFT_TO_RIGHT);
207:
208: try {
209: policy.getLastComponent(null);
210: fail("IllegalArgumentException should have been thrown");
211: } catch (IllegalArgumentException e) {
212: // Expected
213: }
214: }
215:
216: public void testGetFirstComponent() throws Exception {
217: JPanel cycleRoot = createTestPanel(ComponentOrientation.LEFT_TO_RIGHT);
218: assertEquals(button2, policy.getFirstComponent(cycleRoot));
219: cycleRoot = createTestPanel(ComponentOrientation.RIGHT_TO_LEFT);
220: assertEquals(button2, policy.getFirstComponent(cycleRoot));
221: }
222:
223: public void testGetFirstComponent_Null() throws Exception {
224: JPanel cycleRoot = createTestPanel(ComponentOrientation.LEFT_TO_RIGHT);
225:
226: try {
227: policy.getFirstComponent(null);
228: fail("IllegalArgumentException should have been thrown");
229: } catch (IllegalArgumentException e) {
230: // Expected
231: }
232: }
233:
234: private JPanel createTestPanel(final ComponentOrientation co)
235: throws Exception {
236: JPanel result = new JPanel(new GridLayout(2, 2));
237: frame.getContentPane().add(result);
238: result.setFocusCycleRoot(true);
239: result.setFocusable(false);
240: result.setFocusTraversalPolicy(policy);
241: result.setComponentOrientation(co);
242: result.add(button2);
243: result.add(button3);
244: result.add(button4);
245: result.add(button1);
246: frame.pack();
247: frame.setVisible(true);
248: SwingWaitTestCase.isRealized(frame);
249: return result;
250: }
251:
252: private JButton createButton(final String name) {
253: JButton result = new JButton(name);
254: result.setName(name);
255: return result;
256: }
257:
258: private class TestComboBoxUI extends ComboBoxUI {
259: private final boolean isFocusTraversable;
260:
261: public TestComboBoxUI(final boolean isFocusTraversable) {
262: this .isFocusTraversable = isFocusTraversable;
263: }
264:
265: @Override
266: public boolean isFocusTraversable(final JComboBox comboBox) {
267: return isFocusTraversable;
268: }
269:
270: @Override
271: public boolean isPopupVisible(final JComboBox comboBox) {
272: return false;
273: }
274:
275: @Override
276: public void setPopupVisible(final JComboBox comboBox,
277: final boolean isVisible) {
278: }
279: }
280: }
|