jdk/test/javax/swing/JMenuItem/6209975/bug6209975.java
changeset 12031 9994f2041065
child 28071 3acb75b8df45
equal deleted inserted replaced
12030:e7055cf1294e 12031:9994f2041065
       
     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 6209975
       
    27  * @summary regression: JMenuItem icons overimposed on JMenuItem labels under Metal LAF
       
    28  * @library ../../regtesthelpers
       
    29  * @build Util
       
    30  * @author Alexander Zuev
       
    31  * @run main bug6209975
       
    32  */
       
    33 import javax.swing.*;
       
    34 import java.awt.*;
       
    35 import java.awt.event.InputEvent;
       
    36 import sun.awt.SunToolkit;
       
    37 
       
    38 public class bug6209975 {
       
    39 
       
    40     private static final ReturnObject RO1 = new ReturnObject();
       
    41     private static final ReturnObject RO2 = new ReturnObject();
       
    42 
       
    43     private static JMenu menu;
       
    44     private static JButton button;
       
    45 
       
    46     public static void main(String[] args) throws Exception {
       
    47 
       
    48         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
       
    49         Robot robot = new Robot();
       
    50         robot.setAutoDelay(500);
       
    51 
       
    52 
       
    53         SwingUtilities.invokeAndWait(new Runnable() {
       
    54 
       
    55             @Override
       
    56             public void run() {
       
    57                 createAndShowGUI();
       
    58             }
       
    59         });
       
    60 
       
    61         toolkit.realSync();
       
    62 
       
    63         Point clickPoint = getButtonClickPoint();
       
    64         robot.mouseMove(clickPoint.x, clickPoint.y);
       
    65         robot.mousePress(InputEvent.BUTTON1_MASK);
       
    66         robot.mouseRelease(InputEvent.BUTTON1_MASK);
       
    67         toolkit.realSync();
       
    68 
       
    69         clickPoint = getMenuClickPoint();
       
    70         robot.mouseMove(clickPoint.x, clickPoint.y);
       
    71         robot.mousePress(InputEvent.BUTTON1_MASK);
       
    72         robot.mouseRelease(InputEvent.BUTTON1_MASK);
       
    73         toolkit.realSync();
       
    74 
       
    75         if (RO1.itsValue <= RO2.itsValue) {
       
    76             throw new RuntimeException("Offset if the second icon is invalid.");
       
    77         }
       
    78     }
       
    79 
       
    80     private static Point getButtonClickPoint() throws Exception {
       
    81         final Point[] result = new Point[1];
       
    82 
       
    83         SwingUtilities.invokeAndWait(new Runnable() {
       
    84 
       
    85             @Override
       
    86             public void run() {
       
    87                 Point p = button.getLocationOnScreen();
       
    88                 Dimension size = button.getSize();
       
    89                 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
       
    90             }
       
    91         });
       
    92         return result[0];
       
    93     }
       
    94 
       
    95     private static Point getMenuClickPoint() throws Exception {
       
    96         final Point[] result = new Point[1];
       
    97 
       
    98         SwingUtilities.invokeAndWait(new Runnable() {
       
    99 
       
   100             @Override
       
   101             public void run() {
       
   102                 Point p = menu.getLocationOnScreen();
       
   103                 Dimension size = menu.getSize();
       
   104                 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
       
   105             }
       
   106         });
       
   107         return result[0];
       
   108     }
       
   109 
       
   110     private static void createAndShowGUI() {
       
   111         JFrame frame = new JFrame("Test6209975");
       
   112         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
   113         frame.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
       
   114         frame.setLayout(new BorderLayout());
       
   115         button = new JButton("Focus holder");
       
   116         frame.add(button);
       
   117 
       
   118         JMenuBar mb = new JMenuBar();
       
   119         menu = new JMenu("File");
       
   120 
       
   121         JMenuItem item;
       
   122 
       
   123         item = new JMenuItem("Just a menu item");
       
   124         item.setIcon(new MyIcon(RO1));
       
   125         item.setHorizontalTextPosition(SwingConstants.LEADING);
       
   126         menu.add(item);
       
   127 
       
   128         item = new JMenuItem("Menu Item with another icon");
       
   129         item.setIcon(new MyIcon(RO2));
       
   130         item.setHorizontalTextPosition(SwingConstants.TRAILING);
       
   131         menu.add(item);
       
   132 
       
   133         mb.add(menu);
       
   134 
       
   135         frame.setJMenuBar(mb);
       
   136         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
   137         frame.pack();
       
   138         frame.setLocation(400, 300);
       
   139         frame.setVisible(true);
       
   140     }
       
   141 
       
   142     public static class ReturnObject {
       
   143 
       
   144         public volatile int itsValue;
       
   145     }
       
   146 
       
   147     public static class MyIcon implements Icon {
       
   148 
       
   149         ReturnObject thisObject = null;
       
   150 
       
   151         public MyIcon(ReturnObject ro) {
       
   152             super();
       
   153             thisObject = ro;
       
   154         }
       
   155 
       
   156         public void paintIcon(Component c, Graphics g, int x, int y) {
       
   157             Color color = g.getColor();
       
   158             g.setColor(Color.BLACK);
       
   159             g.fillRect(x, y, 10, 10);
       
   160             g.setColor(color);
       
   161             thisObject.itsValue = x;
       
   162         }
       
   163 
       
   164         public int getIconWidth() {
       
   165             return 10;
       
   166         }
       
   167 
       
   168         public int getIconHeight() {
       
   169             return 10;
       
   170         }
       
   171     }
       
   172 }