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