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 8028271 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 + 10); 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 firstFrame.setPreferredSize(new Dimension(600,100)); 116 117 JMenuBar bar = new JMenuBar(); 118 JMenu menu = new JMenu("File"); 119 JMenuItem item = new JMenuItem("Save"); 120 121 mainFrameTf1 = new JTextField(10); 122 mainFrameTf2 = new JTextField(10); 123 124 mainFrameTf1.addKeyListener(new KeyAdapter() { 125 public void keyPressed(KeyEvent EVT) { 126 if (EVT.getKeyChar() >= 'a' && EVT.getKeyChar() <= 'z') { 127 try { 128 // imitate some long processing 129 Thread.sleep(2000); 130 } catch (InterruptedException e) { 131 throw new RuntimeException(e); 132 } 133 } 134 } 135 }); 136 137 menu.add(item); 138 bar.add(menu); 139 firstFrame.setJMenuBar(bar); 140 141 142 firstFrame.add(mainFrameTf1); 143 firstFrame.add(mainFrameTf2); 144 145 firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 146 147 firstFrame.pack(); 148 149 secondFrame = new JFrame("Frame 2"); 150 secondFrame.setPreferredSize(new Dimension(600,100)); 151 secondFrame.setLocation(0, 150); 152 secondFrameTf = new JTextField(20); 153 secondFrame.add(secondFrameTf); 154 secondFrame.pack(); 155 SwingUtilities.invokeLater(new Runnable() { 156 @Override 157 public void run() { 158 secondFrame.setVisible(true); 159 } 160 }); 161 SwingUtilities.invokeLater(new Runnable() { 162 @Override 163 public void run() { 164 firstFrame.setVisible(true); 165 } 166 }); 167 168 mainFrameTf1.requestFocus(); 169 sync(); 170 } 171 }