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