1 /*
   2  * @test
   3  * @bug 6438430
   4  * @summary Tests that submenu title doesn't overlap with submenu indicator
   5  *          in JPopupMenu
   6  * @author Mikhail Lapshin
   7  * @run main bug6438430
   8  */
   9 
  10 import javax.swing.JMenuItem;
  11 import javax.swing.JMenu;
  12 import javax.swing.JCheckBoxMenuItem;
  13 
  14 public class bug6438430 {
  15     public static void main(String[] args) {
  16         JMenu subMenu1 = new JMenu("Long-titled Sub Menu");
  17         subMenu1.add(new JMenuItem("SubMenu Item"));
  18         JMenuItem checkBoxMenuItem1 = new JCheckBoxMenuItem("CheckBox");
  19 
  20         JMenu menu1 = new JMenu("It works always");
  21         menu1.add(checkBoxMenuItem1);
  22         menu1.add(subMenu1);
  23 
  24         // Simulate DefaultMenuLayout calls.
  25         // The latest traversed menu item must be the widest.
  26         checkBoxMenuItem1.getPreferredSize();
  27         int width1 = subMenu1.getPreferredSize().width;
  28         System.out.println("width1 = " + width1);
  29 
  30 
  31         JMenu subMenu2 = new JMenu("Long-titled Sub Menu");
  32         subMenu2.add(new JMenuItem("SubMenu Item"));
  33         JMenuItem checkBoxMenuItem2 = new JCheckBoxMenuItem("CheckBox");
  34 
  35         JMenu menu2 = new JMenu("It did not work before the fix");
  36         menu2.add(subMenu2);
  37         menu2.add(checkBoxMenuItem2);
  38 
  39         // Simulate DefaultMenuLayout calls.
  40         // The latest traversed menu item must be the widest.
  41         subMenu2.getPreferredSize();
  42         int width2 = checkBoxMenuItem2.getPreferredSize().width;
  43         System.out.println("width2 = " + width2);
  44 
  45         if (width1 != width2) {
  46             throw new RuntimeException( "Submenu title and submenu indicator " +
  47                     "overlap on JMenuItem!" );
  48         }
  49 
  50         System.out.println("Test passed");
  51     }
  52 }