1 /*
   2  * Copyright (c) 2015, 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 /* @test
  25  * @bug 8073453
  26  * @summary Focus doesn't move when pressing Shift + Tab keys
  27  * @author Dmitry Markov
  28  * @compile AWTFocusTransitionTest.java
  29  * @run main/othervm AWTFocusTransitionTest
  30  */
  31 
  32 import java.awt.*;
  33 import java.awt.event.KeyEvent;
  34 
  35 public class AWTFocusTransitionTest {
  36     private static Robot robot;
  37 
  38     private static Frame frame;
  39     private static TextField textField;
  40     private static Button button;
  41 
  42     public static void main(String[] args) throws Exception {
  43         robot = new Robot();
  44         robot.setAutoDelay(50);
  45 
  46         try {
  47             createAndShowGUI();
  48 
  49             robot.waitForIdle();
  50 
  51             checkFocusOwner(textField);
  52 
  53             robot.keyPress(KeyEvent.VK_TAB);
  54             robot.keyRelease(KeyEvent.VK_TAB);
  55             robot.waitForIdle();
  56 
  57             checkFocusOwner(button);
  58 
  59             robot.keyPress(KeyEvent.VK_SHIFT);
  60             robot.keyPress(KeyEvent.VK_TAB);
  61             robot.keyRelease(KeyEvent.VK_TAB);
  62             robot.keyRelease(KeyEvent.VK_SHIFT);
  63             robot.waitForIdle();
  64 
  65             checkFocusOwner(textField);
  66 
  67             robot.keyPress(KeyEvent.VK_SHIFT);
  68             robot.keyPress(KeyEvent.VK_TAB);
  69             robot.keyRelease(KeyEvent.VK_TAB);
  70             robot.keyRelease(KeyEvent.VK_SHIFT);
  71             robot.waitForIdle();
  72 
  73             checkFocusOwner(button);
  74         } finally {
  75             if (frame != null) {
  76                 frame.dispose();
  77             }
  78         }
  79         System.out.println("Test Passed!");
  80     }
  81 
  82     private static void createAndShowGUI() {
  83         frame = new Frame("AWTFocusTransitionTest");
  84         frame.setSize(300, 300);
  85         frame.setFocusTraversalPolicyProvider(true);
  86         frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
  87 
  88         textField = new TextField();
  89         button = new Button();
  90 
  91         Panel panel = new Panel();
  92         panel.setFocusTraversalPolicyProvider(true);
  93         panel.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
  94 
  95         Panel p = new Panel();
  96         p.setLayout(new GridLayout(3, 1));
  97         p.add(textField);
  98         p.add(button);
  99         p.add(panel);
 100 
 101         frame.add(p);
 102         frame.setVisible(true);
 103     }
 104 
 105     private static void checkFocusOwner(Component component) {
 106         if (component != frame.getFocusOwner()) {
 107             throw new RuntimeException("Test Failed! Incorrect focus owner: " + frame.getFocusOwner() +
 108                     ", but expected: " + component);
 109         }
 110     }
 111 }
 112