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.Container;
024: import java.awt.FocusTraversalPolicy;
025: import java.util.ArrayList;
026: import java.util.List;
027: import junit.framework.TestCase;
028:
029: public class DefaultFocusManagerTest extends TestCase {
030: private DefaultFocusManager focusManager;
031:
032: private List<JButton> components;
033:
034: public DefaultFocusManagerTest(final String name) {
035: super (name);
036: }
037:
038: @Override
039: protected void setUp() throws Exception {
040: components = new ArrayList<JButton>();
041: components.add(new JButton("1"));
042: components.add(new JButton("2"));
043: components.add(new JButton("3"));
044: components.add(new JButton("4"));
045: focusManager = new DefaultFocusManager();
046: focusManager
047: .setDefaultFocusTraversalPolicy(new TestFocusTraversalPolicy(
048: components));
049: }
050:
051: @Override
052: protected void tearDown() throws Exception {
053: components = null;
054: focusManager = null;
055: }
056:
057: public void testGetComponentBefore() throws Exception {
058: assertEquals(components.get(0), focusManager
059: .getComponentBefore(null, components.get(1)));
060: assertEquals(components.get(1), focusManager
061: .getComponentBefore(null, components.get(2)));
062: assertEquals(components.get(2), focusManager
063: .getComponentBefore(null, components.get(3)));
064: assertEquals(components.get(3), focusManager
065: .getComponentBefore(null, components.get(0)));
066: }
067:
068: public void testGetComponentAfter() throws Exception {
069: assertEquals(components.get(0), focusManager.getComponentAfter(
070: null, components.get(3)));
071: assertEquals(components.get(1), focusManager.getComponentAfter(
072: null, components.get(0)));
073: assertEquals(components.get(2), focusManager.getComponentAfter(
074: null, components.get(1)));
075: assertEquals(components.get(3), focusManager.getComponentAfter(
076: null, components.get(2)));
077: }
078:
079: public void testGetLastComponent() throws Exception {
080: assertEquals(components.get(3), focusManager
081: .getLastComponent(null));
082: }
083:
084: public void testGetFirstComponent() throws Exception {
085: assertEquals(components.get(0), focusManager
086: .getFirstComponent(null));
087: }
088:
089: //TODO: Is not clear how this method should work
090: public void testCompareTabOrder() throws Exception {
091: assertFalse(focusManager.compareTabOrder(components.get(1),
092: components.get(2)));
093: }
094:
095: private class TestFocusTraversalPolicy extends FocusTraversalPolicy {
096: private List<JButton> components;
097:
098: public TestFocusTraversalPolicy(final List<JButton> components) {
099: this .components = components;
100: }
101:
102: @Override
103: public Component getComponentAfter(
104: final Container focusCycleRoot,
105: final Component component) {
106: int index = components.indexOf(component);
107: if (index == -1) {
108: return null;
109: }
110: if (index == components.size() - 1) {
111: return components.get(0);
112: }
113: return components.get(index + 1);
114: }
115:
116: @Override
117: public Component getComponentBefore(
118: final Container focusCycleRoot,
119: final Component component) {
120: int index = components.indexOf(component);
121: if (index == -1) {
122: return null;
123: }
124: if (index == 0) {
125: return components.get(components.size() - 1);
126: }
127: return components.get(index - 1);
128: }
129:
130: @Override
131: public Component getDefaultComponent(
132: final Container focusCycleRoot) {
133: return getFirstComponent(focusCycleRoot);
134: }
135:
136: @Override
137: public Component getFirstComponent(
138: final Container focusCycleRoot) {
139: return components.get(0);
140: }
141:
142: @Override
143: public Component getLastComponent(final Container focusCycleRoot) {
144: return components.get(components.size() - 1);
145: }
146: }
147: }
|