1 /*
   2  * Copyright (c) 2015, 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 /**
  25  * @test
  26  * @key headful
  27  * @bug 8067346
  28  * @summary Submenu has a changed offset on Windows7 with Windows look and feel
  29  * @requires (os.family == "windows")
  30  * @run main bug8067346
  31  */
  32 
  33 import java.awt.Insets;
  34 import javax.swing.JFrame;
  35 import javax.swing.JMenu;
  36 import javax.swing.JMenuBar;
  37 import javax.swing.JMenuItem;
  38 import javax.swing.SwingUtilities;
  39 import javax.swing.UIManager;
  40 import javax.swing.UnsupportedLookAndFeelException;
  41 
  42 
  43 public class bug8067346 {
  44 
  45     private JMenuBar menuBar;
  46     private JFrame frame;
  47     private String[] menuClasses = {"MenuItem", "Menu",
  48         "CheckBoxMenuItem", "RadioButtonMenuItem"};
  49     private String MARGIN = ".margin";
  50     private String CHECKICONOFFSET = ".checkIconOffset";
  51     private static boolean runTest = true;
  52 
  53     public static void main(String[] args) throws Exception {
  54         SwingUtilities.invokeAndWait(new Runnable() {
  55 
  56             @Override
  57             public void run() {
  58                 bug8067346 test = new bug8067346();
  59                 try {
  60                     // set windows look and feel
  61                     String lnf =
  62                            "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
  63                     UIManager.setLookAndFeel(lnf);
  64                 } catch (UnsupportedLookAndFeelException e) {
  65                     runTest = false;
  66                 } catch (ClassNotFoundException e) {
  67                     runTest = false;
  68                 } catch (InstantiationException e) {
  69                     runTest = false;
  70                 } catch (IllegalAccessException e) {
  71                     runTest = false;
  72                 }
  73                 if(runTest) {
  74                     test.createUI();
  75                     test.performTest();
  76                     test.dispose();
  77                 }
  78             }
  79         });
  80     }
  81 
  82     public void createUI() {
  83 
  84         frame = new JFrame();
  85         menuBar = new JMenuBar();
  86         frame.setJMenuBar(menuBar);
  87         JMenu menu, submenu;
  88         JMenuItem menuItem;
  89 
  90         menu = new JMenu("A Menu");
  91         menuBar.add(menu);
  92         menu.addSeparator();
  93 
  94         submenu = new JMenu("A submenu");
  95 
  96         menuItem = new JMenuItem("An item in the submenu");
  97         submenu.add(menuItem);
  98         menu.add(submenu);
  99     }
 100 
 101     public void performTest() {
 102         try {
 103             String errorMessage = "Incorrect value for ";
 104             StringBuilder errorMessageBuilder = new StringBuilder(errorMessage);
 105             boolean error = false;
 106             int retVal = testMargin();
 107             if (retVal != 0) {
 108                 errorMessageBuilder.append(menuClasses[retVal])
 109                         .append(MARGIN).append("\n");
 110                 error = true;
 111             }
 112             retVal = testCheckIconOffset();
 113             if (retVal != 0) {
 114                 errorMessageBuilder.append(errorMessage)
 115                         .append(menuClasses[retVal]).append(CHECKICONOFFSET);
 116             }
 117             if (error || retVal != 0) {
 118                 throw new RuntimeException(errorMessageBuilder.toString());
 119             }
 120         } finally {
 121             dispose();
 122         }
 123     }
 124 
 125     private int testMargin() {
 126 
 127         for (int inx = 0; inx < menuClasses.length; inx++) {
 128             Insets margin = (Insets) UIManager.get(menuClasses[inx] + MARGIN);
 129             if (margin != null && margin.bottom == 0 && margin.left == 0
 130                     && margin.right == 0 && margin.top == 0) {
 131                 return inx + 1;
 132             }
 133         }
 134         return 0;
 135     }
 136 
 137     private int testCheckIconOffset() {
 138 
 139         for (int inx = 0; inx < menuClasses.length; inx++) {
 140             Object checkIconOffset = UIManager.get(menuClasses[inx]
 141                     + CHECKICONOFFSET);
 142             if (checkIconOffset != null && ((Integer) checkIconOffset) == 0) {
 143                 return inx + 1;
 144             }
 145         }
 146         return 0;
 147     }
 148 
 149     public void dispose() {
 150         frame.dispose();
 151     }
 152 }