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