1 /*
   2  * Copyright (c) 2012, 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 8001633
  26    @summary Wrong alt processing during switching between windows
  27    @author mikhail.cherkasov@oracle.com
  28    @run main WrongAltProcessing
  29 */
  30 
  31 import sun.awt.SunToolkit;
  32 
  33 import javax.swing.*;
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 
  37 
  38 public class WrongAltProcessing {
  39 
  40     private static Robot robot;
  41     private static JFrame firstFrame;
  42     private static JFrame secondFrame;
  43     private static JTextField mainFrameTf1;
  44     private static JTextField mainFrameTf2;
  45     private static JTextField secondFrameTf;
  46 
  47     public static void main(String[] args) throws AWTException {
  48         try {
  49             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  50         } catch (Exception e) {
  51             return;// miss unsupported platforms.
  52         }
  53         createWindows();
  54         initRobot();
  55         runScript();
  56         verify();
  57     }
  58 
  59     private static void verify() {
  60         Component c = DefaultKeyboardFocusManager
  61                 .getCurrentKeyboardFocusManager().getFocusOwner();
  62         if (!(c == mainFrameTf2)) {
  63             throw new RuntimeException("Wrong focus owner.");
  64         }
  65     }
  66 
  67     public static void sync() {
  68         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  69         toolkit.realSync();
  70     }
  71 
  72     public static void initRobot() throws AWTException {
  73         robot = new Robot();
  74         robot.setAutoDelay(100);
  75     }
  76 
  77     private static void clickWindowsTitle(JFrame frame) {
  78         Point point = frame.getLocationOnScreen();
  79         robot.mouseMove(point.x + (frame.getWidth() / 2), point.y + 5);
  80         robot.mousePress(InputEvent.BUTTON1_MASK);
  81         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  82     }
  83 
  84     public static void runScript() {
  85         robot.delay(1000);
  86         printABCD();
  87         pressTab();
  88         clickWindowsTitle(secondFrame);
  89         robot.delay(500);
  90         robot.keyPress(KeyEvent.VK_ALT);
  91         robot.keyRelease(KeyEvent.VK_ALT);
  92         clickWindowsTitle(firstFrame);
  93         sync();
  94     }
  95 
  96     private static void pressTab() {
  97         robot.keyPress(KeyEvent.VK_TAB);
  98         robot.keyRelease(KeyEvent.VK_TAB);
  99     }
 100 
 101     private static void printABCD() {
 102         robot.keyPress(KeyEvent.VK_A);
 103         robot.keyRelease(KeyEvent.VK_A);
 104         robot.keyPress(KeyEvent.VK_B);
 105         robot.keyRelease(KeyEvent.VK_B);
 106         robot.keyPress(KeyEvent.VK_C);
 107         robot.keyRelease(KeyEvent.VK_C);
 108         robot.keyPress(KeyEvent.VK_D);
 109         robot.keyRelease(KeyEvent.VK_D);
 110     }
 111 
 112     public static void createWindows() {
 113         firstFrame = new JFrame("Frame");
 114         firstFrame.setLayout(new FlowLayout());
 115 
 116         JMenuBar bar = new JMenuBar();
 117         JMenu menu = new JMenu("File");
 118         JMenuItem item = new JMenuItem("Save");
 119 
 120         mainFrameTf1 = new JTextField(10);
 121         mainFrameTf2 = new JTextField(10);
 122 
 123         mainFrameTf1.addKeyListener(new KeyAdapter() {
 124             public void keyPressed(KeyEvent EVT) {
 125                 if (EVT.getKeyChar() >= 'a' && EVT.getKeyChar() <= 'z') {
 126                     try {
 127                         // imitate some long processing
 128                         Thread.sleep(2000);
 129                     } catch (InterruptedException e) {
 130                         throw new RuntimeException(e);
 131                     }
 132                 }
 133             }
 134         });
 135 
 136         menu.add(item);
 137         bar.add(menu);
 138         firstFrame.setJMenuBar(bar);
 139 
 140 
 141         firstFrame.add(mainFrameTf1);
 142         firstFrame.add(mainFrameTf2);
 143 
 144         firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 145 
 146         firstFrame.pack();
 147 
 148         secondFrame = new JFrame("Frame 2");
 149         secondFrame.setLocation(0, 150);
 150         secondFrameTf = new JTextField(20);
 151         secondFrame.add(secondFrameTf);
 152         secondFrame.pack();
 153         SwingUtilities.invokeLater(new Runnable() {
 154             @Override
 155             public void run() {
 156                 secondFrame.setVisible(true);
 157             }
 158         });
 159         SwingUtilities.invokeLater(new Runnable() {
 160             @Override
 161             public void run() {
 162                 firstFrame.setVisible(true);
 163             }
 164         });
 165 
 166         mainFrameTf1.requestFocus();
 167         sync();
 168     }
 169 }