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