Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JMenuItem/6209975/bug6209975.java
38854 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* @bug 620997526* @summary regression: JMenuItem icons overimposed on JMenuItem labels under Metal LAF27* @author Alexander Zuev28* @run main bug620997529*/30import javax.swing.*;31import java.awt.*;32import java.awt.event.InputEvent;3334public class bug6209975 {3536private static final ReturnObject RO1 = new ReturnObject();37private static final ReturnObject RO2 = new ReturnObject();3839private static JMenu menu;40private static JButton button;4142public static void main(String[] args) throws Exception {4344Robot robot = new Robot();45robot.setAutoDelay(500);464748SwingUtilities.invokeAndWait(new Runnable() {4950@Override51public void run() {52createAndShowGUI();53}54});5556robot.waitForIdle();5758Point clickPoint = getButtonClickPoint();59robot.mouseMove(clickPoint.x, clickPoint.y);60robot.mousePress(InputEvent.BUTTON1_MASK);61robot.mouseRelease(InputEvent.BUTTON1_MASK);62robot.waitForIdle();6364clickPoint = getMenuClickPoint();65robot.mouseMove(clickPoint.x, clickPoint.y);66robot.mousePress(InputEvent.BUTTON1_MASK);67robot.mouseRelease(InputEvent.BUTTON1_MASK);68robot.waitForIdle();6970if (RO1.itsValue <= RO2.itsValue) {71throw new RuntimeException("Offset if the second icon is invalid.");72}73}7475private static Point getButtonClickPoint() throws Exception {76final Point[] result = new Point[1];7778SwingUtilities.invokeAndWait(new Runnable() {7980@Override81public void run() {82Point p = button.getLocationOnScreen();83Dimension size = button.getSize();84result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);85}86});87return result[0];88}8990private static Point getMenuClickPoint() throws Exception {91final Point[] result = new Point[1];9293SwingUtilities.invokeAndWait(new Runnable() {9495@Override96public void run() {97Point p = menu.getLocationOnScreen();98Dimension size = menu.getSize();99result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);100}101});102return result[0];103}104105private static void createAndShowGUI() {106JFrame frame = new JFrame("Test6209975");107frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);108frame.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);109frame.setLayout(new BorderLayout());110button = new JButton("Focus holder");111frame.add(button);112113JMenuBar mb = new JMenuBar();114menu = new JMenu("File");115116JMenuItem item;117118item = new JMenuItem("Just a menu item");119item.setIcon(new MyIcon(RO1));120item.setHorizontalTextPosition(SwingConstants.LEADING);121menu.add(item);122123item = new JMenuItem("Menu Item with another icon");124item.setIcon(new MyIcon(RO2));125item.setHorizontalTextPosition(SwingConstants.TRAILING);126menu.add(item);127128mb.add(menu);129130frame.setJMenuBar(mb);131frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);132frame.pack();133frame.setLocation(400, 300);134frame.setVisible(true);135}136137public static class ReturnObject {138139public volatile int itsValue;140}141142public static class MyIcon implements Icon {143144ReturnObject thisObject = null;145146public MyIcon(ReturnObject ro) {147super();148thisObject = ro;149}150151public void paintIcon(Component c, Graphics g, int x, int y) {152Color color = g.getColor();153g.setColor(Color.BLACK);154g.fillRect(x, y, 10, 10);155g.setColor(color);156thisObject.itsValue = x;157}158159public int getIconWidth() {160return 10;161}162163public int getIconHeight() {164return 10;165}166}167}168169170