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 import sun.awt.SunToolkit;
  36 
  37 /**
  38  * @test
  39  * @bug 8020708
  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         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  83         Robot robot = new Robot();
  84         robot.setAutoDelay(50);
  85 
  86         SwingUtilities.invokeAndWait(new Runnable() {
  87             @Override
  88             public void run() {
  89                 frame = new JFrame("Test");
  90                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  91                 frame.setSize(300, 200);
  92 
  93                 JDesktopPane desktop = new JDesktopPane();
  94                 internalFrame = new JInternalFrame("Test");
  95                 internalFrame.setSize(200, 100);
  96                 internalFrame.setClosable(true);
  97                 desktop.add(internalFrame);
  98                 internalFrame.setVisible(true);
  99                 internalFrame.setMaximizable(true);
 100 
 101                 frame.getContentPane().add(desktop);
 102                 frame.setVisible(true);
 103             }
 104         });
 105 
 106         toolkit.realSync();
 107 
 108         Point clickPoint = Util.getCenterPoint(internalFrame);
 109         robot.mouseMove(clickPoint.x, clickPoint.y);
 110         robot.mousePress(InputEvent.BUTTON1_MASK);
 111         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 112         toolkit.realSync();
 113 
 114         Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_SPACE);
 115         toolkit.realSync();
 116 
 117         Util.hitKeys(robot, KeyEvent.VK_C);
 118         toolkit.realSync();
 119 
 120         SwingUtilities.invokeAndWait(new Runnable() {
 121             @Override
 122             public void run() {
 123                 if (internalFrame.isVisible()) {
 124                     throw new RuntimeException("Close mnemonic does not work");
 125                 }
 126                 frame.dispose();
 127             }
 128         });
 129     }
 130 
 131     static final boolean installLookAndFeel(String lafName) throws Exception {
 132         UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
 133         for (UIManager.LookAndFeelInfo info : infos) {
 134             if (info.getClassName().contains(lafName)) {
 135                 UIManager.setLookAndFeel(info.getClassName());
 136                 return true;
 137             }
 138         }
 139         return false;
 140     }
 141 }