1 /*
   2  * Copyright (c) 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 import java.awt.Point;
  25 import java.awt.Robot;
  26 import java.awt.Toolkit;
  27 import java.awt.event.InputEvent;
  28 import java.awt.event.KeyEvent;
  29 import java.util.Locale;
  30 import javax.swing.JDesktopPane;
  31 import javax.swing.JFrame;
  32 import javax.swing.JInternalFrame;
  33 import javax.swing.SwingUtilities;
  34 import javax.swing.UIManager;
  35 
  36 /**
  37  * @test
  38  * @key headful
  39  * @bug 8020708 8032568
  40  * @author Alexander Scherbatiy
  41  * @summary NLS: mnemonics missing in SwingSet2/JInternalFrame demo
  42  * @library ../../regtesthelpers
  43  * @build Util
  44  * @run main bug8020708
  45  */
  46 public class bug8020708 {
  47 
  48     private static final Locale[] SUPPORTED_LOCALES = {
  49         Locale.ENGLISH,
  50         new Locale("de"),
  51         new Locale("es"),
  52         new Locale("fr"),
  53         new Locale("it"),
  54         new Locale("ja"),
  55         new Locale("ko"),
  56         new Locale("pt", "BR"),
  57         new Locale("sv"),
  58         new Locale("zh", "CN"),
  59         new Locale("zh", "TW")
  60     };
  61     private static final String[] LOOK_AND_FEELS = {
  62         "Nimbus",
  63         "Windows",
  64         "Motif"
  65     };
  66     private static JInternalFrame internalFrame;
  67     private static JFrame frame;
  68 
  69     public static void main(String[] args) throws Exception {
  70         for (Locale locale : SUPPORTED_LOCALES) {
  71             for (String laf : LOOK_AND_FEELS) {
  72                 Locale.setDefault(locale);
  73                 if (!installLookAndFeel(laf)) {
  74                     continue;
  75                 }
  76                 testInternalFrameMnemonic();
  77             }
  78         }
  79     }
  80 
  81     static void testInternalFrameMnemonic() throws Exception {
  82         Robot robot = new Robot();
  83         robot.setAutoDelay(50);
  84 
  85         SwingUtilities.invokeAndWait(new Runnable() {
  86             @Override
  87             public void run() {
  88                 frame = new JFrame("Test");
  89                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  90                 frame.setSize(300, 200);
  91 
  92                 JDesktopPane desktop = new JDesktopPane();
  93                 internalFrame = new JInternalFrame("Test");
  94                 internalFrame.setSize(200, 100);
  95                 internalFrame.setClosable(true);
  96                 desktop.add(internalFrame);
  97                 internalFrame.setVisible(true);
  98                 internalFrame.setMaximizable(true);
  99 
 100                 frame.getContentPane().add(desktop);
 101                 frame.setVisible(true);
 102             }
 103         });
 104 
 105         robot.waitForIdle();
 106 
 107         Point clickPoint = Util.getCenterPoint(internalFrame);
 108         robot.mouseMove(clickPoint.x, clickPoint.y);
 109         robot.mousePress(InputEvent.BUTTON1_MASK);
 110         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 111         robot.waitForIdle();
 112 
 113         Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_SPACE);
 114         robot.waitForIdle();
 115         int keyCode = KeyEvent.VK_C;
 116         String mnemonic = UIManager
 117                 .getString("InternalFrameTitlePane.closeButton.mnemonic");
 118         try {
 119             keyCode = Integer.parseInt(mnemonic);
 120         } catch (NumberFormatException e) {
 121         }
 122         Util.hitKeys(robot, keyCode);
 123         robot.waitForIdle();
 124         robot.delay(500);
 125 
 126         SwingUtilities.invokeAndWait(new Runnable() {
 127             @Override
 128             public void run() {
 129                 if (internalFrame.isVisible()) {
 130                     throw new RuntimeException("Close mnemonic does not work in "+UIManager.getLookAndFeel());
 131                 }
 132                 frame.dispose();
 133             }
 134         });
 135     }
 136 
 137     static final boolean installLookAndFeel(String lafName) throws Exception {
 138         UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
 139         for (UIManager.LookAndFeelInfo info : infos) {
 140             if (info.getClassName().contains(lafName)) {
 141                 UIManager.setLookAndFeel(info.getClassName());
 142                 return true;
 143             }
 144         }
 145         return false;
 146     }
 147 }