1 /*
   2  * Copyright (c) 2007, 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   @bug       6463545
  27   @summary   Tests javax.swing.LayoutFocusTraversalPolicy functionality.
  28   @author    anton.tarasov, oleg.sukhodolsky: area=awt.focus
  29   @library   ../../regtesthelpers
  30   @build     AbstractPolicyTest
  31   @run       main LayoutFTPTest
  32 */
  33 
  34 import java.awt.*;
  35 import javax.swing.*;
  36 import java.awt.event.*;
  37 import java.util.*;
  38 import test.java.awt.regtesthelpers.AbstractPolicyTest;
  39 
  40 /*
  41 
  42 Below are some notes about changes in SortingFocusTraversalPolicy behaviour.
  43 
  44 container(root) [...]           - focus traversal cycle with the <container> as the root.
  45 container(provider) [...]       - focus traversal cycle with the <container> as the provider.
  46 container(..)(focusable) [...]  - <container> is implicitly set focusable.
  47 comp[unfocusable]               - <comp> is set unfocusable.
  48 
  49 
  50 1. 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 
  57 3. 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 
  71 4. 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 
  82 5. 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 
  89 6. 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 
  97 7. 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 
 107 public class LayoutFTPTest {
 108     final int TESTS_NUMBER = 11;
 109 
 110     public static void main(String[] args) {
 111         LayoutFTPTest app = new LayoutFTPTest();
 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  */
 141 class PolicyTest1 extends AbstractPolicyTest {
 142     protected Frame createFrame() {
 143         JFrame jframe = (JFrame) registerComponent("jframe", new JFrame("Test Frame"));
 144         jframe.setLayout(new GridLayout(3, 1));
 145 
 146         for (int i = 0; i < 3; i++) {
 147             Container cont = (Container) registerComponent("jpanel" + i, new JPanel());
 148             for (int j = 0; j < 3; j++) {
 149                 cont.add(registerComponent("btn " + (j + i*100), new JButton("jbutton")));
 150             }
 151             jframe.add(cont);
 152         }
 153         return jframe;
 154     }
 155 
 156     protected void customizeHierarchy() {
 157         ((Container)getComponent("jframe")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
 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("jpanel0", "btn 0");
 172         order.put("jpanel1", "btn 100");
 173         order.put("jpanel2", "btn 200");
 174         order.put("jframe", "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("jpanel0", "btn 202");
 190         order.put("jpanel1", "btn 2");
 191         order.put("jpanel2", "btn 102");
 192         order.put("jframe", "btn 202");
 193         return order;
 194     }
 195 
 196     protected String[] getContainersToTest() {
 197         return new String[] {"jframe"};
 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  */
 217 class PolicyTest2 extends AbstractPolicyTest {
 218 
 219     protected Frame createFrame() {
 220         JFrame jframe = (JFrame) registerComponent("jframe", new JFrame("Test Frame"));
 221         jframe.setLayout(new FlowLayout());
 222 
 223         jframe.add(registerComponent("btn 1", new JButton("jbutton")));
 224 
 225         Container cont = (Container)registerComponent("jpanel", new JPanel());
 226         cont.add(registerComponent("btn 2", new JButton("jbutton")));
 227         cont.add(registerComponent("btn 3", new JButton("jbutton")));
 228         jframe.add(cont);
 229 
 230         jframe.add(registerComponent("btn 4", new JButton("jbutton")));
 231 
 232         return jframe;
 233     }
 234 
 235     protected void customizeHierarchy() {
 236         ((Container)getComponent("jframe")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
 237         ((Container)getComponent("jpanel")).setFocusTraversalPolicyProvider(true);
 238     }
 239 
 240     protected Map<String, String> getForwardOrder() {
 241         Map<String, String> order = new HashMap<String, String>();
 242         order.put("jframe", "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("jpanel", "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[] {"jframe", "jpanel"};
 262     }
 263 
 264     protected String getDefaultComp(String focusCycleRoot_id) {
 265         if ("jframe".equals(focusCycleRoot_id)) {
 266             return "btn 1";
 267         } else if ("jpanel".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 ("jframe".equals(focusCycleRoot_id)) {
 279             return "btn 4";
 280         } else if ("jpanel".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  */
 291 class PolicyTest3 extends AbstractPolicyTest {
 292 
 293     protected Frame createFrame() {
 294         JFrame jframe = (JFrame) registerComponent("jframe", new JFrame("Test Frame"));
 295         jframe.setLayout(new FlowLayout());
 296 
 297         jframe.add(registerComponent("btn 1", new JButton("jbutton")));
 298 
 299         Container cont = (Container)registerComponent("jpanel", new JPanel());
 300         cont.add(registerComponent("btn 2", new JButton("jbutton")));
 301         cont.add(registerComponent("btn 3", new JButton("jbutton")));
 302         jframe.add(cont);
 303 
 304         jframe.add(registerComponent("btn 4", new JButton("jbutton")));
 305 
 306         return jframe;
 307     }
 308 
 309     protected void customizeHierarchy() {
 310         ((Container)getComponent("jframe")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
 311         ((Container)getComponent("jpanel")).setFocusCycleRoot(true);
 312     }
 313 
 314     protected Map<String, String> getForwardOrder() {
 315         Map<String, String> order = new HashMap<String, String>();
 316         order.put("jframe", "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("jpanel", "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[] {"jframe", "jpanel"};
 336     }
 337 
 338     protected String getDefaultComp(String focusCycleRoot_id) {
 339         if ("jframe".equals(focusCycleRoot_id)) {
 340             return "btn 1";
 341         } else if ("jpanel".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 ("jframe".equals(focusCycleRoot_id)) {
 353             return "btn 4";
 354         } else if ("jpanel".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  */
 365 class PolicyTest4 extends AbstractPolicyTest {
 366 
 367     protected Frame createFrame() {
 368         JFrame jframe = (JFrame) registerComponent("jframe", new JFrame("Test Frame"));
 369         jframe.setLayout(new FlowLayout());
 370 
 371         Container cont = (Container)registerComponent("jpanel", new JPanel());
 372         cont.add(registerComponent("btn 1", new JButton("jbutton")));
 373         cont.add(registerComponent("btn 2", new JButton("jbutton")));
 374         jframe.add(cont);
 375 
 376         jframe.add(registerComponent("btn 3", new JButton("jbutton")));
 377         jframe.add(registerComponent("btn 4", new JButton("jbutton")));
 378 
 379         return jframe;
 380     }
 381 
 382     protected void customizeHierarchy() {
 383         ((Container)getComponent("jframe")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
 384         ((Container)getComponent("jpanel")).setFocusTraversalPolicyProvider(true);
 385         ((JButton)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  */
 418 class PolicyTest5 extends AbstractPolicyTest {
 419 
 420     protected Frame createFrame() {
 421         JFrame jframe = (JFrame) registerComponent("jframe", new JFrame("Test Frame"));
 422         jframe.setLayout(new FlowLayout());
 423 
 424         Container cont = (Container)registerComponent("jpanel", new JPanel());
 425         cont.add(registerComponent("btn 1", new JButton("jbutton")));
 426         cont.add(registerComponent("btn 2", new JButton("jbutton")));
 427         jframe.add(cont);
 428 
 429         jframe.add(registerComponent("btn 3", new JButton("jbutton")));
 430         jframe.add(registerComponent("btn 4", new JButton("jbutton")));
 431 
 432         return jframe;
 433     }
 434 
 435     protected void customizeHierarchy() {
 436         ((Container)getComponent("jframe")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
 437         ((Container)getComponent("jpanel")).setFocusCycleRoot(true);
 438         ((JButton)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  */
 471 class PolicyTest6 extends AbstractPolicyTest {
 472 
 473     protected Frame createFrame() {
 474         JFrame jframe = (JFrame) registerComponent("jframe", new JFrame("Test Frame"));
 475         jframe.setLayout(new FlowLayout());
 476 
 477         jframe.add(registerComponent("btn 1", new JButton("jbutton")));
 478 
 479         Container cont = (Container)registerComponent("jpanel", new JPanel());
 480         cont.add(registerComponent("btn 2", new JButton("jbutton")));
 481         cont.add(registerComponent("btn 3", new JButton("jbutton")));
 482         jframe.add(cont);
 483 
 484         jframe.add(registerComponent("btn 4", new JButton("jbutton")));
 485 
 486         return jframe;
 487     }
 488 
 489     protected void customizeHierarchy() {
 490         ((Container)getComponent("jframe")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
 491         ((Container)getComponent("jpanel")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
 492                 public Component getDefaultComponent(Container aContainer) {
 493                     return getComponent("btn 2");
 494                 }
 495             });
 496         ((Container)getComponent("jpanel")).setFocusTraversalPolicyProvider(true);
 497         ((Container)getComponent("jpanel")).setFocusable(true);
 498     }
 499 
 500     protected Map<String, String> getForwardOrder() {
 501         Map<String, String> order = new HashMap<String, String>();
 502         order.put("jframe", "btn 1");
 503         order.put("btn 1", "jpanel");
 504         order.put("btn 2", "btn 3");
 505         order.put("btn 3", "btn 4");
 506         order.put("btn 4", "btn 1");
 507         order.put("jpanel", "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", "jpanel");
 516         order.put("btn 1", "btn 4");
 517         order.put("jpanel", "btn 1");
 518         return order;
 519     }
 520 
 521     protected String[] getContainersToTest() {
 522         return new String[] {"jpanel"};
 523     }
 524 
 525     protected String getDefaultComp(String focusCycleRoot_id) {
 526         return "btn 2";
 527     }
 528 
 529     protected String getFirstComp(String focusCycleRoot_id) {
 530         return "jpanel";
 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  */
 542 class PolicyTest7 extends AbstractPolicyTest {
 543 
 544     protected Frame createFrame() {
 545         JFrame jframe = (JFrame) registerComponent("jframe", new JFrame("Test Frame"));
 546         jframe.setLayout(new FlowLayout());
 547 
 548         jframe.add(registerComponent("btn 1", new JButton("jbutton")));
 549 
 550         Container cont = (Container)registerComponent("jpanel", new JPanel());
 551         cont.add(registerComponent("btn 2", new JButton("jbutton")));
 552         cont.add(registerComponent("btn 3", new JButton("jbutton")));
 553         jframe.add(cont);
 554 
 555         jframe.add(registerComponent("btn 4", new JButton("jbutton")));
 556 
 557         return jframe;
 558     }
 559 
 560     protected void customizeHierarchy() {
 561         ((Container)getComponent("jframe")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
 562         ((Container)getComponent("jpanel")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
 563                 public Component getDefaultComponent(Container aContainer) {
 564                     return getComponent("btn 2");
 565                 }
 566             });
 567         ((Container)getComponent("jpanel")).setFocusCycleRoot(true);
 568         ((Container)getComponent("jpanel")).setFocusable(true);
 569     }
 570 
 571     protected Map<String, String> getForwardOrder() {
 572         Map<String, String> order = new HashMap<String, String>();
 573         order.put("jframe", "btn 1");
 574         order.put("btn 1", "jpanel");
 575         order.put("btn 2", "btn 3");
 576         order.put("btn 3", "jpanel");
 577         order.put("btn 4", "btn 1");
 578         order.put("jpanel", "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", "jpanel");
 587         order.put("btn 1", "btn 4");
 588         order.put("jpanel", "btn 1");
 589         return order;
 590     }
 591 
 592     protected String[] getContainersToTest() {
 593         return new String[] {"jpanel"};
 594     }
 595 
 596     protected String getDefaultComp(String focusCycleRoot_id) {
 597         return "btn 2";
 598     }
 599 
 600     protected String getFirstComp(String focusCycleRoot_id) {
 601         return "jpanel";
 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  */
 613 class PolicyTest8 extends AbstractPolicyTest {
 614 
 615     protected Frame createFrame() {
 616         JFrame jframe = (JFrame) registerComponent("frame", new JFrame("Test Frame"));
 617         jframe.setLayout(new FlowLayout());
 618 
 619         jframe.add(registerComponent("btn-1", new JButton("jbutton")));
 620         jframe.add(registerComponent("btn-2", new JButton("jbutton")));
 621 
 622         Container cont1 = (Container)registerComponent("panel-1", new JPanel());
 623         cont1.add(registerComponent("btn-3", new JButton("jbutton")));
 624         cont1.add(registerComponent("btn-4", new JButton("jbutton")));
 625         cont1.add(registerComponent("btn-5", new JButton("jbutton")));
 626 
 627         Container cont2 = (Container)registerComponent("panel-2", new JPanel());
 628         cont2.add(registerComponent("btn-6", new JButton("jbutton")));
 629         cont2.add(registerComponent("btn-7", new JButton("jbutton")));
 630         cont2.add(registerComponent("btn-8", new JButton("jbutton")));
 631 
 632         jframe.add(cont1);
 633         jframe.add(cont2);
 634 
 635         return jframe;
 636     }
 637 
 638     protected void customizeHierarchy() {
 639         ((Container)getComponent("panel-1")).setFocusTraversalPolicyProvider(true);
 640         ((Container)getComponent("panel-1")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
 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 LayoutFocusTraversalPolicy() {
 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  */
 725 class PolicyTest9 extends AbstractPolicyTest {
 726 
 727     protected Frame createFrame() {
 728         JFrame jframe = (JFrame) registerComponent("frame", new JFrame("Test Frame"));
 729         jframe.setLayout(new FlowLayout());
 730 
 731         jframe.add(registerComponent("btn-1", new JButton("jbutton")));
 732         jframe.add(registerComponent("btn-2", new JButton("jbutton")));
 733 
 734         Container cont1 = (Container)registerComponent("panel-1", new JPanel());
 735         cont1.add(registerComponent("btn-3", new JButton("jbutton")));
 736         cont1.add(registerComponent("btn-4", new JButton("jbutton")));
 737         cont1.add(registerComponent("btn-5", new JButton("jbutton")));
 738 
 739         Container cont2 = (Container)registerComponent("panel-2", new JPanel());
 740         cont2.add(registerComponent("btn-6", new JButton("jbutton")));
 741         cont2.add(registerComponent("btn-7", new JButton("jbutton")));
 742         cont2.add(registerComponent("btn-8", new JButton("jbutton")));
 743 
 744         jframe.add(cont1);
 745         jframe.add(cont2);
 746 
 747         return jframe;
 748     }
 749 
 750     protected void customizeHierarchy() {
 751         ((Container)getComponent("panel-1")).setFocusCycleRoot(true);
 752         ((Container)getComponent("panel-1")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
 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 LayoutFocusTraversalPolicy() {
 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  */
 837 class PolicyTest10 extends AbstractPolicyTest {
 838 
 839     protected Frame createFrame() {
 840         JFrame jframe = (JFrame) registerComponent("frame", new JFrame("Test Frame"));
 841         jframe.setLayout(new GridLayout(2, 1));
 842 
 843         Container cont0 = new JPanel();
 844         cont0.add(registerComponent("btn-1", new JButton("jbutton")));
 845         cont0.add(registerComponent("btn-2", new JButton("jbutton")));
 846 
 847         Container cont1 = (Container)registerComponent("panel-1", new JPanel());
 848         cont1.add(registerComponent("btn-3", new JButton("jbutton")));
 849         cont1.add(registerComponent("btn-4", new JButton("jbutton")));
 850 
 851         Container cont2 = (Container)registerComponent("panel-2", new JPanel());
 852         cont2.add(registerComponent("btn-5", new JButton("jbutton")));
 853         cont2.add(registerComponent("btn-6", new JButton("jbutton")));
 854 
 855         cont1.add(cont2);
 856         jframe.add(cont0);
 857         jframe.add(cont1);
 858 
 859         return jframe;
 860     }
 861 
 862     protected void customizeHierarchy() {
 863         ((Container)getComponent("panel-1")).setFocusCycleRoot(true);
 864         ((Container)getComponent("panel-1")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
 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 LayoutFocusTraversalPolicy());
 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  */
 938 class PolicyTest11 extends AbstractPolicyTest {
 939     protected Frame createFrame() {
 940         JFrame jframe = (JFrame) registerComponent("jframe", new JFrame("Test Frame"));
 941         jframe.setLayout(new FlowLayout());
 942 
 943         Container cont = (Container)registerComponent("jpanel", new JPanel());
 944         cont.add(registerComponent("btn-1", new JButton("jbutton")));
 945         cont.add(registerComponent("btn-2", new JButton("jbutton")));
 946 
 947         jframe.add(cont);
 948         jframe.add(registerComponent("btn-3", new JButton("jbutton")));
 949 
 950         return jframe;
 951     }
 952 
 953     protected void customizeHierarchy() {
 954         ((Container)getComponent("jframe")).setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
 955         ((Container)getComponent("jpanel")).setFocusCycleRoot(true);
 956     }
 957 
 958     protected Map<String, String> getForwardOrder() {
 959         Map<String, String> order = new HashMap<String, String>();
 960         order.put("jframe", "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("jframe", "btn-3");
 973         return order;
 974     }
 975 
 976     protected String[] getContainersToTest() {
 977         return new String[] {"jframe"};
 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 }