1 /*
   2  * Copyright (c) 2012, 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  * @bug 6438430
  27  * @summary Tests that submenu title doesn't overlap with submenu indicator
  28  *          in JPopupMenu
  29  * @author Mikhail Lapshin
  30  * @run main/othervm -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel bug6438430
  31  * @run main/othervm -Dswing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel bug6438430
  32  */
  33 
  34 import javax.swing.JMenuItem;
  35 import javax.swing.JMenu;
  36 import javax.swing.JCheckBoxMenuItem;
  37 
  38 public class bug6438430 {
  39     public static void main(String[] args) {
  40         JMenu subMenu1 = new JMenu("Long-titled Sub Menu");
  41         subMenu1.add(new JMenuItem("SubMenu Item"));
  42         JMenuItem checkBoxMenuItem1 = new JCheckBoxMenuItem("CheckBox");
  43 
  44         JMenu menu1 = new JMenu("It works always");
  45         menu1.add(checkBoxMenuItem1);
  46         menu1.add(subMenu1);
  47 
  48         // Simulate DefaultMenuLayout calls.
  49         // The latest traversed menu item must be the widest.
  50         checkBoxMenuItem1.getPreferredSize();
  51         int width1 = subMenu1.getPreferredSize().width;
  52         System.out.println("width1 = " + width1);
  53 
  54 
  55         JMenu subMenu2 = new JMenu("Long-titled Sub Menu");
  56         subMenu2.add(new JMenuItem("SubMenu Item"));
  57         JMenuItem checkBoxMenuItem2 = new JCheckBoxMenuItem("CheckBox");
  58 
  59         JMenu menu2 = new JMenu("It did not work before the fix");
  60         menu2.add(subMenu2);
  61         menu2.add(checkBoxMenuItem2);
  62 
  63         // Simulate DefaultMenuLayout calls.
  64         // The latest traversed menu item must be the widest.
  65         subMenu2.getPreferredSize();
  66         int width2 = checkBoxMenuItem2.getPreferredSize().width;
  67         System.out.println("width2 = " + width2);
  68 
  69         if (width1 != width2) {
  70             throw new RuntimeException( "Submenu title and submenu indicator " +
  71                                         "overlap on JMenuItem!" );
  72         }
  73 
  74         System.out.println("Test passed");
  75     }
  76 }