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