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  * @run main bug8073453
  29  */
  30 import sun.awt.SunToolkit;
  31 
  32 import java.awt.*;
  33 import java.awt.event.KeyEvent;
  34 
  35 public class bug8073453 {
  36     private static Frame frame;
  37 
  38     public static void main(String[] args) throws Exception {
  39         SunToolkit toolkit = (SunToolkit)Toolkit.getDefaultToolkit();
  40         Robot robot = new Robot();
  41         robot.setAutoDelay(50);
  42 
  43         frame = new Frame("bug8073453");
  44         frame.setSize(300, 300);
  45 
  46         TextField textField = new TextField();
  47         Button button = new Button();
  48 
  49         Panel panel = new Panel();
  50         panel.setFocusTraversalPolicyProvider(true);
  51         panel.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
  52 
  53         Panel p = new Panel();
  54         p.setLayout(new GridLayout(3, 1));
  55         p.add(textField);
  56         p.add(button);
  57         p.add(panel);
  58 
  59         frame.add(p);
  60         frame.setVisible(true);
  61 
  62         toolkit.realSync();
  63 
  64         checkFocusOwner(textField);
  65 
  66         robot.keyPress(KeyEvent.VK_TAB);
  67         robot.keyRelease(KeyEvent.VK_TAB);
  68         toolkit.realSync();
  69 
  70         checkFocusOwner(button);
  71 
  72         robot.keyPress(KeyEvent.VK_SHIFT);
  73         robot.keyPress(KeyEvent.VK_TAB);
  74         robot.keyRelease(KeyEvent.VK_TAB);
  75         robot.keyRelease(KeyEvent.VK_SHIFT);
  76         toolkit.realSync();
  77 
  78         checkFocusOwner(textField);
  79 
  80         robot.keyPress(KeyEvent.VK_SHIFT);
  81         robot.keyPress(KeyEvent.VK_TAB);
  82         robot.keyRelease(KeyEvent.VK_TAB);
  83         robot.keyRelease(KeyEvent.VK_SHIFT);
  84         toolkit.realSync();
  85 
  86         checkFocusOwner(button);
  87 
  88         frame.dispose();
  89 
  90         System.out.println("Test Passed!");
  91     }
  92 
  93     private static void checkFocusOwner(Component component) {
  94         if (component != frame.getFocusOwner()) {
  95             throw new RuntimeException("Test Failed! Incorrect focus owner: " + frame.getFocusOwner() +
  96                     ", but expected: " + component);
  97         }
  98     }
  99 
 100 }
 101