DefaultFTPTest.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25  @test
26  @key headful
27  @bug       6463545
28  @summary   Tests java.awt.DefaultFocusTraversalPolicy functionality.
29  @author    anton.tarasov area=awt.focus
30  @library   ../../regtesthelpers
31  @build     AbstractPolicyTest
32  @run       main DefaultFTPTest
33*/
34
35import java.awt.*;
36import java.awt.event.*;
37import java.util.*;
38import test.java.awt.regtesthelpers.AbstractPolicyTest;
39
40/*
41
42Below are some notes about changes in DefaultFocusTraversalPolicy behaviour.
43
44container(root) [...]           - focus traversal cycle with the <container> as the root.
45container(provider) [...]       - focus traversal cycle with the <container> as the provider.
46container(..)(focusable) [...]  - <container> is implicitly set focusable.
47comp[unfocusable]               - <comp> is set unfocusable.
48
49
501. frame [ container(root)(focusable) [...] ]
51
52- getComponentAfter(<frame>, <container>) returns <container>.
53
54  If <container> is the default component to focus in its own cycle.  * NO CHANGE *
55
56
573. frame [ comp1 container(root)(focusable) [ comp2 ] comp3 ]
58
59- getComponentBefore(<frame>, <comp3>) returns <comp2>.                                 ** BEHAVIOUR CHANGE **
60
61  Previously <container> would be returned. This was a bug as it
62  wasn't according to the spec.
63
64- getComponentBefore(<container>, <comp2>) returns <container>.     * NO CHANGE *
65
66- getComponentBefore(<frame>, <container>) returns <comp1>.         * NO CHANGE *
67
68- getComponentBefore(<container>, <container>) returns <comp2>.     * NO CHANGE *
69
70
714. frame [ container(provider) [...] comp ]
72
73- getComponentAfter(<frame>, <container>) returns <container>'s default.                ** BEHAVIOUR CHANGE. SPEC ADDITION **
74
75  Previously <comp> would be returned. Not specified in the spec.
76
77- getComponentBefore(<frame>, <comp>) returns <container>'s last.                       ** SPEC CHANGE **
78
79  The spec says (incorrectly) that default should be returned.
80
81
825. frame [ container(provider)(focusable) [...] comp2 ]
83
84- getComponentBefore(<frame>, <comp2>) returns <container>'s last.                      ** BEHAVIOUR CHANGE. SPEC ADDITION **
85
86  Previously <container> would be returned. Not specified in the spec.
87
88
896. frame [ comp1 container(root) [...] comp2 ]
90
91- getComponentAfter(<frame>, <comp1>) returns <container>'s default.                    ** BEHAVIOUR CHANGE. SPEC ADDITION **
92
93  Previously <comp2> would be returned. It's just the fix for 6240842.
94  Not specified in the spec.
95
96
977. frame [ comp1 container(root) [...] comp2(unfocusable) comp3 ]
98
99- getComponentBefore(<frame>, <comp3>) returns <container>'s default.                   ** BEHAVIOUR CHANGE **
100
101  Previously <comp1> would be returned. This was a bug, because
102  in case if <comp2> is focusable getComponentBefore(<frame>, <comp2>) would
103  return <container>'s default.
104
105*/
106
107public class DefaultFTPTest {
108    final int TESTS_NUMBER = 11;
109
110    public static void main(String[] args) {
111        DefaultFTPTest app = new DefaultFTPTest();
112        app.start();
113    }
114
115    public void start() {
116        try {
117            Class clazz = null;
118            AbstractPolicyTest test = null;
119
120            for (int i = 1; i <= TESTS_NUMBER; i++) {
121                clazz = Class.forName("PolicyTest" + i);
122                if (clazz != null) {
123                    test = (AbstractPolicyTest)clazz.newInstance();
124                    System.out.print("Test " + i + " is in progress...");
125                    test.testIt();
126                    System.out.println(" passed.");
127                }
128            }
129        } catch (RuntimeException rte) {
130            throw rte;
131        } catch (Exception e) {
132            throw new RuntimeException("Error: unexpected exception cought!", e);
133        }
134    }
135}
136
137/*
138 * frame [ container1 [...] container2 [...] container3 [...] ]
139 * - verifies simple configuration.
140 */
141class PolicyTest1 extends AbstractPolicyTest {
142    protected Frame createFrame() {
143        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
144        frame.setLayout(new GridLayout(3, 1));
145
146        for (int i = 0; i < 3; i++) {
147            Container cont = (Container) registerComponent("panel" + i, new Panel());
148            for (int j = 0; j < 3; j++) {
149                cont.add(registerComponent("btn " + (j + i*100), new Button("button")));
150            }
151            frame.add(cont);
152        }
153        return frame;
154    }
155
156    protected void customizeHierarchy() {
157        ((Container)getComponent("frame")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
158    }
159
160    protected Map<String, String> getForwardOrder() {
161        Map<String, String> order = new HashMap<String, String>();
162        order.put("btn 0", "btn 1");
163        order.put("btn 1", "btn 2");
164        order.put("btn 2", "btn 100");
165        order.put("btn 100", "btn 101");
166        order.put("btn 101", "btn 102");
167        order.put("btn 102", "btn 200");
168        order.put("btn 200", "btn 201");
169        order.put("btn 201", "btn 202");
170        order.put("btn 202", "btn 0");
171        order.put("panel0", "btn 0");
172        order.put("panel1", "btn 100");
173        order.put("panel2", "btn 200");
174        order.put("frame", "btn 0");
175        return order;
176    }
177
178    protected Map<String, String> getBackwardOrder() {
179        Map<String, String> order = new HashMap<String, String>();
180        order.put("btn 0", "btn 202");
181        order.put("btn 1", "btn 0");
182        order.put("btn 2", "btn 1");
183        order.put("btn 100", "btn 2");
184        order.put("btn 101", "btn 100");
185        order.put("btn 102", "btn 101");
186        order.put("btn 200", "btn 102");
187        order.put("btn 201", "btn 200");
188        order.put("btn 202", "btn 201");
189        order.put("panel0", "btn 202");
190        order.put("panel1", "btn 2");
191        order.put("panel2", "btn 102");
192        order.put("frame", "btn 202");
193        return order;
194    }
195
196    protected String[] getContainersToTest() {
197        return new String[] {"frame"};
198    }
199
200    protected String getDefaultComp(String focusCycleRoot_id) {
201        return "btn 0";
202    }
203
204    protected String getFirstComp(String focusCycleRoot_id) {
205        return "btn 0";
206    }
207
208    protected String getLastComp(String focusCycleRoot_id) {
209        return "btn 202";
210    }
211}
212
213/*
214 * frame [ comp container(provider) [...] comp ]
215 * - transfering focus through a provider.
216 */
217class PolicyTest2 extends AbstractPolicyTest {
218
219    protected Frame createFrame() {
220        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
221        frame.setLayout(new FlowLayout());
222
223        frame.add(registerComponent("btn 1", new Button("button")));
224
225        Container cont = (Container)registerComponent("panel", new Panel());
226        cont.add(registerComponent("btn 2", new Button("button")));
227        cont.add(registerComponent("btn 3", new Button("button")));
228        frame.add(cont);
229
230        frame.add(registerComponent("btn 4", new Button("button")));
231
232        return frame;
233    }
234
235    protected void customizeHierarchy() {
236        ((Container)getComponent("frame")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
237        ((Container)getComponent("panel")).setFocusTraversalPolicyProvider(true);
238    }
239
240    protected Map<String, String> getForwardOrder() {
241        Map<String, String> order = new HashMap<String, String>();
242        order.put("frame", "btn 1");
243        order.put("btn 1", "btn 2");
244        order.put("btn 2", "btn 3");
245        order.put("btn 3", "btn 4");
246        order.put("btn 4", "btn 1");
247        order.put("panel", "btn 2");
248        return order;
249    }
250
251    protected Map<String, String> getBackwardOrder() {
252        Map<String, String> order = new HashMap<String, String>();
253        order.put("btn 4", "btn 3");
254        order.put("btn 3", "btn 2");
255        order.put("btn 2", "btn 1");
256        order.put("btn 1", "btn 4");
257        return order;
258    }
259
260    protected String[] getContainersToTest() {
261        return new String[] {"frame", "panel"};
262    }
263
264    protected String getDefaultComp(String focusCycleRoot_id) {
265        if ("frame".equals(focusCycleRoot_id)) {
266            return "btn 1";
267        } else if ("panel".equals(focusCycleRoot_id)) {
268            return "btn 2";
269        }
270        return null;
271    }
272
273    protected String getFirstComp(String focusCycleRoot_id) {
274        return getDefaultComp(focusCycleRoot_id);
275    }
276
277    protected String getLastComp(String focusCycleRoot_id) {
278        if ("frame".equals(focusCycleRoot_id)) {
279            return "btn 4";
280        } else if ("panel".equals(focusCycleRoot_id)) {
281            return "btn 3";
282        }
283        return null;
284    }
285}
286
287/*
288 * frame [ comp container(root) [...] comp ]
289 * - transfering focus through a root (includes the case reported in the CR 6240842).
290 */
291class PolicyTest3 extends AbstractPolicyTest {
292
293    protected Frame createFrame() {
294        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
295        frame.setLayout(new FlowLayout());
296
297        frame.add(registerComponent("btn 1", new Button("button")));
298
299        Container cont = (Container)registerComponent("panel", new Panel());
300        cont.add(registerComponent("btn 2", new Button("button")));
301        cont.add(registerComponent("btn 3", new Button("button")));
302        frame.add(cont);
303
304        frame.add(registerComponent("btn 4", new Button("button")));
305
306        return frame;
307    }
308
309    protected void customizeHierarchy() {
310        ((Container)getComponent("frame")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
311        ((Container)getComponent("panel")).setFocusCycleRoot(true);
312    }
313
314    protected Map<String, String> getForwardOrder() {
315        Map<String, String> order = new HashMap<String, String>();
316        order.put("frame", "btn 1");
317        order.put("btn 1", "btn 2");
318        order.put("btn 2", "btn 3");
319        order.put("btn 3", "btn 2");
320        order.put("btn 4", "btn 1");
321        order.put("panel", "btn 2");
322        return order;
323    }
324
325    protected Map<String, String> getBackwardOrder() {
326        Map<String, String> order = new HashMap<String, String>();
327        order.put("btn 4", "btn 2");
328        order.put("btn 3", "btn 2");
329        order.put("btn 2", "btn 3");
330        order.put("btn 1", "btn 4");
331        return order;
332    }
333
334    protected String[] getContainersToTest() {
335        return new String[] {"frame", "panel"};
336    }
337
338    protected String getDefaultComp(String focusCycleRoot_id) {
339        if ("frame".equals(focusCycleRoot_id)) {
340            return "btn 1";
341        } else if ("panel".equals(focusCycleRoot_id)) {
342            return "btn 2";
343        }
344        return null;
345    }
346
347    protected String getFirstComp(String focusCycleRoot_id) {
348        return getDefaultComp(focusCycleRoot_id);
349    }
350
351    protected String getLastComp(String focusCycleRoot_id) {
352        if ("frame".equals(focusCycleRoot_id)) {
353            return "btn 4";
354        } else if ("panel".equals(focusCycleRoot_id)) {
355            return "btn 3";
356        }
357        return null;
358    }
359}
360
361/*
362 * frame [ container(provider) [...] comp1(unfocusable) comp2 ]
363 * - getComponentBefore(<frame>, <comp2>) should return <container>'s last.
364 */
365class PolicyTest4 extends AbstractPolicyTest {
366
367    protected Frame createFrame() {
368        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
369        frame.setLayout(new FlowLayout());
370
371        Container cont = (Container)registerComponent("panel", new Panel());
372        cont.add(registerComponent("btn 1", new Button("button")));
373        cont.add(registerComponent("btn 2", new Button("button")));
374        frame.add(cont);
375
376        frame.add(registerComponent("btn 3", new Button("button")));
377        frame.add(registerComponent("btn 4", new Button("button")));
378
379        return frame;
380    }
381
382    protected void customizeHierarchy() {
383        ((Container)getComponent("frame")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
384        ((Container)getComponent("panel")).setFocusTraversalPolicyProvider(true);
385        ((Button)getComponent("btn 3")).setFocusable(false);
386    }
387
388    protected Map<String, String> getBackwardOrder() {
389        Map<String, String> order = new HashMap<String, String>();
390        order.put("btn 4", "btn 2");
391        order.put("btn 2", "btn 1");
392        order.put("btn 1", "btn 4");
393        return order;
394    }
395
396    // no testing
397    protected Map<String, String> getForwardOrder() {
398        return null;
399    }
400    protected String[] getContainersToTest() {
401        return null;
402    }
403    protected String getDefaultComp(String focusCycleRoot_id) {
404        return null;
405    }
406    protected String getFirstComp(String focusCycleRoot_id) {
407        return null;
408    }
409    protected String getLastComp(String focusCycleRoot_id) {
410        return null;
411    }
412}
413
414/*
415 * frame [ container(root) [...] comp1(unfocusable) comp2 ]
416 * - getComponentBefore(<frame>, <comp2>) should return <container>'s default.
417 */
418class PolicyTest5 extends AbstractPolicyTest {
419
420    protected Frame createFrame() {
421        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
422        frame.setLayout(new FlowLayout());
423
424        Container cont = (Container)registerComponent("panel", new Panel());
425        cont.add(registerComponent("btn 1", new Button("button")));
426        cont.add(registerComponent("btn 2", new Button("button")));
427        frame.add(cont);
428
429        frame.add(registerComponent("btn 3", new Button("button")));
430        frame.add(registerComponent("btn 4", new Button("button")));
431
432        return frame;
433    }
434
435    protected void customizeHierarchy() {
436        ((Container)getComponent("frame")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
437        ((Container)getComponent("panel")).setFocusCycleRoot(true);
438        ((Button)getComponent("btn 3")).setFocusable(false);
439    }
440
441    protected Map<String, String> getBackwardOrder() {
442        Map<String, String> order = new HashMap<String, String>();
443        order.put("btn 4", "btn 1");
444        order.put("btn 2", "btn 1");
445        order.put("btn 1", "btn 2");
446        return order;
447    }
448
449    // no testing
450    protected Map<String, String> getForwardOrder() {
451        return null;
452    }
453    protected String[] getContainersToTest() {
454        return null;
455    }
456    protected String getDefaultComp(String focusCycleRoot_id) {
457        return null;
458    }
459    protected String getFirstComp(String focusCycleRoot_id) {
460        return null;
461    }
462    protected String getLastComp(String focusCycleRoot_id) {
463        return null;
464    }
465}
466
467/*
468 * frame [ comp container(provider)(focusable) [...] comp ]
469 * - transfering focus through a focusable provider.
470 */
471class PolicyTest6 extends AbstractPolicyTest {
472
473    protected Frame createFrame() {
474        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
475        frame.setLayout(new FlowLayout());
476
477        frame.add(registerComponent("btn 1", new Button("button")));
478
479        Container cont = (Container)registerComponent("panel", new Panel());
480        cont.add(registerComponent("btn 2", new Button("button")));
481        cont.add(registerComponent("btn 3", new Button("button")));
482        frame.add(cont);
483
484        frame.add(registerComponent("btn 4", new Button("button")));
485
486        return frame;
487    }
488
489    protected void customizeHierarchy() {
490        ((Container)getComponent("frame")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
491        ((Container)getComponent("panel")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
492                public Component getDefaultComponent(Container aContainer) {
493                    return getComponent("btn 2");
494                }
495            });
496        ((Container)getComponent("panel")).setFocusTraversalPolicyProvider(true);
497        ((Container)getComponent("panel")).setFocusable(true);
498    }
499
500    protected Map<String, String> getForwardOrder() {
501        Map<String, String> order = new HashMap<String, String>();
502        order.put("frame", "btn 1");
503        order.put("btn 1", "panel");
504        order.put("btn 2", "btn 3");
505        order.put("btn 3", "btn 4");
506        order.put("btn 4", "btn 1");
507        order.put("panel", "btn 2");
508        return order;
509    }
510
511    protected Map<String, String> getBackwardOrder() {
512        Map<String, String> order = new HashMap<String, String>();
513        order.put("btn 4", "btn 3");
514        order.put("btn 3", "btn 2");
515        order.put("btn 2", "panel");
516        order.put("btn 1", "btn 4");
517        order.put("panel", "btn 1");
518        return order;
519    }
520
521    protected String[] getContainersToTest() {
522        return new String[] {"panel"};
523    }
524
525    protected String getDefaultComp(String focusCycleRoot_id) {
526        return "btn 2";
527    }
528
529    protected String getFirstComp(String focusCycleRoot_id) {
530        return "panel";
531    }
532
533    protected String getLastComp(String focusCycleRoot_id) {
534        return "btn 3";
535    }
536}
537
538/*
539 * frame [ comp container(root)(focusable) [...] comp ]
540 * - transfering focus through a focusable root.
541 */
542class PolicyTest7 extends AbstractPolicyTest {
543
544    protected Frame createFrame() {
545        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
546        frame.setLayout(new FlowLayout());
547
548        frame.add(registerComponent("btn 1", new Button("button")));
549
550        Container cont = (Container)registerComponent("panel", new Panel());
551        cont.add(registerComponent("btn 2", new Button("button")));
552        cont.add(registerComponent("btn 3", new Button("button")));
553        frame.add(cont);
554
555        frame.add(registerComponent("btn 4", new Button("button")));
556
557        return frame;
558    }
559
560    protected void customizeHierarchy() {
561        ((Container)getComponent("frame")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
562        ((Container)getComponent("panel")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
563                public Component getDefaultComponent(Container aContainer) {
564                    return getComponent("btn 2");
565                }
566            });
567        ((Container)getComponent("panel")).setFocusCycleRoot(true);
568        ((Container)getComponent("panel")).setFocusable(true);
569    }
570
571    protected Map<String, String> getForwardOrder() {
572        Map<String, String> order = new HashMap<String, String>();
573        order.put("frame", "btn 1");
574        order.put("btn 1", "panel");
575        order.put("btn 2", "btn 3");
576        order.put("btn 3", "panel");
577        order.put("btn 4", "btn 1");
578        order.put("panel", "btn 2");
579        return order;
580    }
581
582    protected Map<String, String> getBackwardOrder() {
583        Map<String, String> order = new HashMap<String, String>();
584        order.put("btn 4", "btn 2");
585        order.put("btn 3", "btn 2");
586        order.put("btn 2", "panel");
587        order.put("btn 1", "btn 4");
588        order.put("panel", "btn 1");
589        return order;
590    }
591
592    protected String[] getContainersToTest() {
593        return new String[] {"panel"};
594    }
595
596    protected String getDefaultComp(String focusCycleRoot_id) {
597        return "btn 2";
598    }
599
600    protected String getFirstComp(String focusCycleRoot_id) {
601        return "panel";
602    }
603
604    protected String getLastComp(String focusCycleRoot_id) {
605        return "btn 3";
606    }
607}
608
609/*
610 * frame [ comp1 comp2 container1(provider) [...] container2(root) [...] ]
611 * - verifies a case when a provider is followed by a root.
612 */
613class PolicyTest8 extends AbstractPolicyTest {
614
615    protected Frame createFrame() {
616        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
617        frame.setLayout(new FlowLayout());
618
619        frame.add(registerComponent("btn-1", new Button("button")));
620        frame.add(registerComponent("btn-2", new Button("button")));
621
622        Container cont1 = (Container)registerComponent("panel-1", new Panel());
623        cont1.add(registerComponent("btn-3", new Button("button")));
624        cont1.add(registerComponent("btn-4", new Button("button")));
625        cont1.add(registerComponent("btn-5", new Button("button")));
626
627        Container cont2 = (Container)registerComponent("panel-2", new Panel());
628        cont2.add(registerComponent("btn-6", new Button("button")));
629        cont2.add(registerComponent("btn-7", new Button("button")));
630        cont2.add(registerComponent("btn-8", new Button("button")));
631
632        frame.add(cont1);
633        frame.add(cont2);
634
635        return frame;
636    }
637
638    protected void customizeHierarchy() {
639        ((Container)getComponent("panel-1")).setFocusTraversalPolicyProvider(true);
640        ((Container)getComponent("panel-1")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
641                public Component getDefaultComponent(Container aContainer) {
642                    return getComponent("btn-4");
643                }
644            });
645
646        ((Container)getComponent("panel-2")).setFocusCycleRoot(true);
647        ((Container)getComponent("panel-2")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
648                public Component getDefaultComponent(Container aContainer) {
649                    return getComponent("btn-7");
650                }
651            });
652    }
653
654    protected Map<String, String> getForwardOrder() {
655        Map<String, String> order = new HashMap<String, String>();
656        order.put("frame", "btn-1");
657        order.put("btn-1", "btn-2");
658        order.put("btn-2", "btn-4");
659        order.put("btn-3", "btn-4");
660        order.put("btn-4", "btn-5");
661        order.put("btn-5", "btn-7");
662        order.put("btn-6", "btn-7");
663        order.put("btn-7", "btn-8");
664        order.put("btn-8", "btn-6");
665        order.put("panel-1", "btn-4");
666        order.put("panel-2", "btn-7");
667        return order;
668    }
669
670    protected Map<String, String> getBackwardOrder() {
671        Map<String, String> order = new HashMap<String, String>();
672        order.put("btn-1", "btn-5");
673        order.put("btn-2", "btn-1");
674        order.put("btn-3", "btn-2");
675        order.put("btn-4", "btn-3");
676        order.put("btn-5", "btn-4");
677        order.put("btn-6", "btn-8");
678        order.put("btn-7", "btn-6");
679        order.put("btn-8", "btn-7");
680        return order;
681    }
682
683    protected String[] getContainersToTest() {
684        return new String[] {"frame", "panel-1", "panel-2"};
685    }
686
687    protected String getDefaultComp(String focusCycleRoot_id) {
688        if ("frame".equals(focusCycleRoot_id)) {
689            return "btn-1";
690        } else if ("panel-1".equals(focusCycleRoot_id)) {
691            return "btn-4";
692        } else if ("panel-2".equals(focusCycleRoot_id)) {
693            return "btn-7";
694        }
695        return null;
696    }
697
698    protected String getFirstComp(String focusCycleRoot_id) {
699        if ("frame".equals(focusCycleRoot_id)) {
700            return "btn-1";
701        } else if ("panel-1".equals(focusCycleRoot_id)) {
702            return "btn-3";
703        } else if ("panel-2".equals(focusCycleRoot_id)) {
704            return "btn-6";
705        }
706        return null;
707    }
708
709    protected String getLastComp(String focusCycleRoot_id) {
710        if ("frame".equals(focusCycleRoot_id)) {
711            return "btn-5";
712        } else if ("panel-1".equals(focusCycleRoot_id)) {
713            return "btn-5";
714        } else if ("panel-2".equals(focusCycleRoot_id)) {
715            return "btn-8";
716        }
717        return null;
718    }
719}
720
721/*
722 * frame [ comp1 comp2 container1(root) [...] container2(provider) [...] ]
723 * - verifies a case when a root is followed by a provider.
724 */
725class PolicyTest9 extends AbstractPolicyTest {
726
727    protected Frame createFrame() {
728        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
729        frame.setLayout(new FlowLayout());
730
731        frame.add(registerComponent("btn-1", new Button("button")));
732        frame.add(registerComponent("btn-2", new Button("button")));
733
734        Container cont1 = (Container)registerComponent("panel-1", new Panel());
735        cont1.add(registerComponent("btn-3", new Button("button")));
736        cont1.add(registerComponent("btn-4", new Button("button")));
737        cont1.add(registerComponent("btn-5", new Button("button")));
738
739        Container cont2 = (Container)registerComponent("panel-2", new Panel());
740        cont2.add(registerComponent("btn-6", new Button("button")));
741        cont2.add(registerComponent("btn-7", new Button("button")));
742        cont2.add(registerComponent("btn-8", new Button("button")));
743
744        frame.add(cont1);
745        frame.add(cont2);
746
747        return frame;
748    }
749
750    protected void customizeHierarchy() {
751        ((Container)getComponent("panel-1")).setFocusCycleRoot(true);
752        ((Container)getComponent("panel-1")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
753                public Component getDefaultComponent(Container aContainer) {
754                    return getComponent("btn-4");
755                }
756            });
757
758        ((Container)getComponent("panel-2")).setFocusTraversalPolicyProvider(true);
759        ((Container)getComponent("panel-2")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
760                public Component getDefaultComponent(Container aContainer) {
761                    return getComponent("btn-7");
762                }
763            });
764    }
765
766    protected Map<String, String> getForwardOrder() {
767        Map<String, String> order = new HashMap<String, String>();
768        order.put("frame", "btn-1");
769        order.put("btn-1", "btn-2");
770        order.put("btn-2", "btn-4");
771        order.put("btn-3", "btn-4");
772        order.put("btn-4", "btn-5");
773        order.put("btn-5", "btn-3");
774        order.put("btn-6", "btn-7");
775        order.put("btn-7", "btn-8");
776        order.put("btn-8", "btn-1");
777        order.put("panel-1", "btn-4");
778        order.put("panel-2", "btn-7");
779        return order;
780    }
781
782    protected Map<String, String> getBackwardOrder() {
783        Map<String, String> order = new HashMap<String, String>();
784        order.put("btn-1", "btn-8");
785        order.put("btn-2", "btn-1");
786        order.put("btn-3", "btn-5");
787        order.put("btn-4", "btn-3");
788        order.put("btn-5", "btn-4");
789        order.put("btn-6", "btn-4");
790        order.put("btn-7", "btn-6");
791        order.put("btn-8", "btn-7");
792        return order;
793    }
794
795    protected String[] getContainersToTest() {
796        return new String[] {"frame", "panel-1", "panel-2"};
797    }
798
799    protected String getDefaultComp(String focusCycleRoot_id) {
800        if ("frame".equals(focusCycleRoot_id)) {
801            return "btn-1";
802        } else if ("panel-1".equals(focusCycleRoot_id)) {
803            return "btn-4";
804        } else if ("panel-2".equals(focusCycleRoot_id)) {
805            return "btn-7";
806        }
807        return null;
808    }
809
810    protected String getFirstComp(String focusCycleRoot_id) {
811        if ("frame".equals(focusCycleRoot_id)) {
812            return "btn-1";
813        } else if ("panel-1".equals(focusCycleRoot_id)) {
814            return "btn-3";
815        } else if ("panel-2".equals(focusCycleRoot_id)) {
816            return "btn-6";
817        }
818        return null;
819    }
820
821    protected String getLastComp(String focusCycleRoot_id) {
822        if ("frame".equals(focusCycleRoot_id)) {
823            return "btn-8";
824        } else if ("panel-1".equals(focusCycleRoot_id)) {
825            return "btn-5";
826        } else if ("panel-2".equals(focusCycleRoot_id)) {
827            return "btn-8";
828        }
829        return null;
830    }
831}
832
833/*
834 * frame [ container0 [...] container1(root) [ comp1 comp2 container2(provider) [...] ] ]
835 * - verifies a case when a provider is nested in a root.
836 */
837class PolicyTest10 extends AbstractPolicyTest {
838
839    protected Frame createFrame() {
840        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
841        frame.setLayout(new GridLayout(2, 1));
842
843        Container cont0 = new Panel();
844        cont0.add(registerComponent("btn-1", new Button("button")));
845        cont0.add(registerComponent("btn-2", new Button("button")));
846
847        Container cont1 = (Container)registerComponent("panel-1", new Panel());
848        cont1.add(registerComponent("btn-3", new Button("button")));
849        cont1.add(registerComponent("btn-4", new Button("button")));
850
851        Container cont2 = (Container)registerComponent("panel-2", new Panel());
852        cont2.add(registerComponent("btn-5", new Button("button")));
853        cont2.add(registerComponent("btn-6", new Button("button")));
854
855        cont1.add(cont2);
856        frame.add(cont0);
857        frame.add(cont1);
858
859        return frame;
860    }
861
862    protected void customizeHierarchy() {
863        ((Container)getComponent("panel-1")).setFocusCycleRoot(true);
864        ((Container)getComponent("panel-1")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
865                public Component getDefaultComponent(Container aContainer) {
866                    return getComponent("panel-2");
867                }
868            });
869        ((Container)getComponent("panel-2")).setFocusTraversalPolicyProvider(true);
870        ((Container)getComponent("panel-2")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
871    }
872
873    protected Map<String, String> getForwardOrder() {
874        Map<String, String> order = new HashMap<String, String>();
875        order.put("frame", "btn-1");
876        order.put("btn-1", "btn-2");
877        order.put("btn-2", "panel-2");
878        order.put("btn-3", "btn-4");
879        order.put("btn-4", "btn-5");
880        order.put("btn-5", "btn-6");
881        order.put("btn-6", "btn-3");
882        order.put("panel-1", "panel-2");
883        order.put("panel-2", "btn-5");
884        return order;
885    }
886
887    protected Map<String, String> getBackwardOrder() {
888        Map<String, String> order = new HashMap<String, String>();
889        order.put("btn-1", "btn-2");
890        order.put("btn-2", "btn-1");
891        order.put("btn-3", "btn-6");
892        order.put("btn-4", "btn-3");
893        order.put("btn-5", "btn-4");
894        order.put("btn-6", "btn-5");
895        return order;
896    }
897
898    protected String[] getContainersToTest() {
899        return new String[] {"frame", "panel-1", "panel-2"};
900    }
901
902    protected String getDefaultComp(String focusCycleRoot_id) {
903        if ("frame".equals(focusCycleRoot_id)) {
904            return "btn-1";
905        } else if ("panel-1".equals(focusCycleRoot_id)) {
906            return "panel-2";
907        } else if ("panel-2".equals(focusCycleRoot_id)) {
908            return "btn-5";
909        }
910        return null;
911    }
912
913    protected String getFirstComp(String focusCycleRoot_id) {
914        if ("frame".equals(focusCycleRoot_id)) {
915            return "btn-1";
916        } else if ("panel-1".equals(focusCycleRoot_id)) {
917            return "btn-3";
918        } else if ("panel-2".equals(focusCycleRoot_id)) {
919            return "btn-5";
920        }
921        return null;
922    }
923
924    protected String getLastComp(String focusCycleRoot_id) {
925        if ("frame".equals(focusCycleRoot_id)) {
926            return "btn-2";
927        } else {
928            return "btn-6";
929        }
930    }
931}
932
933/*
934 * frame [ container(root) [...] comp ]
935 * - getDefaultComponent(<frame>) should implicitly down-cycle into the <container>.
936 * - getFirstComponent(<frame>) should implicitly down-cycle into the <container>.
937 */
938class PolicyTest11 extends AbstractPolicyTest {
939    protected Frame createFrame() {
940        Frame frame = (Frame) registerComponent("frame", new Frame("Test Frame"));
941        frame.setLayout(new FlowLayout());
942
943        Container cont = (Container)registerComponent("panel", new Panel());
944        cont.add(registerComponent("btn-1", new Button("button")));
945        cont.add(registerComponent("btn-2", new Button("button")));
946
947        frame.add(cont);
948        frame.add(registerComponent("btn-3", new Button("button")));
949
950        return frame;
951    }
952
953    protected void customizeHierarchy() {
954        ((Container)getComponent("frame")).setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
955        ((Container)getComponent("panel")).setFocusCycleRoot(true);
956    }
957
958    protected Map<String, String> getForwardOrder() {
959        Map<String, String> order = new HashMap<String, String>();
960        order.put("frame", "btn-1");
961        order.put("btn-1", "btn-2");
962        order.put("btn-2", "btn-1");
963        order.put("btn-3", "btn-1");
964        return order;
965    }
966
967    protected Map<String, String> getBackwardOrder() {
968        Map<String, String> order = new HashMap<String, String>();
969        order.put("btn-3", "btn-1");
970        order.put("btn-2", "btn-1");
971        order.put("btn-1", "btn-2");
972        order.put("frame", "btn-3");
973        return order;
974    }
975
976    protected String[] getContainersToTest() {
977        return new String[] {"frame"};
978    }
979
980    protected String getDefaultComp(String focusCycleRoot_id) {
981        return "btn-1";
982    }
983
984    protected String getFirstComp(String focusCycleRoot_id) {
985        return "btn-1";
986    }
987
988    protected String getLastComp(String focusCycleRoot_id) {
989        return "btn-3";
990    }
991}
992