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: @SuppressWarnings("unchecked")
026: public class SortingFocusTraversalPolicyTest extends 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: public SortingFocusTraversalPolicyTest(final String name) {
040: super (name);
041: }
042:
043: @Override
044: protected void setUp() throws Exception {
045: super .setUp();
046: policy = new SortingFocusTraversalPolicy(new TestComparator());
047: button1 = createButton("1");
048: button2 = createButton("2");
049: button3 = createButton("3");
050: button4 = createButton("4");
051: frame = new JFrame();
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: super .tearDown();
064: }
065:
066: public void testSortingFocusTraversalPolicy() throws Exception {
067: Comparator cmp = new TestComparator();
068: policy = new SortingFocusTraversalPolicy(cmp);
069: assertEquals(cmp, policy.getComparator());
070: }
071:
072: public void testSetComparator() throws Exception {
073: Comparator cmp = new TestComparator();
074: policy.setComparator(cmp);
075: assertEquals(cmp, policy.getComparator());
076: }
077:
078: public void testAccept() throws Exception {
079: frame.setVisible(true);
080: SwingWaitTestCase.isRealized(frame);
081: Component invisible = new JPanel();
082: frame.getContentPane().add(invisible);
083: invisible.setVisible(false);
084: assertFalse(policy.accept(invisible));
085: Component undisplayable = new JPanel();
086: assertFalse(policy.accept(undisplayable));
087: Component disabled = new JPanel();
088: frame.getContentPane().add(disabled);
089: disabled.setEnabled(false);
090: assertFalse(policy.accept(disabled));
091: Component unfocusable = new JPanel();
092: frame.getContentPane().add(unfocusable);
093: unfocusable.setFocusable(false);
094: assertFalse(policy.accept(unfocusable));
095: Component acceptable = new JPanel();
096: frame.getContentPane().add(acceptable);
097: assertTrue(policy.accept(acceptable));
098: }
099:
100: public void testGetComponentBeforeNoInnerCycleRoots()
101: throws Exception {
102: JPanel cycleRoot = createPanelWithButtons();
103: frame.getContentPane().add(cycleRoot);
104: cycleRoot.setFocusCycleRoot(true);
105: cycleRoot.setFocusable(false);
106: frame.setVisible(true);
107: SwingWaitTestCase.isRealized(frame);
108: assertEquals(button4, policy.getComponentBefore(cycleRoot,
109: button1));
110: assertEquals(button1, policy.getComponentBefore(cycleRoot,
111: button2));
112: assertEquals(button2, policy.getComponentBefore(cycleRoot,
113: button3));
114: assertEquals(button3, policy.getComponentBefore(cycleRoot,
115: button4));
116: }
117:
118: public void testGetComponentBeforeForNotCycleRoot()
119: throws Exception {
120: testExceptionalCase(new IllegalArgumentCase() {
121: @Override
122: public void exceptionalAction() throws Exception {
123: policy.getComponentBefore(createNotCycleRootPanel(),
124: button1);
125: }
126: });
127: }
128:
129: public void testGetComponentBeforeForNullCycleRoot()
130: throws Exception {
131: testExceptionalCase(new IllegalArgumentCase() {
132: @Override
133: public void exceptionalAction() throws Exception {
134: policy.getComponentBefore(null, button1);
135: }
136: });
137: }
138:
139: public void testGetComponentBeforeForNullComponent()
140: throws Exception {
141: final JPanel cycleRoot = createPanelWithButtons();
142: frame.getContentPane().add(cycleRoot);
143: cycleRoot.setFocusCycleRoot(true);
144: cycleRoot.setFocusable(false);
145: frame.setVisible(true);
146: SwingWaitTestCase.isRealized(frame);
147: testExceptionalCase(new IllegalArgumentCase() {
148: @Override
149: public void exceptionalAction() throws Exception {
150: policy.getComponentBefore(cycleRoot, null);
151: }
152: });
153: }
154:
155: public void testGetComponentAfterNoInnerCycleRoots()
156: throws Exception {
157: JFrame f = new JFrame();
158: f.setVisible(true);
159: SwingWaitTestCase.isRealized(f);
160: JPanel cycleRoot = createPanelWithButtons();
161: f.getContentPane().add(cycleRoot);
162: cycleRoot.setFocusCycleRoot(true);
163: cycleRoot.setFocusable(false);
164: assertEquals(button2, policy.getComponentAfter(cycleRoot,
165: button1));
166: assertEquals(button3, policy.getComponentAfter(cycleRoot,
167: button2));
168: assertEquals(button4, policy.getComponentAfter(cycleRoot,
169: button3));
170: assertEquals(button1, policy.getComponentAfter(cycleRoot,
171: button4));
172: }
173:
174: public void testGetComponentAfterForNotCycleRoot() throws Exception {
175: testExceptionalCase(new IllegalArgumentCase() {
176: @Override
177: public void exceptionalAction() throws Exception {
178: policy.getComponentAfter(createNotCycleRootPanel(),
179: button1);
180: }
181: });
182: }
183:
184: public void testGetComponentAfterForNullCycleRoot()
185: throws Exception {
186: testExceptionalCase(new IllegalArgumentCase() {
187: @Override
188: public void exceptionalAction() throws Exception {
189: policy.getComponentAfter(null, button1);
190: }
191: });
192: }
193:
194: public void testGetComponentAfterForNullComponent()
195: throws Exception {
196: JFrame f = new JFrame();
197: f.setVisible(true);
198: final JPanel cycleRoot = createPanelWithButtons();
199: f.getContentPane().add(cycleRoot);
200: cycleRoot.setFocusCycleRoot(true);
201: cycleRoot.setFocusable(false);
202: testExceptionalCase(new IllegalArgumentCase() {
203: @Override
204: public void exceptionalAction() throws Exception {
205: policy.getComponentAfter(cycleRoot, null);
206: }
207: });
208: }
209:
210: public void testGetLastComponent() throws Exception {
211: JPanel cycleRoot = createPanelWithButtons();
212: frame.getContentPane().add(cycleRoot);
213: cycleRoot.setFocusCycleRoot(true);
214: cycleRoot.setFocusable(false);
215: frame.setVisible(true);
216: SwingWaitTestCase.isRealized(frame);
217: assertEquals(button4, policy.getLastComponent(cycleRoot));
218: }
219:
220: public void testGetLastComponentForNotCycleRoot() throws Exception {
221: testExceptionalCase(new IllegalArgumentCase() {
222: @Override
223: public void exceptionalAction() throws Exception {
224: policy.getLastComponent(null);
225: }
226: });
227: }
228:
229: public void testGetFirstComponent() throws Exception {
230: JPanel cycleRoot = createPanelWithButtons();
231: frame.getContentPane().add(cycleRoot);
232: cycleRoot.setFocusCycleRoot(true);
233: cycleRoot.setFocusable(false);
234: frame.setVisible(true);
235: SwingWaitTestCase.isRealized(frame);
236: assertEquals(button1, policy.getFirstComponent(cycleRoot));
237: }
238:
239: public void testGetDefaultComponent() throws Exception {
240: JPanel cycleRoot = createPanelWithButtons();
241: frame.getContentPane().add(cycleRoot);
242: cycleRoot.setFocusCycleRoot(true);
243: cycleRoot.setFocusable(false);
244: frame.setVisible(true);
245: SwingWaitTestCase.isRealized(frame);
246: assertEquals(policy.getFirstComponent(cycleRoot), policy
247: .getDefaultComponent(cycleRoot));
248: }
249:
250: public void testComponentIsInACycleRoot() throws Exception {
251: final JPanel cycleRoot = createPanelWithButtons();
252: JPanel innerCycleRoot = new JPanel();
253: innerCycleRoot.setName("9 - the latest");
254: innerCycleRoot.setFocusCycleRoot(true);
255: final JButton innerButton1 = createButton("1");
256: innerCycleRoot.add(innerButton1);
257: cycleRoot.add(innerCycleRoot);
258: frame.getContentPane().add(cycleRoot);
259: cycleRoot.setFocusCycleRoot(true);
260: cycleRoot.setFocusable(false);
261: frame.setVisible(true);
262: SwingWaitTestCase.isRealized(frame);
263: testExceptionalCase(new IllegalArgumentCase() {
264: @Override
265: public void exceptionalAction() throws Exception {
266: policy.getComponentAfter(cycleRoot, innerButton1);
267: }
268: });
269: }
270:
271: public void testInnerCycleRootsProcessingWithImplicitTraversal()
272: throws Exception {
273: JPanel cycleRoot = createPanelWithButtons();
274: JPanel innerCycleRoot = new JPanel();
275: innerCycleRoot.setName("9 - the latest");
276: innerCycleRoot.setFocusCycleRoot(true);
277: innerCycleRoot.setFocusTraversalPolicy(policy);
278: JButton innerButton1 = createButton("1");
279: JButton innerButton2 = createButton("2");
280: JButton innerButton3 = createButton("3");
281: innerCycleRoot.add(innerButton2);
282: innerCycleRoot.add(innerButton3);
283: innerCycleRoot.add(innerButton1);
284: cycleRoot.add(innerCycleRoot);
285: frame.getContentPane().add(cycleRoot);
286: cycleRoot.setFocusCycleRoot(true);
287: cycleRoot.setFocusable(false);
288: frame.setVisible(true);
289: SwingWaitTestCase.isRealized(frame);
290: assertEquals(innerCycleRoot, policy.getComponentAfter(
291: cycleRoot, button4));
292: assertEquals(innerButton1, policy.getComponentAfter(cycleRoot,
293: innerCycleRoot));
294: assertEquals(innerButton1, policy.getComponentAfter(
295: innerCycleRoot, innerCycleRoot));
296: assertEquals(innerButton2, policy.getComponentAfter(
297: innerCycleRoot, innerButton1));
298: assertEquals(innerButton3, policy.getComponentAfter(
299: innerCycleRoot, innerButton2));
300: assertEquals(innerCycleRoot, policy.getComponentAfter(
301: innerCycleRoot, innerButton3));
302: assertEquals(innerButton1, policy.getComponentAfter(
303: innerCycleRoot, innerCycleRoot));
304: assertEquals(innerCycleRoot, policy.getComponentBefore(
305: innerCycleRoot, innerButton1));
306: assertEquals(innerButton3, policy.getComponentBefore(
307: innerCycleRoot, innerCycleRoot));
308: assertEquals(button4, policy.getComponentBefore(cycleRoot,
309: innerCycleRoot));
310: policy.setImplicitDownCycleTraversal(false);
311: assertEquals(innerCycleRoot, policy.getComponentAfter(
312: cycleRoot, button4));
313: assertEquals(button1, policy.getComponentAfter(cycleRoot,
314: innerCycleRoot));
315: assertEquals(innerCycleRoot, policy.getComponentBefore(
316: cycleRoot, button1));
317: assertEquals(button4, policy.getComponentBefore(cycleRoot,
318: innerCycleRoot));
319: }
320:
321: private JPanel createPanelWithButtons() {
322: JPanel result = new JPanel();
323: result.setFocusTraversalPolicy(policy);
324: result.add(button3);
325: result.add(button4);
326: result.add(button1);
327: result.add(button2);
328: return result;
329: }
330:
331: private JPanel createNotCycleRootPanel() {
332: JPanel result = createPanelWithButtons();
333: frame.getContentPane().add(result);
334: result.setFocusCycleRoot(false);
335: result.setFocusable(false);
336: frame.setVisible(true);
337: SwingWaitTestCase.isRealized(frame);
338: return result;
339: }
340:
341: private JButton createButton(final String name) {
342: JButton result = new JButton(name);
343: result.setName(name);
344: return result;
345: }
346:
347: private class TestComparator implements Comparator {
348: public int compare(final Object o1, final Object o2) {
349: Component c1 = (Component) o1;
350: Component c2 = (Component) o2;
351: if (c1.getName() == null) {
352: return -1;
353: } else if (c2.getName() == null) {
354: return 1;
355: } else {
356: return c1.getName().compareTo(c2.getName());
357: }
358: }
359: }
360: }
|