1 /*
   2  * Copyright (c) 2013, 2015, 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 import java.awt.Robot;
  25 import java.awt.event.KeyEvent;
  26 import javax.swing.JFileChooser;
  27 import javax.swing.SwingUtilities;
  28 import javax.swing.UIManager;
  29 import javax.swing.UIManager.LookAndFeelInfo;
  30 
  31 /**
  32  * @test
  33  * @key headful
  34  * @bug 8002077
  35  * @author Alexander Scherbatiy
  36  * @summary Possible mnemonic issue on JFileChooser Save button on nimbus L&F
  37  * @library ../../regtesthelpers/
  38  * @build Util
  39  * @run main bug8002077
  40  */
  41 public class bug8002077 {
  42 
  43     private static volatile int fileChooserState = JFileChooser.ERROR_OPTION;
  44 
  45     public static void main(String[] args) throws Exception {
  46         for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
  47             if ("Nimbus".equals(info.getName())) {
  48                 UIManager.setLookAndFeel(info.getClassName());
  49                 UIManager.put("FileChooser.openButtonMnemonic", KeyEvent.VK_O);
  50                 UIManager.put("FileChooser.saveButtonMnemonic", KeyEvent.VK_S);
  51                 runTest();
  52                 break;
  53             }
  54         }
  55     }
  56 
  57     private static void runTest() throws Exception {
  58         Robot robot = new Robot();
  59         robot.setAutoDelay(50);
  60 
  61         SwingUtilities.invokeLater(() ->
  62                 fileChooserState = new JFileChooser().showSaveDialog(null));
  63         robot.waitForIdle();
  64 
  65         Util.hitMnemonics(robot, KeyEvent.VK_N);
  66         robot.waitForIdle();
  67 
  68         Util.hitKeys(robot, KeyEvent.VK_A);
  69         robot.waitForIdle();
  70 
  71         Util.hitMnemonics(robot, KeyEvent.VK_S);
  72         robot.waitForIdle();
  73 
  74         if (fileChooserState != JFileChooser.APPROVE_OPTION) {
  75             // Close the dialog
  76             Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
  77             robot.waitForIdle();
  78 
  79             throw new RuntimeException("Save button is not pressed!");
  80         }
  81     }
  82 }