1 /*
   2  * Copyright (c) 2011, 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 6596966
  28  * @summary Some JFileChooser mnemonics do not work with sticky keys
  29  * @library ../../regtesthelpers
  30  * @library ../../../../lib/testlibrary
  31  * @build Util jdk.testlibrary.OSInfo
  32  * @run main bug6596966
  33  * @author Pavel Porvatov
  34  */
  35 
  36 import java.awt.*;
  37 import java.awt.event.KeyEvent;
  38 import java.util.ArrayList;
  39 import javax.swing.*;
  40 import sun.awt.SunToolkit;
  41 
  42 public class bug6596966 {
  43     private static JFrame frame;
  44 
  45     private static JLabel label;
  46     private static JButton button;
  47     private static JComboBox comboBox;
  48 
  49     public static void main(String[] args) throws Exception {
  50         Robot robot = new Robot();
  51         SunToolkit toolkit = (SunToolkit) SunToolkit.getDefaultToolkit();
  52 
  53         SwingUtilities.invokeAndWait(new Runnable() {
  54             public void run() {
  55                 button = new JButton("Button");
  56                 comboBox = new JComboBox();
  57 
  58                 label = new JLabel("Label");
  59                 label.setDisplayedMnemonic('L');
  60                 label.setLabelFor(comboBox);
  61 
  62                 JPanel pnContent = new JPanel();
  63 
  64                 pnContent.add(button);
  65                 pnContent.add(label);
  66                 pnContent.add(comboBox);
  67 
  68                 frame = new JFrame();
  69 
  70                 frame.add(pnContent);
  71                 frame.pack();
  72                 frame.setVisible(true);
  73             }
  74         });
  75 
  76         toolkit.realSync();
  77 
  78         ArrayList<Integer> keys = Util.getSystemMnemonicKeyCodes();
  79         for (int i = 0; i < keys.size(); ++i) {
  80             robot.keyPress(keys.get(i));
  81         }
  82 
  83         robot.keyPress(KeyEvent.VK_L);
  84 
  85         toolkit.realSync();
  86         toolkit.getSystemEventQueue().postEvent(new KeyEvent(label, KeyEvent.KEY_RELEASED,
  87                 EventQueue.getMostRecentEventTime(), 0, KeyEvent.VK_L, 'L'));
  88 
  89         toolkit.realSync();
  90 
  91         try {
  92             SwingUtilities.invokeAndWait(new Runnable() {
  93                 public void run() {
  94                     if (!comboBox.isFocusOwner()) {
  95                         throw new RuntimeException("comboBox isn't focus owner");
  96                     }
  97                 }
  98             });
  99         } finally {
 100             robot.keyRelease(KeyEvent.VK_L);
 101             for (int i = 0; i < keys.size(); ++i) {
 102                 robot.keyRelease(keys.get(i));
 103             }
 104             toolkit.realSync();
 105         }
 106     }
 107 }