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 import sun.awt.SunToolkit;
  32 
  33 import java.awt.*;
  34 import java.awt.event.KeyEvent;
  35 
  36 public class AWTFocusTransitionTest {
  37     private static SunToolkit toolkit;
  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         toolkit = (SunToolkit)Toolkit.getDefaultToolkit();
  46         robot = new Robot();
  47         robot.setAutoDelay(50);
  48 
  49         try {
  50             createAndShowGUI();
  51 
  52             toolkit.realSync();
  53 
  54             checkFocusOwner(textField);
  55 
  56             robot.keyPress(KeyEvent.VK_TAB);
  57             robot.keyRelease(KeyEvent.VK_TAB);
  58             toolkit.realSync();
  59 
  60             checkFocusOwner(button);
  61 
  62             robot.keyPress(KeyEvent.VK_SHIFT);
  63             robot.keyPress(KeyEvent.VK_TAB);
  64             robot.keyRelease(KeyEvent.VK_TAB);
  65             robot.keyRelease(KeyEvent.VK_SHIFT);
  66             toolkit.realSync();
  67 
  68             checkFocusOwner(textField);
  69 
  70             robot.keyPress(KeyEvent.VK_SHIFT);
  71             robot.keyPress(KeyEvent.VK_TAB);
  72             robot.keyRelease(KeyEvent.VK_TAB);
  73             robot.keyRelease(KeyEvent.VK_SHIFT);
  74             toolkit.realSync();
  75 
  76             checkFocusOwner(button);
  77         } finally {
  78             if (frame != null) {
  79                 frame.dispose();
  80             }
  81         }
  82         System.out.println("Test Passed!");
  83     }
  84 
  85     private static void createAndShowGUI() {
  86         frame = new Frame("AWTFocusTransitionTest");
  87         frame.setSize(300, 300);
  88         frame.setFocusTraversalPolicyProvider(true);
  89         frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
  90 
  91         textField = new TextField();
  92         button = new Button();
  93 
  94         Panel panel = new Panel();
  95         panel.setFocusTraversalPolicyProvider(true);
  96         panel.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
  97 
  98         Panel p = new Panel();
  99         p.setLayout(new GridLayout(3, 1));
 100         p.add(textField);
 101         p.add(button);
 102         p.add(panel);
 103 
 104         frame.add(p);
 105         frame.setVisible(true);
 106     }
 107 
 108     private static void checkFocusOwner(Component component) {
 109         if (component != frame.getFocusOwner()) {
 110             throw new RuntimeException("Test Failed! Incorrect focus owner: " + frame.getFocusOwner() +
 111                     ", but expected: " + component);
 112         }
 113     }
 114 }
 115