test/javax/swing/plaf/windows/WindowsRootPaneUI/WrongAltProcessing/WrongAltProcessing.java

Print this page




   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 }


   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 + 10);
  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 }