Path: blob/master/test/jdk/javax/swing/JMenuItem/8152981/MenuItemIconTest.java
41154 views
/*1* Copyright (c) 2016, 2017, 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 8152981 815913527* @summary Double icons with JMenuItem setHorizontalTextPosition on Win 1028* @requires (os.family == "windows")29* @run main MenuItemIconTest30*/3132import java.awt.Color;33import java.awt.Dimension;34import java.awt.Graphics;35import java.awt.Point;36import java.awt.Robot;37import java.awt.image.BufferedImage;38import javax.swing.ImageIcon;39import javax.swing.JFrame;40import javax.swing.JMenuBar;41import javax.swing.JMenuItem;42import javax.swing.SwingConstants;43import javax.swing.SwingUtilities;44import javax.swing.UIManager;45import javax.swing.UnsupportedLookAndFeelException;4647public class MenuItemIconTest {4849private static JFrame frame;50private static Robot robot;51private static String errorMessage = "";52private static JMenuItem menuItem;53private static final int IMAGE_WIDTH_AND_HEIGHT = 25;5455public static void main(String[] args) throws Exception {56robot = new Robot();57String name = UIManager.getSystemLookAndFeelClassName();58try {59UIManager.setLookAndFeel(name);60} catch (ClassNotFoundException | InstantiationException |61IllegalAccessException | UnsupportedLookAndFeelException e) {62throw new RuntimeException("Test Failed");63}64createUI();65robot.waitForIdle();66executeTest();67if (!"".equals(errorMessage)) {68throw new RuntimeException(errorMessage);69}70}7172private static void createUI() throws Exception {73SwingUtilities.invokeAndWait(() -> {74frame = new JFrame();75frame.setTitle("Test");76JMenuBar menuBar = new JMenuBar();77ImageIcon icon = createIcon();78menuItem = new JMenuItem("Command", icon);79menuItem.setHorizontalTextPosition(SwingConstants.LEFT);80menuBar.add(menuItem);81frame.setJMenuBar(menuBar);82frame.setPreferredSize(new Dimension(500, 500));83frame.pack();84frame.setVisible(true);85frame.setLocationRelativeTo(null);86});87}8889private static void checkPixeclColor(int x, int y) {90robot.delay(2000);91robot.mouseMove(x, y);92Color c = robot.getPixelColor(x, y);93if (Color.RED.equals(c)) {94errorMessage = "Test Failed";95}96robot.delay(5000);97frame.dispose();98}99100protected static ImageIcon createIcon() {101BufferedImage bi = new BufferedImage(IMAGE_WIDTH_AND_HEIGHT,102IMAGE_WIDTH_AND_HEIGHT, BufferedImage.TYPE_INT_ARGB);103Graphics g = bi.createGraphics();104g.setColor(Color.RED);105g.fillOval(0, 0, IMAGE_WIDTH_AND_HEIGHT, IMAGE_WIDTH_AND_HEIGHT);106return new ImageIcon(bi);107}108109private static void executeTest() throws Exception {110Point point = menuItem.getLocationOnScreen();111checkPixeclColor(point.x + IMAGE_WIDTH_AND_HEIGHT / 2,112point.y + IMAGE_WIDTH_AND_HEIGHT / 2);113}114}115116117118