Path: blob/master/test/jdk/javax/swing/JMenu/8067346/bug8067346.java
41153 views
/*1* Copyright (c) 2015, 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/**24* @test25* @key headful26* @bug 806734627* @summary Submenu has a changed offset on Windows7 with Windows look and feel28* @requires (os.family == "windows")29* @run main bug806734630*/3132import java.awt.Insets;33import javax.swing.JFrame;34import javax.swing.JMenu;35import javax.swing.JMenuBar;36import javax.swing.JMenuItem;37import javax.swing.SwingUtilities;38import javax.swing.UIManager;39import javax.swing.UnsupportedLookAndFeelException;404142public class bug8067346 {4344private JMenuBar menuBar;45private JFrame frame;46private String[] menuClasses = {"MenuItem", "Menu",47"CheckBoxMenuItem", "RadioButtonMenuItem"};48private String MARGIN = ".margin";49private String CHECKICONOFFSET = ".checkIconOffset";50private static boolean runTest = true;5152public static void main(String[] args) throws Exception {53SwingUtilities.invokeAndWait(new Runnable() {5455@Override56public void run() {57bug8067346 test = new bug8067346();58try {59// set windows look and feel60String lnf =61"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";62UIManager.setLookAndFeel(lnf);63} catch (UnsupportedLookAndFeelException e) {64runTest = false;65} catch (ClassNotFoundException e) {66runTest = false;67} catch (InstantiationException e) {68runTest = false;69} catch (IllegalAccessException e) {70runTest = false;71}72if(runTest) {73test.createUI();74test.performTest();75test.dispose();76}77}78});79}8081public void createUI() {8283frame = new JFrame();84menuBar = new JMenuBar();85frame.setJMenuBar(menuBar);86JMenu menu, submenu;87JMenuItem menuItem;8889menu = new JMenu("A Menu");90menuBar.add(menu);91menu.addSeparator();9293submenu = new JMenu("A submenu");9495menuItem = new JMenuItem("An item in the submenu");96submenu.add(menuItem);97menu.add(submenu);98}99100public void performTest() {101try {102String errorMessage = "Incorrect value for ";103StringBuilder errorMessageBuilder = new StringBuilder(errorMessage);104boolean error = false;105int retVal = testMargin();106if (retVal != 0) {107errorMessageBuilder.append(menuClasses[retVal])108.append(MARGIN).append("\n");109error = true;110}111retVal = testCheckIconOffset();112if (retVal != 0) {113errorMessageBuilder.append(errorMessage)114.append(menuClasses[retVal]).append(CHECKICONOFFSET);115}116if (error || retVal != 0) {117throw new RuntimeException(errorMessageBuilder.toString());118}119} finally {120dispose();121}122}123124private int testMargin() {125126for (int inx = 0; inx < menuClasses.length; inx++) {127Insets margin = (Insets) UIManager.get(menuClasses[inx] + MARGIN);128if (margin != null && margin.bottom == 0 && margin.left == 0129&& margin.right == 0 && margin.top == 0) {130return inx + 1;131}132}133return 0;134}135136private int testCheckIconOffset() {137138for (int inx = 0; inx < menuClasses.length; inx++) {139Object checkIconOffset = UIManager.get(menuClasses[inx]140+ CHECKICONOFFSET);141if (checkIconOffset != null && ((Integer) checkIconOffset) == 0) {142return inx + 1;143}144}145return 0;146}147148public void dispose() {149frame.dispose();150}151}152153154