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.util.Comparator;
024:
025: public class SortingFocusTraversalPolicyRTest extends
026: BasicSwingTestCase {
027: private SortingFocusTraversalPolicy policy;
028:
029: private JButton button1;
030:
031: private JButton button2;
032:
033: private JButton button3;
034:
035: private JButton button4;
036:
037: private JFrame frame;
038:
039: @SuppressWarnings("unchecked")
040: @Override
041: protected void setUp() throws Exception {
042: super .setUp();
043: policy = new SortingFocusTraversalPolicy(new TestComparator());
044: button1 = createButton("1");
045: button2 = createButton("2");
046: button3 = createButton("3");
047: button4 = createButton("4");
048: frame = new JFrame();
049: }
050:
051: @Override
052: protected void tearDown() throws Exception {
053: policy = null;
054: button1 = null;
055: button2 = null;
056: button3 = null;
057: button4 = null;
058: frame.dispose();
059: frame = null;
060: super .tearDown();
061: }
062:
063: public void testGetComponentAfterInNonDisplayableHierarchy()
064: throws Exception {
065: JFrame f = new JFrame();
066: f.setVisible(true);
067: SwingWaitTestCase.isRealized(f);
068: JPanel cycleRoot = createPanelWithButtons();
069: f.getContentPane().add(cycleRoot);
070: cycleRoot.setFocusCycleRoot(true);
071: cycleRoot.setFocusable(false);
072: assertEquals(button2, policy.getComponentAfter(cycleRoot,
073: button1));
074: assertEquals(button3, policy.getComponentAfter(cycleRoot,
075: button2));
076: assertEquals(button4, policy.getComponentAfter(cycleRoot,
077: button3));
078: assertEquals(button1, policy.getComponentAfter(cycleRoot,
079: button4));
080: f.setVisible(false);
081: assertNull(policy.getComponentAfter(cycleRoot, button1));
082: f.setVisible(true);
083: assertEquals(button2, policy.getComponentAfter(cycleRoot,
084: button1));
085: f.getContentPane().remove(cycleRoot);
086: assertNull(policy.getComponentAfter(cycleRoot, button1));
087: }
088:
089: public void testGetComponentBeforeAfterNotAcceptable()
090: throws Exception {
091: JFrame f = new JFrame();
092: f.setVisible(true);
093: SwingWaitTestCase.isRealized(f);
094: JPanel cycleRoot = createPanelWithButtons();
095: f.getContentPane().add(cycleRoot);
096: cycleRoot.setFocusCycleRoot(true);
097: cycleRoot.setFocusable(false);
098: button2.setEnabled(false);
099: f.setVisible(true);
100: assertEquals(button3, policy.getComponentAfter(cycleRoot,
101: button1));
102: assertEquals(button3, policy.getComponentAfter(cycleRoot,
103: button2));
104: assertEquals(button4, policy.getComponentAfter(cycleRoot,
105: button3));
106: assertEquals(button1, policy.getComponentAfter(cycleRoot,
107: button4));
108: assertEquals(button4, policy.getComponentBefore(cycleRoot,
109: button1));
110: assertEquals(button1, policy.getComponentBefore(cycleRoot,
111: button2));
112: assertEquals(button1, policy.getComponentBefore(cycleRoot,
113: button3));
114: assertEquals(button3, policy.getComponentBefore(cycleRoot,
115: button4));
116: }
117:
118: private JPanel createPanelWithButtons() {
119: JPanel result = new JPanel();
120: result.setFocusTraversalPolicy(policy);
121: result.add(button3);
122: result.add(button4);
123: result.add(button1);
124: result.add(button2);
125: return result;
126: }
127:
128: private JButton createButton(final String name) {
129: JButton result = new JButton(name);
130: result.setName(name);
131: return result;
132: }
133:
134: @SuppressWarnings("unchecked")
135: private class TestComparator implements Comparator {
136: public int compare(final Object o1, final Object o2) {
137: Component c1 = (Component) o1;
138: Component c2 = (Component) o2;
139: if (c1.getName() == null) {
140: return -1;
141: } else if (c2.getName() == null) {
142: return 1;
143: } else {
144: return c1.getName().compareTo(c2.getName());
145: }
146: }
147: }
148: }
|