1 /*
   2  * Copyright (c) 2008, 2017, 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 /* @test
  25  * @key headful
  26  * @bug 6725409
  27  * @requires (os.family == "windows")
  28  * @summary Checks that JInternalFrame's system menu
  29  *          can be localized during run-time
  30  * @author Mikhail Lapshin
  31  * @library ../../../../lib/testlibrary/
  32  * @modules java.desktop/com.sun.java.swing.plaf.windows
  33  * @build ExtendedRobot
  34  * @run main bug6725409
  35  */
  36 
  37 import javax.swing.*;
  38 import java.awt.*;
  39 
  40 public class bug6725409 {
  41     private JFrame frame;
  42     private JInternalFrame iFrame;
  43     private TestTitlePane testTitlePane;
  44     private boolean passed;
  45     private static ExtendedRobot robot = createRobot();
  46 
  47     public static void main(String[] args) throws Exception {
  48         try {
  49             UIManager.setLookAndFeel(
  50                     new com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel());
  51         } catch(UnsupportedLookAndFeelException e) {
  52             System.out.println("The test is for Windows LaF only");
  53             return;
  54         }
  55 
  56         final bug6725409 bug6725409 = new bug6725409();
  57         try {
  58             SwingUtilities.invokeAndWait(new Runnable() {
  59                 public void run() {
  60                     bug6725409.setupUIStep1();
  61                 }
  62             });
  63             sync();
  64             SwingUtilities.invokeAndWait(new Runnable() {
  65                 public void run() {
  66                     bug6725409.setupUIStep2();
  67                 }
  68             });
  69             sync();
  70             SwingUtilities.invokeAndWait(new Runnable() {
  71                 public void run() {
  72                     bug6725409.test();
  73                 }
  74             });
  75             sync();
  76             bug6725409.checkResult();
  77         } finally {
  78             if (bug6725409.frame != null) {
  79                 bug6725409.frame.dispose();
  80             }
  81         }
  82     }
  83 
  84     private void setupUIStep1() {
  85         frame = new JFrame();
  86         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  87 
  88         JDesktopPane desktop = new JDesktopPane();
  89         iFrame = new JInternalFrame("Internal Frame", true, true, true, true);
  90         iFrame.setSize(200, 100);
  91         desktop.add(iFrame);
  92         frame.add(desktop);
  93         iFrame.setVisible(true);
  94 
  95         frame.setSize(500, 300);
  96         frame.setLocationRelativeTo(null);
  97         frame.setVisible(true);
  98     }
  99 
 100     private void setupUIStep2() {
 101         UIManager.put("InternalFrameTitlePane.restoreButtonText",
 102                 "CUSTOM.restoreButtonText");
 103         UIManager.put("InternalFrameTitlePane.moveButtonText",
 104                 "CUSTOM.moveButtonText");
 105         UIManager.put("InternalFrameTitlePane.sizeButtonText",
 106                 "CUSTOM.sizeButtonText");
 107         UIManager.put("InternalFrameTitlePane.minimizeButtonText",
 108                 "CUSTOM.minimizeButtonText");
 109         UIManager.put("InternalFrameTitlePane.maximizeButtonText",
 110                 "CUSTOM.maximizeButtonText");
 111         UIManager.put("InternalFrameTitlePane.closeButtonText",
 112                 "CUSTOM.closeButtonText");
 113         SwingUtilities.updateComponentTreeUI(frame);
 114     }
 115 
 116     // The test depends on the order of the menu items in
 117     // WindowsInternalFrameTitlePane.systemPopupMenu
 118     private void test() {
 119         testTitlePane = new TestTitlePane(iFrame);
 120         passed = true;
 121         checkMenuItemText(0, "CUSTOM.restoreButtonText");
 122         checkMenuItemText(1, "CUSTOM.moveButtonText");
 123         checkMenuItemText(2, "CUSTOM.sizeButtonText");
 124         checkMenuItemText(3, "CUSTOM.minimizeButtonText");
 125         checkMenuItemText(4, "CUSTOM.maximizeButtonText");
 126         // Skip separator
 127         checkMenuItemText(6, "CUSTOM.closeButtonText");
 128     }
 129 
 130     private void checkMenuItemText(int index, String text) {
 131         JMenuItem menuItem = (JMenuItem)
 132                 testTitlePane.getSystemPopupMenu().getComponent(index);
 133         if (!text.equals(menuItem.getText())) {
 134             passed = false;
 135         }
 136     }
 137 
 138     private void checkResult() {
 139         if (passed) {
 140             System.out.println("Test passed");
 141         } else {
 142             throw new RuntimeException("Unable to localize " +
 143                     "JInternalFrame's system menu during run-time");
 144         }
 145     }
 146 
 147     private static void sync() {
 148         robot.waitForIdle();
 149     }
 150     private static ExtendedRobot createRobot() {
 151         try {
 152              ExtendedRobot robot = new ExtendedRobot();
 153              return robot;
 154          }catch(Exception ex) {
 155              ex.printStackTrace();
 156              throw new Error("Unexpected Failure");
 157          }
 158     }
 159 
 160     // Extend WindowsInternalFrameTitlePane to get access to systemPopupMenu
 161     private class TestTitlePane extends
 162             com.sun.java.swing.plaf.windows.WindowsInternalFrameTitlePane {
 163         private JPopupMenu systemPopupMenu;
 164 
 165         public TestTitlePane(JInternalFrame f) {
 166             super(f);
 167         }
 168 
 169         public JPopupMenu getSystemPopupMenu() {
 170             return systemPopupMenu;
 171         }
 172 
 173         protected void addSystemMenuItems(JPopupMenu menu) {
 174             super.addSystemMenuItems(menu);
 175             systemPopupMenu = menu;
 176         }
 177     }
 178 }