Path: blob/master/test/jdk/javax/swing/JMenuItem/6209975/bug6209975.java
41153 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 620997527* @summary regression: JMenuItem icons overimposed on JMenuItem labels under Metal LAF28* @author Alexander Zuev29* @run main bug620997530*/31import javax.swing.*;32import java.awt.*;33import java.awt.event.InputEvent;3435public class bug6209975 {3637private static final ReturnObject RO1 = new ReturnObject();38private static final ReturnObject RO2 = new ReturnObject();3940private static JMenu menu;41private static JButton button;42private static JFrame frame;4344public static void main(String[] args) throws Exception {45try {46Robot robot = new Robot();47robot.setAutoDelay(500);484950SwingUtilities.invokeAndWait(new Runnable() {5152@Override53public void run() {54createAndShowGUI();55}56});5758robot.waitForIdle();5960Point clickPoint = getButtonClickPoint();61robot.mouseMove(clickPoint.x, clickPoint.y);62robot.mousePress(InputEvent.BUTTON1_MASK);63robot.mouseRelease(InputEvent.BUTTON1_MASK);64robot.waitForIdle();6566clickPoint = getMenuClickPoint();67robot.mouseMove(clickPoint.x, clickPoint.y);68robot.mousePress(InputEvent.BUTTON1_MASK);69robot.mouseRelease(InputEvent.BUTTON1_MASK);70robot.waitForIdle();7172if (RO1.itsValue <= RO2.itsValue) {73throw new RuntimeException("Offset if the second icon is invalid.");74}75} finally {76if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());77}78}7980private static Point getButtonClickPoint() throws Exception {81final Point[] result = new Point[1];8283SwingUtilities.invokeAndWait(new Runnable() {8485@Override86public void run() {87Point p = button.getLocationOnScreen();88Dimension size = button.getSize();89result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);90}91});92return result[0];93}9495private static Point getMenuClickPoint() throws Exception {96final Point[] result = new Point[1];9798SwingUtilities.invokeAndWait(new Runnable() {99100@Override101public void run() {102Point p = menu.getLocationOnScreen();103Dimension size = menu.getSize();104result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);105}106});107return result[0];108}109110private static void createAndShowGUI() {111frame = new JFrame("Test6209975");112frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);113frame.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);114frame.setLayout(new BorderLayout());115button = new JButton("Focus holder");116frame.add(button);117118JMenuBar mb = new JMenuBar();119menu = new JMenu("File");120121JMenuItem item;122123item = new JMenuItem("Just a menu item");124item.setIcon(new MyIcon(RO1));125item.setHorizontalTextPosition(SwingConstants.LEADING);126menu.add(item);127128item = new JMenuItem("Menu Item with another icon");129item.setIcon(new MyIcon(RO2));130item.setHorizontalTextPosition(SwingConstants.TRAILING);131menu.add(item);132133mb.add(menu);134135frame.setJMenuBar(mb);136frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);137frame.pack();138frame.setLocation(400, 300);139frame.setVisible(true);140}141142public static class ReturnObject {143144public volatile int itsValue;145}146147public static class MyIcon implements Icon {148149ReturnObject thisObject = null;150151public MyIcon(ReturnObject ro) {152super();153thisObject = ro;154}155156public void paintIcon(Component c, Graphics g, int x, int y) {157Color color = g.getColor();158g.setColor(Color.BLACK);159g.fillRect(x, y, 10, 10);160g.setColor(color);161thisObject.itsValue = x;162}163164public int getIconWidth() {165return 10;166}167168public int getIconHeight() {169return 10;170}171}172}173174175