Path: blob/master/test/jdk/javax/swing/JInternalFrame/6725409/bug6725409.java
41153 views
/*1* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @key headful25* @bug 672540926* @requires (os.family == "windows")27* @summary Checks that JInternalFrame's system menu28* can be localized during run-time29* @author Mikhail Lapshin30* @library /lib/client/31* @modules java.desktop/com.sun.java.swing.plaf.windows32* @build ExtendedRobot33* @run main bug672540934*/3536import javax.swing.*;37import java.awt.*;3839public class bug6725409 {40private JFrame frame;41private JInternalFrame iFrame;42private TestTitlePane testTitlePane;43private boolean passed;44private static ExtendedRobot robot = createRobot();4546public static void main(String[] args) throws Exception {47try {48UIManager.setLookAndFeel(49new com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel());50} catch(UnsupportedLookAndFeelException e) {51System.out.println("The test is for Windows LaF only");52return;53}5455final bug6725409 bug6725409 = new bug6725409();56try {57SwingUtilities.invokeAndWait(new Runnable() {58public void run() {59bug6725409.setupUIStep1();60}61});62sync();63SwingUtilities.invokeAndWait(new Runnable() {64public void run() {65bug6725409.setupUIStep2();66}67});68sync();69SwingUtilities.invokeAndWait(new Runnable() {70public void run() {71bug6725409.test();72}73});74sync();75bug6725409.checkResult();76} finally {77if (bug6725409.frame != null) {78bug6725409.frame.dispose();79}80}81}8283private void setupUIStep1() {84frame = new JFrame();85frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);8687JDesktopPane desktop = new JDesktopPane();88iFrame = new JInternalFrame("Internal Frame", true, true, true, true);89iFrame.setSize(200, 100);90desktop.add(iFrame);91frame.add(desktop);92iFrame.setVisible(true);9394frame.setSize(500, 300);95frame.setLocationRelativeTo(null);96frame.setVisible(true);97}9899private void setupUIStep2() {100UIManager.put("InternalFrameTitlePane.restoreButtonText",101"CUSTOM.restoreButtonText");102UIManager.put("InternalFrameTitlePane.moveButtonText",103"CUSTOM.moveButtonText");104UIManager.put("InternalFrameTitlePane.sizeButtonText",105"CUSTOM.sizeButtonText");106UIManager.put("InternalFrameTitlePane.minimizeButtonText",107"CUSTOM.minimizeButtonText");108UIManager.put("InternalFrameTitlePane.maximizeButtonText",109"CUSTOM.maximizeButtonText");110UIManager.put("InternalFrameTitlePane.closeButtonText",111"CUSTOM.closeButtonText");112SwingUtilities.updateComponentTreeUI(frame);113}114115// The test depends on the order of the menu items in116// WindowsInternalFrameTitlePane.systemPopupMenu117private void test() {118testTitlePane = new TestTitlePane(iFrame);119passed = true;120checkMenuItemText(0, "CUSTOM.restoreButtonText");121checkMenuItemText(1, "CUSTOM.moveButtonText");122checkMenuItemText(2, "CUSTOM.sizeButtonText");123checkMenuItemText(3, "CUSTOM.minimizeButtonText");124checkMenuItemText(4, "CUSTOM.maximizeButtonText");125// Skip separator126checkMenuItemText(6, "CUSTOM.closeButtonText");127}128129private void checkMenuItemText(int index, String text) {130JMenuItem menuItem = (JMenuItem)131testTitlePane.getSystemPopupMenu().getComponent(index);132if (!text.equals(menuItem.getText())) {133passed = false;134}135}136137private void checkResult() {138if (passed) {139System.out.println("Test passed");140} else {141throw new RuntimeException("Unable to localize " +142"JInternalFrame's system menu during run-time");143}144}145146private static void sync() {147robot.waitForIdle();148}149private static ExtendedRobot createRobot() {150try {151ExtendedRobot robot = new ExtendedRobot();152return robot;153}catch(Exception ex) {154ex.printStackTrace();155throw new Error("Unexpected Failure");156}157}158159// Extend WindowsInternalFrameTitlePane to get access to systemPopupMenu160private class TestTitlePane extends161com.sun.java.swing.plaf.windows.WindowsInternalFrameTitlePane {162private JPopupMenu systemPopupMenu;163164public TestTitlePane(JInternalFrame f) {165super(f);166}167168public JPopupMenu getSystemPopupMenu() {169return systemPopupMenu;170}171172protected void addSystemMenuItems(JPopupMenu menu) {173super.addSystemMenuItems(menu);174systemPopupMenu = menu;175}176}177}178179180