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